简体   繁体   English

如何处理 groovy.lang.MissingMethodException

[英]How to deal with groovy.lang.MissingMethodException

I'm using Groovy in my program because I need to run some pieces of code generated by string.我在我的程序中使用 Groovy 因为我需要运行一些由字符串生成的代码。 In this very simple example I show my problem related to a very well known error in Groovy.在这个非常简单的示例中,我展示了与 Groovy 中一个众所周知的错误相关的问题。

public static void main(String[] args) {
    Binding binding = new Binding();
    GroovyShell shell = new GroovyShell(binding);
    path(Arrays.asList("A", "B", "C"), Arrays.asList("A", "B"));
    String s = "path(Arrays.asList(\"A\", \"B\", \"C\"), Arrays.asList(\"A\", \"B\"))"; 
    Object value = shell.evaluate(s);
}

private static List<String> path(List<String> dstString, List<String> srcString) {
    System.out.println("it works!");
    return dstString;
}

I pass inline the two Lists using: Arrays.asList("A", "B", "C"), Arrays.asList("A", "B") .我通过内联两个列表使用: Arrays.asList("A", "B", "C"), Arrays.asList("A", "B") When I call the function path with the inline params without involving Groovy it works.当我使用内联参数调用 function 路径而不涉及 Groovy 时,它可以工作。 But, when I do exactly the same but by Groovy, it fails and returns the following error:但是,当我通过 Groovy 执行完全相同的操作时,它会失败并返回以下错误:

Exception in thread "main" groovy.lang.MissingMethodException: No signature        of method: Script1.path() is applicable for argument types: (java.util.Arrays$ArrayList, java.util.Arrays$ArrayList) values: [[A, B, C], [A, B]]
 Possible solutions: wait(), any(), with(groovy.lang.Closure),    each(groovy.lang.Closure), run(), run()
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:81)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:174)
at Script1.run(Script1.groovy:1)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:585)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:623)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:594)
at Test.main(Test.java:16)

I'm trying to figure out why is not working and how to solve it.我试图弄清楚为什么不起作用以及如何解决它。 Are there any suggestions?有什么建议吗?

I've solved this problem changing the scope of the function. 我已经解决了更改功能范围的问题。 Seems to be that Groovy is able to execute piece of code inside a string with functions, only if those function are declared in a static way into the class is calling the method evaluate. 似乎Groovy能够使用函数在字符串内部执行一段代码,只有当这些函数以静态方式声明到类中时,才调用方法validate。

You can bypass the current context, or in detail create a binding of it:您可以绕过当前上下文,或者详细创建它的绑定:

sh "echo wurst > any.txt"
def shell = new GroovyShell(new Binding(x:"**/*.txt", y: this))
def script1 = shell.parse("y.archiveArtifacts artifacts: x")
script1.run()

Its another example, but should solve your problem, too.它是另一个例子,但也应该解决你的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复groovy.lang.MissingMethodException:没有方法的签名 - how to fix groovy.lang.MissingMethodException: No signature of method groovy.lang.MissingMethodException:Jmeter 上的错误 - groovy.lang.MissingMethodException: Error on Jmeter 运行代码为 a.jar 时出现 groovy.lang.missingMethodException - groovy.lang.missingMethodException when running code as a .jar groovy.lang.MissingMethodException:方法的无签名:java.util.ArrayList - groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList eachParallel() 在使用 GPars 时抛出 groovy.lang.MissingMethodException - eachParallel() throwing groovy.lang.MissingMethodException when using GPars 如何修复 groovy.lang.MissingMethodException:没有方法签名:java.util.ArrayList.get() 适用于参数类型:() 值: - how to fix groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.get() is applicable for argument types: () values: [] groovy.lang.MissingMethodException:无方法签名:java.lang.String.name() 适用于参数类型:() 值:[] - groovy.lang.MissingMethodException: No signature of method: java.lang.String.name() is applicable for argument types: () values: [] 通过集成测试用例发送到服务的参数有问题:groovy.lang.MissingMethodException - Params sent through integration test case to the service having an issue:groovy.lang.MissingMethodException 构建 android 项目时出现 intellij idea 错误 - groovy.lang.MissingMethodException:没有方法签名: - intellij idea error while building android project - groovy.lang.MissingMethodException: No signature of method: groovy.lang.MissingMethodException:方法的无签名:java.io.FileWriter.append() - groovy.lang.MissingMethodException: No signature of method: java.io.FileWriter.append()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM