@@ -433,18 +433,12 @@ public static void cacheAnnotatedObjects(final Class<?> scannedClass,
433433 final Set <Class <? extends Annotation >> keysToDrop =
434434 new HashSet <Class <? extends Annotation >>();
435435 for (final Class <? extends Annotation > annotationClass : query .keySet ()) {
436- final Class <? extends AnnotatedElement > objectClass =
437- query .get (annotationClass );
438-
439436 // Fields
440- if (Field .class .isAssignableFrom (objectClass )) {
441- if (fieldCache .getList (scannedClass , annotationClass ) != null ) keysToDrop
442- .add (annotationClass );
437+ if (fieldCache .getList (scannedClass , annotationClass ) != null ) {
438+ keysToDrop .add (annotationClass );
443439 }
444- // Methods
445- else if (Method .class .isAssignableFrom (objectClass )) {
446- if (methodCache .getList (scannedClass , annotationClass ) != null ) keysToDrop
447- .add (annotationClass );
440+ else if (methodCache .getList (scannedClass , annotationClass ) != null ) {
441+ keysToDrop .add (annotationClass );
448442 }
449443 }
450444
@@ -483,75 +477,54 @@ else if (Method.class.isAssignableFrom(objectClass)) {
483477
484478 // Methods
485479 if (Method .class .isAssignableFrom (objectClass )) {
486- for (final Class <?> inheritedClass : inherited ) {
487- final List <Method > annotatedMethods =
488- methodCache .getList (inheritedClass , annotationClass );
489-
490- if (annotatedMethods != null && !annotatedMethods .isEmpty ()) {
491- final List <Method > scannedMethods =
492- methodCache .makeList (scannedClass , annotationClass );
493-
494- scannedMethods .addAll (annotatedMethods );
495- }
496- }
497-
498- // Add declared methods
499- final Method [] declaredMethods = scannedClass .getDeclaredMethods ();
500- if (declaredMethods != null && declaredMethods .length > 0 ) {
501- List <Method > scannedMethods = null ;
502-
503- for (final Method m : declaredMethods ) {
504- if (m .getAnnotation (annotationClass ) != null ) {
505- if (scannedMethods == null ) {
506- scannedMethods = methodCache .makeList (scannedClass , annotationClass );
507- }
508- scannedMethods .add (m );
509- }
510- }
511- }
512-
513- // If there were no methods for this query, map an empty
514- // list to mark the query complete
515- if (methodCache .getList (scannedClass , annotationClass ) == null ) {
516- methodCache .putList (scannedClass , annotationClass , Collections .<Method >emptyList ());
517- }
480+ populateCache (scannedClass , inherited , annotationClass , methodCache ,
481+ scannedClass .getDeclaredMethods ());
518482 }
519483 // Fields
520484 else if (Field .class .isAssignableFrom (objectClass )) {
521- for (final Class <?> inheritedClass : inherited ) {
522- final List <Field > annotatedFields =
523- fieldCache .getList (inheritedClass , annotationClass );
485+ populateCache (scannedClass , inherited , annotationClass , fieldCache ,
486+ scannedClass .getDeclaredFields ());
487+ }
488+ }
489+ }
524490
525- if (annotatedFields != null && !annotatedFields .isEmpty ()) {
526- final List <Field > scannedFields =
527- fieldCache .makeList (scannedClass , annotationClass );
491+ private static <T extends AnnotatedElement > void
492+ populateCache (final Class <?> scannedClass , final List <Class <?>> inherited ,
493+ final Class <? extends Annotation > annotationClass ,
494+ CacheMap <T > cacheMap , T [] declaredElements )
495+ {
496+ // Add inherited elements
497+ for (final Class <?> inheritedClass : inherited ) {
498+ final List <T > annotatedElements =
499+ cacheMap .getList (inheritedClass , annotationClass );
528500
529- scannedFields . addAll ( annotatedFields );
530- }
531- }
501+ if ( annotatedElements != null && ! annotatedElements . isEmpty ()) {
502+ final List < T > scannedElements =
503+ cacheMap . makeList ( scannedClass , annotationClass );
532504
533- // Add declared fields
534- final Field [] declaredFields = scannedClass .getDeclaredFields ();
535- if (declaredFields != null && declaredFields .length > 0 ) {
536- List <Field > scannedFields = null ;
537-
538- for (final Field f : declaredFields ) {
539- if (f .getAnnotation (annotationClass ) != null ) {
540- if (scannedFields == null ) {
541- scannedFields = fieldCache .makeList (scannedClass , annotationClass );
542- }
543- scannedFields .add (f );
544- }
545- }
546- }
505+ scannedElements .addAll (annotatedElements );
506+ }
507+ }
508+
509+ // Add declared elements
510+ if (declaredElements != null && declaredElements .length > 0 ) {
511+ List <T > scannedElements = null ;
547512
548- // If there were no fields for this query, map an empty
549- // list to mark the query complete
550- if (fieldCache .getList (scannedClass , annotationClass ) == null ) {
551- fieldCache .putList (scannedClass , annotationClass , Collections .<Field >emptyList ());
513+ for (final T t : declaredElements ) {
514+ if (t .getAnnotation (annotationClass ) != null ) {
515+ if (scannedElements == null ) {
516+ scannedElements = cacheMap .makeList (scannedClass , annotationClass );
517+ }
518+ scannedElements .add (t );
552519 }
553520 }
554521 }
522+
523+ // If there were no elements for this query, map an empty
524+ // list to mark the query complete
525+ if (cacheMap .getList (scannedClass , annotationClass ) == null ) {
526+ cacheMap .putList (scannedClass , annotationClass , Collections .<T >emptyList ());
527+ }
555528 }
556529
557530 /**
0 commit comments