我有一个SubClass和一个SuperClass,以及一个注释DocAnnotation。我需要调用SubClass.foo()从SubClass和SuperClass获得所有类注释。类的定义如下:
SuperClass
package Code
import Annotations.DocAnnotation;
import java.util.Arrays;
@DocAnnotation("Super Annotation")
public class SuperClass {
public void foo() {
System.out.println(Arrays.toString(this.getClass().getAnnotations()));
System.out.println(Arrays.toString(this.getClass().getDeclaredAnnotations()));
}
}SubClass
package Code;
import Annotations.DocAnnotation;
@DocAnnotation("Sub Annotation")
public class SubClass extends SuperClass{
}DocAnnotation
package Annotations;
import java.lang.annotation.Inherited;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(DocAnnotations.class)
public @interface DocAnnotation {
String value();
}运行SubClass.foo时,我希望看到"Super Annotation"和"Sub Annotation",但是我只看到了[@Annotations.DocAnnotation(value=Sub Annotation)]。我是误解了“继承”的作用,还是做错了什么?
编辑:
在将注释@DocAnnotation("Super Annotation")添加到SubClass (与SuperClass中相同)之后,它实际上显示了两次,一次用于SubClass,一次用于SuperClass!现在我几乎可以肯定我误解了什么..。
发布于 2017-09-07 04:58:20
这似乎是预期的行为,或者至少指定为这样工作(文档):
如果..。用户在类声明上查询注释类型,和类声明对这个类型的没有注释,那么类的超类将自动被查询到注释类型。
换句话说,由于SubClass已经用@DocAnnotation注释了,所以不需要查询超类。
但是,在进一步的检查中,这种行为似乎有点奇怪,特别是在尝试了包含注释类型之后。我想出了下面的例子,说明了这一点(链接):
import java.lang.annotation.*;
import java.util.*;
@Inherited
@Repeatable(Container.class)
@Retention(RetentionPolicy.RUNTIME)
@interface Ann {
String value();
}
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@interface Container {
Ann[] value();
}
// Basic case. Result is that
// only @Ann("2") is present on
// ExhibitASub.
@Ann("1")
class ExhibitASuper {
}
@Ann("2")
class ExhibitASub extends ExhibitASuper {
}
// Because this case results in the
// @Container being present on ExhibitBSuper,
// rather than @Ann, all three annotations
// end up appearing on ExhibitBSub.
@Ann("1")
@Ann("2")
class ExhibitBSuper {
}
@Ann("3")
class ExhibitBSub extends ExhibitBSuper {
}
// Similar to the preceding case, by
// forcing the use of @Container, both
// annotations are present on ExhibitCSub.
@Container(@Ann("1"))
class ExhibitCSuper {
}
@Ann("2")
class ExhibitCSub extends ExhibitCSuper {
}
// Yet when we force both to use @Container,
// only @Container(@Ann("2")) is present on
// ExhibitDSub.
@Container(@Ann("1"))
class ExhibitDSuper {
}
@Container(@Ann("2"))
class ExhibitDSub extends ExhibitDSuper {
}
class Main {
public static void main(String[] args) {
for (Class<?> cls : Arrays.asList(ExhibitASub.class,
ExhibitBSub.class,
ExhibitCSub.class,
ExhibitDSub.class)) {
System.out.printf("%s:%n", cls);
for (Annotation ann : cls.getAnnotations()) {
System.out.printf(" %s%n", ann);
}
System.out.println();
}
}
}其产出如下:
class ExhibitASub:
@Ann(value=2)
class ExhibitBSub:
@Container(value=[@Ann(value=1), @Ann(value=2)])
@Ann(value=3)
class ExhibitCSub:
@Container(value=[@Ann(value=1)])
@Ann(value=2)
class ExhibitDSub:
@Container(value=[@Ann(value=2)])注意,对于B和C,我们看到了超类和子类的注释。这大概是因为(严格意义上)超类上的注释与子类上的注释类型不同。注意,对于D,我们返回到只看到子类注释,因为这两个类都使用容器类型。
在某些情况下,显式使用包含注释类型可能是一种解决办法,但由于案例D,它不是一般的解决方案。
我明天可能会为此提交一份错误报告,因为这看起来非常不受欢迎。因为@Inherited早于@Repeatable,所以这种行为可能来自于以前的版本,在这些版本中,这种情况不可能发生。
发布于 2017-09-07 04:25:37
您弄错了这个注释。javadoc明确指出:
指示注释类型自动继承。如果注释类型声明中存在继承的元注释,并且用户在类声明中查询注释类型,而类声明对该类型没有注释,那么该类的超类将自动被查询为注释类型。
换句话说:如果查询子类,则会发现超类正在被注释。但这是,而不是,意思是"OO“意义上的继承。如果希望看到这两个注释,则必须编写手动检查类继承树中每个类的代码。
https://stackoverflow.com/questions/46087372
复制相似问题