Java Code Examples for org.reflections.Reflections#getMethodsAnnotatedWith()

The following examples show how to use org.reflections.Reflections#getMethodsAnnotatedWith() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataScopeSqlConfigService.java    From smart-admin with MIT License 7 votes vote down vote up
/**
 * 刷新 所有添加数据范围注解的接口方法配置<class.method,DataScopeSqlConfigDTO></>
 *
 * @return
 */
private Map<String, DataScopeSqlConfigDTO> refreshDataScopeMethodMap() {
    Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage(scanPackage)).setScanners(new MethodAnnotationsScanner()));
    Set<Method> methods = reflections.getMethodsAnnotatedWith(DataScope.class);
    for (Method method : methods) {
        DataScope dataScopeAnnotation = method.getAnnotation(DataScope.class);
        if (dataScopeAnnotation != null) {
            DataScopeSqlConfigDTO configDTO = new DataScopeSqlConfigDTO();
            configDTO.setDataScopeType(dataScopeAnnotation.dataScopeType().getType());
            configDTO.setJoinSql(dataScopeAnnotation.joinSql());
            configDTO.setWhereIndex(dataScopeAnnotation.whereIndex());
            dataScopeMethodMap.put(method.getDeclaringClass().getSimpleName() + "." + method.getName(), configDTO);
        }
    }
    return dataScopeMethodMap;
}
 
Example 2
Source File: ExoTransactionRouter.java    From exo-demo with MIT License 6 votes vote down vote up
/**
 * Scans a package, e.g. "com.txmq.exo.messaging.rest" for 
 * @ExoTransaction annotations using reflection and sets up the
 * internal mapping of transaction type to processing method.
 */
public ExoTransactionRouter addPackage(String transactionPackage) {
	/*
	Reflections reflections = new Reflections(new ConfigurationBuilder()
	     .setUrls(ClasspathHelper.forPackage(transactionPackage))
	     .setScanners(new MethodAnnotationsScanner())
	);*/
	Reflections reflections = new Reflections(transactionPackage, new MethodAnnotationsScanner());			
	
	Set<Method> methods = reflections.getMethodsAnnotatedWith(ExoTransaction.class);
	for (Method method : methods) {
		ExoTransaction annotation = method.getAnnotation(ExoTransaction.class);
		this.transactionMap.put(annotation.value(), method);
	}
	
	return this;
}
 
Example 3
Source File: AnnotationUrlProvider.java    From play-sitemap-module.edulify.com with Apache License 2.0 6 votes vote down vote up
@Override
public void addUrlsTo(WebSitemapGenerator generator) {
  String baseUrl = configuration.getString("sitemap.baseUrl");

  ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  Reflections reflections = new Reflections("controllers", new MethodAnnotationsScanner());

  Set<Method> actions = reflections.getMethodsAnnotatedWith(SitemapItem.class);
  for(Method method : actions) {
    String actionUrl = actionUrl(classLoader, method);
    SitemapItem annotation = method.getAnnotation(SitemapItem.class);
    if(annotation != null) {
      WebSitemapUrl url = webSitemapUrl(baseUrl, actionUrl, annotation);
      generator.addUrl(url);
    }
  }
}
 
Example 4
Source File: BeanAggregateAutoConfiguration.java    From spring-boot-data-aggregator with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public DataBeanAggregateService dataBeanAggregateQueryService (
        @Qualifier("dataProviderRepository") DataProviderRepository dataProviderRepository) {
    if(properties.getBasePackages() != null) {
        Map<String,Set<String>> provideDependMap = new HashMap<>(64);
        for (String basePackage : properties.getBasePackages()) {
            Reflections reflections = new Reflections(basePackage, new MethodAnnotationsScanner());
            Set<Method> providerMethods = reflections.getMethodsAnnotatedWith(DataProvider.class);
            for (Method method : providerMethods) {
                DataProvider beanProvider = AnnotationUtils.findAnnotation(method, DataProvider.class);
                @SuppressWarnings("ConstantConditions")
                String dataId = beanProvider.id();
                Assert.isTrue(Modifier.isPublic(method.getModifiers()),"data provider method must be public");
                Assert.isTrue(! StringUtils.isEmpty(dataId),"data id must be not null!");
                DataProvideDefinition provider = DefinitionUtils.getProvideDefinition(method);

                provider.setId(dataId);
                provider.setIdempotent(beanProvider.idempotent());
                provider.setTimeout(beanProvider.timeout() > 0 ? beanProvider.timeout() : properties.getDefaultTimeout());
                Assert.isTrue(! dataProviderRepository.contains(dataId), "Data providers with the same name are not allowed. dataId: " + dataId);
                provideDependMap.put(dataId,provider.getDepends().stream().map(DataConsumeDefinition::getId).collect(Collectors.toSet()));
                dataProviderRepository.put(provider);
            }
        }
        checkCycle(provideDependMap);
    }

    DataBeanAggregateServiceImpl service = new DataBeanAggregateServiceImpl();
    RuntimeSettings runtimeSettings = new RuntimeSettings();
    runtimeSettings.setIgnoreException(properties.isIgnoreException());
    runtimeSettings.setTimeout(properties.getDefaultTimeout());
    service.setRepository(dataProviderRepository);
    service.setRuntimeSettings(runtimeSettings);
    service.setExecutorService(aggregateExecutorService());
    service.setInterceptorChain(aggregateQueryInterceptorChain());
    service.setTaskWrapperClazz(properties.getTaskWrapperClass());
    service.setApplicationContext(applicationContext);
    return service;
}
 
Example 5
Source File: CustomProviderVerifier.java    From code-examples with MIT License 5 votes vote down vote up
@Override
public boolean verifyResponseByInvokingProviderMethods(ProviderInfo providerInfo, ConsumerInfo consumer,
																											 Object interaction, String interactionMessage, Map failures) {
	try {

		ConfigurationBuilder configurationBuilder = new ConfigurationBuilder()
						.setScanners(new MethodAnnotationsScanner())
						.forPackages(packagesToScan.toArray(new String[]{}));

		Reflections reflections = new Reflections(configurationBuilder);
		Set<Method> methodsAnnotatedWith = reflections.getMethodsAnnotatedWith(PactVerifyProvider.class);
		Set<Method> providerMethods = methodsAnnotatedWith.stream()
						.filter(m -> {
							PactVerifyProvider annotation = m.getAnnotation(PactVerifyProvider.class);
							return annotation.value().equals(((Interaction)interaction).getDescription());
						})
						.collect(Collectors.toSet());

		if (providerMethods.isEmpty()) {
			throw new RuntimeException("No annotated methods were found for interaction " +
							"'${interaction.description}'. You need to provide a method annotated with " +
							"@PactVerifyProvider(\"${interaction.description}\") that returns the message contents.");
		} else {
			if (interaction instanceof Message) {
				verifyMessagePact(providerMethods, (Message) interaction, interactionMessage, failures);
			} else {
				throw new RuntimeException("only supports Message interactions!");
			}
		}
	} catch (Exception e) {
		throw new RuntimeException("verification failed", e);
	}
	return true;
}
 
Example 6
Source File: GlobalContext.java    From drftpd with GNU General Public License v2.0 5 votes vote down vote up
/**
 * If you're creating a GlobalContext object and it's not part of a TestCase
 * you're not doing it correctly, GlobalContext is a Singleton
 */
protected GlobalContext() {
    Reflections reflections = new Reflections(new ConfigurationBuilder()
            .setUrls(ClasspathHelper.forPackage("org.drftpd"))
            .setScanners(new MethodAnnotationsScanner()));

    hooksMethods = reflections.getMethodsAnnotatedWith(CommandHook.class);
    logger.debug("We have annotated (found) [{}] hook methods", hooksMethods.size());
}
 
Example 7
Source File: GlobalContext.java    From drftpd with GNU General Public License v2.0 5 votes vote down vote up
/**
 * If you're creating a GlobalContext object and it's not part of a TestCase
 * you're not doing it correctly, GlobalContext is a Singleton
 */
protected GlobalContext() {
    Reflections reflections = new Reflections(new ConfigurationBuilder()
            .setUrls(ClasspathHelper.forPackage("org.drftpd"))
            .setScanners(new MethodAnnotationsScanner()));

    hooksMethods = reflections.getMethodsAnnotatedWith(CommandHook.class);
    logger.debug("We have annotated (found) [{}] hook methods", hooksMethods.size());
}
 
Example 8
Source File: MethodStorage.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
public void reload(){
	Reflections reflections = new Reflections(paths,
											  OrienteerClassLoader.getClassLoader(),
											  new MethodAnnotationsScanner(),
											  new TypeAnnotationsScanner(),
											  new SubTypesScanner());
	methodFields = reflections.getMethodsAnnotatedWith(OMethod.class);
	
	methodClasses = reflections.getTypesAnnotatedWith(OMethod.class);
	methodClasses.removeIf(c -> !IMethod.class.isAssignableFrom(c) && !Command.class.isAssignableFrom(c));
}
 
Example 9
Source File: AbstractAssertTestsClass.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void checkTestClasses(){
    Reflections reflections = new Reflections(new ConfigurationBuilder()
            .setUrls(ClasspathHelper.forPackage(getPackageName()))
            .setScanners(new MethodAnnotationsScanner()));
    Set<Method> methods = reflections.getMethodsAnnotatedWith(Test.class);
    Set<Class<?>> s = new HashSet<>();
    for(Method m : methods){
        s.add(m.getDeclaringClass());
    }

    List<Class<?>> l = new ArrayList<>(s);
    Collections.sort(l, new Comparator<Class<?>>() {
        @Override
        public int compare(Class<?> aClass, Class<?> t1) {
            return aClass.getName().compareTo(t1.getName());
        }
    });

    int count = 0;
    for(Class<?> c : l){
        if(!getBaseClass().isAssignableFrom(c) && !getExclusions().contains(c)){
            log.error("Test {} does not extend {} (directly or indirectly). All tests must extend this class for proper memory tracking and timeouts",
                    c, getBaseClass());
            count++;
        }
    }
    assertEquals("Number of tests not extending BaseND4JTest", 0, count);
}
 
Example 10
Source File: TestRunner.java    From at.info-knowledge-base with MIT License 5 votes vote down vote up
private static Set<Method> getMethodsForTag(String tag) {
    Reflections reflections = new Reflections(new ConfigurationBuilder()
            .setUrls(ClasspathHelper.forPackage("info.at.tagging"))
            .setScanners(new MethodAnnotationsScanner()));
    Set<Method> testMethods = new HashSet<>();
    Set<Method> allMethods = reflections.getMethodsAnnotatedWith(Tag.class);
    for (Method klass : allMethods) {
        if (Arrays.asList(klass.getAnnotation(Tag.class).value()).contains(tag)) {
            testMethods.add(klass);
        }
    }
    return testMethods;
}
 
Example 11
Source File: ClasspathUtils.java    From gridgo with MIT License 4 votes vote down vote up
public static <A extends Annotation> void scanForAnnotatedMethods(Reflections reflections, Class<A> annotationType,
        BiConsumer<Method, A> acceptor) {
    var methods = reflections.getMethodsAnnotatedWith(annotationType);
    methods.forEach(method -> acceptor.accept(method, method.getAnnotation(annotationType)));
}
 
Example 12
Source File: StepsScanner.java    From gauge-java with Apache License 2.0 4 votes vote down vote up
public void scan(Reflections reflections) {
    Logger.debug("Scanning packages for steps");
    Set<Method> stepImplementations = reflections.getMethodsAnnotatedWith(Step.class);
    buildStepRegistry(stepImplementations);
}
 
Example 13
Source File: ReflectionScanStatic.java    From disconf with Apache License 2.0 4 votes vote down vote up
/**
 * 扫描基本信息
 */
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;
}
 
Example 14
Source File: ReflectionScanStatic.java    From disconf with Apache License 2.0 4 votes vote down vote up
/**
 * 扫描基本信息
 */
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;
}
 
Example 15
Source File: ReflectionsApp.java    From tutorials with MIT License 4 votes vote down vote up
public Set<Method> getDateDeprecatedMethods() {
    Reflections reflections = new Reflections(java.util.Date.class, new MethodAnnotationsScanner());
    Set<Method> deprecatedMethodsSet = reflections.getMethodsAnnotatedWith(Deprecated.class);
    return deprecatedMethodsSet;
}