@Test public void getAllFields() throws Exception { List<Field> fields = ClassUtils.getAllFields(TestBean.class); Assert.assertEquals(fields.size(), 4); }
public static List<Field> getAllFields(Class clazz) { List<Field> all = new ArrayList<Field>(); for (Class<?> c = clazz; c != Object.class && c != null; c = c.getSuperclass()) { Field[] fields = c.getDeclaredFields(); for (Field field : fields) { int mod = field.getModifiers(); if (Modifier.isStatic(mod) || Modifier.isTransient(mod)) { continue; } field.setAccessible(true); all.add(field); } } return all; }
ClassUtils { public static List<Field> getAllFields(Class clazz) { List<Field> all = new ArrayList<Field>(); for (Class<?> c = clazz; c != Object.class && c != null; c = c.getSuperclass()) { Field[] fields = c.getDeclaredFields(); for (Field field : fields) { int mod = field.getModifiers(); if (Modifier.isStatic(mod) || Modifier.isTransient(mod)) { continue; } field.setAccessible(true); all.add(field); } } return all; } }
ClassUtils { public static List<Field> getAllFields(Class clazz) { List<Field> all = new ArrayList<Field>(); for (Class<?> c = clazz; c != Object.class && c != null; c = c.getSuperclass()) { Field[] fields = c.getDeclaredFields(); for (Field field : fields) { int mod = field.getModifiers(); if (Modifier.isStatic(mod) || Modifier.isTransient(mod)) { continue; } field.setAccessible(true); all.add(field); } } return all; }  }
ClassUtils { public static List<Field> getAllFields(Class clazz) { List<Field> all = new ArrayList<Field>(); for (Class<?> c = clazz; c != Object.class && c != null; c = c.getSuperclass()) { Field[] fields = c.getDeclaredFields(); for (Field field : fields) { int mod = field.getModifiers(); if (Modifier.isStatic(mod) || Modifier.isTransient(mod)) { continue; } field.setAccessible(true); all.add(field); } } return all; }  static Class forName(String className); static Class forName(String className, boolean initialize); static Class forName(String className, ClassLoader cl); static List<Method> getAllMethods(Class clazz); static List<Field> getAllFields(Class clazz); static T newInstance(Class<T> clazz); static T newInstanceWithArgs(Class<T> clazz, Class<?>[] argTypes, Object[] args); static Object getDefaultPrimitiveValue(Class clazz); static T getDefaultWrapperValue(Class<T> clazz); static String getMethodKey(String interfaceName, String methodName); static boolean isAssignableFrom(Class<?> interfaceClass, Class<?> implementClass); }
ClassUtils { public static List<Field> getAllFields(Class clazz) { List<Field> all = new ArrayList<Field>(); for (Class<?> c = clazz; c != Object.class && c != null; c = c.getSuperclass()) { Field[] fields = c.getDeclaredFields(); for (Field field : fields) { int mod = field.getModifiers(); if (Modifier.isStatic(mod) || Modifier.isTransient(mod)) { continue; } field.setAccessible(true); all.add(field); } } return all; }  static Class forName(String className); static Class forName(String className, boolean initialize); static Class forName(String className, ClassLoader cl); static List<Method> getAllMethods(Class clazz); static List<Field> getAllFields(Class clazz); static T newInstance(Class<T> clazz); static T newInstanceWithArgs(Class<T> clazz, Class<?>[] argTypes, Object[] args); static Object getDefaultPrimitiveValue(Class clazz); static T getDefaultWrapperValue(Class<T> clazz); static String getMethodKey(String interfaceName, String methodName); static boolean isAssignableFrom(Class<?> interfaceClass, Class<?> implementClass);  }