Search in sources :

Example 16 with MetadataReader

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

the class AssignableTypeFilterTests method interfaceThroughSuperClassMatch.

@Test
public void interfaceThroughSuperClassMatch() throws Exception {
    MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
    String classUnderTest = "org.springframework.core.type.AssignableTypeFilterTests$SomeDaoLikeImpl";
    MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);
    AssignableTypeFilter filter = new AssignableTypeFilter(JdbcDaoSupport.class);
    assertTrue(filter.match(metadataReader, metadataReaderFactory));
    ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
}
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) AssignableTypeFilter(org.springframework.core.type.filter.AssignableTypeFilter) Test(org.junit.Test)

Example 17 with MetadataReader

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

the class CachingMetadataReaderLeakTests method testSignificantLoad.

@Test
public void testSignificantLoad() throws Exception {
    Assume.group(TestGroup.LONG_RUNNING);
    // the biggest public class in the JDK (>60k)
    URL url = getClass().getResource("/java/awt/Component.class");
    assertThat(url, notNullValue());
    // look at a LOT of items
    for (int i = 0; i < ITEMS_TO_LOAD; i++) {
        Resource resource = new UrlResource(url) {

            @Override
            public boolean equals(Object obj) {
                return (obj == this);
            }

            @Override
            public int hashCode() {
                return System.identityHashCode(this);
            }
        };
        MetadataReader reader = mrf.getMetadataReader(resource);
        assertThat(reader, notNullValue());
    }
// useful for profiling to take snapshots
// System.in.read();
}
Also used : UrlResource(org.springframework.core.io.UrlResource) UrlResource(org.springframework.core.io.UrlResource) Resource(org.springframework.core.io.Resource) MetadataReader(org.springframework.core.type.classreading.MetadataReader) URL(java.net.URL) Test(org.junit.Test)

Example 18 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 19 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 20 with MetadataReader

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

the class ConcurrentReferenceCachingMetadataReaderFactoryTests method getMetadataReaderUsesCache.

@Test
public void getMetadataReaderUsesCache() throws Exception {
    TestConcurrentReferenceCachingMetadataReaderFactory factory = spy(new TestConcurrentReferenceCachingMetadataReaderFactory());
    MetadataReader metadataReader1 = factory.getMetadataReader(getClass().getName());
    MetadataReader metadataReader2 = factory.getMetadataReader(getClass().getName());
    assertThat(metadataReader1).isSameAs(metadataReader2);
    verify(factory, times(1)).createMetadataReader((Resource) any());
}
Also used : MetadataReader(org.springframework.core.type.classreading.MetadataReader) Test(org.junit.Test)

Aggregations

MetadataReader (org.springframework.core.type.classreading.MetadataReader)36 MetadataReaderFactory (org.springframework.core.type.classreading.MetadataReaderFactory)26 SimpleMetadataReaderFactory (org.springframework.core.type.classreading.SimpleMetadataReaderFactory)24 Test (org.junit.Test)23 IOException (java.io.IOException)7 AnnotationTypeFilter (org.springframework.core.type.filter.AnnotationTypeFilter)7 Resource (org.springframework.core.io.Resource)6 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)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 AnnotationMetadata (org.springframework.core.type.AnnotationMetadata)2 AspectJTypeFilter (org.springframework.core.type.filter.AspectJTypeFilter)2 ArrayList (java.util.ArrayList)1 TreeSet (java.util.TreeSet)1