use of org.reflections.scanners.TypeAnnotationsScanner in project che by eclipse.
the class TypeScriptDtoGenerator method init.
/**
* Init stuff is responsible to grab all DTOs found in classpath (classloader) and setup model for String Template
*/
protected void init() {
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder().setScanners(new SubTypesScanner(), new TypeAnnotationsScanner());
if (useClassPath) {
configurationBuilder.setUrls(forJavaClassPath());
} else {
configurationBuilder.setUrls(forClassLoader());
}
// keep only DTO interfaces
Reflections reflections = new Reflections(configurationBuilder);
List<Class<?>> annotatedWithDtos = new ArrayList<>(reflections.getTypesAnnotatedWith(DTO.class));
List<Class<?>> interfacesDtos = annotatedWithDtos.stream().filter(clazz -> clazz.isInterface()).collect(Collectors.toList());
interfacesDtos.stream().forEach(this::analyze);
}
use of org.reflections.scanners.TypeAnnotationsScanner in project che by eclipse.
the class DtoGenerator method generate.
public void generate() {
Set<URL> urls = getClasspathForPackages(dtoPackages);
genFileName = genFileName.replace('/', File.separatorChar);
String outputFilePath = genFileName;
// Extract the name of the output file that will contain all the DTOs and its package.
String myPackageBase = packageBase;
if (!myPackageBase.endsWith("/")) {
myPackageBase += "/";
}
myPackageBase = myPackageBase.replace('/', File.separatorChar);
int packageStart = outputFilePath.lastIndexOf(myPackageBase) + myPackageBase.length();
int packageEnd = outputFilePath.lastIndexOf(File.separatorChar);
String fileName = outputFilePath.substring(packageEnd + 1);
String className = fileName.substring(0, fileName.indexOf(".java"));
String packageName = outputFilePath.substring(packageStart, packageEnd).replace(File.separatorChar, '.');
File outFile = new File(outputFilePath);
try {
DtoTemplate dtoTemplate = new DtoTemplate(packageName, className, impl);
Reflections reflection = new Reflections(new ConfigurationBuilder().setUrls(urls).setScanners(new SubTypesScanner(), new TypeAnnotationsScanner()));
List<Class<?>> dtos = new ArrayList<>(reflection.getTypesAnnotatedWith(DTO.class));
// We sort alphabetically to ensure deterministic order of routing types.
Collections.sort(dtos, new ClassesComparator());
for (Class<?> clazz : dtos) {
// DTO are interface
if (clazz.isInterface()) {
dtoTemplate.addInterface(clazz);
}
}
reflection = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forClassLoader()).setScanners(new SubTypesScanner(), new TypeAnnotationsScanner()));
List<Class<?>> dtosDependencies = new ArrayList<>(reflection.getTypesAnnotatedWith(DTO.class));
dtosDependencies.removeAll(dtos);
reflection = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forClassLoader()).setScanners(new SubTypesScanner()));
for (Class<?> clazz : dtosDependencies) {
for (Class impl : reflection.getSubTypesOf(clazz)) {
if (!(impl.isInterface() || urls.contains(impl.getProtectionDomain().getCodeSource().getLocation()))) {
if ("client".equals(dtoTemplate.getImplType())) {
if (isClientImpl(impl)) {
dtoTemplate.addImplementation(clazz, impl);
}
} else if ("server".equals(dtoTemplate.getImplType())) {
if (isServerImpl(impl)) {
dtoTemplate.addImplementation(clazz, impl);
}
}
}
}
}
// Emit the generated file.
Files.createDirectories(outFile.toPath().getParent());
try (BufferedWriter writer = new BufferedWriter(new FileWriter(outFile))) {
writer.write(dtoTemplate.toString());
}
if ("server".equals(impl)) {
// Create file in META-INF/services/
File outServiceFile = new File(myPackageBase + "META-INF/services/" + DtoFactoryVisitor.class.getCanonicalName());
Files.createDirectories(outServiceFile.toPath().getParent());
try (BufferedWriter serviceFileWriter = new BufferedWriter(new FileWriter(outServiceFile))) {
serviceFileWriter.write(packageName + "." + className);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
use of org.reflections.scanners.TypeAnnotationsScanner in project swagger-core by swagger-api.
the class ReflectiveJaxrsScanner method getReflections.
protected Reflections getReflections() {
if (reflections == null) {
ConfigurationBuilder config = new ConfigurationBuilder();
acceptablePackages = new HashSet<String>();
if (resourcePackage != "") {
String[] parts = resourcePackage.split(",");
for (String pkg : parts) {
if (!"".equals(pkg)) {
acceptablePackages.add(pkg);
config.addUrls(ClasspathHelper.forPackage(pkg));
}
}
}
config.setScanners(new ResourcesScanner(), new TypeAnnotationsScanner(), new SubTypesScanner());
this.reflections = new Reflections(config);
}
return this.reflections;
}
use of org.reflections.scanners.TypeAnnotationsScanner in project kanonizo by kanonizo.
the class Util method getReflections.
public static Reflections getReflections() {
if (r == null) {
Set<URL> packages = new HashSet<>(ClasspathHelper.forPackage("org.kanonizo"));
packages.addAll(ClasspathHelper.forPackage("com.scythe"));
r = new Reflections(new ConfigurationBuilder().setUrls(packages).setScanners(new SubTypesScanner(), new TypeAnnotationsScanner(), new FieldAnnotationsScanner()));
}
return r;
}
use of org.reflections.scanners.TypeAnnotationsScanner in project serenity-jbehave by serenity-bdd.
the class ClassFinder method annotatedClassesInPackage.
public List<Class<?>> annotatedClassesInPackage(String packageName) {
Reflections reflections = new Reflections(packageName, new SubTypesScanner(), new TypeAnnotationsScanner(), new MethodAnnotationsScanner(), new ResourcesScanner(), getClassLoader());
Set<Class<?>> matchingClasses = new HashSet<>();
for (Class<? extends Annotation> expectedAnnotation : expectedAnnotations) {
matchingClasses.addAll(reflections.getTypesAnnotatedWith(expectedAnnotation));
matchingClasses.addAll(classesFrom(reflections.getMethodsAnnotatedWith(expectedAnnotation)));
}
return ImmutableList.copyOf(matchingClasses);
}
Aggregations