扫描 Java 包以获取注释 [英] Scanning Java package for annotations

查看:72
本文介绍了扫描 Java 包以获取注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现递归扫描带注释类的逻辑.我可以毫无问题地扫描包结构:

I want to implement logic that will recursively scan for annotated classes. I can scan the package structure without issue:

Stack<Package> stack = new Stack<Package>();
stack.push(Package.getPackage(ROOT_PACKAGE));
while(!stack.isEmpty()) {
    Package temp = stack.pop();             
    annotatedClasses.addAll(getAnnotatedClasses(temp));         
    for(Package p : temp.getPackages()) {
        stack.push(p);
    }
}

让我感到困惑的是我似乎无法找到实现此方法的方法:

Where I've come unstuck is that I can't seem to find a way to implement this method:

public List<Class> getAnnotatedClasses(Package p);

给定一个 Package 对象,有没有办法获取其中的所有类?

Given a Package object is there a way to get all Classes within it?

推荐答案

  • 您可以使用反射扫描包中的类 使用反射获取包的所有类,然后递归地进入所有类中以找到注释.
    • You can scan for the classes in the package using reflection Using Reflections to get all classes of package and then recursively go inside all the classes to find the annotations.
    • 这篇关于扫描 Java 包以获取注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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