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

Java 如何使用给定的注释运行所有方法?,java,reflection,annotations,Java,Reflection,Annotations,这就是我希望发生的事情: 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

这就是我希望发生的事情:

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

或者还有其他方法可以实现同样的效果吗?

您可以使用类路径扫描来实现:基本上,您可以检查类路径中每个类的每个方法,并使用给定的注释对所有方法进行注释。然后,调用找到的方法

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

注意:不使用也可以,但是代码会越来越脏

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

import java.lang.annotation.annotation;
导入java.lang.annotation.ElementType;
导入java.lang.annotation.Retention;
导入java.lang.annotation.RetentionPolicy;
导入java.lang.annotation.Target;
导入java.lang.reflect.Method;
导入java.util.Set;
导入org.reflections.reflections;
导入org.reflections.scanners.MethodAnnotationsCanner;
导入org.reflections.util.ClasspathHelper;
导入org.reflections.util.ConfigurationBuilder;
公共类运行类{
公共静态void main(字符串args[])引发异常{
runAllAnnotatedWith(mod.class);
}

publicstaticvoid runAllAnnotatedWith(Class以下是一个使用我最近发布的开源库的示例,名为Reflect:

List<Method> methods = Reflect.on(someClass).methods().annotatedWith(mod.class);
for (Method m : methods) {
  m.invoke(null);
}
List methods=Reflect.on(someClass).methods().annotatedWith(mod.class);
用于(方法m:方法){
m、 调用(空);
}
Maven依赖项:

<dependency>
    <groupId>org.pacesys</groupId>
    <artifactId>reflect</artifactId>
    <version>1.0.0</version>
</dependency>

org.pacesys

我想你可以使用反射技术

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

public class Fundamental {
   public static void main(String[] args) {
      // Get all methods in order.
      // runClass is the class you declare all methods with annotations.
      Method[] methods = runClass.getMethods();
      for(Method mt : methods) {
        if (mt.isAnnotationPresent(mod.class)) {
            // Invoke method with appropriate arguments
            Object obj = mt.invoke(runClass, null);
        }
      } 
   }
}

这是不可能的。你到底想要实现什么?你想在你的示例中实现什么?能够在不进入其他类的情况下调用函数。正如Arian所说,你并没有真正描述你想要实现什么,也没有描述你的问题是什么。我希望能更清楚地了解这个问题的意图。下面有一些有效的建议,但是方向有问题。请添加免责声明,说明您正在推广自己的项目。
<dependency>
    <groupId>org.reflections</groupId>
    <artifactId>reflections</artifactId>
    <version>0.9.9-RC1</version>
</dependency>
List<Method> methods = Reflect.on(someClass).methods().annotatedWith(mod.class);
for (Method m : methods) {
  m.invoke(null);
}
<dependency>
    <groupId>org.pacesys</groupId>
    <artifactId>reflect</artifactId>
    <version>1.0.0</version>
</dependency>
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface mod {
   public String name() default "";
}

public class Fundamental {
   public static void main(String[] args) {
      // Get all methods in order.
      // runClass is the class you declare all methods with annotations.
      Method[] methods = runClass.getMethods();
      for(Method mt : methods) {
        if (mt.isAnnotationPresent(mod.class)) {
            // Invoke method with appropriate arguments
            Object obj = mt.invoke(runClass, null);
        }
      } 
   }
}