Search in sources :

Example 66 with Reflections

use of org.reflections.Reflections in project Gaffer by gchq.

the class StreamUtil method openStreams.

/**
 * Open all of the files found in the specified subdirectory of the provided
 * class.
 *
 * @param clazz      the class location
 * @param folderPath the subdirectory in the class location
 * @return an array of {@link InputStream}s representing the files found
 */
public static InputStream[] openStreams(final Class clazz, final String folderPath) {
    if (null == folderPath) {
        return new InputStream[0];
    }
    String folderPathChecked = getFormattedPath(folderPath);
    final HashSet<InputStream> inputStreams = Sets.newHashSet();
    new Reflections(new ConfigurationBuilder().setScanners(new ResourcesScanner()).setUrls(ClasspathHelper.forClass(clazz))).getResources(Pattern.compile(".*")).stream().filter(file -> file.startsWith(folderPathChecked)).forEach(file -> {
        try {
            inputStreams.add(openStream(clazz, file));
        } catch (final Exception e) {
            int closedStreamsCount = closeStreams(inputStreams.toArray(new InputStream[inputStreams.size()]));
            LOGGER.info(String.format("Closed %s input streams", closedStreamsCount));
        }
    });
    if (inputStreams.isEmpty()) {
        throw new IllegalArgumentException("No file could be found in path: " + folderPath);
    }
    return inputStreams.toArray(new InputStream[inputStreams.size()]);
}
Also used : Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) Reflections(org.reflections.Reflections) StringUtils(org.apache.commons.lang3.StringUtils) Sets(com.google.common.collect.Sets) ClasspathHelper(org.reflections.util.ClasspathHelper) HashSet(java.util.HashSet) ResourcesScanner(org.reflections.scanners.ResourcesScanner) URI(java.net.URI) Pattern(java.util.regex.Pattern) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) InputStream(java.io.InputStream) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) InputStream(java.io.InputStream) ResourcesScanner(org.reflections.scanners.ResourcesScanner) IOException(java.io.IOException) Reflections(org.reflections.Reflections)

Example 67 with Reflections

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

the class CollectInfoTest method getNumberOfCommands.

private int getNumberOfCommands() {
    Reflections reflections = new Reflections(AbstractCollectInfoCommand.class.getPackage().getName());
    int cnt = 0;
    for (Class<? extends Command> cls : reflections.getSubTypesOf(Command.class)) {
        if (!Modifier.isAbstract(cls.getModifiers())) {
            cnt++;
        }
    }
    return cnt;
}
Also used : Reflections(org.reflections.Reflections)

Example 68 with Reflections

use of org.reflections.Reflections in project Smack by igniterealtime.

the class SmackIntegrationTestFramework method run.

public synchronized TestRunResult run() throws KeyManagementException, NoSuchAlgorithmException, SmackException, IOException, XMPPException, InterruptedException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    // it is a global setting, but we treat it like a per sinttest run setting.
    switch(config.dnsResolver) {
        case minidns:
            MiniDnsResolver.setup();
            break;
        case javax:
            JavaxResolver.setup();
            break;
        case dnsjava:
            DNSJavaResolver.setup();
            break;
    }
    testRunResult = new TestRunResult();
    // Create a connection manager *after* we created the testRunId (in testRunResult).
    this.connectionManager = new XmppConnectionManager(this);
    LOGGER.info("SmackIntegrationTestFramework [" + testRunResult.testRunId + ']' + ": Starting\nSmack version: " + Smack.getVersion());
    if (config.debugger != Configuration.Debugger.none) {
        // JUL Debugger will not print any information until configured to print log messages of
        // level FINE
        // TODO configure JUL for log?
        SmackConfiguration.addDisabledSmackClass("org.jivesoftware.smack.debugger.JulDebugger");
        SmackConfiguration.DEBUG = true;
    }
    if (config.replyTimeout > 0) {
        SmackConfiguration.setDefaultReplyTimeout(config.replyTimeout);
    }
    if (config.securityMode != SecurityMode.required && config.accountRegistration == AccountRegistration.inBandRegistration) {
        AccountManager.sensitiveOperationOverInsecureConnectionDefault(true);
    }
    // TODO print effective configuration
    String[] testPackages;
    if (config.testPackages == null || config.testPackages.isEmpty()) {
        testPackages = new String[] { "org.jivesoftware.smackx", "org.jivesoftware.smack" };
    } else {
        testPackages = config.testPackages.toArray(new String[config.testPackages.size()]);
    }
    Reflections reflections = new Reflections(testPackages, new SubTypesScanner(), new TypeAnnotationsScanner(), new MethodAnnotationsScanner(), new MethodParameterScanner());
    Set<Class<? extends AbstractSmackIntegrationTest>> inttestClasses = reflections.getSubTypesOf(AbstractSmackIntegrationTest.class);
    Set<Class<? extends AbstractSmackLowLevelIntegrationTest>> lowLevelInttestClasses = reflections.getSubTypesOf(AbstractSmackLowLevelIntegrationTest.class);
    final int builtInTestCount = inttestClasses.size() + lowLevelInttestClasses.size();
    Set<Class<? extends AbstractSmackIntTest>> classes = new HashSet<>(builtInTestCount);
    classes.addAll(inttestClasses);
    classes.addAll(lowLevelInttestClasses);
    {
        // Remove all abstract classes.
        // TODO: This may be a good candidate for Java stream filtering once Smack is Android API 24 or higher.
        Iterator<Class<? extends AbstractSmackIntTest>> it = classes.iterator();
        while (it.hasNext()) {
            Class<? extends AbstractSmackIntTest> clazz = it.next();
            if (Modifier.isAbstract(clazz.getModifiers())) {
                it.remove();
            }
        }
    }
    if (classes.isEmpty()) {
        throw new IllegalStateException("No test classes in " + Arrays.toString(testPackages) + " found");
    }
    LOGGER.info("SmackIntegrationTestFramework [" + testRunResult.testRunId + "]: Finished scanning for tests, preparing environment");
    environment = prepareEnvironment();
    try {
        runTests(classes);
    } catch (Throwable t) {
        // Log the thrown Throwable to prevent it being shadowed in case the finally block below also throws.
        LOGGER.log(Level.SEVERE, "Unexpected abort because runTests() threw throwable", t);
        throw t;
    } finally {
        // Ensure that the accounts are deleted and disconnected before we continue
        connectionManager.disconnectAndCleanup();
    }
    return testRunResult;
}
Also used : MethodParameterScanner(org.reflections.scanners.MethodParameterScanner) MethodAnnotationsScanner(org.reflections.scanners.MethodAnnotationsScanner) SubTypesScanner(org.reflections.scanners.SubTypesScanner) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) Iterator(java.util.Iterator) BeforeClass(org.igniterealtime.smack.inttest.annotations.BeforeClass) AfterClass(org.igniterealtime.smack.inttest.annotations.AfterClass) Reflections(org.reflections.Reflections) HashSet(java.util.HashSet)

Example 69 with Reflections

use of org.reflections.Reflections in project ambrose by twitter.

the class JSONUtil method newMapper.

private static ObjectMapper newMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
    mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT, false);
    mapper.disable(SerializationFeature.FLUSH_AFTER_WRITE_VALUE);
    mapper.disable(SerializationFeature.CLOSE_CLOSEABLE);
    mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    Reflections reflections = new Reflections("com.twitter.ambrose");
    Set<Class<? extends Job>> jobSubTypes = reflections.getSubTypesOf(Job.class);
    mapper.registerSubtypes(jobSubTypes.toArray(new Class<?>[jobSubTypes.size()]));
    return mapper;
}
Also used : Job(com.twitter.ambrose.model.Job) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Reflections(org.reflections.Reflections)

Example 70 with Reflections

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

the class ConfigurationMetadataClassSourceLocator method locatePropertiesClassForType.

/**
 * Locate properties class for type class.
 *
 * @param type the type
 * @return the class
 */
public Class locatePropertiesClassForType(final ClassOrInterfaceType type) {
    if (cachedPropertiesClasses.containsKey(type.getNameAsString())) {
        return cachedPropertiesClasses.get(type.getNameAsString());
    }
    val urls = new ArrayList<>(ClasspathHelper.forPackage("org.apereo.cas"));
    val reflections = new Reflections(new ConfigurationBuilder().filterInputsBy(s -> s != null && s.contains(type.getNameAsString())).setUrls(urls));
    val clz = reflections.getSubTypesOf(Serializable.class).stream().filter(c -> c.getSimpleName().equalsIgnoreCase(type.getNameAsString())).findFirst().orElseThrow(() -> new IllegalArgumentException("Cant locate class for " + type.getNameAsString()));
    cachedPropertiesClasses.put(type.getNameAsString(), clz);
    return clz;
}
Also used : lombok.val(lombok.val) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) ArrayList(java.util.ArrayList) 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