Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override a java method from groovy

Tags:

java

groovy

I have a groovy class that has the ability to write its output to a StringWriter - (via a setStringWriter method).

In java I would use the following code:

filter.setStringWriter(new StringWriter(){
   @Override
   public void write(String string){
       // do something with the string
   }
}); 

For Groovy I'm told to use a closure, I've tried the following with no luck:

def s =  {String line -> print line} as StringWriter
filter.setStringWriter(s)

or

filter.setStringWriter{String line -> print line}

How do I go about doing this, or is it even possible?

like image 607
Craig Avatar asked Apr 16 '09 14:04

Craig


1 Answers

The following link gives a clue, although it only mentions interfaces.

The following works with Groovy 1.6.1:

def s =  [ write: { String line -> print line} ] as StringWriter
filter.setStringWriter(s)
like image 95
toolkit Avatar answered Nov 13 '22 12:11

toolkit