groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ocs@ocs" <>
Subject Can @Delegate wrap stubs in own processor? Or other trick for proxying.
Date Mon, 27 Aug 2018 00:36:59 GMT
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,

View raw message