get Value Of Field - Android java.lang.reflect

Android examples for java.lang.reflect:Field Value

Description

get Value Of Field

Demo Code


//package com.java2s;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import java.util.WeakHashMap;

import android.util.Log;

public class Main {
    private static final WeakHashMap<Field, Method> getterCache = new WeakHashMap<Field, Method>();

    public static Object getValueOfField(Object object, String fieldName) {
        if (object == null) {
            return null;
        }/*w ww  .ja v a  2 s  .co m*/
        Class<?> clazz = object.getClass();
        try {
            final Field field = clazz.getDeclaredField(fieldName);
            Method getter = getGetter(field);

            if (getter != null) {
                return getter.invoke(object);
            }

            field.setAccessible(true);
            return field.get(object);
        } catch (NoSuchFieldException e) {
            throw new IllegalArgumentException(e);
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException(e);
        } catch (InvocationTargetException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public static Field getDeclaredField(Object object, String fieldName) {
        Field field = null;

        Class<?> clazz = object.getClass();

        while (clazz != Object.class) {
            try {
                field = clazz.getDeclaredField(fieldName);
                return field;
            } catch (Exception e) {
                Log.i("ReflectUtil File", "");

                clazz = clazz.getSuperclass();
            }

        }

        return null;
    }

    public static Method getGetter(Field field) {
        String fieldName = field.getName();
        Method getter = (Method) getterCache.get(field);
        if (getter != null) {
            return getter;
        }
        String methodName = generateGetterName(fieldName,
                field.getType() == Boolean.TYPE);
        Class<?> clazz = field.getDeclaringClass();
        try {
            getter = clazz.getMethod(methodName, (Class<?>[]) null);
            getterCache.put(field, getter);
            return getter;
        } catch (SecurityException e) {
            return null;
        } catch (NoSuchMethodException e) {
        }
        return null;
    }

    public static String generateGetterName(String fieldName) {
        return generateGetterName(fieldName, false);
    }

    public static String generateGetterName(String fieldName,
            boolean isPrimitiveBoolean) {
        String firstCH = "" + fieldName.charAt(0);
        Character secondCH = null;
        if (fieldName.length() > 1) {
            secondCH = Character.valueOf(fieldName.charAt(1));
        }
        String getterName = null;
        String replaceStr = null;
        if ((secondCH == null)
                || (!Character.isUpperCase(secondCH.charValue()))) {
            replaceStr = fieldName.replaceFirst(firstCH,
                    firstCH.toUpperCase());
        } else {
            replaceStr = fieldName;
        }
        if (isPrimitiveBoolean) {
            getterName = "is" + replaceStr;
        } else {
            getterName = "get" + replaceStr;
        }
        return getterName;
    }
}

Related Tutorials