所以我正在寻找的是,无论如何,你可以让@Annotation
需要一个方法来获得参数。
我的意思是,如果您的方法有@Command
,则需要使用方法将参数设置为IssuedCommand
喜欢这个
@Command public void command(IssuedCommand cmd) {}
<它要求IssuedCommand在那里,否则会出现错误。
无论如何这是可能的吗?
提前致谢。
答案 0 :(得分:1)
以下是使用反射
的工作示例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 {}
}
使用反射来获取要检查的特定方法。您可以通过检查方法是否已注释来执行此操作。然后,您可以比较方法参数列表中的类型。