Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to instantiate a Java abstract class with a constructor parameter with a groovy closure

I am trying to instantiate a Java abstract class from my Groovy code. Considering the following Java abstract class (non relevant processing is stripped out from the class):

public abstract class StackOverflow{
  public abstract String answerMe();
}

I can easily instantiate it in Groovy this way, and the call to answerMe() will trigger the correct output:

StackOverflow stack = [answerMe : { "Answer" }] as StackOverflow

Now if I modify the StackOverflow class adding a String parameter in the constructor like this :

public abstract class StackOverflowStr{
  public StackOverflowStr(String s){}
  public abstract String answerMe();
}

I don't really know how to instantiate my object, I tried a lot of thing, but I can't seem to find the right syntax, does someone got any clue ?

like image 492
Cedric Gatay Avatar asked Apr 29 '12 09:04

Cedric Gatay


1 Answers

You can instantiate it in classic Java style:

StackOverflowStr stack = new StackOverflowStr("javaish"){
    String answerMe() {"answer"}
}
like image 155
Andrea Parodi Avatar answered Sep 19 '22 19:09

Andrea Parodi