我创建了一个注释:
@Target(ElementType.FIELD )
@Retention( RetentionPolicy.RUNTIME )
public @interface Test
{
Class something();
}
但是当我用Integer.TYPE
(对于int的返回类型)调用它时,它显示了错误。
public class TestA
{
@Test(Integer.TYPE)
public int id;
}
Class
与值Integer.TYPE
不相称
如果元素类型不相称,则是编译时错误具有元素值。元素类型
T
与元素值V
当且仅当以下其中一项为真:
[…]
如果
T
是Class
或调用Class
(§4.5),则V
是一个类文字(§15.8.2)。
来自Integer
的源代码
public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
表达式Integer#TYPE
不是类文字。Integer.class
或int.class
会起作用,如果这就是你想要的。
尝试使用int.class
而不是Integer.TYPE
:
@Test(int.class)