Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何修复groovy.lang.MissingMethodException:方法没有签名:_Groovy - Fatal编程技术网

如何修复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!!)
Yes
copyAndReplaceText()
是方法名称,您要传递四个参数:第一个
、第二个
dest
、第三个
“Visa”
和第四个
Passport静态类Abc{private void test(){foo();}}}}private static void foo(){}
当您的参数列表对于方法签名不正确时,您可能会出现此错误……感谢您为我指明了正确的方向。我的方法名称不相同。哎呀!