I had a similar problem that I solved mixing a @Delegate with the GroovyInterceptable annotation, for example: 

class Foo implements Set, GroovyInterceptable {

    private Set target = new HashSet()

    Object invokeMethod(String name, Object args) {
        def result = InvokerHelper.invokeMethod(target, name, args)
        return result

Hope it helps 


On Mon, Aug 27, 2018 at 2:37 AM ocs@ocs <ocs@ocs.cz> wrote:
Hi there,

I guess this must be Groovy 101, but whatever I try, I can't find a proper solution.

The goal is pretty plain — to implement own set. Conceptually, it should look like this:

class MySet {
  def delegate=[] as Set
  def methodMissing(String name, Object args) {
    ... some preprocessing ...
    def mm=delegate.getClass().metaClass.getMetaMethod(name,args)
    def result=mm.invoke(delegate,args)
    ... some postprocessing ...

Without the implements Set it works like a charm... up to a point. Alas, if I try e.g.

MySet mySet=....
def f=[1,2]+mySet

I don't get the assumed result (of adding the contents of mySet to the list).

Presumably, it would help if I added implements Set. Here's where it gets hairy: the compiler does not allow me to do that, wanting me either to implement a number of completely unnecessary method stubs manually, or make the class abstract, which would prevent its instantiation.

Using @Delegate does not help, for I need pre- and post-process all the methods. Nevertheless, if @Delegate allowed me to use, say, a closure to wrap all the stubs into, or a pair of static methods to be called always pre- and post-calling the delegate method, it would help very much. Is there something like that?

Or, if not, is there some other easy way out, without having to write and maintain a screenful of very ugly very much Java-like boilerplate code?

Thanks and all the best,