Groovy无法解析其他Groovy文件类:SAP Commerce环境异常排查
Groovy跨脚本类引用在SAP Commerce环境中的加载顺序问题
场景与代码示例
尝试通过编程方式创建Groovy上下文并加载多个脚本,故意打乱脚本加载顺序。本地运行正常,但在SAP Commerce(集成Spring的Tomcat环境)中执行时出现编译错误。
test/Helper.groovy
package test class Helper { static void help() { // Do something } }
test/Test.groovy
package test class Test { static void test() { Helper.help() } }
Java加载代码(Runner类)
public class Runner { public static void main(String[] args) throws IOException { GroovyShell shell = new GroovyShell(Runner.class.getClassLoader(), new Binding(), CompilerConfiguration.DEFAULT); load(shell, "/test/Test.groovy"); load(shell, "/test/Helper.groovy"); } private static void load(GroovyShell shell, String name) throws IOException { try(var script = Runner.class.getResourceAsStream(name)) { String scriptSrc = new String(script.readAllBytes(), StandardCharsets.UTF_8); shell.parse(scriptSrc, name); } } }
注:故意按任意顺序加载脚本
报错信息
Caused by: java.lang.reflect.InvocationTargetException ... Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Test.groovy: 5: Apparent variable 'Helper' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes: You attempted to reference a variable in the binding or an instance variable from a static context. You misspelled a classname or statically imported field. Please check the spelling. You attempted to use a method 'Helper' but left out brackets in a place not allowed by the grammar. at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:292) ~[groovy-2.5.14.jar:2.5.14] at org.codehaus.groovy.control.CompilationUnit$IPrimaryClassNodeOperation.doPhaseOperation(CompilationUnit.java:979) ~[groovy-4.0.2.jar:2.5.14] at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:692) ~[groovy-2.5.14.jar:2.5.14] at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:666) ~[groovy-2.5.14.jar:2.5.14] at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:373) ~[groovy-2.5.14.jar:2.5.14] at groovy.lang.GroovyClassLoader.lambda$parseClass$2(GroovyClassLoader.java:316) ~[groovy-2.5.14.jar:2.5.14] at org.codehaus.groovy.runtime.memoize.StampedCommonCache.compute(StampedCommonCache.java:163) ~[groovy-2.5.14.jar:2.5.14] at org.codehaus.groovy.runtime.memoize.StampedCommonCache.getAndPut(StampedCommonCache.java:154) ~[groovy-2.5.14.jar:2.5.14] at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:314) ~[groovy-2.5.14.jar:2.5.14] at groovy.lang.GroovyShell.parseClass(GroovyShell.java:553) ~[groovy-2.5.14.jar:2.5.14] at groovy.lang.GroovyShell.parse(GroovyShell.java:566) ~[groovy-2.5.14.jar:2.5.14] at groovy.lang.GroovyShell.parse(GroovyShell.java:620) ~[groovy-2.5.14.jar:2.5.14] at groovy.lang.GroovyShell.parse(GroovyShell.java:624) ~[groovy-2.5.14.jar:2.5.14]
疑问
环境中存在大量Groovy脚本,受应用设计限制无法调整加载顺序,未发现Tomcat启用安全管理器等相关配置,使用Groovy 2.5.14版本。请问是什么原因导致本地与SAP Commerce环境的行为差异?
内容的提问来源于stack exchange,提问作者SimGel
相关产品推荐
相关产品推荐

