【发布时间】:2014-05-07 02:58:07
【问题描述】:
假设如下:
@SomeAnnotation
public interface Foo {
}
我想知道SomeAnnotation 的定义类加载器是否总是等于Foo 的启动类加载器的父级。
我已阅读JVMS v8 section 5.3。但我不确定这里适用什么。 5.3.4 节讲了加载约束,但是好像没有申请注解。
我问的问题是因为这样的代码:
Class<?> fooClass = //will in some way obtain a reference to class Foo
fooClass.getAnnotation(SomeAnnotation.class);
在存在不同的类加载器时会失败。我知道我可以使用getAnnotations 并在结果数组中搜索其类名称等于SomeAnnotation 名称的元素。但我想知道以下是否也可以:
Class<?> fooClass = //will in some way obtain a reference to class Foo
fooClass.getAnnotation((Class<? extends Annotation>) fooClass
.getClassLoader().loadClass(SomeAnnotation.class.getName()));
【问题讨论】:
标签: java classloader