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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/862106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:22:00  来源:igfitidea点击:

How to find annotated methods in a given package?

javaannotations

提问by Jonik

I have a simple marker annotation for methods (similar to the first example in Item 35 in Effective Java(2nd ed)):

我有一个简单的方法标记注释(类似于Effective Java(第二版)第 35 条中的第一个示例):

/**
 * Marker annotation for methods that are called from installer's 
 * validation scripts etc. 
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface InstallerMethod {
}

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.)

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

What (if any) is the easiest way to do this? Preferably without adding new 3rd party libraries or frameworks.

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

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.

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

采纳答案by parkr

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();
        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;
}

Then you can just filter on those classes with the given annotation:

然后,您可以使用给定的注释过滤这些类:

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

回答by skaffman

If you're happy to use Spring, then that does something along these lines using it's context:component-scan functionality, where Spring scans for annotated classes in a given package. Under the covers, it's pretty gruesome, and involves grubbing about on the filesystem and in JAR files looking for classes in the package.

如果您乐于使用 Spring,那么可以使用它的 context:component-scan 功能执行这些操作,其中 Spring 扫描给定包中的带注释的类。在幕后,它非常可怕,涉及在文件系统和 JAR 文件中寻找包中的类。

Even if you can't use Spring directly, having a look through its source code might give you some ideas.

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

Certainly, the Java reflection APi is no use here, it specifically does not provide a means to obtain all classes in a package.

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

回答by Aleksander Blomsk?ld

You should probably take a look at the open source Reflections library. With it you can easily achieve what you want with few lines of code:

您或许应该看看开源的Reflections 库。有了它,你可以用几行代码轻松实现你想要的:

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