提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
通过指定字段名称不从对象获取静态字段,它搜索所有声明的字段,包括私有保护字段和公共字段
//package com.nowjava;/*from n o w j a v a . c o m - 时 代 Java*/ import java.lang.reflect.Field; 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(getNoStaticFieldArray(obj))); }/**时 代 J a v a 公 众 号 - nowjava.com**/ /** * Get no static 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[] getNoStaticFieldArray(Object obj) { return getNoStaticFieldArray(obj.getClass()); } public static Field[] getNoStaticFieldArray(Class<?> c) { return getNoStaticFieldList(c).toArray(new Field[] {}); } /** * Get no static 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> getNoStaticFieldList(Object obj) { return getNoStaticFieldList(obj.getClass()); } public static List<Field> getNoStaticFieldList(Class<?> c) { Field[] fields = c.getDeclaredFields(); List<Field> nostaticF