Android examples for java.lang.reflect:Field Value
get Object Field Value
//package com.java2s; import java.lang.reflect.Method; import android.text.TextUtils; import android.util.Log; public class Main { private static final String CLASS = "ReflectionUtil"; private static final String METHOD_HEAD_GET = "get"; private static final String METHOD_HEAD_IS = "is"; public static Object getObjectFieldVal(Object object, String fieldName) { if (object == null || TextUtils.isEmpty(fieldName)) return null; Method method = reflectClassMethod(object.getClass(), METHOD_HEAD_GET + fieldName, true); if (method != null) { try { return method.invoke(object); } catch (Exception e) { Log.e(CLASS, e.getMessage() + ""); }//from ww w . j a v a 2 s . c o m } method = reflectClassMethod(object.getClass(), METHOD_HEAD_IS + fieldName, true); if (method != null) { try { return method.invoke(object); } catch (Exception e) { Log.e(CLASS, e.getMessage() + ""); } } return null; } private static Method reflectClassMethod(Class<?> clazz, String methodName, boolean isEqualsIgnoreCase) { if (clazz == null || TextUtils.isEmpty(methodName)) return null; Method[] methods = clazz.getMethods(); for (Method method : methods) { if ((isEqualsIgnoreCase && method.getName().equalsIgnoreCase( methodName)) || method.getName().equals(methodName)) { return method; } } return null; } }