1919import java .lang .annotation .Annotation ;
2020import java .lang .reflect .AnnotatedElement ;
2121import java .util .ArrayList ;
22+ import java .util .HashMap ;
2223import java .util .List ;
24+ import java .util .Map ;
2325
2426/**
2527 * Factory for creating {@link SecurityAnnotationScanner} instances.
2931 */
3032public final class SecurityAnnotationScanners {
3133
34+ private static final Map <Class <? extends Annotation >, SecurityAnnotationScanner <? extends Annotation >> uniqueScanners = new HashMap <>();
35+
36+ private static final Map <Class <? extends Annotation >, SecurityAnnotationScanner <? extends Annotation >> uniqueTemplateScanners = new HashMap <>();
37+
38+ private static final Map <List <Class <? extends Annotation >>, SecurityAnnotationScanner <? extends Annotation >> uniqueTypesScanners = new HashMap <>();
39+
3240 private SecurityAnnotationScanners () {
3341 }
3442
@@ -40,7 +48,8 @@ private SecurityAnnotationScanners() {
4048 * @return the default {@link SecurityAnnotationScanner}
4149 */
4250 public static <A extends Annotation > SecurityAnnotationScanner <A > requireUnique (Class <A > type ) {
43- return new UniqueSecurityAnnotationScanner <>(type );
51+ return (SecurityAnnotationScanner <A >) uniqueScanners .computeIfAbsent (type ,
52+ (t ) -> new UniqueSecurityAnnotationScanner <>(type ));
4453 }
4554
4655 /**
@@ -60,9 +69,10 @@ public static <A extends Annotation> SecurityAnnotationScanner<A> requireUnique(
6069 public static <A extends Annotation > SecurityAnnotationScanner <A > requireUnique (Class <A > type ,
6170 AnnotationTemplateExpressionDefaults templateDefaults ) {
6271 if (templateDefaults == null ) {
63- return new UniqueSecurityAnnotationScanner <> (type );
72+ return requireUnique (type );
6473 }
65- return new ExpressionTemplateSecurityAnnotationScanner <>(type , templateDefaults );
74+ return (SecurityAnnotationScanner <A >) uniqueTemplateScanners .computeIfAbsent (type ,
75+ (t ) -> new ExpressionTemplateSecurityAnnotationScanner <>(t , templateDefaults ));
6676 }
6777
6878 /**
@@ -75,7 +85,8 @@ public static <A extends Annotation> SecurityAnnotationScanner<A> requireUnique(
7585 public static SecurityAnnotationScanner <Annotation > requireUnique (List <Class <? extends Annotation >> types ) {
7686 List <Class <Annotation >> casted = new ArrayList <>();
7787 types .forEach ((type ) -> casted .add ((Class <Annotation >) type ));
78- return new UniqueSecurityAnnotationScanner <>(casted );
88+ return (SecurityAnnotationScanner <Annotation >) uniqueTypesScanners .computeIfAbsent (types ,
89+ (t ) -> new UniqueSecurityAnnotationScanner <>(casted ));
7990 }
8091
8192}
0 commit comments