List < Field > getFields ( Class < ? > clazz ) { List < Field > result = new ArrayList <> ( ) ; Set < String > fieldNames = new HashSet <> ( ) ; Class < ? > searchType = clazz ; while ( ! Object . class . equals ( searchType ) && searchType != null ) { Field [ ] fields = searchType . getDeclaredFields ( ) ; for ( Field field : fields ) { if ( ! fieldNames . contains ( field . getName ( ) ) ) { fieldNames . add ( field . getName ( ) ) ; makeAccessible ( field ) ; result . add ( field ) ; } } searchType = searchType . getSuperclass ( ) ; } return result ; }
Find all declared fields of a class . Fields which are hidden by a child class are not included .