我可以使用 org.reflection 使用通配符获取包类吗? [英] I can use org.reflection to get package classes using wildcard?

查看:81
本文介绍了我可以使用 org.reflection 使用通配符获取包类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

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())

因为我真正需要的方法是在.deep"包级别......我尝试了很多与FilterBuilder的组合,然后我不知道是我做错了什么还是这个API不可能

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

推荐答案

尝试

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());

这篇关于我可以使用 org.reflection 使用通配符获取包类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆