I can use org.reflection to get package classes using wildcard?

蓝咒 提交于 2021-02-05 09:27:47

问题


I was using

Reflections reflections = new Reflections("com.mypackage.root", new MethodAnnotationsScanner()); 

But with this aproach i get back a lot of annotations in packages level that i don't need... What i really want for, is something like this, using an wildcard to get back the deep package level only.

Reflections reflections = new Reflections("com.mypackage.root.*.deep", new MethodAnnotationsScanner())

Because the methods that i really need is in ".deep" package level... I try a lot of combinations with FilterBuilder, then i don't know if i am doing something wrong or this is not possible with this API


回答1:


Try

Reflections reflections = new Reflections("com.mypackage.root", new MethodAnnotationsScanner());

Set<Method> methods = reflections
        .getMethodsAnnotatedWith(MyAnnotation.class).stream()
        .filter(method -> method.getDeclaringClass().getPackage().getName().matches("com.mypackage.root.*.deep"))
        .collect(Collectors.toSet());


来源:https://stackoverflow.com/questions/59145550/i-can-use-org-reflection-to-get-package-classes-using-wildcard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!