如何在给定的包中找到带注释的方法?

时间:2009-05-14 08:04:14

标签: java annotations

我有方法的简单标记注释(类似于 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 {
}

然后,在一个给定的包(比如com.acme.installer)中,它有一些包含20个类的子包,我想找到所有用它注释的方法。 (因为我想对单元测试中的所有注释方法进行一些检查。)

最简单的方法是什么(如果有的话)?最好不要添加新的第三方库或框架。

编辑:澄清一下,显然method.isAnnotationPresent(InstallerMethod.class)将是检查方法是否具有注释的方法 - 但这个问题包括找到所有方法。

3 个答案:

答案 0 :(得分:50)

如果您想自己实现它,这些方法将找到给定包中的所有类:

/**
 * 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;
}

然后你可以使用给定的注释过滤那些类:

for (Method method : testClass.getMethods())
{
    if (method.isAnnotationPresent(InstallerMethod.class))
    {
        // do something
    }
}

答案 1 :(得分:35)

您应该查看开源Reflections library。有了它,您可以通过几行代码轻松实现您想要的目标:

Reflections reflections = new Reflections( 
    new ConfigurationBuilder().setUrls( 
    ClasspathHelper.forPackage( "com.acme.installer" ) ).setScanners(
    new MethodAnnotationsScanner() ) );
Set<Method> methods = reflections.getMethodsAnnotatedWith(InstallerMethod.class);

答案 2 :(得分:1)

如果你很乐意使用Spring,那么就可以使用它的上下文:组件扫描功能,其中Spring扫描给定包中的注释类。在幕后,它非常可怕,并涉及在文件系统和JAR文件中寻找包中的类。

即使您不能直接使用Spring,查看其源代码可能会给您一些想法。

当然,Java反射APi在这里没用,它具体没有提供获取包中所有类的方法。

相关问题