摆脱“注解属性的值必须是常量表达式"信息 [英] Get rid of "The value for annotation attribute must be a constant expression" message

查看:128
本文介绍了摆脱“注解属性的值必须是常量表达式"信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中使用了注解,并尝试使用在运行时确定的值.

I use annotation in my code, and I try to use value which determine in run time.

我将我的列表定义为 static final (lst),并向该列表添加一些元素.

I define my list as static final (lst), and I add to this list some elements.

当我使用 lst.get(i) 时,出现编译错误:

When I use lst.get(i), I get compilation error:

The value for annotation attribute must be a constant expression

这个问题的解决方案是什么?

What is the solution for this issue?

推荐答案

注解的值必须是编译时常量,因此没有简单的方法可以完成您想要做的事情.

The value for an annotation must be a compile time constant, so there is no simple way of doing what you are trying to do.

另见此处:如何从常量 java 中为注解提供值

如果在您尝试运行程序之前已知该值,则可以使用一些编译时工具(ant、maven?)来配置它.

It is possible to use some compile time tools (ant, maven?) to config it if the value is known before you try to run the program.

这篇关于摆脱“注解属性的值必须是常量表达式"信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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