错误设置注释值,类<>从不断的,为什么呢? [英] Error setting annotation value as Class<?> from a constant, why?

查看:195
本文介绍了错误设置注释值,类<>从不断的,为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的批注:

@Target( { ElementType.METHOD } )
@Retention(RetentionPolicy.RUNTIME)
public @interface AuditUpdate
{
    Class<?> value();
}

通过这种方式,它是确定的:

By this way it is ok:

@AuditUpdate(User.class)
void someMethod(){}

不过,通过这样的方式:

But by this way:

private static final Class<?> ENTITY_CLASS = User.class;
@AuditUpdate(ENTITY_CLASS)
void someMethod(){}

我有这样的编译错误:

I have this compilation error:

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).

文字的​​类以及在<定义href=\"http://java.sun.com/docs/books/jls/second_edition/html/ex$p$pssions.doc.html#251530\">JLS:

15.8.2类字面

一个类文字是由类的名称的前pression,
  接口,数组或原始类型后跟一个`。'与令牌
  类。该类型的类字面的是1类。它的计算结果类
  对象的命名类型(或无效)由定义定义
  类加载器类的当前实例的。

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.

所以,你不能这样做。

这篇关于错误设置注释值,类&LT;&GT;从不断的,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆