public static List < ? extends Element > getEnclosedElementsDeclarationOrder ( TypeElement type ) { List < ? extends Element > result = null ; try { Object binding = field ( type , "_binding" ) ; Class < ? > sourceTypeBinding = null ; { Class < ? > c = binding . getClass ( ) ; do { if ( c . getCanonicalName ( ) . equals ( "org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding" ) ) { sourceTypeBinding = c ; break ; } } while ( ( c = c . getSuperclass ( ) ) != null ) ; } final List < Object > declarationOrder ; if ( sourceTypeBinding != null ) { declarationOrder = findSourceOrder ( binding ) ; List < Element > enclosedElements = new ArrayList < Element > ( type . getEnclosedElements ( ) ) ; Collections . sort ( enclosedElements , new Comparator < Element > ( ) { public int compare ( Element o1 , Element o2 ) { try { Object o1Binding = field ( o1 , "_binding" ) ; Object o2Binding = field ( o2 , "_binding" ) ; int i1 = declarationOrder . indexOf ( o1Binding ) ; int i2 = declarationOrder . indexOf ( o2Binding ) ; return i1 - i2 ; } catch ( Exception e ) { return 0 ; } } } ) ; result = enclosedElements ; } } catch ( Exception e ) { // ignore } return ( result != null ) ? result : type . getEnclosedElements ( ) ; }
If given TypeElement is SourceTypeBinding the order of results are corrected .