Groovy 在评估没有前导零的十进制值上的关系运算符时抱怨意外标记

use*_*949 5 groovy

我在执行以下 Groovy 脚本片段时遇到问题。

GroovyShell sh = new GroovyShell();
sh.evaluate("\"abcd\".length() >= .34");
Run Code Online (Sandbox Code Playgroud)

我收到以下异常。下面提到了整个堆栈跟踪。

Exception in thread "main" org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 1: unexpected token: >= @ line 1, column 17.
    "abcd".length() >= .34d
Run Code Online (Sandbox Code Playgroud)

如果我更改.340.34,它就有效。但是,由于某些限制,我无法更改脚本内容。任何克服的帮助将不胜感激。

我遇到以下异常

Exception in thread "main" org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 1: unexpected token: >= @ line 1, column 17.
    "abcd".length() >= .34d
                       ^
1 error

at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:150)
at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:120)
at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:132)
at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:350)
at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:144)
at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:110)
at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:234)
at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:168)
at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:943)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:605)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:584)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:623)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:594)
at groovytest.Testtest.main(Testtest.java:18)
Run Code Online (Sandbox Code Playgroud)

Szy*_*iak 3

您的 Groovy 代码片段不正确 - Groovy 不支持在小数小于 的情况下不带前导零的表示法1.0。如果您尝试使用直接编译以下表达式groovyc

"abcd".length() >= .34
Run Code Online (Sandbox Code Playgroud)

编译将失败并出现如下错误:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
test.groovy: 2: Unexpected input: '.' @ line 2, column 20.
   "abcd".length() >= .34
                      ^

1 error
Run Code Online (Sandbox Code Playgroud)

Java 支持这种表示法,但是 Groovy2.x最新3.0.0-alpha-3版本不支持它。

实在的方法

修复输入 Groovy 代码片段,使其仅包含有效且可编译的代码。任何无效的 Groovy 语句或表达式都将导致失败和编译错误。

replaceAll()解决方法:使用方法添加前导零

编译此类不正确代码片段的唯一方法是将所有 .\d+ (点后跟至少一个空格并以数字结尾)替换为 0.$1. 考虑以下示例:

"abcd".length() >= .34
Run Code Online (Sandbox Code Playgroud)

它添加0到所有缺少前导零的十进制数。运行此示例会将以下输出打印到控制台:

"abcd".length() >= 0.34; "efgh".length() >= 0.22; "xyz".length() >= 0.11;
Run Code Online (Sandbox Code Playgroud)

如果将此类修改后的代码片段传递给GroovyShell.evaluate()方法,它将正常运行。

当然,这不是一个坚如磐石的解决方案,它只是自动修复代码片段中引入的一些语法错误的一种方法。在某些极端情况下,此解决方法可能会导致一些副作用,您必须意识到这一点。