1919import static org .springframework .context .annotation .MetadataUtils .attributesFor ;
2020
2121import java .io .IOException ;
22+ import java .util .ArrayList ;
2223import java .util .Arrays ;
2324import java .util .Collections ;
2425import java .util .Comparator ;
2526import java .util .HashMap ;
2627import java .util .HashSet ;
2728import java .util .Iterator ;
2829import java .util .LinkedHashSet ;
30+ import java .util .List ;
2931import java .util .Map ;
3032import java .util .Set ;
3133import java .util .Stack ;
3234
35+ import org .apache .commons .logging .Log ;
36+ import org .apache .commons .logging .LogFactory ;
3337import org .springframework .beans .BeanUtils ;
3438import org .springframework .beans .factory .Aware ;
3539import org .springframework .beans .factory .BeanClassLoaderAware ;
5559import org .springframework .core .type .StandardAnnotationMetadata ;
5660import org .springframework .core .type .classreading .MetadataReader ;
5761import org .springframework .core .type .classreading .MetadataReaderFactory ;
62+ import org .springframework .core .type .classreading .MetadataReaderLog ;
63+ import org .springframework .core .type .classreading .SimpleMetadataReaderFactory ;
5864import org .springframework .core .type .filter .AssignableTypeFilter ;
5965import org .springframework .util .CollectionUtils ;
6066import org .springframework .util .StringUtils ;
8086 */
8187class ConfigurationClassParser {
8288
89+ protected final Log logger = LogFactory .getLog (getClass ());
90+
8391 private final MetadataReaderFactory metadataReaderFactory ;
8492
8593 private final ProblemReporter problemReporter ;
@@ -131,8 +139,8 @@ public ConfigurationClassParser(MetadataReaderFactory metadataReaderFactory,
131139 * (assumes that this configuration class was configured via XML)
132140 */
133141 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 ));
136144 }
137145
138146 /**
@@ -175,10 +183,10 @@ protected AnnotationMetadata doProcessConfigurationClass(
175183
176184 // recursively process any member (nested) classes first
177185 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 ();
180188 if (ConfigurationClassUtils .isConfigurationCandidate (memberClassMetadata )) {
181- processConfigurationClass (new ConfigurationClass ( reader , configClass ));
189+ processConfigurationClass (reader . getConfigurationClass ( configClass ));
182190 }
183191 }
184192
@@ -298,7 +306,8 @@ else if (superclass.startsWith("java")) {
298306 */
299307 private Set <String > getImports (String className , Set <String > imports , Set <String > visited ) throws IOException {
300308 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 ();
302311 for (String annotationType : metadata .getAnnotationTypes ()) {
303312 imports = getImports (annotationType , imports , visited );
304313 }
@@ -322,9 +331,10 @@ private void processImport(ConfigurationClass configClass, String[] classesToImp
322331 this .importStack .push (configClass );
323332 AnnotationMetadata importingClassMetadata = configClass .getMetadata ();
324333 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 )) {
327336 // the candidate class is an ImportSelector -> delegate to it to determine imports
337+ reader .flushLog ();
328338 try {
329339 ImportSelector selector = BeanUtils .instantiateClass (
330340 this .resourceLoader .getClassLoader ().loadClass (candidate ), ImportSelector .class );
@@ -334,8 +344,9 @@ private void processImport(ConfigurationClass configClass, String[] classesToImp
334344 throw new IllegalStateException (ex );
335345 }
336346 }
337- else if (new AssignableTypeFilter ( ImportBeanDefinitionRegistrar . class ). match (reader , this . metadataReaderFactory )) {
347+ else if (reader . match (ImportBeanDefinitionRegistrar . class )) {
338348 // the candidate class is an ImportBeanDefinitionRegistrar -> delegate to it to register additional bean definitions
349+ reader .flushLog ();
339350 try {
340351 ImportBeanDefinitionRegistrar registrar = BeanUtils .instantiateClass (
341352 this .resourceLoader .getClassLoader ().loadClass (candidate ), ImportBeanDefinitionRegistrar .class );
@@ -349,7 +360,7 @@ else if (new AssignableTypeFilter(ImportBeanDefinitionRegistrar.class).match(rea
349360 else {
350361 // the candidate class not an ImportSelector or ImportBeanDefinitionRegistrar -> process it as a @Configuration class
351362 this .importStack .registerImport (importingClassMetadata .getClassName (), candidate );
352- processConfigurationClass (new ConfigurationClass ( reader , configClass ));
363+ processConfigurationClass (reader . getConfigurationClass ( configClass ));
353364 }
354365 }
355366 this .importStack .pop ();
@@ -474,4 +485,90 @@ public CircularImportProblem(ConfigurationClass attemptedImport, Stack<Configura
474485 }
475486 }
476487
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+ }
477574}
0 commit comments