1 package org.codehaus.groovy.runtime;
2
3 import groovy.lang.ParameterArray;
4 import groovy.lang.Closure;
5 import groovy.lang.MetaClass;
6
7
8 /***
9 * Represents a method on an object using a closure which can be invoked
10 * at any time
11 *
12 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
13 * @version $Revision: 1.7 $
14 */
15 public class MethodClosure extends Closure {
16
17 private String method;
18 MetaClass metaClass = InvokerHelper.getMetaClass(this);
19
20 public MethodClosure(Object delegate) {
21 super(delegate);
22 }
23
24 public MethodClosure(Object owner, String method) {
25 super(owner);
26 this.method = method;
27 }
28
29 public String getMethod() {
30 return method;
31 }
32
33 public Object call(Object arguments) {
34 if (arguments instanceof Object[] && ((Object[])arguments).length > 0)
35 return InvokerHelper.invokeMethod(getDelegate(), method, new ParameterArray(arguments));
36 else
37 return InvokerHelper.invokeMethod(getDelegate(), method, arguments);
38 }
39
40 public MetaClass getMetaClass() {
41 return metaClass;
42 }
43
44 public void setMetaClass(MetaClass metaClass) {
45 this.metaClass = metaClass;
46 }
47
48 protected Object doCall(Object arguments) {
49 if (arguments instanceof Object[] && ((Object[])arguments).length > 0)
50 return InvokerHelper.invokeMethod(getDelegate(), method, new ParameterArray(arguments));
51 else
52 return InvokerHelper.invokeMethod(getDelegate(), method, arguments);
53 }
54 }