Search in sources :

Example 1 with ClassPathScanner

use of de.invesdwin.util.classpath.ClassPathScanner in project invesdwin-context-persistence by subes.

the class JpaRepositoryScanContextLocation method scanForJpaRepositories.

private Map<Class<?>, Set<Class<?>>> scanForJpaRepositories(final String basePackage) {
    final Map<Class<?>, Set<Class<?>>> jpaRepositories = new HashMap<Class<?>, Set<Class<?>>>();
    final ClassPathScanner scanner = new ClassPathScanner().withInterfacesOnly();
    scanner.addIncludeFilter(new AssignableTypeFilter(JpaRepository.class));
    final Set<BeanDefinition> candidateComponents = scanner.findCandidateComponents(basePackage);
    for (final BeanDefinition bd : candidateComponents) {
        final String beanClassName = bd.getBeanClassName();
        final Class<?> repositoryClass = Reflections.classForName(beanClassName);
        if (!IDao.class.isAssignableFrom(repositoryClass)) {
            final Class<?>[] typeArguments = Reflections.resolveTypeArguments(repositoryClass, JpaRepository.class);
            Assertions.assertThat(typeArguments.length).isEqualTo(2);
            final Class<?> entity = typeArguments[0];
            Set<Class<?>> set = jpaRepositories.get(entity);
            if (set == null) {
                set = new HashSet<Class<?>>();
                jpaRepositories.put(entity, set);
            }
            Assertions.assertThat(set.add(repositoryClass)).isTrue();
        }
    }
    return jpaRepositories;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) IDao(de.invesdwin.context.persistence.jpa.api.dao.IDao) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ClassPathScanner(de.invesdwin.util.classpath.ClassPathScanner) JpaRepository(org.springframework.data.jpa.repository.JpaRepository) AssignableTypeFilter(org.springframework.core.type.filter.AssignableTypeFilter)

Example 2 with ClassPathScanner

use of de.invesdwin.util.classpath.ClassPathScanner in project invesdwin-context-persistence by subes.

the class PersistenceUnitAnnotationUtil method scanForEntities.

public static Map<String, Set<Class<?>>> scanForEntities(final String basePackage) {
    final Map<String, Set<Class<?>>> entities = new HashMap<String, Set<Class<?>>>();
    final ClassPathScanner scanner = new ClassPathScanner();
    /*
         * http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd
         * 
         * Managed class to be included in the persistence unit and to scan for annotations. It should be annotated with
         * either @Entity, @Embeddable or @MappedSuperclass.
         * 
         * --> @Embeddable and @MappedSuperclass actually not needed here
         */
    scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
    for (final BeanDefinition bd : scanner.findCandidateComponents(basePackage)) {
        final String beanClassName = bd.getBeanClassName();
        final Class<?> entityClass = Reflections.classForName(beanClassName);
        String persistenceUnitName = PersistenceUnitAnnotationUtil.getPersistenceUnitNameFromPersistenceUnitNameAnnotation(entityClass);
        if (Strings.isBlank(persistenceUnitName)) {
            persistenceUnitName = PersistenceProperties.DEFAULT_PERSISTENCE_UNIT_NAME;
        }
        Set<Class<?>> set = entities.get(persistenceUnitName);
        if (set == null) {
            set = new HashSet<Class<?>>();
            entities.put(persistenceUnitName, set);
        }
        Assertions.assertThat(set.add(entityClass)).isTrue();
    }
    return entities;
}
Also used : AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) Entity(javax.persistence.Entity) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ClassPathScanner(de.invesdwin.util.classpath.ClassPathScanner) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Aggregations

ClassPathScanner (de.invesdwin.util.classpath.ClassPathScanner)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)2 IDao (de.invesdwin.context.persistence.jpa.api.dao.IDao)1 Entity (javax.persistence.Entity)1 AnnotationTypeFilter (org.springframework.core.type.filter.AnnotationTypeFilter)1 AssignableTypeFilter (org.springframework.core.type.filter.AssignableTypeFilter)1 JpaRepository (org.springframework.data.jpa.repository.JpaRepository)1