21
21
import org .geektimes .commons .reflect .util .ClassUtils ;
22
22
import org .geektimes .commons .reflect .util .SimpleClassScanner ;
23
23
import org .geektimes .commons .util .PriorityComparator ;
24
- import org .geektimes .commons .util .ServiceLoaders ;
25
24
import org .geektimes .enterprise .inject .standard .beans .BeanArchiveType ;
26
25
import org .geektimes .enterprise .inject .standard .beans .BeanDiscoveryMode ;
27
26
import org .geektimes .enterprise .inject .standard .beans .BeanTypeSource ;
31
30
import org .geektimes .enterprise .inject .standard .beans .xml .bind .Scan ;
32
31
import org .geektimes .enterprise .inject .util .Decorators ;
33
32
import org .geektimes .enterprise .inject .util .Qualifiers ;
34
- import org .geektimes .enterprise .inject .util .Scopes ;
35
33
import org .geektimes .enterprise .inject .util .Stereotypes ;
36
34
import org .geektimes .interceptor .InterceptorManager ;
37
35
import org .geektimes .interceptor .util .InterceptorUtils ;
57
55
import static org .geektimes .commons .collection .util .CollectionUtils .newLinkedHashSet ;
58
56
import static org .geektimes .commons .lang .util .StringUtils .endsWith ;
59
57
import static org .geektimes .commons .lang .util .StringUtils .isBlank ;
58
+ import static org .geektimes .commons .util .ServiceLoaders .loadSpi ;
60
59
import static org .geektimes .enterprise .inject .standard .beans .BeanArchiveType .EXPLICIT ;
61
60
import static org .geektimes .enterprise .inject .standard .beans .BeanArchiveType .OTHER ;
62
61
import static org .geektimes .enterprise .inject .standard .beans .BeanDiscoveryMode .ALL ;
63
62
import static org .geektimes .enterprise .inject .standard .beans .BeanDiscoveryMode .NONE ;
64
63
import static org .geektimes .enterprise .inject .standard .beans .BeanTypeSource .*;
65
64
import static org .geektimes .enterprise .inject .standard .beans .xml .BeansReader .BEANS_XML_RESOURCE_NAME ;
66
- import static org .geektimes .enterprise .inject .util .Decorators .isDecorator ;
67
65
import static org .geektimes .interceptor .InterceptorManager .getInstance ;
68
66
69
67
@@ -79,7 +77,7 @@ public class BeanArchiveManager {
79
77
80
78
private static final Predicate <Class <?>> nonAnnotationFilter = annotationFilter .negate ();
81
79
82
- private ClassLoader classLoader ;
80
+ private final StandardBeanManager standardBeanManager ;
83
81
84
82
private final BeansReader beansReader ;
85
83
@@ -118,18 +116,6 @@ public class BeanArchiveManager {
118
116
*/
119
117
private final Map <Class <? extends Annotation >, Set <Annotation >> syntheticStereotypes ;
120
118
121
- /**
122
- * Synthetic Scopes from {@link BeforeBeanDiscovery#addScope(Class, boolean, boolean)}
123
- */
124
- private final Set <Class <? extends Annotation >> syntheticScopes ;
125
-
126
- /**
127
- * Synthetic Normal Scopes from {@link BeforeBeanDiscovery#addScope(Class, boolean, boolean)}
128
- * <p>
129
- * The key is annotation type, the value is passivating or not
130
- */
131
- private final Map <Class <? extends Annotation >, Boolean > syntheticNormalScopes ;
132
-
133
119
/**
134
120
* Synthetic InterceptorBindings from {@link BeforeBeanDiscovery#addInterceptorBinding(Class, Annotation...)}
135
121
* <p>
@@ -143,11 +129,12 @@ public class BeanArchiveManager {
143
129
144
130
private volatile boolean discovered ;
145
131
146
- public BeanArchiveManager (ClassLoader classLoader ) {
147
- this .classLoader = classLoader ;
148
- this .beansReader = ServiceLoaders .loadSpi (BeansReader .class , classLoader );
132
+ public BeanArchiveManager (StandardBeanManager standardBeanManager ) {
133
+ this .standardBeanManager = standardBeanManager ;
134
+
135
+ this .beansReader = loadSpi (BeansReader .class , getClassLoader ());
149
136
this .classScanner = SimpleClassScanner .INSTANCE ;
150
- this .interceptorManager = getInstance (classLoader );
137
+ this .interceptorManager = getInstance (getClassLoader () );
151
138
152
139
this .beanClasses = new TreeMap <>();
153
140
this .interceptorClasses = new TreeMap <>();
@@ -159,8 +146,6 @@ public BeanArchiveManager(ClassLoader classLoader) {
159
146
this .syntheticPackagesToScan = new TreeMap <>();
160
147
this .syntheticQualifiers = new LinkedHashSet <>();
161
148
this .syntheticStereotypes = new LinkedHashMap <>();
162
- this .syntheticScopes = new LinkedHashSet <>();
163
- this .syntheticNormalScopes = new LinkedHashMap <>();
164
149
this .syntheticInterceptorBindings = new LinkedHashMap <>();
165
150
166
151
this .discoveryEnabled = true ;
@@ -192,15 +177,6 @@ public BeanArchiveManager addSyntheticStereotype(Class<? extends Annotation> ste
192
177
return this ;
193
178
}
194
179
195
- public BeanArchiveManager addSyntheticScope (Class <? extends Annotation > scopeType , boolean normal , boolean passivating ) {
196
- if (normal ) {
197
- syntheticNormalScopes .put (scopeType , passivating );
198
- } else {
199
- syntheticScopes .add (scopeType );
200
- }
201
- return this ;
202
- }
203
-
204
180
public BeanArchiveManager addSyntheticInterceptorBinding (Class <? extends Annotation > bindingType , Annotation [] bindingTypeDef ) {
205
181
syntheticInterceptorBindings .put (bindingType , CollectionUtils .asSet (bindingTypeDef ));
206
182
return this ;
@@ -368,23 +344,6 @@ private void filterAndHandleBeanTypes(Set<Class<?>> classes,
368
344
}
369
345
}
370
346
371
- public boolean isScope (Class <? extends Annotation > annotationType ) {
372
- return Scopes .isScope (annotationType ) ||
373
- // Extensions
374
- syntheticScopes .contains (annotationType );
375
- }
376
-
377
- public boolean isNormalScope (Class <? extends Annotation > annotationType ) {
378
- return Scopes .isNormalScope (annotationType ) ||
379
- // Extensions
380
- syntheticNormalScopes .containsKey (annotationType );
381
- }
382
-
383
- public boolean isPassivatingScope (Class <? extends Annotation > annotationType ) {
384
- return Scopes .isPassivatingScope (annotationType ) ||
385
- // Extensions
386
- syntheticNormalScopes .getOrDefault (annotationType , Boolean .FALSE );
387
- }
388
347
389
348
public boolean isQualifier (Class <? extends Annotation > annotationType ) {
390
349
return Qualifiers .isQualifier (annotationType ) ||
@@ -405,7 +364,7 @@ public boolean isStereotype(Class<? extends Annotation> annotationType) {
405
364
}
406
365
407
366
public boolean isBeanClass (Class <?> type ) {
408
- return isDefiningAnnotationType (type , false , false );
367
+ return standardBeanManager . isDefiningAnnotationType (type , false , false );
409
368
}
410
369
411
370
public Set <Annotation > getInterceptorBindingDefinition (Class <? extends Annotation > bindingType ) {
@@ -430,6 +389,7 @@ public void discoverTypes() {
430
389
431
390
private void discoverTypesInExplicitBeanArchives () {
432
391
try {
392
+ ClassLoader classLoader = getClassLoader ();
433
393
Enumeration <URL > beansXMLResources = classLoader .getResources (BEANS_XML_RESOURCE_NAME );
434
394
while (beansXMLResources .hasMoreElements ()) {
435
395
URL beansXMLResource = beansXMLResources .nextElement ();
@@ -465,6 +425,7 @@ private void discoverTypesInExplicitBeanArchives() {
465
425
*/
466
426
private void discoverTypesInImplicitBeanArchives () {
467
427
if (isScanImplicitEnabled ()) {
428
+ ClassLoader classLoader = getClassLoader ();
468
429
Set <String > classPaths = ClassPathUtils .getClassPaths ();
469
430
classPaths .stream ().map (File ::new ).forEach (archiveFile -> {
470
431
Set <Class <?>> discoveredTypes = scan (classLoader , archiveFile );
@@ -485,7 +446,7 @@ private void discoverTypesInSyntheticBeanArchives() {
485
446
486
447
487
448
private Set <Class <?>> scan (URL beansXMLResource , Predicate <Class <?>>... classFilters ) {
488
- return new LinkedHashSet <>(classScanner .scan (classLoader , beansXMLResource , true , classFilters ));
449
+ return new LinkedHashSet <>(classScanner .scan (getClassLoader () , beansXMLResource , true , classFilters ));
489
450
}
490
451
491
452
private Set <Class <?>> scan (ClassLoader classLoader , File archiveFile , Predicate <Class <?>>... classFilters ) {
@@ -535,6 +496,7 @@ private void discoverTypesInImplicitBeanArchive(URL beansXMLResource) {
535
496
536
497
private void discoverTypesInSyntheticPackages () {
537
498
if (isDiscoveryEnabled ()) {
499
+ ClassLoader classLoader = getClassLoader ();
538
500
Set <Class <?>> discoveredTypes = new LinkedHashSet <>();
539
501
for (Map .Entry <String , Boolean > packageEntry : syntheticPackagesToScan .entrySet ()) {
540
502
String packageToDiscovery = packageEntry .getKey ();
@@ -757,11 +719,12 @@ private List<Class<?>> loadAnnotatedClasses(List<String> classNames,
757
719
}
758
720
759
721
private Class <?> resolveClass (String className ) {
760
- return ClassUtils .resolveClass (className , classLoader );
722
+ return ClassUtils .resolveClass (className , getClassLoader () );
761
723
}
762
724
763
725
private Class <?> loadClass (String className ) throws IllegalArgumentException {
764
726
try {
727
+ ClassLoader classLoader = getClassLoader ();
765
728
return ClassUtils .forName (className , classLoader );
766
729
} catch (ClassNotFoundException e ) {
767
730
String message = format ("The class[name : %s] can't be found!" , className );
@@ -799,56 +762,6 @@ private BeanArchiveType resolveBeanArchiveType(Beans beans) {
799
762
}
800
763
}
801
764
802
- /**
803
- * Is defining annotation type or not.
804
- * <p>
805
- * A bean class may have a bean defining annotation, allowing it to be placed anywhere in an application,
806
- * as defined in Bean archives. A bean class with a bean defining annotation is said to be an implicit bean.
807
- * The set of bean defining annotations contains:
808
- * <ul>
809
- * <li>{@link ApplicationScoped @ApplicationScoped}, {@link SessionScoped @SessionScoped},
810
- * {@link ConversationScoped @ConversationScoped} and {@link RequestScoped @RequestScoped} annotations
811
- * </li>
812
- * <li>all other normal scope types</li>
813
- * <li>{@link javax.interceptor.Interceptor @Interceptor} and {@link javax.decorator.Decorator @Decorator} annotations</li>
814
- * <li>all stereotype annotations (i.e. annotations annotated with {@link Stereotype @Stereotype})</li>
815
- * <li>the {@link Dependent @Dependent} scope annotation</li>
816
- * </ul>
817
- *
818
- * @param type
819
- * @param includedInterceptor
820
- * @param includedDecorator
821
- * @return
822
- */
823
- public boolean isDefiningAnnotationType (Class <?> type , boolean includedInterceptor , boolean includedDecorator ) {
824
-
825
- if (includedInterceptor && interceptorManager .isInterceptorClass (type )) {
826
- return true ;
827
- }
828
- if (includedDecorator && isDecorator (type )) {
829
- return true ;
830
- }
831
-
832
- boolean hasDefiningAnnotation = false ;
833
-
834
- Annotation [] annotations = type .getAnnotations ();
835
- for (Annotation annotation : annotations ) {
836
- Class <? extends Annotation > annotationType = annotation .annotationType ();
837
- if (isScope (annotationType ) ||
838
- isNormalScope (annotationType ) ||
839
- isStereotype (annotationType )) {
840
- hasDefiningAnnotation = true ;
841
- break ;
842
- }
843
- }
844
-
845
- return hasDefiningAnnotation ;
846
- }
847
-
848
- public void setClassLoader (ClassLoader classLoader ) {
849
- this .classLoader = classLoader ;
850
- }
851
-
852
765
public void disableDiscovery () {
853
766
this .discoveryEnabled = false ;
854
767
}
@@ -931,4 +844,8 @@ private static List<Class<?>> getBeanTypes(Map<BeanTypeSource, List<Class<?>>> r
931
844
}
932
845
return unmodifiableList (beanTypes );
933
846
}
847
+
848
+ private ClassLoader getClassLoader () {
849
+ return standardBeanManager .getClassLoader ();
850
+ }
934
851
}
0 commit comments