get Object By Constructor - Android java.lang.reflect

Android examples for java.lang.reflect:Constructor

Description

get Object By Constructor

Demo Code


import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main{
    /*from   www. j a v  a 2  s. c om*/
    public static Object getObjectByConstructor(String className,
            Class[] intArgsClass, Object[] intArgs) {

        Object returnObj = null;
        try {
            Class classType = Class.forName(className);
            Constructor constructor = classType
                    .getDeclaredConstructor(intArgsClass);
            constructor.setAccessible(true);
            returnObj = constructor.newInstance(intArgs);
        } catch (NoSuchMethodException ex) {
          //  ExceptionHandler.processFatalException(ex);
        } catch (Exception ex) {
          //  ExceptionHandler.processFatalException(ex);
        }
        return returnObj;
    }
}

Related Tutorials