How to run all methods with a given annotation?

后端 未结 3 1540
说谎
说谎 2021-02-06 18:11

This is what I want to happen:

public class MainClass {
    public static void main(String args[]) { 
        run @mod(); // run all methods annotated with @mod          


        
3条回答
  •  被撕碎了的回忆
    2021-02-06 18:49

    You can do it using classpath scanning: Basically you go over every method of every class in the classpath and get all annotated with your given annotation. After that, you invoke the found methods.

    Below is a runAllAnnotatedWith() method that would do it. It uses Reflections to do the dirty work of classpath scanning. For simplicity, it executes all found methods as if they were static and required no parameters.

    public static void runAllAnnotatedWith(Class annotation)
                                                                   throws Exception {
        Reflections reflections = new Reflections(new ConfigurationBuilder()
                                      .setUrls(ClasspathHelper.forJavaClassPath())
                                      .setScanners(new MethodAnnotationsScanner()));
        Set methods = reflections.getMethodsAnnotatedWith(annotation);
    
        for (Method m : methods) {
            // for simplicity, invokes methods as static without parameters
            m.invoke(null); 
        }
    }
    

    You can run it using:

    runAllAnnotatedWith(mod.class);
    

    Note: It is possible to do it without using Reflections, but the code will get dirtier and dirtier.

    Here's the full code (paste it all into a RunClass.java file):

    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 annotation) throws Exception {
            Reflections reflections = new Reflections(new ConfigurationBuilder()
                    .setUrls(ClasspathHelper.forJavaClassPath()).setScanners(
                            new MethodAnnotationsScanner()));
            Set 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 "";
    }
    

    To run it, you have to add the Reflections JAR to your project. Download it here.

    If you use Maven, you can add it using:

    
        org.reflections
        reflections
        0.9.9-RC1
    
    

提交回复
热议问题