Search in sources :

Example 1 with UncategorizedMappingException

use of org.springframework.oxm.UncategorizedMappingException in project spring-framework by spring-projects.

the class ClassPathJaxb2TypeScanner method scanPackages.

/**
	 * Scan the packages for classes marked with JAXB2 annotations.
	 * @throws UncategorizedMappingException in case of errors
	 */
public Class<?>[] scanPackages() throws UncategorizedMappingException {
    try {
        List<Class<?>> jaxb2Classes = new ArrayList<>();
        for (String packageToScan : this.packagesToScan) {
            String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(packageToScan) + RESOURCE_PATTERN;
            Resource[] resources = this.resourcePatternResolver.getResources(pattern);
            MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver);
            for (Resource resource : resources) {
                MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
                if (isJaxb2Class(metadataReader, metadataReaderFactory)) {
                    String className = metadataReader.getClassMetadata().getClassName();
                    Class<?> jaxb2AnnotatedClass = this.resourcePatternResolver.getClassLoader().loadClass(className);
                    jaxb2Classes.add(jaxb2AnnotatedClass);
                }
            }
        }
        return jaxb2Classes.toArray(new Class<?>[jaxb2Classes.size()]);
    } catch (IOException ex) {
        throw new UncategorizedMappingException("Failed to scan classpath for unlisted classes", ex);
    } catch (ClassNotFoundException ex) {
        throw new UncategorizedMappingException("Failed to load annotated classes from classpath", ex);
    }
}
Also used : CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) ArrayList(java.util.ArrayList) Resource(org.springframework.core.io.Resource) MetadataReader(org.springframework.core.type.classreading.MetadataReader) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) IOException(java.io.IOException) UncategorizedMappingException(org.springframework.oxm.UncategorizedMappingException)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Resource (org.springframework.core.io.Resource)1 CachingMetadataReaderFactory (org.springframework.core.type.classreading.CachingMetadataReaderFactory)1 MetadataReader (org.springframework.core.type.classreading.MetadataReader)1 MetadataReaderFactory (org.springframework.core.type.classreading.MetadataReaderFactory)1 UncategorizedMappingException (org.springframework.oxm.UncategorizedMappingException)1