Java tutorial
//package com.java2s; import java.lang.reflect.Field; public class Main { public static Field getField(Class<?> sourceClass, String fieldName, boolean isFindDeclaredField, boolean isUpwardFind) { Field field = null; try { field = isFindDeclaredField ? sourceClass.getDeclaredField(fieldName) : sourceClass.getField(fieldName); } catch (NoSuchFieldException e1) { if (isUpwardFind) { Class<?> classs = sourceClass.getSuperclass(); while (field == null && classs != null) { try { field = isFindDeclaredField ? classs.getDeclaredField(fieldName) : classs.getField(fieldName); } catch (NoSuchFieldException e11) { classs = classs.getSuperclass(); } } } } return field; } public static Field getField(Class<?> sourceClass, String fieldName) { return getField(sourceClass, fieldName, true, true); } }