如何使用给定注释运行所有方法?

MSP*_*SPO 3 java reflection annotations

这就是我想发生的事情:

public class MainClass {
    public static void main(String args[]) { 
        run @mod(); // run all methods annotated with @mod annotation
    }
}
Run Code Online (Sandbox Code Playgroud)

注解声明:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface mod {
   String name() default "";
}
Run Code Online (Sandbox Code Playgroud)

要调用的方法

public class Good {
    @mod (name = "me1")
    public void calledcs(){
        System.out.println("called");
    }

    @mod (name = "me2")
    public void calledcs2(){
        System.out.println("called");
    }
}
Run Code Online (Sandbox Code Playgroud)

还是有另一种方法可以实现同一目标?

acd*_*ior 5

您可以使用类路径扫描来做到这一点:基本上,您遍历了类路径中每个类的每个方法,并使用给定的注释对所有注释进行注释。之后,您调用找到的方法。

下面是runAllAnnotatedWith()可以做到这一点的方法。它使用反射来完成类路径扫描的肮脏工作。为简单起见,它将执行所有找到的方法,就好像它们是过去static且不需要任何参数一样。

public static void runAllAnnotatedWith(Class<? extends Annotation> annotation)
                                                               throws Exception {
    Reflections reflections = new Reflections(new ConfigurationBuilder()
                                  .setUrls(ClasspathHelper.forJavaClassPath())
                                  .setScanners(new MethodAnnotationsScanner()));
    Set<Method> methods = reflections.getMethodsAnnotatedWith(annotation);

    for (Method m : methods) {
        // for simplicity, invokes methods as static without parameters
        m.invoke(null); 
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以使用以下命令运行它:

runAllAnnotatedWith(mod.class);
Run Code Online (Sandbox Code Playgroud)

注意:可以在不使用Reflections的情况下执行此操作,但是代码将变得越来越脏。

这是完整的代码(将其全部粘贴到RunClass.java文件中):

import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.util.Set;

import org.reflections.Reflections;
import org.reflections.scanners.MethodAnnotationsScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;

public class RunClass {
    public static void main(String args[]) throws Exception {
        runAllAnnotatedWith(mod.class);
    }

    public static void runAllAnnotatedWith(Class<? extends Annotation> annotation) throws Exception {
        Reflections reflections = new Reflections(new ConfigurationBuilder()
                .setUrls(ClasspathHelper.forJavaClassPath()).setScanners(
                        new MethodAnnotationsScanner()));
        Set<Method> methods = reflections.getMethodsAnnotatedWith(annotation);

        for (Method m : methods) {
            m.invoke(null); // for simplicity, invoking static methods without parameters
        }
    }

    @mod(name = "me1")
    public static void calledcs() {
        System.out.println("called");
    }

    @mod(name = "me2")
    public static void calledcs2() {
        System.out.println("called2");
    }
}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface mod {
    String name() default "";
}
Run Code Online (Sandbox Code Playgroud)

要运行它,您必须将Reflections JAR 添加到您的项目中。在这里下载

如果使用Maven,则可以使用以下方法添加它:

<dependency>
    <groupId>org.reflections</groupId>
    <artifactId>reflections</artifactId>
    <version>0.9.9-RC1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)