get Instance via Constructor - Android java.lang.reflect

Android examples for java.lang.reflect:Constructor

Description

get Instance via Constructor

Demo Code


import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.annotation.SuppressLint;
import android.util.Log;

public class Main{
    public static <T> T getInstance(Class<T> beanClass) {
        try {//  www .  j a v a2 s .c o  m
            Constructor<T> constructor = beanClass.getConstructor();
            T bean = constructor.newInstance();
            return bean;
        } catch (Exception e) {
        }

        return null;
    }
}

Related Tutorials