如何找到一个给定的包带注释的方法呢? [英] How to find annotated methods in a given package?
问题描述
我有一个方法,一个简单的标记注释(类似于第一个例子中的35项的有效的Java 的(第二版)):
I have a simple marker annotation for methods (similar to the first example in Item 35 in Effective Java (2nd ed)):
/**
* Marker annotation for methods that are called from installer's
* validation scripts etc.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface InstallerMethod {
}
然后,在一个给定的包(比如 com.acme.installer
),其中有一些含有20个班的几个子包,我想找到所有的方法注释都用它。 (因为我想要做的关于单元测试的所有注解的方法进行一些检查。)
Then, in a given package (say com.acme.installer
), which has a few subpackages containing some 20 classes, I'd like to find all methods that are annotated with it. (Because I'd like to do some checks regarding all the annotated methods in a unit test.)
什么(如果有的话)是做最简单的方法? preferably无需添加新的第三方库或框架。
What (if any) is the easiest way to do this? Preferably without adding new 3rd party libraries or frameworks.
修改:澄清一下,明明 method.isAnnotation present(InstallerMethod.class)
将是检查是否有方法的方法有注解 - 但这个问题包括找到的所有方法
Edit: to clarify, obviously method.isAnnotationPresent(InstallerMethod.class)
will be the way to check if a method has the annotation - but this problem includes finding all the methods.
推荐答案
如果你想自己实现它,这些方法会发现在一个给定的包中的所有类:
If you want to implement it yourself, these methods will find all the classes in a given package:
/**
* Scans all classes accessible from the context class loader which belong
* to the given package and subpackages.
*
* @param packageName
* The base package
* @return The classes
* @throws ClassNotFoundException
* @throws IOException
*/
private Iterable<Class> getClasses(String packageName) throws ClassNotFoundException, IOException
{
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
String path = packageName.replace('.', '/');
Enumeration<URL> resources = classLoader.getResources(path);
List<File> dirs = new ArrayList<File>();
while (resources.hasMoreElements())
{
URL resource = resources.nextElement();
dirs.add(new File(resource.getFile()));
}
List<Class> classes = new ArrayList<Class>();
for (File directory : dirs)
{
classes.addAll(findClasses(directory, packageName));
}
return classes;
}
/**
* Recursive method used to find all classes in a given directory and
* subdirs.
*
* @param directory
* The base directory
* @param packageName
* The package name for classes found inside the base directory
* @return The classes
* @throws ClassNotFoundException
*/
private List<Class> findClasses(File directory, String packageName) throws ClassNotFoundException
{
List<Class> classes = new ArrayList<Class>();
if (!directory.exists())
{
return classes;
}
File[] files = directory.listFiles();
for (File file : files)
{
if (file.isDirectory())
{
classes.addAll(findClasses(file, packageName + "." + file.getName()));
}
else if (file.getName().endsWith(".class"))
{
classes.add(Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6)));
}
}
return classes;
}
然后,你可以在给定的注解这些类过滤器:
Then you can just filter on those classes with the given annotation:
for (Method method : testClass.getMethods())
{
if (method.isAnnotationPresent(InstallerMethod.class))
{
// do something
}
}
这篇关于如何找到一个给定的包带注释的方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!