我创建了一个注释:
@Target(ElementType.FIELD )
@Retention( RetentionPolicy.RUNTIME )
public @interface Test
{
Class something();
}
但是当我使用 Integer.TYPE
(对于 int 的返回类型)调用它时,它会显示错误。
public class TestA
{
@Test(Integer.TYPE)
public int id;
}
最佳答案
Class
is not commensurate with the value Integer.TYPE
It is a compile-time error if the element type is not commensurate with the element value. An element type
T
is commensurate with an element valueV
if and only if one of the following is true:
[...]
If
T
isClass
or an invocation ofClass
(§4.5), thenV
is a class literal (§15.8.2).
来自Integer
的源代码
public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
表达式Integer#TYPE
不是类文字。如果您想要的话,Integer.class
或 int.class
都可以。
关于java - 为什么 "Integer.TYPE"在注释 "attribute value must be constant"中显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36563235/