Search in sources :

Example 6 with MethodAnnotationsScanner

use of org.reflections.scanners.MethodAnnotationsScanner in project Alpha by alpha-asp.

the class Externals method scan.

public static Map<String, PredicateInterpretation> scan(Package basePackage) {
    Reflections reflections = new Reflections(basePackage.getName(), new MethodAnnotationsScanner());
    Set<Method> methods = reflections.getMethodsAnnotatedWith(Predicate.class);
    return Externals.scanMethods(methods);
}
Also used : MethodAnnotationsScanner(org.reflections.scanners.MethodAnnotationsScanner) Method(java.lang.reflect.Method) Reflections(org.reflections.Reflections)

Example 7 with MethodAnnotationsScanner

use of org.reflections.scanners.MethodAnnotationsScanner in project disconf by knightliao.

the class ReflectionScanStatic method getReflection.

/**
 * 通过扫描,获取反射对象
 */
private Reflections getReflection(List<String> packNameList) {
    // 
    // filter
    // 
    FilterBuilder filterBuilder = new FilterBuilder().includePackage(Constants.DISCONF_PACK_NAME);
    for (String packName : packNameList) {
        filterBuilder = filterBuilder.includePackage(packName);
    }
    Predicate<String> filter = filterBuilder;
    // 
    // urls
    // 
    Collection<URL> urlTotals = new ArrayList<URL>();
    for (String packName : packNameList) {
        Set<URL> urls = ClasspathHelper.forPackage(packName);
        urlTotals.addAll(urls);
    }
    // 
    Reflections reflections = new Reflections(new ConfigurationBuilder().filterInputsBy(filter).setScanners(new SubTypesScanner().filterResultsBy(filter), new TypeAnnotationsScanner().filterResultsBy(filter), new FieldAnnotationsScanner().filterResultsBy(filter), new MethodAnnotationsScanner().filterResultsBy(filter), new MethodParameterScanner()).setUrls(urlTotals));
    return reflections;
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) ArrayList(java.util.ArrayList) URL(java.net.URL) MethodParameterScanner(org.reflections.scanners.MethodParameterScanner) FieldAnnotationsScanner(org.reflections.scanners.FieldAnnotationsScanner) MethodAnnotationsScanner(org.reflections.scanners.MethodAnnotationsScanner) FilterBuilder(org.reflections.util.FilterBuilder) SubTypesScanner(org.reflections.scanners.SubTypesScanner) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) Reflections(org.reflections.Reflections)

Example 8 with MethodAnnotationsScanner

use of org.reflections.scanners.MethodAnnotationsScanner in project graylog2-server by Graylog2.

the class AuditCoverageTest method testAuditCoverage.

@Test
public void testAuditCoverage() throws Exception {
    final ConfigurationBuilder configurationBuilder = new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage("org.graylog2")).setScanners(new MethodAnnotationsScanner());
    // TODO: Dynamically discover event types?
    final Set<String> auditEventTypes = ImmutableSet.<String>builder().addAll(new AuditEventTypes().auditEventTypes()).addAll(new PipelineProcessorAuditEventTypes().auditEventTypes()).addAll(new SidecarAuditEventTypes().auditEventTypes()).addAll(new ViewsAuditEventTypes().auditEventTypes()).addAll(new JobSchedulerAuditEventTypes().auditEventTypes()).addAll(new EventsAuditEventTypes().auditEventTypes()).addAll(new SecurityAuditEventTypes().auditEventTypes()).build();
    final Reflections reflections = new Reflections(configurationBuilder);
    final ImmutableSet.Builder<Method> methods = ImmutableSet.builder();
    final ImmutableSet.Builder<Method> missing = ImmutableSet.builder();
    final ImmutableSet.Builder<Method> unregisteredAction = ImmutableSet.builder();
    methods.addAll(reflections.getMethodsAnnotatedWith(POST.class));
    methods.addAll(reflections.getMethodsAnnotatedWith(PUT.class));
    methods.addAll(reflections.getMethodsAnnotatedWith(DELETE.class));
    for (Method method : methods.build()) {
        if (!method.isAnnotationPresent(AuditEvent.class) && !method.isAnnotationPresent(NoAuditEvent.class)) {
            missing.add(method);
        } else {
            if (method.isAnnotationPresent(AuditEvent.class)) {
                final AuditEvent annotation = method.getAnnotation(AuditEvent.class);
                if (!auditEventTypes.contains(annotation.type())) {
                    unregisteredAction.add(method);
                }
            }
        }
    }
    assertThat(missing.build()).describedAs("Check that there are no POST, PUT and DELETE resources which do not have the @AuditEvent annotation").isEmpty();
    assertThat(unregisteredAction.build()).describedAs("Check that there are no @AuditEvent annotations with unregistered event types").isEmpty();
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) SecurityAuditEventTypes(org.graylog.security.SecurityAuditEventTypes) POST(javax.ws.rs.POST) PipelineProcessorAuditEventTypes(org.graylog.plugins.pipelineprocessor.audit.PipelineProcessorAuditEventTypes) Method(java.lang.reflect.Method) PUT(javax.ws.rs.PUT) DELETE(javax.ws.rs.DELETE) MethodAnnotationsScanner(org.reflections.scanners.MethodAnnotationsScanner) SidecarAuditEventTypes(org.graylog.plugins.sidecar.audit.SidecarAuditEventTypes) ImmutableSet(com.google.common.collect.ImmutableSet) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent) ViewsAuditEventTypes(org.graylog.plugins.views.audit.ViewsAuditEventTypes) ViewsAuditEventTypes(org.graylog.plugins.views.audit.ViewsAuditEventTypes) SecurityAuditEventTypes(org.graylog.security.SecurityAuditEventTypes) SidecarAuditEventTypes(org.graylog.plugins.sidecar.audit.SidecarAuditEventTypes) PipelineProcessorAuditEventTypes(org.graylog.plugins.pipelineprocessor.audit.PipelineProcessorAuditEventTypes) JobSchedulerAuditEventTypes(org.graylog.scheduler.audit.JobSchedulerAuditEventTypes) EventsAuditEventTypes(org.graylog.events.audit.EventsAuditEventTypes) JobSchedulerAuditEventTypes(org.graylog.scheduler.audit.JobSchedulerAuditEventTypes) EventsAuditEventTypes(org.graylog.events.audit.EventsAuditEventTypes) Reflections(org.reflections.Reflections) Test(org.junit.Test)

Example 9 with MethodAnnotationsScanner

use of org.reflections.scanners.MethodAnnotationsScanner 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 10 with MethodAnnotationsScanner

use of org.reflections.scanners.MethodAnnotationsScanner in project ninja by ninjaframework.

the class JaxyRoutes method configureReflections.

private void configureReflections() {
    ConfigurationBuilder builder = new ConfigurationBuilder();
    Set<URL> packagesToScan = getPackagesToScanForRoutes();
    builder.addUrls(packagesToScan);
    builder.addScanners(new MethodAnnotationsScanner());
    reflections = new Reflections(builder);
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) MethodAnnotationsScanner(org.reflections.scanners.MethodAnnotationsScanner) URL(java.net.URL) Reflections(org.reflections.Reflections)

Aggregations

Reflections (org.reflections.Reflections)12 MethodAnnotationsScanner (org.reflections.scanners.MethodAnnotationsScanner)12 Method (java.lang.reflect.Method)6 SubTypesScanner (org.reflections.scanners.SubTypesScanner)4 TypeAnnotationsScanner (org.reflections.scanners.TypeAnnotationsScanner)4 ConfigurationBuilder (org.reflections.util.ConfigurationBuilder)4 URL (java.net.URL)3 FileWriter (java.io.FileWriter)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 DataSave (me.shadorc.shadbot.data.annotation.DataSave)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 MethodParameterScanner (org.reflections.scanners.MethodParameterScanner)2 FilterBuilder (org.reflections.util.FilterBuilder)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 File (java.io.File)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URLClassLoader (java.net.URLClassLoader)1