如何修复groovy.lang.MissingMethodException:方法没有签名:
我试图在没有闭包的情况下使用此方法如何修复groovy.lang.MissingMethodException:方法没有签名:,groovy,Groovy,我试图在没有闭包的情况下使用此方法 def copyAndReplaceText(source, dest, targetText, replaceText){ dest.write(source.text.replaceAll(targetText, replaceText)) } def source = new File('C:/geretd/resumebak.txt') //Hello World def dest = new File('C:/geretd/resume.t
def copyAndReplaceText(source, dest, targetText, replaceText){
dest.write(source.text.replaceAll(targetText, replaceText))
}
def source = new File('C:/geretd/resumebak.txt') //Hello World
def dest = new File('C:/geretd/resume.txt') //blank
copyAndReplaceText(source, dest){
it.replaceAll('Visa', 'Passport!!!!')
}
但当我运行它时,我得到以下错误:
groovy.lang.MissingMethodException: No signature of method: ConsoleScript3.copyAndReplaceText() is applicable for argument types: (java.io.File, java.io.File, ConsoleScript3$_run_closure1) values: [C:\geretd\resumebak.txt, C:\geretd\resume.txt, ...]
Possible solutions: copyAndReplaceText(java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:78)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:149)
我做错了什么?因为您正在将三个参数传递给一个四参数方法。另外,您没有使用传递的闭包 如果要指定要在
源
内容上执行的操作,请使用闭包。应该是这样的:
def copyAndReplaceText(source, dest, closure){
dest.write(closure( source.text ))
}
// And you can keep your usage as:
copyAndReplaceText(source, dest){
it.replaceAll('Visa', 'Passport!!!!')
}
如果总是交换字符串,则传递这两个字符串,因为方法签名已经声明:
def copyAndReplaceText(source, dest, targetText, replaceText){
dest.write(source.text.replaceAll(targetText, replaceText))
}
copyAndReplaceText(source, dest, 'Visa', 'Passport!!!!')
如果传递给方法的对象顺序不正确,也会出现此错误。换句话说,假设您的方法按顺序接受字符串、整数和日期。如果先传递一个日期,然后传递一个字符串,再传递一个整数,您将收到相同的错误消息。这也可能是因为您可能给了classname,其中所有字母都是小写的,groovy(已知版本2.5.0)不支持这一点
类名-接受用户,但不接受用户。以帮助其他bug搜索者。我有这个错误,因为函数不存在
我有一个拼写错误。在我的例子中,只是我有一个名为与函数相同的变量 例如:
def cleanCache = functionReturningABoolean()
if( cleanCache ){
echo "Clean cache option is true, do not uninstall previous features / urls"
uninstallCmd = ""
// and we call the cleanCache method
cleanCache(userId, serverName)
}
...
稍后在我的代码中,我有一个函数:
def cleanCache(user, server){
//some operations to the server
}
显然Groovy语言不支持这一点(但其他语言如Java也支持)。
我刚刚将我的函数重命名为
executeCleanCache
,它工作得很好(或者您也可以根据自己的喜好重命名变量)。非常感谢,这是我第一次使用groovy,我需要帮助,快速问一下,如果我使用下面的代码,我将传递4个参数,对吗def copyAndReplaceText(source,dest,targetText,replaceText){dest.write(source.text.replacetall(targetText,replaceText))}def source=new File('C:/geretd/resumebak.txt')//Hello World def dest=new File('C:/geretd/resume.txt')//空白copyAndReplaceText(source,dest,'Visa,'Passport!!)
YescopyAndReplaceText()
是方法名称,您要传递四个参数:第一个源、第二个dest
、第三个“Visa”
和第四个Passport老实说,直到星期五我才听说过groovy,我不得不自己研究,现在我被这个需求困住了。搜索并替换为名为xxxx~的临时文件,旧文本替换为新文本不知道怎么做,但我发现groovy非常好,它不像java那么死板,即使它有它的基础。@如果您熟悉Groovy的语义和语法,那么除了您现在使用的问题之外,您前面的问题也会很有用。来自Groovy中的noob:我有相同的例外,但原因不同。我在代码开始时定义了一个比我想使用的更晚的方法。当我替换它时,它起了作用。另一个警告:如果您从内部类调用外部类的静态方法,而没有完全限定外部方法:静态类Abc{private void test(){foo();}}}}private static void foo(){}
当您的参数列表对于方法签名不正确时,您可能会出现此错误……感谢您为我指明了正确的方向。我的方法名称不相同。哎呀!