Android examples for java.lang.reflect:Field
get Object's Field
//package com.java2s; import java.lang.reflect.*; public class Main { public static Object getObjectField(Object o, String fieldName) throws NoSuchFieldException, IllegalAccessException { Field f = getClassField(o.getClass(), fieldName); makeAccessible(f);/* w w w .j a v a2 s . c om*/ return f.get(o); } public static Field getClassField(Class clazz, String fieldName) throws NoSuchFieldException { try { return clazz.getDeclaredField(fieldName); } catch (NoSuchFieldException e) { Class superClass = clazz.getSuperclass(); if (superClass == null) { throw e; } else { return getClassField(superClass, fieldName); } } } private static void makeAccessible(Method method) { if (!Modifier.isPublic(method.getModifiers()) || !Modifier.isPublic(method.getDeclaringClass() .getModifiers())) { method.setAccessible(true); } } private static void makeAccessible(Field field) { if (!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass() .getModifiers())) { field.setAccessible(true); } } }