【发布时间】:2018-01-19 12:20:04
【问题描述】:
我有课
@ReadOnlyViewable(category = "Cache", description = "View the contents of a cache")
public class ReflectingCacheJmx<K, V> extends CacheJmx<K, V> {
我在上面添加了注释
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ReadOnlyViewable {
String description();
String category();
}
然后我有一个子类:
public class ObservableReflectingCacheJmx<K, V> extends ReflectingCacheJmx<K, V> {
还有一个子类:
public class CounterpartyCacheJmx extends ObservableReflectingCacheJmx<CounterpartyKey, Counterparty> {
当我获取CounterpartyCacheJmx 的实例并调用getClass().getAnnotation(ReadOnlyViewable.class) 时,我得到null 的结果。
另一方面,如果我调用 getClass().getSuperclass().getSuperclass().getAnnotation(ReadOnlyViewable.class),我会返回注释。
我认为getAnnotation 应该也返回超类的注释,而getDeclaredAnnotation 没有。
我的理解错了吗?我是否需要自己手动运行超类链(或使用第 3 方库来这样做)才能找到此注释?
Spring ApplicationContext::getBeansWithAnnotation 在生成的 Map 中返回这些 bean,因此似乎对注释进行了某种深度解析。
【问题讨论】:
标签: java annotations