get Class with or without package name - Android java.lang.reflect

Android examples for java.lang.reflect:Package

Description

get Class with or without package name

Demo Code


import android.content.Context;
import android.util.Log;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main{
    private static final String TAG = "";
    public static Class<?> getClass(Context context, String className) {
        Class<?> classDefinition = null;
        if (className.contains(context.getPackageName())) {
            classDefinition = getClass(className);
        } else {/*from   w  w  w . j  av a2 s  .  c o m*/
            classDefinition = getClass(context.getPackageName() + className);
        }
        return classDefinition;
    }
    public static Class<?> getClass(String className) {
        Class<?> classDefinition = null;
        try {
            classDefinition = Class.forName(className);
        } catch (ClassNotFoundException e) {
            Log.w(TAG, "Class \"" + className + "\" not found");
        }
        return classDefinition;
    }
}

Related Tutorials