use of org.springframework.core.type.classreading.MetadataReader in project spring-boot by spring-projects.
the class ConcurrentReferenceCachingMetadataReaderFactory method getMetadataReader.
@Override
public MetadataReader getMetadataReader(Resource resource) throws IOException {
MetadataReader metadataReader = this.cache.get(resource);
if (metadataReader == null) {
metadataReader = createMetadataReader(resource);
this.cache.put(resource, metadataReader);
}
return metadataReader;
}
use of org.springframework.core.type.classreading.MetadataReader in project spring-framework by spring-projects.
the class ConfigurationClassUtils method checkConfigurationClassCandidate.
/**
* Check whether the given bean definition is a candidate for a configuration class
* (or a nested component class declared within a configuration/component class,
* to be auto-registered as well), and mark it accordingly.
* @param beanDef the bean definition to check
* @param metadataReaderFactory the current factory in use by the caller
* @return whether the candidate qualifies as (any kind of) configuration class
*/
public static boolean checkConfigurationClassCandidate(BeanDefinition beanDef, MetadataReaderFactory metadataReaderFactory) {
String className = beanDef.getBeanClassName();
if (className == null) {
return false;
}
AnnotationMetadata metadata;
if (beanDef instanceof AnnotatedBeanDefinition && className.equals(((AnnotatedBeanDefinition) beanDef).getMetadata().getClassName())) {
// Can reuse the pre-parsed metadata from the given BeanDefinition...
metadata = ((AnnotatedBeanDefinition) beanDef).getMetadata();
} else if (beanDef instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) beanDef).hasBeanClass()) {
// Check already loaded Class if present...
// since we possibly can't even load the class file for this Class.
Class<?> beanClass = ((AbstractBeanDefinition) beanDef).getBeanClass();
metadata = new StandardAnnotationMetadata(beanClass, true);
} else {
try {
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(className);
metadata = metadataReader.getAnnotationMetadata();
} catch (IOException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Could not find class file for introspecting configuration annotations: " + className, ex);
}
return false;
}
}
if (isFullConfigurationCandidate(metadata)) {
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL);
} else if (isLiteConfigurationCandidate(metadata)) {
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE);
} else {
return false;
}
// It's a full or lite configuration candidate... Let's determine the order value, if any.
Map<String, Object> orderAttributes = metadata.getAnnotationAttributes(Order.class.getName());
if (orderAttributes != null) {
beanDef.setAttribute(ORDER_ATTRIBUTE, orderAttributes.get(AnnotationUtils.VALUE));
}
return true;
}
use of org.springframework.core.type.classreading.MetadataReader in project spring-boot by spring-projects.
the class FilterAnnotationsTests method match.
private boolean match(FilterAnnotations filterAnnotations, Class<?> type) throws IOException {
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(type.getName());
return filterAnnotations.anyMatches(metadataReader, metadataReaderFactory);
}
use of org.springframework.core.type.classreading.MetadataReader in project spring-boot by spring-projects.
the class TypeExcludeFiltersContextCustomizerFactoryTests method getContextCustomizerShouldAddExcludeFilters.
@Test
public void getContextCustomizerShouldAddExcludeFilters() throws Exception {
ContextCustomizer customizer = this.factory.createContextCustomizer(WithExcludeFilters.class, null);
customizer.customizeContext(this.context, this.mergedContextConfiguration);
this.context.refresh();
TypeExcludeFilter filter = this.context.getBean(TypeExcludeFilter.class);
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(NoAnnotation.class.getName());
assertThat(filter.match(metadataReader, metadataReaderFactory)).isFalse();
metadataReader = metadataReaderFactory.getMetadataReader(SimpleExclude.class.getName());
assertThat(filter.match(metadataReader, metadataReaderFactory)).isTrue();
metadataReader = metadataReaderFactory.getMetadataReader(TestClassAwareExclude.class.getName());
assertThat(filter.match(metadataReader, metadataReaderFactory)).isTrue();
}
use of org.springframework.core.type.classreading.MetadataReader in project spring-boot by spring-projects.
the class ConcurrentReferenceCachingMetadataReaderFactoryTests method clearResetsCache.
@Test
public void clearResetsCache() throws Exception {
TestConcurrentReferenceCachingMetadataReaderFactory factory = spy(new TestConcurrentReferenceCachingMetadataReaderFactory());
MetadataReader metadataReader1 = factory.getMetadataReader(getClass().getName());
factory.clearCache();
MetadataReader metadataReader2 = factory.getMetadataReader(getClass().getName());
assertThat(metadataReader1).isNotEqualTo(sameInstance(metadataReader2));
verify(factory, times(2)).createMetadataReader((Resource) any());
}
Aggregations