由于属性必须是常量表达式错误,Java 代码将无法编译

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39157370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:54:35  来源:igfitidea点击:

Java code won't compile due to attribute must be a constant expression error

javastringspringannotationsconstants

提问by Kassandra Hinojosa Rodriguez

I can't figure out why the following won't compile. The error the IDE gives me is "The value for annotation attribute RequestParam.defaultValue must be a constant expression".

我无法弄清楚为什么以下内容无法编译。IDE 给我的错误是“注释属性 RequestParam.defaultValue 的值必须是常量表达式”。

My project involves Spring and Maven, and it goes the following:

我的项目涉及 Spring 和 Maven,它如下:

private static final String MAX_LONG_AS_STRING = Long.toString(Long.MAX_VALUE);

@RequestMapping(method=RequestMethod.GET)
public List<Spittle> spittles(
        @RequestParam(value="max",
                    defaultValue=MAX_LONG_AS_STRING) long max,
        @RequestParam(value="count", defaultValue="20") int count) {
    return spittleRepository.findSpittles(max, count);
}

I'm thinking the error comes from the conversion of Long to String, but I do not know how to fix it. I will appreciate any help, I am new to annotations and Spring.

我认为错误来自 Long 到 String 的转换,但我不知道如何修复它。我将不胜感激,我是注释和 Spring 的新手。

采纳答案by ajb

The Java rules say that when you have an annotation, and it has a parameter that expects a primitive type (such as an int) or a String, the value must be a constant expression. [This has nothing to do with Spring.] Roughly speaking, a constant expression is one whose value the compiler can figure out at compile time. However, there are rules for what constitutes a constant expression. These rules are in JLS 15.28. Only certain types of operations can be used in a constant expression. A method call, such as Long.toString(), isn't one of those. So using that makes your expression nota constant expression, even though it looks like it should be. (It looks like it to you, because you know what Long.toStringdoes. However, the compiler doesn't keep a catalog of all methods to know which ones are "constant" methods whose values can be figured out at compile time.)

Java 规则规定,当您有一个注解,并且它有一个需要原始类型(例如 an int)或 a 的参数时String,该值必须是一个常量表达式。[这与 Spring 无关。] 粗略地说,常量表达式是编译器可以在编译时计算出其值的表达式。但是,对于什么构成常量表达式有一些规则。这些规则在JLS 15.28 中。常量表达式中只能使用某些类型的操作。方法调用,例如Long.toString(),不是其中之一。所以使用它会使你的表达式不是一个常量表达式,即使它看起来应该是。(在你看来很像,因为你知道什么Long.toString做。但是,编译器不会保留所有方法的目录,以了解哪些是“常量”方法,其值可以在编译时计算出来。)

However, the example at the link shows that the +operator can be used, even when one of the arguments is not a string and therefore a toString()method is implicitly called. This suggests that you might be able to make things work like this:

但是,链接中的示例显示+可以使用运算符,即使其中一个参数不是字符串,因此toString()隐式调用了一个方法。这表明您可以使事情像这样工作:

private static final String MAX_LONG_AS_STRING = "" + Long.MAX_VALUE;

I haven't tried it, though.

不过我没试过。