Search in sources :

Example 61 with Reflections

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

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

use of org.reflections.Reflections in project disconf by knightliao.

the class ReflectionScanStatic method scanBasicInfo.

/**
 * 扫描基本信息
 */
private ScanStaticModel scanBasicInfo(List<String> packNameList) {
    ScanStaticModel scanModel = new ScanStaticModel();
    // 
    // 扫描对象
    // 
    Reflections reflections = getReflection(packNameList);
    scanModel.setReflections(reflections);
    // 
    // 获取DisconfFile class
    // 
    Set<Class<?>> classdata = reflections.getTypesAnnotatedWith(DisconfFile.class);
    scanModel.setDisconfFileClassSet(classdata);
    // 
    // 获取DisconfFileItem method
    // 
    Set<Method> af1 = reflections.getMethodsAnnotatedWith(DisconfFileItem.class);
    scanModel.setDisconfFileItemMethodSet(af1);
    // 
    // 获取DisconfItem method
    // 
    af1 = reflections.getMethodsAnnotatedWith(DisconfItem.class);
    scanModel.setDisconfItemMethodSet(af1);
    // 
    // 获取DisconfActiveBackupService
    // 
    classdata = reflections.getTypesAnnotatedWith(DisconfActiveBackupService.class);
    scanModel.setDisconfActiveBackupServiceClassSet(classdata);
    // 
    // 获取DisconfUpdateService
    // 
    classdata = reflections.getTypesAnnotatedWith(DisconfUpdateService.class);
    scanModel.setDisconfUpdateService(classdata);
    // update pipeline
    Set<Class<? extends IDisconfUpdatePipeline>> iDisconfUpdatePipeline = reflections.getSubTypesOf(IDisconfUpdatePipeline.class);
    if (iDisconfUpdatePipeline != null && iDisconfUpdatePipeline.size() != 0) {
        scanModel.setiDisconfUpdatePipeline((Class<IDisconfUpdatePipeline>) iDisconfUpdatePipeline.toArray()[0]);
    }
    return scanModel;
}
Also used : DisconfActiveBackupService(com.baidu.disconf.client.common.annotations.DisconfActiveBackupService) IDisconfUpdatePipeline(com.baidu.disconf.client.common.update.IDisconfUpdatePipeline) ScanStaticModel(com.baidu.disconf.client.scan.inner.statically.model.ScanStaticModel) Method(java.lang.reflect.Method) DisconfUpdateService(com.baidu.disconf.client.common.annotations.DisconfUpdateService) Reflections(org.reflections.Reflections) DisconfItem(com.baidu.disconf.client.common.annotations.DisconfItem)

Example 64 with Reflections

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

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

the class ExampleGeneratorTest method instancesToTest.

public static Collection<Object[]> instancesToTest() {
    final Reflections reflections = new Reflections("uk.gov.gchq");
    final Set<Class<? extends Operation>> clazzes = reflections.getSubTypesOf(Operation.class);
    return clazzes.stream().filter(clazz -> !clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers())).map(clazz -> new Object[] { clazz }).collect(Collectors.toList());
}
Also used : DefaultGraphFactory(uk.gov.gchq.gaffer.rest.factory.DefaultGraphFactory) BeforeEach(org.junit.jupiter.api.BeforeEach) Files(java.nio.file.Files) Collection(java.util.Collection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DefaultExamplesFactory(uk.gov.gchq.gaffer.rest.service.v2.example.DefaultExamplesFactory) Set(java.util.Set) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) Reflections(org.reflections.Reflections) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) File(java.io.File) IOUtils(org.apache.commons.io.IOUtils) GraphFactory(uk.gov.gchq.gaffer.rest.factory.GraphFactory) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Operation(uk.gov.gchq.gaffer.operation.Operation) TempDir(org.junit.jupiter.api.io.TempDir) Modifier(java.lang.reflect.Modifier) Path(java.nio.file.Path) MethodSource(org.junit.jupiter.params.provider.MethodSource) StreamUtil(uk.gov.gchq.gaffer.commonutil.StreamUtil) Operation(uk.gov.gchq.gaffer.operation.Operation) 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