Search in sources :

Example 71 with Reflections

use of org.reflections.Reflections in project cas by apereo.

the class CasEmbeddedContainerUtils method getLoggingInitialization.

/**
 * Gets logging initialization.
 *
 * @return the logging initialization
 */
@SneakyThrows
public static Optional<LoggingInitialization> getLoggingInitialization() {
    val packageName = CasEmbeddedContainerUtils.class.getPackage().getName();
    val reflections = new Reflections(new ConfigurationBuilder().filterInputsBy(new FilterBuilder().includePackage(packageName)).setUrls(ClasspathHelper.forPackage(packageName)));
    val subTypes = reflections.getSubTypesOf(LoggingInitialization.class);
    return subTypes.isEmpty() ? Optional.empty() : Optional.of(subTypes.iterator().next().getDeclaredConstructor().newInstance());
}
Also used : lombok.val(lombok.val) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) FilterBuilder(org.reflections.util.FilterBuilder) Reflections(org.reflections.Reflections) SneakyThrows(lombok.SneakyThrows)

Example 72 with Reflections

use of org.reflections.Reflections in project cas by apereo.

the class CasEmbeddedContainerUtils method getCasBannerInstance.

/**
 * Gets cas banner instance.
 *
 * @return the cas banner instance
 */
public static Banner getCasBannerInstance() {
    val packageName = CasEmbeddedContainerUtils.class.getPackage().getName();
    val reflections = new Reflections(new ConfigurationBuilder().filterInputsBy(new FilterBuilder().includePackage(packageName)).setExpandSuperTypes(true).setUrls(ClasspathHelper.forPackage(packageName)));
    val subTypes = reflections.getSubTypesOf(AbstractCasBanner.class);
    subTypes.remove(DefaultCasBanner.class);
    if (subTypes.isEmpty()) {
        return new DefaultCasBanner();
    }
    try {
        val clz = subTypes.iterator().next();
        return clz.getDeclaredConstructor().newInstance();
    } catch (final Exception e) {
        LoggingUtils.error(LOGGER, e);
    }
    return new DefaultCasBanner();
}
Also used : lombok.val(lombok.val) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) FilterBuilder(org.reflections.util.FilterBuilder) DefaultCasBanner(org.apereo.cas.util.spring.boot.DefaultCasBanner) Reflections(org.reflections.Reflections)

Example 73 with Reflections

use of org.reflections.Reflections 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 74 with Reflections

use of org.reflections.Reflections 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 75 with Reflections

use of org.reflections.Reflections in project che by eclipse.

the class ExtensionManagerGenerator method findExtensions.

/**
     * Find all the Java Classes that have proper @Extension declaration
     *
     * @throws IOException
     */
@SuppressWarnings("unchecked")
public static void findExtensions() throws IOException {
    Reflections reflection = new Reflections(getConfigurationBuilder());
    Set<Class<?>> classes = reflection.getTypesAnnotatedWith(Extension.class);
    for (Class clazz : classes) {
        EXTENSIONS_FQN.put(clazz.getCanonicalName(), clazz.getSimpleName());
        System.out.println(String.format("New Extension Found: %s", clazz.getCanonicalName()));
    }
    System.out.println(String.format("Found: %d extensions", EXTENSIONS_FQN.size()));
}
Also used : Reflections(org.reflections.Reflections)

Aggregations

Reflections (org.reflections.Reflections)160 ConfigurationBuilder (org.reflections.util.ConfigurationBuilder)54 SubTypesScanner (org.reflections.scanners.SubTypesScanner)41 ArrayList (java.util.ArrayList)26 Set (java.util.Set)23 ResourcesScanner (org.reflections.scanners.ResourcesScanner)21 Test (org.junit.Test)20 FilterBuilder (org.reflections.util.FilterBuilder)20 HashSet (java.util.HashSet)19 URL (java.net.URL)18 TypeAnnotationsScanner (org.reflections.scanners.TypeAnnotationsScanner)18 IOException (java.io.IOException)17 Collectors (java.util.stream.Collectors)17 Method (java.lang.reflect.Method)16 List (java.util.List)15 File (java.io.File)13 InputStream (java.io.InputStream)9 Field (java.lang.reflect.Field)9 MethodAnnotationsScanner (org.reflections.scanners.MethodAnnotationsScanner)9 ClasspathHelper (org.reflections.util.ClasspathHelper)9