Java tutorial
//package com.java2s; import java.lang.reflect.Field; import java.util.HashMap; import android.util.Log; public class Main { private static final HashMap<String, Field> fieldCache = new HashMap<String, Field>(); public static int getIntField(Object obj, String fieldName) { try { return findField(obj.getClass(), fieldName).getInt(obj); } catch (IllegalAccessException e) { // should not happen //XposedBridge.log(e); Log.v("test", e.getMessage()); throw new IllegalAccessError(e.getMessage()); } catch (IllegalArgumentException e) { throw e; } } /** * Look up a field in a class and set it to accessible. The result is cached. * If the field was not found, a {@link NoSuchFieldError} will be thrown. */ public static Field findField(Class<?> clazz, String fieldName) { StringBuilder sb = new StringBuilder(clazz.getName()); sb.append('#'); sb.append(fieldName); String fullFieldName = sb.toString(); if (fieldCache.containsKey(fullFieldName)) { Field field = fieldCache.get(fullFieldName); if (field == null) throw new NoSuchFieldError(fullFieldName); return field; } try { Field field = findFieldRecursiveImpl(clazz, fieldName); field.setAccessible(true); fieldCache.put(fullFieldName, field); return field; } catch (NoSuchFieldException e) { fieldCache.put(fullFieldName, null); throw new NoSuchFieldError(fullFieldName); } } private static Field findFieldRecursiveImpl(Class<?> clazz, String fieldName) throws NoSuchFieldException { try { return clazz.getDeclaredField(fieldName); } catch (NoSuchFieldException e) { while (true) { clazz = clazz.getSuperclass(); if (clazz == null || clazz.equals(Object.class)) break; try { return clazz.getDeclaredField(fieldName); } catch (NoSuchFieldException ignored) { } } throw e; } } }