如何使用给定的注释运行所有方法? [英] How to run all methods with a given annotation?

查看:107
本文介绍了如何使用给定的注释运行所有方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要发生的事情:

This is what I want to happen:

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

注释声明:

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

要调用的方法

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

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

还是有另一种方法可以达到同样的效果吗?

or is there another way to achieve the same thing?

推荐答案

你可以使用类路径扫描来做到这一点:基本上你会查看类路径中每个类的每个方法并获得所有注释你给出的注释。之后,你调用找到的方法。

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.

下面是一个 runAllAnnotatedWith()方法。它使用 反思 来完成类路径扫描的脏工作。 为简单起见,它执行所有找到的方法,就好像它们是 static 并且不需要任何参数。

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

您可以使用以下方式运行它:

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.

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

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<? 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 "";
}

要运行它,你必须添加反思 JAR到您的项目。 在此处下载

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

如果您使用Maven,可以使用以下方式添加:

If you use Maven, you can add it using:

<dependency>
    <groupId>org.reflections</groupId>
    <artifactId>reflections</artifactId>
    <version>0.9.9-RC1</version>
</dependency>

这篇关于如何使用给定的注释运行所有方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆