get Field Value via its getter method - Android java.lang.reflect

Android examples for java.lang.reflect:Method Getter Setter

Description

get Field Value via its getter method

Demo Code


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

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 getValue(Object object, String fieldName) {
        if (object == null) {
            return null;
        }//from   w  ww .j  a v a  2 s  .  c o  m
        Class<?> clazz = object.getClass();
        try {
            Field field = clazz.getDeclaredField(fieldName);
            Method getter = getGetter(field);
            if (getter == null) {
                throw new IllegalArgumentException("getValue:"
                        + object.getClass().getName() + "." + fieldName
                        + " getter not found");
            }

            return getter.invoke(object);
        } catch (Exception e) {
            Log.e("ReflectUtil File", "getValue", e);
        }

        return null;
    }

    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