Java 仅显示一个注释
如您所见,我有3个不同的注释类。除了类的名称之外,所有3个都是相同的。调用Java 仅显示一个注释,java,reflection,annotations,Java,Reflection,Annotations,如您所见,我有3个不同的注释类。除了类的名称之外,所有3个都是相同的。调用Tree.getAnnotations()时唯一显示的是注释@元素 @Element(name = "node") public class Tree<T> { private Tree<T>[] children = null; private T value; public Tree(T v, Tree<T>[] trees){ c
Tree.getAnnotations()
时唯一显示的是注释@元素
@Element(name = "node")
public class Tree<T> {
private Tree<T>[] children = null;
private T value;
public Tree(T v, Tree<T>[] trees){
children = trees;
value = v;
}
public Tree(T v){
value = v;
}
@SubElements(name = "subnodes")
public Tree<T>[] getChildren(){
return children;
}
@ElementField(name = "value")
public T getValue(){
return value;
}
}
该类实际上只有一个注释。另外两个属于类方法。要想找到他们,你可以做如下事情:
Method getValue = Tree.class.getMethod("getValue"); //get the method
getValue.getAnnotations(); //get annotations, only ElementField in this case
其他方法也一样。请不要链接屏幕截图,而是直接发布代码。对不起,我不知道怎么做that@AntonGustafsson复制+粘贴?这是因为其他两个注释在方法上,而不是在类上,你不觉得吗?在创建问题时,有一个帮助部分:空行和4个空格…那么我应该如何访问它们?第一次使用此命令时,请添加一个example@kaqqao谢谢,我工作忙得不可开交
Method getValue = Tree.class.getMethod("getValue"); //get the method
getValue.getAnnotations(); //get annotations, only ElementField in this case