get Declared Field in current object - Android java.lang.reflect

Android examples for java.lang.reflect:Field

Description

get Declared Field in current object

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 Field getDeclaredField(Object object, String fieldName) {
        Field field = null;//from  w  ww  . j  av  a  2  s .  c  o  m
        try {
            field = object.getClass().getDeclaredField(fieldName);
        } catch (SecurityException e) {
            Log.e(TAG, e.getMessage());
        } catch (NoSuchFieldException e) {
            Log.w(TAG, "Field \"" + field + "\" not found for "
                    + object.getClass().toString());
        }
        return field;
    }
    public static Class<?> getClass(Context context, String className) {
        Class<?> classDefinition = null;
        if (className.contains(context.getPackageName())) {
            classDefinition = getClass(className);
        } else {
            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