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;
}
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;
}
Aggregations