如何解决groovy.lang.MissingMethodException:没有方法的签名: [英] how to fix groovy.lang.MissingMethodException: No signature of method:

查看:9621
本文介绍了如何解决groovy.lang.MissingMethodException:没有方法的签名:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  def copyAndReplaceText(source,dest,targetText,replaceText){
dest.write(source.text.replaceAll(targetText,replaceText))
}

def source = new文件('C:/geretd/resumebak.txt')// Hello World
def dest = new File('C:/geretd/resume.txt')//空白

copyAndReplaceText(source,dest){
it.replaceAll(' Visa','Passport !!!!')
}

但是当我运行它时我得到以下错误:

  groovy.lang.MissingMethodException:没有方法的签名:ConsoleScript3.copyAndReplaceText()适用于参数类型:(java.io.File,java.io.File,ConsoleScript3 $ _run_closure1)values:[C:\geretd\resumebak.txt,C:\geretd\resume.txt,...] 
可能的解决方案:copyAndReplaceText(java.lang.Object,java.lang.Object,java.lang.Object,java.lang.Object)
$ b $ org.codehaus.groovy.runtime.S criptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)
$ b $ 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)

我在做什么错误?

解决方案

因为您将三个参数传递给四个参数方法。此外,您没有使用传递的闭包。



如果要指定在内容,然后使用闭包。它会是这样的:

$ p $ def copyAndReplaceText(source,dest,closure){
dest.write(closure (source.text))
}

//你可以保留你的用法:
copyAndReplaceText(source,dest){
it.replaceAll('Visa ','Passport !!!!')
}

如果您总是会交换字符串,通过这两个,因为你的方法签名已经说明:

$ $ p $ def copyAndReplaceText(source,dest,targetText,replaceText){
dest.write(source.text.replaceAll(targetText,replaceText))
}

copyAndReplaceText(source,dest,'Visa','Passport !!!!')


I am trying to use this method without a closure

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!!!!')
}

but when I run it I get the following error:

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)

What am I doing wrong?

解决方案

Because you are passing three arguments to a four arguments method. Also, you are not using the passed closure.

If you want to specify the operations to be made on top of the source contents, then use a closure. It would be something like this:

def copyAndReplaceText(source, dest, closure){
    dest.write(closure( source.text ))
}

// And you can keep your usage as:
copyAndReplaceText(source, dest){
    it.replaceAll('Visa', 'Passport!!!!')
}

If you will always swap strings, pass both, as your method signature already states:

def copyAndReplaceText(source, dest, targetText, replaceText){
    dest.write(source.text.replaceAll(targetText, replaceText))
}

copyAndReplaceText(source, dest, 'Visa', 'Passport!!!!')

这篇关于如何解决groovy.lang.MissingMethodException:没有方法的签名:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆