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.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Reflection » InstanceScreenshots 
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.