这是我的注释:
@Target( { ElementType.METHOD } )
@Retention(RetentionPolicy.RUNTIME)
public @interface AuditUpdate
{
Class<?> value();
}
这样就可以了:
@AuditUpdate(User.class)
void someMethod(){}
但是通过这种方式:
private static final Class<?> ENTITY_CLASS = User.class;
@AuditUpdate(ENTITY_CLASS)
void someMethod(){}
我有这个编译错误:
The value for annotation attribute AuditUpdate.value must be a class literal
为什么?这是什么意思?
Why? What does that mean?
谢谢.
这意味着你必须传递一个类字面量,而不是一个变量(即使是常量).
It means that you must pass a class literal, rather than a variable (even if constant).
类文字在 JLS:
15.8.2 类文字
类字面量是由类名组成的表达式,接口、数组或原始类型,后跟."和令牌班级.类文字的类型是 Class.它评估为类由定义定义的命名类型(或 void)的对象当前实例的类的类加载器.
A class literal is an expression consisting of the name of a class, interface, array, or primitive type followed by a `.' and the token class. The type of a class literal is Class. It evaluates to the Class object for the named type (or for void) as defined by the defining class loader of the class of the current instance.
所以,你不能那样做.
这篇关于将注释值设置为 Class<?> 时出错从一个常数,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何检测 32 位 int 上的整数溢出?How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
return 语句之前的局部变量,这有关系吗?Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
如何将整数转换为整数?How to convert Integer to int?(如何将整数转换为整数?)
如何在给定范围内创建一个随机打乱数字的 intHow do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
java的行为不一致==Inconsistent behavior on java#39;s ==(java的行为不一致==)
为什么 Java 能够将 0xff000000 存储为 int?Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 int?)