Java tutorial
//package com.java2s; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.ArrayList; public class Main { public static Field findFiledWithName(Class clazz, String filedName) { Field[] fields = getAllFiedFromClassAndSuper(clazz, false); if (fields != null) { for (Field field : fields) { field.setAccessible(true); if (field.getName().equals(filedName)) { return field; } } } return null; } public static Field findFiledWithName(Field[] fields, String filedName) { if (fields != null) { for (Field field : fields) { field.setAccessible(true); if (field.getName().equals(filedName)) { return field; } } } return null; } public static Field[] getAllFiedFromClassAndSuper(Class clazz, boolean needStatic) { ArrayList<Field> fields = new ArrayList<>(); if (clazz != null) { Field[] classFields = clazz.getDeclaredFields(); if (classFields != null) { for (Field field : classFields) { boolean isStatic = Modifier.isStatic(field.getModifiers()); if (isStatic && !needStatic) { continue; } fields.add(field); } } Field[] superFields = getAllFiedFromClassAndSuper(clazz.getSuperclass(), needStatic); if (superFields != null) { for (Field field : superFields) { boolean isStatic = Modifier.isStatic(field.getModifiers()); if (isStatic && !needStatic) { continue; } fields.add(field); } } } return fields.toArray(new Field[fields.size()]); } }