com.cmeza.sdgenerator.support.ScanningConfigurationSupport Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-data-generator Show documentation
Show all versions of spring-data-generator Show documentation
Spring Data Generator for JPA repositories and managers.
The newest version!
package com.cmeza.sdgenerator.support;
import com.cmeza.sdgenerator.annotation.SDGenerate;
import com.cmeza.sdgenerator.annotation.SDNoGenerate;
import com.cmeza.sdgenerator.provider.ClassPathScanningProvider;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import javax.persistence.Entity;
import java.util.*;
/**
* Created by carlos on 08/04/17.
*/
public class ScanningConfigurationSupport {
private final Environment environment;
private final AnnotationAttributes attributes;
private final AnnotationMetadata annotationMetadata;
private final String[] entityPackage;
private final boolean onlyAnnotations;
public ScanningConfigurationSupport(AnnotationMetadata annotationMetadata, AnnotationAttributes attributes, Environment environment){
Assert.notNull(environment, "Environment must not be null!");
Assert.notNull(environment, "AnnotationMetadata must not be null!");
this.environment = environment;
this.attributes = attributes;
this.annotationMetadata = annotationMetadata;
this.entityPackage = this.attributes.getStringArray("entityPackage");
this.onlyAnnotations = this.attributes.getBoolean("onlyAnnotations");
}
public ScanningConfigurationSupport(String[] entityPackage, boolean onlyAnnotations) {
this.entityPackage = entityPackage;
this.onlyAnnotations = onlyAnnotations;
this.environment = null;
this.annotationMetadata = null;
this.attributes = null;
}
public Iterable getBasePackages() {
if (entityPackage.length == 0) {
String className = this.annotationMetadata.getClassName();
return Collections.singleton(ClassUtils.getPackageName(className));
} else {
return new HashSet<>(Arrays.asList(entityPackage));
}
}
public Collection getCandidates(ResourceLoader resourceLoader) {
if(this.getBasePackages() == null){
return Collections.emptyList();
}
ClassPathScanningProvider scanner = new ClassPathScanningProvider();
scanner.setResourceLoader(resourceLoader);
if (environment != null) {
scanner.setEnvironment(this.environment);
}
scanner.setIncludeAnnotation(SDGenerate.class);
scanner.setExcludeAnnotation(SDNoGenerate.class);
if (!onlyAnnotations) {
scanner.setIncludeAnnotation(Entity.class);
scanner.setIncludeAnnotation(jakarta.persistence.Entity.class);
}
Iterator filterPackages = this.getBasePackages().iterator();
HashSet candidates = new HashSet<>();
while(filterPackages.hasNext()) {
String basePackage = filterPackages.next();
Set candidate = scanner.findCandidateComponents(basePackage);
candidates.addAll(candidate);
}
return candidates;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy