Search in sources :

Example 41 with Reflections

use of org.reflections.Reflections in project cloudstack by apache.

the class ReflectUtil method getClassesWithAnnotation.

// Gets all classes with some annotation from a package
public static Set<Class<?>> getClassesWithAnnotation(Class<? extends Annotation> annotation, String[] packageNames) {
    Reflections reflections;
    Set<Class<?>> classes = new HashSet<Class<?>>();
    ConfigurationBuilder builder = new ConfigurationBuilder();
    for (String packageName : packageNames) {
        builder.addUrls(ClasspathHelper.forPackage(packageName));
    }
    builder.setScanners(new SubTypesScanner(), new TypeAnnotationsScanner());
    reflections = new Reflections(builder);
    classes.addAll(reflections.getTypesAnnotatedWith(annotation));
    return classes;
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) SubTypesScanner(org.reflections.scanners.SubTypesScanner) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) Reflections(org.reflections.Reflections) HashSet(java.util.HashSet)

Example 42 with Reflections

use of org.reflections.Reflections in project jstructure by JonStargaryen.

the class FeatureProviderRegistry method scanForFeatureProviders.

private void scanForFeatureProviders() {
    Reflections reflections = new Reflections("de.bioforscher");
    Set<Class<?>> annotatedFeatureProviders = reflections.getTypesAnnotatedWith(FeatureProvider.class);
    for (Class<?> annotatedFeatureProvider : annotatedFeatureProviders) {
        FeatureProvider annotation = annotatedFeatureProvider.getDeclaredAnnotation(FeatureProvider.class);
        logger.info("registering provider {} with priority {}", annotatedFeatureProvider.getSimpleName(), annotation.priority());
        for (Class<? extends FeatureContainerEntry> providedFeature : annotation.provides()) {
            TreeMap<Integer, AbstractFeatureProvider> providers = registeredFeatureProviders.getOrDefault(providedFeature, new TreeMap<>());
            try {
                int priority = annotation.priority();
                if (providers.containsKey(priority)) {
                    providers.put(priority, (AbstractFeatureProvider) annotatedFeatureProvider.newInstance());
                } else {
                    // decrease priority to dodge duplicates, as only 1 would be registered
                    while (providers.containsKey(priority)) {
                        priority++;
                    }
                    providers.put(priority, (AbstractFeatureProvider) annotatedFeatureProvider.newInstance());
                }
            } catch (InstantiationException | IllegalAccessException e) {
                throw new InstantiationError("could not create instance of feature provider " + annotatedFeatureProvider.getSimpleName());
            }
            registeredFeatureProviders.put(providedFeature, providers);
        }
    }
}
Also used : Reflections(org.reflections.Reflections)

Example 43 with Reflections

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

Example 44 with Reflections

use of org.reflections.Reflections in project asterixdb by apache.

the class TypeComputerTest method test.

@Test
public void test() throws Exception {
    // Several exceptional type computers.
    Set<String> exceptionalTypeComputers = new HashSet<>();
    exceptionalTypeComputers.add("InjectFailureTypeComputer");
    exceptionalTypeComputers.add("RecordAddFieldsTypeComputer");
    exceptionalTypeComputers.add("OpenRecordConstructorResultType");
    exceptionalTypeComputers.add("RecordRemoveFieldsTypeComputer");
    exceptionalTypeComputers.add("ClosedRecordConstructorResultType");
    exceptionalTypeComputers.add("LocalAvgTypeComputer");
    exceptionalTypeComputers.add("BooleanOnlyTypeComputer");
    exceptionalTypeComputers.add("AMissingTypeComputer");
    exceptionalTypeComputers.add("NullableDoubleTypeComputer");
    exceptionalTypeComputers.add("RecordMergeTypeComputer");
    exceptionalTypeComputers.add("BooleanOrMissingTypeComputer");
    // 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);
    for (Class<? extends IResultTypeComputer> c : classes) {
        if (exceptionalTypeComputers.contains(c.getSimpleName()) || Modifier.isAbstract(c.getModifiers())) {
            continue;
        }
        System.out.println("Test type computer: " + c.getName());
        Assert.assertTrue(testTypeComputer(c));
    }
}
Also used : IResultTypeComputer(org.apache.asterix.om.typecomputer.base.IResultTypeComputer) SubTypesScanner(org.reflections.scanners.SubTypesScanner) HashSet(java.util.HashSet) Reflections(org.reflections.Reflections) Test(org.junit.Test)

Example 45 with Reflections

use of org.reflections.Reflections in project asterixdb by apache.

the class EvaluatorGeneratorMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    baseDir = project.getBuild().getDirectory() + File.separator + "classes";
    try {
        // Finds all sub-classes of AbstractScalarFunctionDynamicDescriptor with in the package
        // org.apache.asterix.runtime.evaluators.
        Reflections reflections = new Reflections(evaluatorPackagePrefix);
        Set<Class<? extends AbstractScalarFunctionDynamicDescriptor>> allClasses = reflections.getSubTypesOf(AbstractScalarFunctionDynamicDescriptor.class);
        // Generates byte code for all sub-classes of AbstractScalarFunctionDynamicDescriptor.
        for (Class<?> cl : allClasses) {
            getLog().info("Generating byte code for " + cl.getName());
            CodeGenUtil.generateScalarFunctionDescriptorBinary(evaluatorPackagePrefix, cl.getName(), CodeGenUtil.DEFAULT_SUFFIX_FOR_GENERATED_CLASS, reflections.getClass().getClassLoader(), (name, bytes) -> writeFile(name, bytes));
        }
    } catch (Exception e) {
        getLog().error(e);
        throw new MojoFailureException(e.toString());
    }
}
Also used : MojoFailureException(org.apache.maven.plugin.MojoFailureException) AbstractScalarFunctionDynamicDescriptor(org.apache.asterix.runtime.evaluators.base.AbstractScalarFunctionDynamicDescriptor) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Reflections(org.reflections.Reflections)

Aggregations

Reflections (org.reflections.Reflections)60 ConfigurationBuilder (org.reflections.util.ConfigurationBuilder)26 SubTypesScanner (org.reflections.scanners.SubTypesScanner)20 ArrayList (java.util.ArrayList)10 URL (java.net.URL)9 Test (org.junit.Test)9 TypeAnnotationsScanner (org.reflections.scanners.TypeAnnotationsScanner)9 HashSet (java.util.HashSet)7 Set (java.util.Set)6 IOException (java.io.IOException)5 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 ResourcesScanner (org.reflections.scanners.ResourcesScanner)5 Slf4j (lombok.extern.slf4j.Slf4j)4 ClasspathHelper (org.reflections.util.ClasspathHelper)4 FilterBuilder (org.reflections.util.FilterBuilder)4 Bean (org.springframework.context.annotation.Bean)4 File (java.io.File)3 Method (java.lang.reflect.Method)3 HashMap (java.util.HashMap)3