一个注释属性的值为什么是一个常数前pression? [英] Why should the value for an annotation attribute be a constant expression?

查看:183
本文介绍了一个注释属性的值为什么是一个常数前pression?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面这段code的

I am having the following piece of code

 @UIUnitTimeout(8*60*1000) // works
 @UIUnitTimeout(TimeUnit.MINUTES.toMillis(8)) // does not work

我知道,根据JLS唯一不变的前pressions被允许作为值来标注属性。但为什么?为什么如果数据类型匹配是不够的?是否有任何可能出问题,如果前pressions是在运行时进行评估?是否每个规格都有背后的逻辑推理?

I know that according to the JLS only constant expressions are allowed as values to annotation attributes. But why? Why it isn't sufficient if the data types match? Is there anything that could possibly go wrong if the expressions were to be evaluated at runtime? Does every specification have a logical reasoning behind?

推荐答案

这是注释就像是一个类型的扩展名或元数据类型。

An annotation is like a type extension or metadata about the type.

由于Java是一种静态类型语言(即类型在编译时已知),这似乎是合理的注解属性数据(元数据),在编译时也被称为 - 您定义/有关注释声明的数据(扩展名)。

Because java is a statically typed language (meaning that types are known at compile time), it seems reasonable that annotation attribute data (metadata) be known at compile time too - you're defining/declaring data about the annotation (extension).

而作为一个纯粹的实际情况来看,对于注释处理,这是一个编译时间(可选)的步骤,属性数据的必须的在编译时知道 - 你还没有达到一个运行环境,但你需要的属性数据

And as a purely practical point, for annotation processing, which is a compile-time (optional) step, attribute data must be known at compile time - you haven't yet reached a runtime environment, yet you need the attribute data.

这篇关于一个注释属性的值为什么是一个常数前pression?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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