我创建了一个注释:
@Target(ElementType.FIELD )
@Retention( RetentionPolicy.RUNTIME )
public @interface Test
{
Class something();
}
但是当我用Integer.TYPE(对于 int 的返回类型)调用它时,它会显示错误。
public class TestA
{
@Test(Integer.TYPE)
public int id;
}
我创建了一个注释:
@Target(ElementType.FIELD )
@Retention( RetentionPolicy.RUNTIME )
public @interface Test
{
Class something();
}
但是当我用Integer.TYPE(对于 int 的返回类型)调用它时,它会显示错误。
public class TestA
{
@Test(Integer.TYPE)
public int id;
}
如果元素类型与元素值不相称,则为编译时错误。当且仅当以下条件之一为真时,元素类型
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)