get Inner Class Instance - Android java.lang.reflect

Android examples for java.lang.reflect:Inner Class

Description

get Inner Class Instance

Demo Code


//package com.java2s;
import java.lang.reflect.Constructor;

public class Main {
    public static Object getInnerClassInstance(Class clazz,
            boolean isPublicConstructor, Class constructorArgsTypes[],
            Object constructorArgs[]) throws Exception {
        return getClassInstance(clazz, isPublicConstructor,
                constructorArgsTypes, constructorArgs);
    }//from  w ww. java  2  s . co m

    public static Object getClassInstance(Class clazz,
            boolean isPublicConstructor, Class constructorArgsTypes[],
            Object constructorArgs[]) throws Exception {
        Constructor constructor;
        if (isPublicConstructor)
            constructor = clazz.getConstructor(constructorArgsTypes);
        else
            constructor = clazz
                    .getDeclaredConstructor(constructorArgsTypes);
        if (!constructor.isAccessible())
            constructor.setAccessible(true);
        return constructor.newInstance(constructorArgs);
    }
}

Related Tutorials