Dynamic groovy proxy for another object. All method invocations get forwarded to actual object, unless the proxy overrides it. See groovy/util/ProxyTest.groovy for usage details.
| 30 | * See groovy/util/ProxyTest.groovy for usage details. |
| 31 | */ |
| 32 | public class Proxy extends GroovyObjectSupport { |
| 33 | |
| 34 | private Object adaptee = null; |
| 35 | |
| 36 | /** |
| 37 | * This method is for convenience. |
| 38 | * It allows to get around the need for defining dump ctors in subclasses. |
| 39 | * See unit tests for details. |
| 40 | */ |
| 41 | public Proxy wrap(Object adaptee){ |
| 42 | setAdaptee(adaptee); |
| 43 | return this; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Returns the wrapped adaptee. |
| 48 | * |
| 49 | * @return the adaptee |
| 50 | */ |
| 51 | public Object getAdaptee() { |
| 52 | return adaptee; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Sets the wrapped adaptee. |
| 57 | * |
| 58 | * @param adaptee the adaptee to wrap |
| 59 | */ |
| 60 | public void setAdaptee(Object adaptee) { |
| 61 | this.adaptee = adaptee; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Invokes a proxy method or forwards the call to the adaptee. |
| 66 | * |
| 67 | * @param name the method name |
| 68 | * @param args the invocation arguments |
| 69 | * @return the invocation result |
| 70 | */ |
| 71 | @Override |
| 72 | public Object invokeMethod(String name, Object args) { |
| 73 | try { |
| 74 | return super.invokeMethod(name, args); |
| 75 | } |
| 76 | catch (MissingMethodException e) { |
| 77 | return InvokerHelper.invokeMethod(adaptee, name, args); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns an iterator over the adaptee. |
| 83 | * |
| 84 | * @return an iterator for the adaptee |
| 85 | */ |
| 86 | public Iterator iterator() { |
| 87 | return InvokerHelper.asIterator(adaptee); |
| 88 | } |
| 89 |
nothing calls this directly
no outgoing calls
no test coverage detected