get Value By Field via reflection - Android java.lang.reflect

Android examples for java.lang.reflect:Field Value

Description

get Value By Field via reflection

Demo Code


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

public class Main {
    public static Object getValueByField(Object obj, String fieldName) {
        Object retObj = null;/* ww w .j a  va2 s.  co  m*/
        boolean isFound = false;
        String _fieldName = fieldName;
        String _fieldName2 = null;
        int pointIndex = fieldName.indexOf(".");
        try {
            if (pointIndex != -1) {
                _fieldName = fieldName.substring(0, pointIndex);
                _fieldName2 = fieldName.substring(pointIndex + 1);
            }

            Field field = obj.getClass().getDeclaredField(_fieldName);
            field.setAccessible(true); 
            isFound = true;

            if (pointIndex != -1) {
                Object _obj = field.get(obj);
                if (_obj != null) {
                    retObj = getValueByField(_obj, _fieldName2);
                } else {
                    retObj = null;
                }
            } else {
                String fieldType = field.getType().getSimpleName();
                if (fieldType.equals("int")) {
                    retObj = Integer.valueOf(field.getInt(obj));
                } else if (fieldType.equals("float")) {
                    retObj = Float.valueOf(field.getFloat(obj));
                } else if (fieldType.equals("double")) {
                    retObj = Double.valueOf(field.getDouble(obj));
                } else if (fieldType.equals("String")
                        || fieldType.equals("Date")) {
                    retObj = field.get(obj);
                } else if (fieldType.equals("boolean")) {
                    retObj = Boolean.valueOf(field.getBoolean(obj));
                }
            }
        } catch (Exception e) {
            isFound = false;
        }

        if (isFound == false) {
            if (pointIndex != -1) {
                Object _obj = getValueByMethod(obj, _fieldName);
                if (_obj != null) {
                    retObj = getValueByField(_obj, _fieldName2);
                } else {
                    retObj = null;
                }
            } else {
                retObj = getValueByMethod(obj, fieldName);
            }
        }
        return retObj;
    }

    public static Object getValueByMethod(Object obj, String s) {
        Object ret = null;
        String methodName = String.valueOf(s.charAt(0)).toUpperCase()
                + s.substring(1);

        try {
            Method m = obj.getClass().getMethod("get" + methodName);
            ret = m.invoke(obj);

        } catch (NoSuchMethodException e) {
            //            Log.w("method.invoke Failed. can't find the method", e);
            throw new RuntimeException(e);
        } catch (IllegalArgumentException e) {
            //            Log.w("method.invoke Failed. argument is illegal", e);
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            //            Log.w("method.invoke Failed. can't access the method", e);
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            //            Log.w("method.invoke Failed. can't invoke the target", e);
            throw new RuntimeException(e);
        }

        return ret;
    }
}

Related Tutorials