Mega Code Archive

 
Categories / Java / Reflection
 

Get all fields of a class

//package org.nestframework.utils; import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.Reader; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; /**  * Nest utility class.  *   * @author audin  *  */ public class NestUtil {   /**    * Get all fields of a class.    *     * @param clazz The class.    * @return All fields of a class.    */   public static Collection<Field> getFields(Class<?> clazz) { //    if (log.isDebugEnabled()) {   //    log.debug("getFields(Class<?>) - start");     //}     Map<String, Field> fields = new HashMap<String, Field>();     while (clazz != null) {       for (Field field : clazz.getDeclaredFields()) {         if (!fields.containsKey(field.getName())) {           fields.put(field.getName(), field);         }       }       clazz = clazz.getSuperclass();     }     Collection<Field> returnCollection = fields.values();   //  if (log.isDebugEnabled()) {     //  log.debug("getFields(Class<?>) - end");   //  }     return returnCollection;   } }