Search in sources :

Example 51 with Reflections

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

the class TestforBaseTestInheritance method verifyInheritance.

@Test
@Category(UnlikelyTest.class)
public void verifyInheritance() {
    // Get all BaseTest inheritors
    Reflections reflections = new Reflections("org.apache.drill", new SubTypesScanner(false));
    Set<Class<? extends BaseTest>> baseTestInheritors = reflections.getSubTypesOf(BaseTest.class);
    // Get all tests that are not inherited from BaseTest
    Set<String> testClasses = reflections.getSubTypesOf(Object.class).stream().filter(c -> !c.isInterface()).filter(c -> c.getSimpleName().toLowerCase().contains("test")).filter(c -> Arrays.stream(c.getDeclaredMethods()).anyMatch(m -> m.getAnnotation(Test.class) != null)).filter(c -> !baseTestInheritors.contains(c)).map(Class::getName).collect(Collectors.toSet());
    Assert.assertEquals("Found test classes that are not inherited from BaseTest:", Collections.emptySet(), testClasses);
}
Also used : BaseTest(org.apache.drill.test.BaseTest) Arrays(java.util.Arrays) UnlikelyTest(org.apache.drill.categories.UnlikelyTest) Set(java.util.Set) Test(org.junit.Test) Reflections(org.reflections.Reflections) Assert(org.junit.Assert) Category(org.junit.experimental.categories.Category) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) SubTypesScanner(org.reflections.scanners.SubTypesScanner) SubTypesScanner(org.reflections.scanners.SubTypesScanner) BaseTest(org.apache.drill.test.BaseTest) Reflections(org.reflections.Reflections) Category(org.junit.experimental.categories.Category) BaseTest(org.apache.drill.test.BaseTest) UnlikelyTest(org.apache.drill.categories.UnlikelyTest) Test(org.junit.Test)

Example 52 with Reflections

use of org.reflections.Reflections in project EventHub by Codecademy.

the class Module method getEventHubHandler.

@Provides
private EventHubHandler getEventHubHandler(Injector injector, EventHub eventHub) throws ClassNotFoundException {
    Map<String, Provider<Command>> commandsMap = Maps.newHashMap();
    Reflections reflections = new Reflections(PACKAGE_NAME);
    Set<Class<? extends Command>> commandClasses = reflections.getSubTypesOf(Command.class);
    for (Class<? extends Command> commandClass : commandClasses) {
        String path = commandClass.getAnnotation(Path.class).value();
        // noinspection unchecked
        commandsMap.put(path, (Provider<Command>) injector.getProvider(commandClass));
    }
    return new EventHubHandler(eventHub, commandsMap);
}
Also used : Path(com.codecademy.eventhub.web.commands.Path) Command(com.codecademy.eventhub.web.commands.Command) Provider(javax.inject.Provider) Reflections(org.reflections.Reflections) Provides(com.google.inject.Provides)

Example 53 with Reflections

use of org.reflections.Reflections in project ninja by ninjaframework.

the class JaxyRoutes method findControllerMethods.

/**
 * Searches for Methods that have either a Path Annotation or a HTTP-Method Annotation
 */
@SuppressWarnings("unchecked")
private Set<Method> findControllerMethods() {
    Set<Method> methods = Sets.newLinkedHashSet();
    methods.addAll(reflections.getMethodsAnnotatedWith(Path.class));
    boolean enableCustomHttpMethods = ninjaProperties.getBooleanWithDefault(NINJA_CUSTOM_HTTP_METHODS, false);
    if (enableCustomHttpMethods) {
        Reflections annotationReflections = new Reflections("", new TypeAnnotationsScanner(), new SubTypesScanner());
        for (Class<?> httpMethod : annotationReflections.getTypesAnnotatedWith(HttpMethod.class)) {
            if (httpMethod.isAnnotation()) {
                methods.addAll(reflections.getMethodsAnnotatedWith((Class<? extends Annotation>) httpMethod));
            }
        }
    } else {
        // Only look for standard HTTP methods annotations
        Reflections annotationReflections = new Reflections("ninja.jaxy", new TypeAnnotationsScanner(), new SubTypesScanner());
        for (Class<?> httpMethod : annotationReflections.getTypesAnnotatedWith(HttpMethod.class)) {
            if (httpMethod.isAnnotation()) {
                methods.addAll(reflections.getMethodsAnnotatedWith((Class<? extends Annotation>) httpMethod));
            }
        }
    }
    return methods;
}
Also used : SubTypesScanner(org.reflections.scanners.SubTypesScanner) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) Reflections(org.reflections.Reflections)

Example 54 with Reflections

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

the class DelegatingClassLoader method scanPluginPath.

private PluginScanResult scanPluginPath(ClassLoader loader, URL[] urls) throws ReflectiveOperationException {
    ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.setClassLoaders(new ClassLoader[] { loader });
    builder.addUrls(urls);
    builder.setScanners(new SubTypesScanner());
    builder.useParallelExecutor();
    Reflections reflections = new InternalReflections(builder);
    return new PluginScanResult(getPluginDesc(reflections, Connector.class, loader), getPluginDesc(reflections, Converter.class, loader), getPluginDesc(reflections, HeaderConverter.class, loader), getTransformationPluginDesc(loader, reflections), getPredicatePluginDesc(loader, reflections), getServiceLoaderPluginDesc(ConfigProvider.class, loader), getServiceLoaderPluginDesc(ConnectRestExtension.class, loader), getServiceLoaderPluginDesc(ConnectorClientConfigOverridePolicy.class, loader));
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) Connector(org.apache.kafka.connect.connector.Connector) ConfigProvider(org.apache.kafka.common.config.provider.ConfigProvider) SubTypesScanner(org.reflections.scanners.SubTypesScanner) HeaderConverter(org.apache.kafka.connect.storage.HeaderConverter) Converter(org.apache.kafka.connect.storage.Converter) HeaderConverter(org.apache.kafka.connect.storage.HeaderConverter) ConnectorClientConfigOverridePolicy(org.apache.kafka.connect.connector.policy.ConnectorClientConfigOverridePolicy) ConnectRestExtension(org.apache.kafka.connect.rest.ConnectRestExtension) Reflections(org.reflections.Reflections)

Example 55 with Reflections

use of org.reflections.Reflections in project alluxio by Alluxio.

the class CommandUtils method loadCommands.

/**
 * Get instances of all subclasses of {@link Command} in a sub-package called "command" the given
 * package.
 *
 * @param pkgName package prefix to look in
 * @param classArgs type of args to instantiate the class
 * @param objectArgs args to instantiate the class
 * @return a mapping from command name to command instance
 */
public static Map<String, Command> loadCommands(String pkgName, Class[] classArgs, Object[] objectArgs) {
    Map<String, Command> commandsMap = new HashMap<>();
    Reflections reflections = new Reflections(Command.class.getPackage().getName());
    for (Class<? extends Command> cls : reflections.getSubTypesOf(Command.class)) {
        // Add commands from <pkgName>.command.*
        if (cls.getPackage().getName().equals(pkgName + ".command") && !Modifier.isAbstract(cls.getModifiers())) {
            // Only instantiate a concrete class
            Command cmd = CommonUtils.createNewClassInstance(cls, classArgs, objectArgs);
            commandsMap.put(cmd.getCommandName(), cmd);
        }
    }
    return commandsMap;
}
Also used : HashMap(java.util.HashMap) 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