1 package groovy.lang;
2
3 import org.codehaus.groovy.runtime.InvokerHelper;
4
5 import java.beans.IntrospectionException;
6
7 /***
8 * As subclass of MetaClass, ProxyMetaClass manages calls from Groovy Objects to POJOs.
9 * It enriches MetaClass with the feature of making method invokations interceptable by
10 * an Interceptor. To this end, it acts as a decorator (decorator pattern) allowing
11 * to add or withdraw this feature at runtime.
12 * See groovy/lang/InterceptorTest.groovy for details.
13 * @author Dierk Koenig
14 */
15 public class ProxyMetaClass extends MetaClass {
16
17 protected MetaClass adaptee = null;
18 protected Interceptor interceptor = null;
19
20 /***
21 * convenience factory method for the most usual case.
22 */
23 public static ProxyMetaClass getInstance(Class theClass) throws IntrospectionException {
24 MetaClassRegistry metaRegistry = InvokerHelper.getInstance().getMetaRegistry();
25 MetaClass meta = metaRegistry.getMetaClass(theClass);
26 return new ProxyMetaClass(metaRegistry, theClass, meta);
27 }
28 /***
29 * @param adaptee the MetaClass to decorate with interceptability
30 */
31 public ProxyMetaClass(MetaClassRegistry registry, Class theClass, MetaClass adaptee) throws IntrospectionException {
32 super(registry, theClass);
33 this.adaptee = adaptee;
34 if (null == adaptee) throw new IllegalArgumentException("adaptee must not be null");
35 }
36
37 /***
38 * Use the ProxyMetaClass for the given Closure.
39 * Cares for balanced register/unregister.
40 * @param closure piece of code to be executed with registered ProxyMetaClass
41 */
42 public void use(Closure closure){
43 registry.setMetaClass(theClass, this);
44 closure.call();
45 registry.setMetaClass(theClass, adaptee);
46 }
47
48 /***
49 * Use the ProxyMetaClass for the given Closure.
50 * Cares for balanced setting/unsetting ProxyMetaClass.
51 * @param closure piece of code to be executed with ProxyMetaClass
52 */
53 public void use(GroovyObject object, Closure closure){
54 object.setMetaClass(this);
55 closure.call();
56 object.setMetaClass(adaptee);
57 }
58
59 /***
60 * @return the interceptor in use or null if no interceptor is used
61 */
62 public Interceptor getInterceptor() {
63 return interceptor;
64 }
65
66 /***
67 * @param interceptor may be null to reset any interception
68 */
69 public void setInterceptor(Interceptor interceptor) {
70 this.interceptor = interceptor;
71 }
72
73 /***
74 * Call invokeMethod on adaptee with logic like in MetaClass unless we have an Interceptor.
75 * With Interceptor the call is nested in its beforeInvoke and afterInvoke methods.
76 * The method call is suppressed if Interceptor.doInvoke() returns false.
77 * See Interceptor for details.
78 */
79 public Object invokeMethod(final Object object, final String methodName, final Object[] arguments) {
80 return doCall(object, methodName, arguments, new Callable(){
81 public Object call() {
82 return adaptee.invokeMethod(object, methodName, arguments);
83 }
84 });
85 }
86 /***
87 * Call invokeStaticMethod on adaptee with logic like in MetaClass unless we have an Interceptor.
88 * With Interceptor the call is nested in its beforeInvoke and afterInvoke methods.
89 * The method call is suppressed if Interceptor.doInvoke() returns false.
90 * See Interceptor for details.
91 */
92 public Object invokeStaticMethod(final Object object, final String methodName, final Object[] arguments) {
93 return doCall(object, methodName, arguments, new Callable(){
94 public Object call() {
95 return adaptee.invokeStaticMethod(object, methodName, arguments);
96 }
97 });
98 }
99
100 /***
101 * Call invokeConstructor on adaptee with logic like in MetaClass unless we have an Interceptor.
102 * With Interceptor the call is nested in its beforeInvoke and afterInvoke methods.
103 * The method call is suppressed if Interceptor.doInvoke() returns false.
104 * See Interceptor for details.
105 */
106 public Object invokeConstructor(final Object[] arguments) {
107 return doCall(theClass, "ctor", arguments, new Callable(){
108 public Object call() {
109 return adaptee.invokeConstructor(arguments);
110 }
111 });
112 }
113
114
115 private interface Callable{
116 Object call();
117 }
118 private Object doCall(Object object, String methodName, Object[] arguments, Callable howToInvoke) {
119 if (null == interceptor) {
120 return howToInvoke.call();
121 }
122 Object result = interceptor.beforeInvoke(object, methodName, arguments);
123 if (interceptor.doInvoke()) {
124 result = howToInvoke.call();
125 }
126 result = interceptor.afterInvoke(object, methodName, arguments, result);
127 return result;
128 }
129 }