Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to fix groovy.lang.MissingMethodException: No signature of method:

Tags:

groovy

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?

like image 610
geretd Avatar asked Jul 01 '13 18:07

geretd


2 Answers

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!!!!') 
like image 172
Will Avatar answered Sep 23 '22 00:09

Will


To help other bug-hunters. I had this error because the function didn't exist.

I had a spelling error.

like image 21
Glycerine Avatar answered Sep 21 '22 00:09

Glycerine