gpt4 book ai didi

java - 为什么 "Integer.TYPE"在注释 "attribute value must be constant"中显示错误

转载 作者:行者123 更新时间:2023-12-02 03:43:37 24 4
gpt4 key购买 nike

我创建了一个注释:

@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 value V if and only if one of the following is true:

  • [...]

  • If T is Class or an invocation of Class (§4.5), then V is a class literal (§15.8.2).

来自Integer的源代码

public static final Class<Integer>  TYPE = (Class<Integer>) Class.getPrimitiveClass("int");

表达式Integer#TYPE不是类文字。如果您想要的话,Integer.classint.class 都可以。

关于java - 为什么 "Integer.TYPE"在注释 "attribute value must be constant"中显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36563235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com