Search in sources :

Example 56 with Reflections

use of org.reflections.Reflections in project perun by CESNET.

the class PerunCLI method run.

@Override
public void run(String... args) throws Exception {
    // find all classes implementing commands and put them into the "commands" variable
    log.debug("finding available commands...");
    Reflections reflections = new Reflections("cz.metacentrum.perun.cli.commands");
    List<Class<? extends PerunCommand>> classes = new ArrayList<>(reflections.getSubTypesOf(PerunCommand.class));
    List<PerunCommand> commands = new ArrayList<>(classes.size());
    for (Class<? extends PerunCommand> aClass : classes) {
        commands.add(aClass.getDeclaredConstructor().newInstance());
    }
    commands.sort(Comparator.comparing(PerunCommand::getName));
    // if no arguments specified, print list of available commands
    if (args.length == 0) {
        System.err.println();
        System.err.println("Usage: <command> <options>");
        System.err.println();
        System.err.println("run a command with -h or --help to see a list of its available options");
        System.err.println();
        System.err.println("available commands:");
        for (PerunCommand command : commands) {
            System.err.println("  " + command.getName() + " ... " + command.getCommandDescription());
        }
        System.exit(1);
    }
    // call the command from class specified as first argument
    String[] options = args.length == 1 ? new String[] {} : Arrays.copyOfRange(args, 1, args.length);
    for (PerunCommand command : commands) {
        if (command.getName().equals(args[0])) {
            call(command, options);
            return;
        }
    }
    System.err.println("Command not recognized: " + args[0]);
}
Also used : ArrayList(java.util.ArrayList) Reflections(org.reflections.Reflections)

Example 57 with Reflections

use of org.reflections.Reflections in project druid by druid-io.

the class SubclassesMustOverrideEqualsAndHashCodeTest method testEqualsAndHashCode.

@Test
public void testEqualsAndHashCode() throws NoSuchMethodException {
    // Exclude test classes
    Set<URL> urls = ClasspathHelper.forPackage("org.apache.druid").stream().filter(url -> !url.toString().contains("/target/test-classes")).collect(Collectors.toSet());
    Reflections reflections = new Reflections(urls);
    Set<Class<?>> classes = reflections.getTypesAnnotatedWith(SubclassesMustOverrideEqualsAndHashCode.class);
    Set<String> failed = new HashSet<>();
    for (Class<?> clazz : classes) {
        if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) {
            continue;
        }
        Method m = clazz.getMethod("hashCode");
        String className = clazz.getName();
        try {
            Assert.assertNotSame(className + " does not implment hashCode", Object.class, m.getDeclaringClass());
        } catch (AssertionError e) {
            failed.add(className);
        }
    }
    if (!failed.isEmpty()) {
        System.err.println("failed classes [" + failed.size() + "] : ");
        failed.forEach(c -> System.err.println("\t" + c));
        Assert.fail();
    }
}
Also used : HashSet(java.util.HashSet) URL(java.net.URL) Modifier(java.lang.reflect.Modifier) Set(java.util.Set) Test(org.junit.Test) Reflections(org.reflections.Reflections) Assert(org.junit.Assert) Method(java.lang.reflect.Method) Collectors(java.util.stream.Collectors) ClasspathHelper(org.reflections.util.ClasspathHelper) Method(java.lang.reflect.Method) URL(java.net.URL) Reflections(org.reflections.Reflections) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 58 with Reflections

use of org.reflections.Reflections in project hazelcast by hazelcast.

the class AbstractYamlSchemaTest method buildTestcases.

protected static List<Object[]> buildTestcases(String rootDir) {
    ConfigurationBuilder configuration = new ConfigurationBuilder().setUrls(ClasspathHelper.forJavaClassPath()).setScanners(new ResourcesScanner());
    Reflections reflections = new Reflections(configuration);
    return reflections.getResources(Pattern.compile(".*\\.json")).stream().filter(e -> e.startsWith(rootDir)).map(path -> buildArgs(rootDir, path)).collect(toList());
}
Also used : ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) RunWith(org.junit.runner.RunWith) SchemaClient(org.everit.json.schema.loader.SchemaClient) Reflections(org.reflections.Reflections) ArrayList(java.util.ArrayList) JSONObject(org.json.JSONObject) ILogger(com.hazelcast.logging.ILogger) Assert.fail(org.junit.Assert.fail) SchemaLoader(org.everit.json.schema.loader.SchemaLoader) Parameterized(org.junit.runners.Parameterized) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) UseParametersRunnerFactory(org.junit.runners.Parameterized.UseParametersRunnerFactory) Logger(com.hazelcast.logging.Logger) ValidationException(org.everit.json.schema.ValidationException) HazelcastParametrizedRunner(com.hazelcast.test.HazelcastParametrizedRunner) JSONTokener(org.json.JSONTokener) HazelcastParallelParametersRunnerFactory(com.hazelcast.test.HazelcastParallelParametersRunnerFactory) Test(org.junit.Test) Category(org.junit.experimental.categories.Category) ClasspathHelper(org.reflections.util.ClasspathHelper) ResourcesScanner(org.reflections.scanners.ResourcesScanner) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Schema(org.everit.json.schema.Schema) Pattern(java.util.regex.Pattern) Comparator(java.util.Comparator) JSONArray(org.json.JSONArray) Assert.assertEquals(org.junit.Assert.assertEquals) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) ResourcesScanner(org.reflections.scanners.ResourcesScanner) Reflections(org.reflections.Reflections)

Example 59 with Reflections

use of org.reflections.Reflections in project hazelcast by hazelcast.

the class XmlConfigSchemaLocationTest method testSchemaLocationsExist.

@Test
public void testSchemaLocationsExist() throws Exception {
    assumeTls12Available();
    ResourcesScanner scanner = new ResourcesScanner();
    Reflections reflections = new Reflections(scanner);
    Set<String> resources = reflections.getResources(Pattern.compile(".*\\.xml"));
    ClassLoader classLoader = getClass().getClassLoader();
    for (String resource : resources) {
        System.out.println(resource);
        URL resourceUrl = classLoader.getResource(resource);
        String protocol = resourceUrl.getProtocol();
        // do not validate schemas from JARs (libraries). we are interested in local project files only.
        if (protocol.startsWith("jar")) {
            continue;
        }
        InputStream stream = null;
        try {
            stream = classLoader.getResourceAsStream(resource);
            validateSchemaLocationUrl(stream, resource);
        } finally {
            IOUtil.closeResource(stream);
        }
    }
}
Also used : InputStream(java.io.InputStream) ResourcesScanner(org.reflections.scanners.ResourcesScanner) URL(java.net.URL) Reflections(org.reflections.Reflections) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Example 60 with Reflections

use of org.reflections.Reflections in project hazelcast by hazelcast.

the class ReflectionUtils method getReflectionsForTestPackage.

public static Reflections getReflectionsForTestPackage(String forPackage) {
    try {
        URL testClassesURL = new File("target/test-classes").toURI().toURL();
        URLClassLoader classLoader = newInstance(new URL[] { testClassesURL }, ClasspathHelper.staticClassLoader());
        return new Reflections(new ConfigurationBuilder().addUrls(ClasspathHelper.forPackage(forPackage, classLoader)).addClassLoader(classLoader).filterInputsBy(new FilterBuilder().includePackage(forPackage)).setScanners(new SubTypesScanner(false), new TypeAnnotationsScanner(), new MethodAnnotationsScanner()));
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) MalformedURLException(java.net.MalformedURLException) MethodAnnotationsScanner(org.reflections.scanners.MethodAnnotationsScanner) FilterBuilder(org.reflections.util.FilterBuilder) SubTypesScanner(org.reflections.scanners.SubTypesScanner) URLClassLoader(java.net.URLClassLoader) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) File(java.io.File) URL(java.net.URL) 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