Java examples for Reflection:Field Get
Get no static field form an object by appointing field name, it search form all declared fields including private protected and public field
//package com.java2s; 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 = "java2s.com"; System.out.println(java.util.Arrays .toString(getNoStaticFieldArray(obj))); }/* ww w .j ava 2 s . c om*/ /** * 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> nostaticFieldList = new ArrayList<Field>(); for (Field field : fields) { field.setAccessible(true); if (!Modifier.isStatic(field.getModifiers())) { nostaticFieldList.add(field); } } return nostaticFieldList; } }