Search in sources :

Example 21 with SubTypesScanner

use of org.reflections.scanners.SubTypesScanner 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);
}
Also used : Resources(com.google.common.io.Resources) URL(java.net.URL) UTF_8(java.nio.charset.StandardCharsets.UTF_8) IOException(java.io.IOException) Reflections(org.reflections.Reflections) Collectors(java.util.stream.Collectors) SubTypesScanner(org.reflections.scanners.SubTypesScanner) ArrayList(java.util.ArrayList) List(java.util.List) DtoModel(org.eclipse.che.plugin.typescript.dto.model.DtoModel) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) ST(org.stringtemplate.v4.ST) ClasspathHelper.forJavaClassPath(org.reflections.util.ClasspathHelper.forJavaClassPath) DTO(org.eclipse.che.dto.shared.DTO) ClasspathHelper.forClassLoader(org.reflections.util.ClasspathHelper.forClassLoader) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) SubTypesScanner(org.reflections.scanners.SubTypesScanner) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) ArrayList(java.util.ArrayList) DTO(org.eclipse.che.dto.shared.DTO) Reflections(org.reflections.Reflections)

Example 22 with SubTypesScanner

use of org.reflections.scanners.SubTypesScanner 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();
    }
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URL(java.net.URL) BufferedWriter(java.io.BufferedWriter) SubTypesScanner(org.reflections.scanners.SubTypesScanner) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) File(java.io.File) DTO(org.eclipse.che.dto.shared.DTO) Reflections(org.reflections.Reflections)

Example 23 with SubTypesScanner

use of org.reflections.scanners.SubTypesScanner in project querydsl by querydsl.

the class ClassPathUtils method scanPackage.

/**
     * Return the classes from the given package and subpackages using the supplied classloader
     *
     * @param classLoader classloader to be used
     * @param pkg package to scan
     * @return set of found classes
     * @throws IOException
     */
public static Set<Class<?>> scanPackage(ClassLoader classLoader, String pkg) throws IOException {
    Reflections reflections = new Reflections(new ConfigurationBuilder().addUrls(ClasspathHelper.forPackage(pkg, classLoader)).addClassLoader(classLoader).setScanners(new SubTypesScanner(false)));
    Set<Class<?>> classes = new HashSet<Class<?>>();
    for (String typeNames : reflections.getStore().get(SubTypesScanner.class.getSimpleName()).values()) {
        Class<?> clazz = safeClassForName(classLoader, typeNames);
        if (clazz != null) {
            classes.add(clazz);
        }
    }
    return classes;
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) SubTypesScanner(org.reflections.scanners.SubTypesScanner) Reflections(org.reflections.Reflections) HashSet(java.util.HashSet)

Example 24 with SubTypesScanner

use of org.reflections.scanners.SubTypesScanner 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;
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) SubTypesScanner(org.reflections.scanners.SubTypesScanner) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) ResourcesScanner(org.reflections.scanners.ResourcesScanner) Reflections(org.reflections.Reflections)

Example 25 with SubTypesScanner

use of org.reflections.scanners.SubTypesScanner in project asterixdb by apache.

the class ExceptionTest method test.

@Test
public void test() throws Exception {
    // Tests all usual type computers.
    Reflections reflections = new Reflections("org.apache.asterix.om.typecomputer", new SubTypesScanner(false));
    Set<Class<? extends IResultTypeComputer>> classes = reflections.getSubTypesOf(IResultTypeComputer.class);
    int numTypeComputers = 0;
    for (Class<? extends IResultTypeComputer> c : classes) {
        if (Modifier.isAbstract(c.getModifiers())) {
            continue;
        }
        testTypeComputer(c);
        ++numTypeComputers;
    }
    // Currently, there are 83 type computers.
    Assert.assertTrue(numTypeComputers >= 83);
}
Also used : IResultTypeComputer(org.apache.asterix.om.typecomputer.base.IResultTypeComputer) SubTypesScanner(org.reflections.scanners.SubTypesScanner) Reflections(org.reflections.Reflections) Test(org.junit.Test)

Aggregations

Reflections (org.reflections.Reflections)41 SubTypesScanner (org.reflections.scanners.SubTypesScanner)41 ConfigurationBuilder (org.reflections.util.ConfigurationBuilder)29 TypeAnnotationsScanner (org.reflections.scanners.TypeAnnotationsScanner)18 HashSet (java.util.HashSet)9 FilterBuilder (org.reflections.util.FilterBuilder)9 ResourcesScanner (org.reflections.scanners.ResourcesScanner)8 URL (java.net.URL)7 ArrayList (java.util.ArrayList)7 FieldAnnotationsScanner (org.reflections.scanners.FieldAnnotationsScanner)6 Set (java.util.Set)5 Collectors (java.util.stream.Collectors)5 File (java.io.File)4 List (java.util.List)4 Bean (org.springframework.context.annotation.Bean)4 HashMap (java.util.HashMap)3 Slf4j (lombok.extern.slf4j.Slf4j)3 MethodAnnotationsScanner (org.reflections.scanners.MethodAnnotationsScanner)3 MethodParameterScanner (org.reflections.scanners.MethodParameterScanner)3 IOException (java.io.IOException)2