...
Proxies created with Proxy-o-Matic also suffer from the this/super problem but they add a couple of features that the standard proxy creation mechanism dondoesn't offer:
- ability to define overloaded methods
- ability to call its own methods from within
- ability to proxy more than 1 interface at a time
- ability to proxy from Expandos as well
...
| Code Block |
|---|
import static org.kordamp.groovy.util.ProxyOMatic.proxy
interface Foo { String foo() }
interface Bar { String bar() }
interface FooBar extends Foo, Bar {
String foobar()
}
def f = proxy( Foo ) {
foo { -> "Foo" }
}
assert f instanceof Foo
assert f.foo() == "Foo"
def fb = proxy( FooBar ) {
foo { -> "Foo" }
bar { -> "Bar" }
foobar { -> foo() + bar() }
}
assert fb instanceof FooBar
assert ffb.foo() == "Foo"
assert ffb.bar() == "Bar"
assert ffb.foobar() == "FooBar"
interface Fooz extends Foo {
String foo( String n )
}
def fz = proxy( Fooz ) {
foo { -> "Foo" }
foo { String n -> "Foo$n".toString() }
}
assert fz instanceof Fooz
assert fz.foo() == "Foo"
assert fz.foo("Groovy") == "FooGroovy"
def bf = proxy( Bar, [Foo] ) {
foo { -> "Foo" }
bar { -> "Bar" }
}
assert bf instanceof Bar
assert bf instanceof Foo
assert bf.foo() == "Foo"
assert bf.bar() == "Bar"
|
Credit must be given when credit is due, in this case Proxy-o-Matic emerged from an idea Alex Tkachman pitched at the Groovy-dev mailing list, thanks Alex for the marvelous idea of a Proxy builder DSL.
...
Download
...
Pending.
Installing
Just drop proxyomatic-<version>.jar into $GROOVY_HOME/lib or ~/.groovy/lib and your done.
...
Have the latest stable version of Groovy installed, that's all baby!
Documentation
Pending
Using Proxy-o-Matic is pretty straight forward: call any of ProxyOMatic's proxy() methods. To achieve a DSL like usage remember to import statically ProxyOMatic.proxy. Proxy-o-Matic can only create proxies from interfaces for the time being, abstract/concrete classes will be supported in a following version. These are the method signatures you would need to work with
- proxy( Class type, source )
- proxy( Class type, List<Class>, source )
- proxy( Class type, Class[], source )
where source can be any of [Closure, Map, Expando]
Another thing to consider is that given the nature of closures in Groovy the following would be treated as equivalent definitions:
| Code Block |
|---|
interface Baz {
String baz( Object b )
}
def b = proxy( Baz ) {
baz { "BAZ" }
baz { String n -> n }
}
assert b.baz("gotcha") == "gotcha"
|
So please avoid using the default parameter and always qualify the number of parameters a closure must have.
Contributing
Please contact the team members by e-mail.
...