public static Field [ ] getMatchingFields ( Class < ? > clazz , final int modifiers ) { final Set < Field > fields = new TreeSet < Field > ( FIELD_NAME_AND_DECLARING_CLASS_COMPARATOR ) ; traverseHierarchy ( clazz , new TraverseTask < Field > ( ) { @ Override public Field run ( Class < ? > clazz ) { Field [ ] fieldArray = clazz . getDeclaredFields ( ) ; for ( int i = 0 ; i < fieldArray . length ; i ++ ) { if ( ( modifiers & fieldArray [ i ] . getModifiers ( ) ) != 0 ) { fields . add ( fieldArray [ i ] ) ; } } return null ; } } ) ; return fields . toArray ( new Field [ fields . size ( ) ] ) ; }
Returns the field objects that are declared in the given class or any of it s super types that have any of the given modifiers . The type hierarchy is traversed upwards and all declared fields that match the given modifiers are added to the result array . The elements in the array are sorted by their names and declaring classes .