提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
通过指定字段名称不获取对象的任何静态字段或最终字段,它搜索所有声明的字段,包括私有保护字段和公共字段
//package com.nowjava; import java.lang.reflect.Field;/*来 自 NowJava.com - 时代Java*/ import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] argv) throws Exception { Object obj = "nowjava.com"; System.out.println(java.util.Arrays .toString(getNoStaticNorFinalFieldArray(obj))); } /** * Get no static field nor final field form an object by appointing field name, it search form all declared fields * including private protected and public field * @param obj the object to fetch fields * @return Field array */ public static Field[] getNoStaticNorFinalFieldArray(Object obj) { return getNoStaticNorFinalFieldArray(obj.getClass()); }/*来自 时 代 J a v a 公 众 号 - N o w J a v a . c o m*/ public static Field[] getNoStaticNorFinalFieldArray(Class<?> c) { return getNoStaticNorFinalFieldList(c).toArray(new Field[] {}); } /** * Get no static field nor final field form an object by appointing field name, it search form all declared fields * including private protected and public fields * @param obj the object to fetch field * @return Field list */ public static List<Field> getNoStaticNorFinalFieldList(Object obj) { return getNoStaticNorFinalFieldList(obj.getClass()); } public static List<Field> getNoStaticNorFinalFieldList(Class<?> c) { Field[] fields = c.getDeclaredFields(); List<Field> nostaticFieldList = new