我有一个方法的简单标记注释(类似于Effective Java(第2版)第35项中的第一个例子):
/**
* Marker annotation for methods that are called from installer's
* validation scripts etc.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface InstallerMethod {
}
Run Code Online (Sandbox Code Playgroud)
然后,在一个给定的包(比如说com.acme.installer)中,它有几个包含20个类的子包,我想找到所有用它注释的方法.(因为我想对单元测试中所有带注释的方法进行一些检查.)
什么(如果有的话)是最简单的方法呢?最好不要添加新的第三方库或框架.
编辑:澄清,显然method.isAnnotationPresent(InstallerMethod.class)将是检查方法是否具有注释的方法 - 但是这个问题包括找到所有方法.
par*_*rkr 51
如果您想自己实现它,这些方法将找到给定包中的所有类:
/**
* 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();
URI uri = new URI(resource.toString());
dirs.add(new File(uri.getPath()));
}
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;
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用给定的注释过滤这些类:
for (Method method : testClass.getMethods())
{
if (method.isAnnotationPresent(InstallerMethod.class))
{
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
Ale*_*øld 35
你应该看一下开源的Reflections库.有了它,您只需几行代码即可轻松实现您的目标:
Reflections reflections = new Reflections(
new ConfigurationBuilder().setUrls(
ClasspathHelper.forPackage( "com.acme.installer" ) ).setScanners(
new MethodAnnotationsScanner() ) );
Set<Method> methods = reflections.getMethodsAnnotatedWith(InstallerMethod.class);
Run Code Online (Sandbox Code Playgroud)