包中的注释扫描器
我正在寻找一种简单的方法来查看 java 包中我自己的注释。
我想做的是用我自己的注释标记一些类。每分钟扫描仪都应该扫描一些包以查找此注释。
使用 scanotation jar 我尝试过
URL url = ClasspathUrlFinder.findClassBase(FileWatcher.class);
,使用 AnnotationDB 我可以检查这个类(FileWatcher.class)是否有注释。现在我需要一个类列表才能扫描所有类。
我读了一些文章,大多数人说这是不可能的,因为类加载器也可以加载外部 jar 文件。
我的另一个选择是复制子目录中包含这些注释的所有 EJB 项目并扫描所有这些 jar 文件。我认为这可能可行,但是没有更好的方法吗?
许多问候, 豪克
I am looking for an easy way to look through a java package for my own annotation.
What I am trying to do is mark some classes with my own annotation. Every minute a scanner should scan some packages for this annotation.
With scanotation jar I tried that
URL url = ClasspathUrlFinder.findClassBase(FileWatcher.class);
and with AnnotationDB I could check if this class (FileWatcher.class) has the annotation. Now I need a list of classes in order to scan them all.
I read some articles and most people said it is not possible because of the classloader could load external jar files as well.
The other option I have is to copy all EJB Projects containing those annotations inside a subdirectory and scan all those jar files. I think that would might work, but is there no better way?
Many greetings,
Hauke
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用反射。有一些
Try using reflections. There are some examples in the test code.
谢谢。这很有帮助,而且工作起来非常容易。如果有人需要:
您还可以调用注释方法,而不是 getSubtypes。
如果你使用maven,请将其放入pom.xml中
Thanks. That helped a lot and it is working very easy. In case somebody needs that:
Instead of getSubtypes you can call also the annotation method.
If you use maven put this in the pom.xml
如果您想要一个真正轻量级(无依赖项、简单的 API、15 kb jar 文件)和非常快的解决方案,请查看 https://github.com/rmuller/infomas-asl。
扫描特定包的速度非常快(只需几毫秒)。
另请参阅问题在运行时扫描 Java 注释以获取其他选项
免责声明:我是作者。
If you want a really light weight (no dependencies, simple API, 15 kb jar file) and very fast solution, take a look at annotation-detector found at https://github.com/rmuller/infomas-asl.
Scanning specific packages is very fast (matter of a few ms).
See also question Scanning Java annotations at runtime for other options
Disclaimer: I am the author.