Search in sources :

Example 6 with MetadataReader

use of org.springframework.core.type.classreading.MetadataReader in project spring-boot by spring-projects.

the class ManagementContextConfigurationsImportSelector method getConfiguration.

private void getConfiguration(SimpleMetadataReaderFactory readerFactory, List<ManagementConfiguration> configurations, String className) {
    try {
        MetadataReader metadataReader = readerFactory.getMetadataReader(className);
        configurations.add(new ManagementConfiguration(metadataReader));
    } catch (IOException ex) {
        throw new RuntimeException("Failed to read annotation metadata for '" + className + "'", ex);
    }
}
Also used : MetadataReader(org.springframework.core.type.classreading.MetadataReader) IOException(java.io.IOException)

Example 7 with MetadataReader

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;
}
Also used : MetadataReader(org.springframework.core.type.classreading.MetadataReader)

Example 8 with 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;
}
Also used : Order(org.springframework.core.annotation.Order) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) StandardAnnotationMetadata(org.springframework.core.type.StandardAnnotationMetadata) MetadataReader(org.springframework.core.type.classreading.MetadataReader) IOException(java.io.IOException) AnnotationMetadata(org.springframework.core.type.AnnotationMetadata) StandardAnnotationMetadata(org.springframework.core.type.StandardAnnotationMetadata)

Example 9 with MetadataReader

use of org.springframework.core.type.classreading.MetadataReader in project spring-framework by spring-projects.

the class DefaultPersistenceUnitManager method scanPackage.

private void scanPackage(SpringPersistenceUnitInfo scannedUnit, String pkg) {
    try {
        String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(pkg) + CLASS_RESOURCE_PATTERN;
        Resource[] resources = this.resourcePatternResolver.getResources(pattern);
        MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver);
        for (Resource resource : resources) {
            if (resource.isReadable()) {
                MetadataReader reader = readerFactory.getMetadataReader(resource);
                String className = reader.getClassMetadata().getClassName();
                if (matchesFilter(reader, readerFactory)) {
                    scannedUnit.addManagedClassName(className);
                    if (scannedUnit.getPersistenceUnitRootUrl() == null) {
                        URL url = resource.getURL();
                        if (ResourceUtils.isJarURL(url)) {
                            scannedUnit.setPersistenceUnitRootUrl(ResourceUtils.extractJarFileURL(url));
                        }
                    }
                } else if (className.endsWith(PACKAGE_INFO_SUFFIX)) {
                    scannedUnit.addManagedPackage(className.substring(0, className.length() - PACKAGE_INFO_SUFFIX.length()));
                }
            }
        }
    } catch (IOException ex) {
        throw new PersistenceException("Failed to scan classpath for unlisted entity classes", ex);
    }
}
Also used : CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) Resource(org.springframework.core.io.Resource) PersistenceException(javax.persistence.PersistenceException) MetadataReader(org.springframework.core.type.classreading.MetadataReader) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) IOException(java.io.IOException) URL(java.net.URL)

Example 10 with MetadataReader

use of org.springframework.core.type.classreading.MetadataReader in project spring-framework by spring-projects.

the class AnnotationMetadataTests method asmAnnotationMetadataForSubclass.

@Test
public void asmAnnotationMetadataForSubclass() throws Exception {
    MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
    MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(AnnotatedComponentSubClass.class.getName());
    AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
    doTestSubClassAnnotationInfo(metadata);
}
Also used : MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) SimpleMetadataReaderFactory(org.springframework.core.type.classreading.SimpleMetadataReaderFactory) SimpleMetadataReaderFactory(org.springframework.core.type.classreading.SimpleMetadataReaderFactory) MetadataReader(org.springframework.core.type.classreading.MetadataReader) Test(org.junit.Test)

Aggregations

MetadataReader (org.springframework.core.type.classreading.MetadataReader)37 MetadataReaderFactory (org.springframework.core.type.classreading.MetadataReaderFactory)27 SimpleMetadataReaderFactory (org.springframework.core.type.classreading.SimpleMetadataReaderFactory)24 Test (org.junit.Test)23 IOException (java.io.IOException)7 Resource (org.springframework.core.io.Resource)7 AnnotationTypeFilter (org.springframework.core.type.filter.AnnotationTypeFilter)7 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)5 AssignableTypeFilter (org.springframework.core.type.filter.AssignableTypeFilter)5 AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)4 CachingMetadataReaderFactory (org.springframework.core.type.classreading.CachingMetadataReaderFactory)4 AnnotationMetadata (org.springframework.core.type.AnnotationMetadata)3 URL (java.net.URL)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)2 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)2 AspectJTypeFilter (org.springframework.core.type.filter.AspectJTypeFilter)2 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1