76
76
* @author Jakub Kubrynski
77
77
* @author Stephane Nicoll
78
78
* @author Andy Wilkinson
79
+ * @author Uladzislau Seuruk
79
80
* @see ConditionalOnBean
80
81
* @see ConditionalOnMissingBean
81
82
* @see ConditionalOnSingleCandidate
@@ -205,13 +206,13 @@ protected final MatchResult getMatchingBeans(Spec<?> spec) {
205
206
ConfigurableListableBeanFactory beanFactory = getSearchBeanFactory (spec );
206
207
ClassLoader classLoader = spec .getContext ().getClassLoader ();
207
208
boolean considerHierarchy = spec .getStrategy () != SearchStrategy .CURRENT ;
208
- Set <Class <?> > parameterizedContainers = spec .getParameterizedContainers ();
209
+ Set <ResolvableType > parameterizedContainers = spec .getParameterizedContainers ();
209
210
MatchResult result = new MatchResult ();
210
- Set <String > beansIgnoredByType = getNamesOfBeansIgnoredByType (classLoader , beanFactory , considerHierarchy ,
211
+ Set <String > beansIgnoredByType = getNamesOfBeansIgnoredByType (beanFactory , considerHierarchy ,
211
212
spec .getIgnoredTypes (), parameterizedContainers );
212
- for (String type : spec .getTypes ()) {
213
- Map <String , BeanDefinition > typeMatchedDefinitions = getBeanDefinitionsForType (classLoader ,
214
- considerHierarchy , beanFactory , type , parameterizedContainers );
213
+ for (ResolvableType type : spec .getTypes ()) {
214
+ Map <String , BeanDefinition > typeMatchedDefinitions = getBeanDefinitionsForType (beanFactory ,
215
+ considerHierarchy , type , parameterizedContainers );
215
216
Set <String > typeMatchedNames = matchedNamesFrom (typeMatchedDefinitions ,
216
217
(name , definition ) -> !ScopedProxyUtils .isScopedTarget (name )
217
218
&& isCandidate (beanFactory , name , definition , beansIgnoredByType ));
@@ -296,42 +297,31 @@ private boolean isDefaultCandidate(BeanDefinition definition) {
296
297
return true ;
297
298
}
298
299
299
- private Set <String > getNamesOfBeansIgnoredByType (ClassLoader classLoader , ListableBeanFactory beanFactory ,
300
- boolean considerHierarchy , Set <String > ignoredTypes , Set <Class <?> > parameterizedContainers ) {
300
+ private Set <String > getNamesOfBeansIgnoredByType (ListableBeanFactory beanFactory , boolean considerHierarchy ,
301
+ Set <ResolvableType > ignoredTypes , Set <ResolvableType > parameterizedContainers ) {
301
302
Set <String > result = null ;
302
- for (String ignoredType : ignoredTypes ) {
303
- Collection <String > ignoredNames = getBeanDefinitionsForType (classLoader , considerHierarchy , beanFactory ,
304
- ignoredType , parameterizedContainers )
303
+ for (ResolvableType ignoredType : ignoredTypes ) {
304
+ Collection <String > ignoredNames = getBeanDefinitionsForType (beanFactory , considerHierarchy , ignoredType ,
305
+ parameterizedContainers )
305
306
.keySet ();
306
307
result = addAll (result , ignoredNames );
307
308
}
308
309
return (result != null ) ? result : Collections .emptySet ();
309
310
}
310
311
311
- private Map <String , BeanDefinition > getBeanDefinitionsForType (ClassLoader classLoader , boolean considerHierarchy ,
312
- ListableBeanFactory beanFactory , String type , Set <Class <?>> parameterizedContainers ) throws LinkageError {
313
- try {
314
- return getBeanDefinitionsForType (beanFactory , considerHierarchy , resolve (type , classLoader ),
315
- parameterizedContainers );
316
- }
317
- catch (ClassNotFoundException | NoClassDefFoundError ex ) {
318
- return Collections .emptyMap ();
319
- }
320
- }
321
-
322
312
private Map <String , BeanDefinition > getBeanDefinitionsForType (ListableBeanFactory beanFactory ,
323
- boolean considerHierarchy , Class <?> type , Set <Class <?> > parameterizedContainers ) {
313
+ boolean considerHierarchy , ResolvableType type , Set <ResolvableType > parameterizedContainers ) {
324
314
Map <String , BeanDefinition > result = collectBeanDefinitionsForType (beanFactory , considerHierarchy , type ,
325
315
parameterizedContainers , null );
326
316
return (result != null ) ? result : Collections .emptyMap ();
327
317
}
328
318
329
319
private Map <String , BeanDefinition > collectBeanDefinitionsForType (ListableBeanFactory beanFactory ,
330
- boolean considerHierarchy , Class <?> type , Set <Class <?> > parameterizedContainers ,
320
+ boolean considerHierarchy , ResolvableType type , Set <ResolvableType > parameterizedContainers ,
331
321
Map <String , BeanDefinition > result ) {
332
322
result = putAll (result , beanFactory .getBeanNamesForType (type , true , false ), beanFactory );
333
- for (Class <?> container : parameterizedContainers ) {
334
- ResolvableType generic = ResolvableType .forClassWithGenerics (container , type );
323
+ for (ResolvableType parameterizedContainer : parameterizedContainers ) {
324
+ ResolvableType generic = ResolvableType .forClassWithGenerics (parameterizedContainer . resolve () , type );
335
325
result = putAll (result , beanFactory .getBeanNamesForType (generic , true , false ), beanFactory );
336
326
}
337
327
if (considerHierarchy && beanFactory instanceof HierarchicalBeanFactory hierarchicalBeanFactory ) {
@@ -550,13 +540,13 @@ private static class Spec<A extends Annotation> {
550
540
551
541
private final Set <String > names ;
552
542
553
- private final Set <String > types ;
543
+ private final Set <ResolvableType > types ;
554
544
555
545
private final Set <String > annotations ;
556
546
557
- private final Set <String > ignoredTypes ;
547
+ private final Set <ResolvableType > ignoredTypes ;
558
548
559
- private final Set <Class <?> > parameterizedContainers ;
549
+ private final Set <ResolvableType > parameterizedContainers ;
560
550
561
551
private final SearchStrategy strategy ;
562
552
@@ -570,10 +560,10 @@ private static class Spec<A extends Annotation> {
570
560
this .annotationType = annotationType ;
571
561
this .names = extract (attributes , "name" );
572
562
this .annotations = extract (attributes , "annotation" );
573
- this .ignoredTypes = extract (attributes , "ignored" , "ignoredType" );
563
+ this .ignoredTypes = resolveWhenPossible ( extract (attributes , "ignored" , "ignoredType" ) );
574
564
this .parameterizedContainers = resolveWhenPossible (extract (attributes , "parameterizedContainer" ));
575
565
this .strategy = annotation .getValue ("search" , SearchStrategy .class ).orElse (null );
576
- Set <String > types = extractTypes (attributes );
566
+ Set <ResolvableType > types = resolveWhenPossible ( extractTypes (attributes ) );
577
567
BeanTypeDeductionException deductionException = null ;
578
568
if (types .isEmpty () && this .names .isEmpty () && this .annotations .isEmpty ()) {
579
569
try {
@@ -614,17 +604,18 @@ private void merge(Set<String> result, String... additional) {
614
604
Collections .addAll (result , additional );
615
605
}
616
606
617
- private Set <Class <?> > resolveWhenPossible (Set <String > classNames ) {
607
+ private Set <ResolvableType > resolveWhenPossible (Set <String > classNames ) {
618
608
if (classNames .isEmpty ()) {
619
609
return Collections .emptySet ();
620
610
}
621
- Set <Class <?> > resolved = new LinkedHashSet <>(classNames .size ());
611
+ Set <ResolvableType > resolved = new LinkedHashSet <>(classNames .size ());
622
612
for (String className : classNames ) {
623
613
try {
624
- resolved .add (resolve (className , this .context .getClassLoader ()));
614
+ Class <?> type = resolve (className , this .context .getClassLoader ());
615
+ resolved .add (ResolvableType .forRawClass (type ));
625
616
}
626
617
catch (ClassNotFoundException | NoClassDefFoundError ex ) {
627
- // Ignore
618
+ resolved . add ( ResolvableType . NONE );
628
619
}
629
620
}
630
621
return resolved ;
@@ -653,48 +644,44 @@ protected final String getAnnotationName() {
653
644
return "@" + ClassUtils .getShortName (this .annotationType );
654
645
}
655
646
656
- private Set <String > deducedBeanType (ConditionContext context , AnnotatedTypeMetadata metadata ) {
647
+ private Set <ResolvableType > deducedBeanType (ConditionContext context , AnnotatedTypeMetadata metadata ) {
657
648
if (metadata instanceof MethodMetadata && metadata .isAnnotated (Bean .class .getName ())) {
658
649
return deducedBeanTypeForBeanMethod (context , (MethodMetadata ) metadata );
659
650
}
660
651
return Collections .emptySet ();
661
652
}
662
653
663
- private Set <String > deducedBeanTypeForBeanMethod (ConditionContext context , MethodMetadata metadata ) {
654
+ private Set <ResolvableType > deducedBeanTypeForBeanMethod (ConditionContext context , MethodMetadata metadata ) {
664
655
try {
665
- Class <?> returnType = getReturnType (context , metadata );
666
- return Collections .singleton (returnType .getName ());
656
+ return Set .of (getReturnType (context , metadata ));
667
657
}
668
658
catch (Throwable ex ) {
669
659
throw new BeanTypeDeductionException (metadata .getDeclaringClassName (), metadata .getMethodName (), ex );
670
660
}
671
661
}
672
662
673
- private Class <?> getReturnType (ConditionContext context , MethodMetadata metadata )
663
+ private ResolvableType getReturnType (ConditionContext context , MethodMetadata metadata )
674
664
throws ClassNotFoundException , LinkageError {
675
665
// Safe to load at this point since we are in the REGISTER_BEAN phase
676
666
ClassLoader classLoader = context .getClassLoader ();
677
- Class <?> returnType = resolve (metadata . getReturnTypeName () , classLoader );
678
- if (isParameterizedContainer (returnType )) {
679
- returnType = getReturnTypeGeneric ( metadata , classLoader );
667
+ ResolvableType returnType = getMethodReturnType (metadata , classLoader );
668
+ if (isParameterizedContainer (returnType . resolve () )) {
669
+ returnType = returnType . getGeneric ( );
680
670
}
681
671
return returnType ;
682
672
}
683
673
684
674
private boolean isParameterizedContainer (Class <?> type ) {
685
- for (Class <?> parameterizedContainer : this .parameterizedContainers ) {
686
- if (parameterizedContainer .isAssignableFrom (type )) {
687
- return true ;
688
- }
689
- }
690
- return false ;
675
+ return (type != null ) && this .parameterizedContainers .stream ()
676
+ .map (ResolvableType ::resolve )
677
+ .anyMatch ((container ) -> container != null && container .isAssignableFrom (type ));
691
678
}
692
679
693
- private Class <?> getReturnTypeGeneric (MethodMetadata metadata , ClassLoader classLoader )
680
+ private ResolvableType getMethodReturnType (MethodMetadata metadata , ClassLoader classLoader )
694
681
throws ClassNotFoundException , LinkageError {
695
682
Class <?> declaringClass = resolve (metadata .getDeclaringClassName (), classLoader );
696
683
Method beanMethod = findBeanMethod (declaringClass , metadata .getMethodName ());
697
- return ResolvableType .forMethodReturnType (beanMethod ). resolveGeneric () ;
684
+ return ResolvableType .forMethodReturnType (beanMethod );
698
685
}
699
686
700
687
private Method findBeanMethod (Class <?> declaringClass , String methodName ) {
@@ -720,6 +707,10 @@ private SearchStrategy getStrategy() {
720
707
return (this .strategy != null ) ? this .strategy : SearchStrategy .ALL ;
721
708
}
722
709
710
+ Set <ResolvableType > getTypes () {
711
+ return this .types ;
712
+ }
713
+
723
714
private ConditionContext getContext () {
724
715
return this .context ;
725
716
}
@@ -728,19 +719,15 @@ private Set<String> getNames() {
728
719
return this .names ;
729
720
}
730
721
731
- protected Set <String > getTypes () {
732
- return this .types ;
733
- }
734
-
735
722
private Set <String > getAnnotations () {
736
723
return this .annotations ;
737
724
}
738
725
739
- private Set <String > getIgnoredTypes () {
726
+ private Set <ResolvableType > getIgnoredTypes () {
740
727
return this .ignoredTypes ;
741
728
}
742
729
743
- private Set <Class <?> > getParameterizedContainers () {
730
+ private Set <ResolvableType > getParameterizedContainers () {
744
731
return this .parameterizedContainers ;
745
732
}
746
733
@@ -847,13 +834,13 @@ private void recordUnmatchedAnnotation(String annotation) {
847
834
this .unmatchedAnnotations .add (annotation );
848
835
}
849
836
850
- private void recordMatchedType (String type , Collection <String > matchingNames ) {
851
- this .matchedTypes .put (type , matchingNames );
837
+ private void recordMatchedType (ResolvableType type , Collection <String > matchingNames ) {
838
+ this .matchedTypes .put (type . toString () , matchingNames );
852
839
this .namesOfAllMatches .addAll (matchingNames );
853
840
}
854
841
855
- private void recordUnmatchedType (String type ) {
856
- this .unmatchedTypes .add (type );
842
+ private void recordUnmatchedType (ResolvableType type ) {
843
+ this .unmatchedTypes .add (type . toString () );
857
844
}
858
845
859
846
boolean isAllMatched () {
0 commit comments