19
19
import static org .springframework .context .annotation .MetadataUtils .attributesFor ;
20
20
21
21
import java .io .IOException ;
22
+ import java .util .ArrayList ;
22
23
import java .util .Arrays ;
23
24
import java .util .Collections ;
24
25
import java .util .Comparator ;
25
26
import java .util .HashMap ;
26
27
import java .util .HashSet ;
27
28
import java .util .Iterator ;
28
29
import java .util .LinkedHashSet ;
30
+ import java .util .List ;
29
31
import java .util .Map ;
30
32
import java .util .Set ;
31
33
import java .util .Stack ;
32
34
35
+ import org .apache .commons .logging .Log ;
36
+ import org .apache .commons .logging .LogFactory ;
33
37
import org .springframework .beans .BeanUtils ;
34
38
import org .springframework .beans .factory .Aware ;
35
39
import org .springframework .beans .factory .BeanClassLoaderAware ;
55
59
import org .springframework .core .type .StandardAnnotationMetadata ;
56
60
import org .springframework .core .type .classreading .MetadataReader ;
57
61
import org .springframework .core .type .classreading .MetadataReaderFactory ;
62
+ import org .springframework .core .type .classreading .MetadataReaderLog ;
63
+ import org .springframework .core .type .classreading .SimpleMetadataReaderFactory ;
58
64
import org .springframework .core .type .filter .AssignableTypeFilter ;
59
65
import org .springframework .util .CollectionUtils ;
60
66
import org .springframework .util .StringUtils ;
80
86
*/
81
87
class ConfigurationClassParser {
82
88
89
+ protected final Log logger = LogFactory .getLog (getClass ());
90
+
83
91
private final MetadataReaderFactory metadataReaderFactory ;
84
92
85
93
private final ProblemReporter problemReporter ;
@@ -131,8 +139,8 @@ public ConfigurationClassParser(MetadataReaderFactory metadataReaderFactory,
131
139
* (assumes that this configuration class was configured via XML)
132
140
*/
133
141
public void parse (String className , String beanName ) throws IOException {
134
- MetadataReader reader = this . metadataReaderFactory . getMetadataReader (className );
135
- processConfigurationClass (new ConfigurationClass ( reader , beanName ));
142
+ ConfigurationMetadataReader reader = new ConfigurationMetadataReader (className );
143
+ processConfigurationClass (reader . getConfigurationClass ( beanName ));
136
144
}
137
145
138
146
/**
@@ -175,10 +183,10 @@ protected AnnotationMetadata doProcessConfigurationClass(
175
183
176
184
// recursively process any member (nested) classes first
177
185
for (String memberClassName : metadata .getMemberClassNames ()) {
178
- MetadataReader reader = this . metadataReaderFactory . getMetadataReader (memberClassName );
179
- AnnotationMetadata memberClassMetadata = reader .getAnnotationMetadata ();
186
+ ConfigurationMetadataReader reader = new ConfigurationMetadataReader (memberClassName );
187
+ AnnotationMetadata memberClassMetadata = reader .getReader (). getAnnotationMetadata ();
180
188
if (ConfigurationClassUtils .isConfigurationCandidate (memberClassMetadata )) {
181
- processConfigurationClass (new ConfigurationClass ( reader , configClass ));
189
+ processConfigurationClass (reader . getConfigurationClass ( configClass ));
182
190
}
183
191
}
184
192
@@ -298,7 +306,8 @@ else if (superclass.startsWith("java")) {
298
306
*/
299
307
private Set <String > getImports (String className , Set <String > imports , Set <String > visited ) throws IOException {
300
308
if (visited .add (className ) && !className .startsWith ("java" )) {
301
- AnnotationMetadata metadata = metadataReaderFactory .getMetadataReader (className ).getAnnotationMetadata ();
309
+ ConfigurationMetadataReader reader = new ConfigurationMetadataReader (className );
310
+ AnnotationMetadata metadata = reader .getReader ().getAnnotationMetadata ();
302
311
for (String annotationType : metadata .getAnnotationTypes ()) {
303
312
imports = getImports (annotationType , imports , visited );
304
313
}
@@ -322,9 +331,10 @@ private void processImport(ConfigurationClass configClass, String[] classesToImp
322
331
this .importStack .push (configClass );
323
332
AnnotationMetadata importingClassMetadata = configClass .getMetadata ();
324
333
for (String candidate : classesToImport ) {
325
- MetadataReader reader = this . metadataReaderFactory . getMetadataReader (candidate );
326
- if (new AssignableTypeFilter ( ImportSelector . class ). match (reader , this . metadataReaderFactory )) {
334
+ ConfigurationMetadataReader reader = new ConfigurationMetadataReader (candidate );
335
+ if (reader . match (ImportSelector . class )) {
327
336
// the candidate class is an ImportSelector -> delegate to it to determine imports
337
+ reader .flushLog ();
328
338
try {
329
339
ImportSelector selector = BeanUtils .instantiateClass (
330
340
this .resourceLoader .getClassLoader ().loadClass (candidate ), ImportSelector .class );
@@ -334,8 +344,9 @@ private void processImport(ConfigurationClass configClass, String[] classesToImp
334
344
throw new IllegalStateException (ex );
335
345
}
336
346
}
337
- else if (new AssignableTypeFilter ( ImportBeanDefinitionRegistrar . class ). match (reader , this . metadataReaderFactory )) {
347
+ else if (reader . match (ImportBeanDefinitionRegistrar . class )) {
338
348
// the candidate class is an ImportBeanDefinitionRegistrar -> delegate to it to register additional bean definitions
349
+ reader .flushLog ();
339
350
try {
340
351
ImportBeanDefinitionRegistrar registrar = BeanUtils .instantiateClass (
341
352
this .resourceLoader .getClassLoader ().loadClass (candidate ), ImportBeanDefinitionRegistrar .class );
@@ -349,7 +360,7 @@ else if (new AssignableTypeFilter(ImportBeanDefinitionRegistrar.class).match(rea
349
360
else {
350
361
// the candidate class not an ImportSelector or ImportBeanDefinitionRegistrar -> process it as a @Configuration class
351
362
this .importStack .registerImport (importingClassMetadata .getClassName (), candidate );
352
- processConfigurationClass (new ConfigurationClass ( reader , configClass ));
363
+ processConfigurationClass (reader . getConfigurationClass ( configClass ));
353
364
}
354
365
}
355
366
this .importStack .pop ();
@@ -474,4 +485,90 @@ public CircularImportProblem(ConfigurationClass attemptedImport, Stack<Configura
474
485
}
475
486
}
476
487
488
+
489
+ private class ConfigurationMetadataReader {
490
+
491
+ private final MetadataReader reader ;
492
+
493
+ private List <MetadataReaderLogEntry > logEntries ;
494
+
495
+ public ConfigurationMetadataReader (String className ) throws IOException {
496
+ MetadataReaderFactory factory = metadataReaderFactory ;
497
+ if (factory instanceof SimpleMetadataReaderFactory ) {
498
+ this .reader = ((SimpleMetadataReaderFactory ) factory ).getMetadataReader (
499
+ className , getLogger ());
500
+ }
501
+ else {
502
+ this .reader = factory .getMetadataReader (className );
503
+ }
504
+ }
505
+
506
+ private MetadataReaderLog getLogger () {
507
+ return new MetadataReaderLog () {
508
+
509
+ public void log (String message , Throwable t ) {
510
+ add (false , message , t );
511
+ }
512
+
513
+ private void add (boolean debug , String message , Throwable t ) {
514
+ if (logEntries == null ) {
515
+ logEntries = new ArrayList <MetadataReaderLogEntry >();
516
+ }
517
+ logEntries .add (new MetadataReaderLogEntry (debug , message , t ));
518
+ }
519
+ };
520
+ }
521
+
522
+ public boolean match (Class <?> targetType ) throws IOException {
523
+ return new AssignableTypeFilter (targetType ).match (reader ,
524
+ metadataReaderFactory );
525
+ }
526
+
527
+ public ConfigurationClass getConfigurationClass (ConfigurationClass importedBy ) {
528
+ return flushLogIfNotSkipped (new ConfigurationClass (reader , importedBy ));
529
+ }
530
+
531
+ public ConfigurationClass getConfigurationClass (String beanName ) {
532
+ return flushLogIfNotSkipped (new ConfigurationClass (reader , beanName ));
533
+ }
534
+
535
+ private ConfigurationClass flushLogIfNotSkipped (
536
+ ConfigurationClass configurationClass ) {
537
+ if (!ConditionalAnnotationHelper .shouldSkip (configurationClass , registry ,
538
+ environment , beanNameGenerator )) {
539
+ flushLog ();
540
+ }
541
+ return configurationClass ;
542
+ }
543
+
544
+ public MetadataReader getReader () {
545
+ return this .reader ;
546
+ }
547
+
548
+ public void flushLog () {
549
+ if (this .logEntries != null ) {
550
+ for (MetadataReaderLogEntry logEntry : logEntries ) {
551
+ logEntry .log ();
552
+ }
553
+ this .logEntries = null ;
554
+ }
555
+ }
556
+ }
557
+
558
+
559
+ private class MetadataReaderLogEntry {
560
+
561
+ private String message ;
562
+
563
+ private Throwable t ;
564
+
565
+ public MetadataReaderLogEntry (boolean debug , String message , Throwable t ) {
566
+ this .message = message ;
567
+ this .t = t ;
568
+ }
569
+
570
+ public void log () {
571
+ logger .debug (message , t );
572
+ }
573
+ }
477
574
}
0 commit comments