Create new instance and invoke methods : Instance « Reflection « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Reflection » Instance 




Create new instance and invoke methods
        

/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *    http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
//package com.utils;

import java.lang.reflect.*;
//import org.apache.log4j.*;

/**
 * Utilities to work with reflection.
 *
 @author Erich Roncarolo
 @version 0.1 - 2004-09-07
 */
public class ClassUtil {

    private ClassUtil() {
    }//ClassUtil()

    /**
     * Returns a new instance of an object using constructor
     * without arguments.
     *
     @param name class name
     @return a new object instance or null if some error occours (exceptions are logged)
     */
    public static Object newInstance(String name) {
        return newInstance(name, null, null, false);
    }//newInstance()

    /**
     * Returns a new instance of an object using constructor
     * with arguments' running classes as parameter types.
     *
     @param name class name
     @param args constructor initialization arguments
     *
     @return a new object instance or null if some error occours (exceptions are logged)
     */
    public static Object newInstance(String name, Object args[]) {
        return newInstance(name, null, args, false);
    }//newInstance()

    /**
     * Returns a new instance of an object using constructor with
     * specified parameter types and arguments.
     *
     @param name class name
     @param param constructor parameter types
     @param args constructor initialization arguments
     *
     @return a new object instance or null if some error occours (exceptions are logged)
     */
    public static Object newInstance(String name, Class param[], Object args[]) {
        return newInstance(name, param, args, false);
    }//newInstance()

    /**
     * Returns a new instance of an object using constructor with
     * specified parameter types and arguments.
     *
     @param name class name
     @param param constructor parameter types
     @param args constructor initialization arguments
     @param logException constructor initialization arguments
     *
     @return a new object instance or null if some error occours (exceptions are logged)
     */
    public static Object newInstance(String name, Class param[], Object args[]boolean logException) {
        if (param == null && args != null) {
            param = new Class[args.length];
            for (int i = 0; i < param.length; i++) {
                if (args[i!= null) {
                    param[i= args[i].getClass();
                else {
                    param[inull;
                }
            }//for
        }//if

        Constructor c = null;
        Object obj = null;
        try {
            c = Class.forName(name).getConstructor(param);
            if (c != null) {
                obj = c.newInstance(args);
            }
        catch (Exception x) {
            if (logException) {
             //   Logger log = Logger.getLogger(ClassUtil.class);
             //   log.error("", x);
            }
            return null;
        }
        return obj;
    }//newInstance()

    /**
     * Returns a new instance of an object using constructor
     * without arguments.
     *
     @param clazz object class
     @return a new object instance or null if some error occours (exceptions are logged)
     */
    public static Object newInstance(Class clazz) {
        return newInstance(clazz, null, null);
    }//newInstance()

    /**
     * Returns a new instance of an object using constructor
     * with arguments' running classes as parameter types.
     *
     @param clazz object class
     @param args constructor initialization arguments
     *
     @return a new object instance or null if some error occours (exceptions are logged)
     */
    public static Object newInstance(Class clazz, Object args[]) {
        return newInstance(clazz, null, args);
    }//newInstance()

    /**
     * Returns a new instance of an object using constructor with
     * specified parameter types and arguments.
     *
     @param clazz object class
     @param param constructor parameter types
     @param args constructor initialization arguments
     *
     @return a new object instance or null if some error occours (exceptions are logged)
     */
    public static Object newInstance(Class clazz, Class param[], Object args[]) {
        if (clazz == null) {
            return null;
        }
        String name = clazz.getName();
        return newInstance(name, param, args);
    }//newInstance()

    /**
     * Invokes the object's method with specified parameter types and arguments.
     *
     @param obj the object the specified method is invoked from (use a Class object if method is static)
     @param name method name
     @param param constructor parameter types
     @param args constructor initialization arguments
     *
     @return a new object instance or null if some error occours (exceptions are logged)
     */
    public static Object invokeMethod(Object obj, String name, Class param[], Object args[]) {
        if (param == null && args != null) {
            param = new Class[args.length];
            for (int i = 0; i < param.length; i++) {
                if (args[i!= null) {
                    param[i= args[i].getClass();
                else {
                    param[inull;
                }
            }//for
        }//if

        Method m = null;
        Object ret = null;
        try {
            if (obj instanceof Class) {
                m = ((Classobj).getMethod(name, param);
            else {
                m = obj.getClass().getMethod(name, param);
            }
            if (m != null) {
                ret = m.invoke(obj, args);
            }
        catch (NoSuchMethodException x) {
            // Logger log = Logger.getLogger(ClassUtil.class);
            //log.warn("No method "+(obj instanceof Class ? (Class)obj : obj.getClass()).toString()+"."+name+"("+Arrays.asList(param)+") found - "+TnesException.firstStackTrace(x));
            return null;
        catch (Exception x) {
            //Logger log = Logger.getLogger(ClassUtil.class);
           // log.error("", x);
            return null;
        }
        return ret;
    }//invokeMethod()

    /**
     * Invokes the object's method with specified arguments.
     *
     @param obj the object the specified method is invoked from (use a Class object if method is static)
     @param name method name
     @param args constructor initialization arguments
     *
     @return a new object instance or null if some error occours (exceptions are logged)
     */
    public static Object invokeMethod(Object obj, String name, Object args[]) {
        return invokeMethod(obj, name, null, args);
    }//invokeMethod()

    /**
     * Invokes the object's method with no arguments.
     *
     @param obj the object the specified method is invoked from (use a Class object if method is static)
     @param name method name
     *
     @return a new object instance or null if some error occours (exceptions are logged)
     */
    public static Object invokeMethod(Object obj, String name) {
        return invokeMethod(obj, name, null, null);
    }//invokeMethod()
}//ClassUtil

   
    
    
    
    
    
    
    
  














Related examples in the same category
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.