Java 如何在具有特定注释的方法上要求参数?

Java 如何在具有特定注释的方法上要求参数?,java,class,annotations,command,public,Java,Class,Annotations,Command,Public,因此,我要寻找的是,如果有任何方法,您可以使一个@注释需要一个方法来拥有一个参数 我的意思是,如果你有一个带有@Command的方法,那么它需要方法本身有一个参数IssuedCommand 像这样 @Command public void Command(IssuedCommand){}

因此,我要寻找的是,如果有任何方法,您可以使一个
@注释
需要一个方法来拥有一个参数

我的意思是,如果你有一个带有
@Command
的方法,那么它需要方法本身有一个参数
IssuedCommand

像这样

@Command public void Command(IssuedCommand){}
<它要求IssuedCommand在那里,否则将出现错误

这有可能吗


提前谢谢

下面是一个使用反射的工作示例

public class Driver {
    public static void main(String[] args) {
        // get all methods
        for (Method method : Driver.class.getDeclaredMethods()) {
            // get your annotation
            Annotation annotation = method.getAnnotation(Command.class); // reference could be of type Command if you want
            if (annotation != null) {
                // check if parameter exists
                List<Class> parameterTypes = new ArrayList<Class>(Arrays.asList(method.getParameterTypes()));
                if (!parameterTypes.contains(IssuedCommand.class)) {
                    System.out.println("trouble");
                }
            }
        }
    }

    @Command
    public void command(IssuedCommand cmd) {

    }

    public static class IssuedCommand {}

    @Retention(RetentionPolicy.RUNTIME)
    @Target(value = ElementType.METHOD)
    public @interface Command {}
}
公共类驱动程序{
公共静态void main(字符串[]args){
//获取所有方法
对于(方法:Driver.class.getDeclaredMethods()){
//获取您的注释
Annotation=method.getAnnotation(Command.class);//如果需要,引用可以是Command类型
if(注释!=null){
//检查参数是否存在
List parameterTypes=new ArrayList(Arrays.asList(method.getParameterTypes());
如果(!parameterTypes.contains(IssuedCommand.class)){
系统输出打印(“故障”);
}
}
}
}
@命令
公共无效命令(IssuedCommand cmd){
}
公共静态类IssuedCommand{}
@保留(RetentionPolicy.RUNTIME)
@目标(值=ElementType.METHOD)
public@interface命令{}
}

您可以使用反射来获取要检查的特定方法。可以通过检查该方法是否已注释来完成此操作。然后可以比较方法参数列表中的类型。

下面是一个使用反射的工作示例

public class Driver {
    public static void main(String[] args) {
        // get all methods
        for (Method method : Driver.class.getDeclaredMethods()) {
            // get your annotation
            Annotation annotation = method.getAnnotation(Command.class); // reference could be of type Command if you want
            if (annotation != null) {
                // check if parameter exists
                List<Class> parameterTypes = new ArrayList<Class>(Arrays.asList(method.getParameterTypes()));
                if (!parameterTypes.contains(IssuedCommand.class)) {
                    System.out.println("trouble");
                }
            }
        }
    }

    @Command
    public void command(IssuedCommand cmd) {

    }

    public static class IssuedCommand {}

    @Retention(RetentionPolicy.RUNTIME)
    @Target(value = ElementType.METHOD)
    public @interface Command {}
}
公共类驱动程序{
公共静态void main(字符串[]args){
//获取所有方法
对于(方法:Driver.class.getDeclaredMethods()){
//获取您的注释
Annotation=method.getAnnotation(Command.class);//如果需要,引用可以是Command类型
if(注释!=null){
//检查参数是否存在
List parameterTypes=new ArrayList(Arrays.asList(method.getParameterTypes());
如果(!parameterTypes.contains(IssuedCommand.class)){
系统输出打印(“故障”);
}
}
}
}
@命令
公共无效命令(IssuedCommand cmd){
}
公共静态类IssuedCommand{}
@保留(RetentionPolicy.RUNTIME)
@目标(值=ElementType.METHOD)
public@interface命令{}
}

您可以使用反射来获取要检查的特定方法。可以通过检查该方法是否已注释来完成此操作。然后,您可以比较方法参数列表中的类型。

在编译时或运行时?我希望它在运行时。在编译时或运行时?我希望它在运行时。