Java tutorial
//package com.java2s; //License from project: Apache License import java.lang.reflect.Field; import java.lang.reflect.Modifier; public class Main { public static Field getField(final Class clazz, String field_name) { if (clazz == null || field_name == null) return null; Field field = null; for (Class curr = clazz; curr != null; curr = curr.getSuperclass()) { try { return curr.getDeclaredField(field_name); } catch (NoSuchFieldException e) { } } return field; } public static Object getField(Field field, Object target) { if (!Modifier.isPublic(field.getModifiers())) { field.setAccessible(true); } try { return field.get(target); } catch (IllegalAccessException iae) { throw new IllegalArgumentException("Could not get field " + field, iae); } } }