001    package org.codehaus.groovy.runtime;
002    
003    import groovy.lang.ParameterArray;
004    import groovy.lang.Closure;
005    import groovy.lang.MetaClass;
006    
007    
008    /**
009     * Represents a method on an object using a closure which can be invoked
010     * at any time
011     * 
012     * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
013     * @version $Revision: 1.7 $
014     */
015    public class MethodClosure extends Closure {
016    
017        private String method;
018        MetaClass metaClass = InvokerHelper.getMetaClass(this);
019        
020        public MethodClosure(Object delegate) {
021            super(delegate);
022        }
023        
024        public MethodClosure(Object owner, String method) {
025            super(owner);
026            this.method = method;
027        }
028        
029        public String getMethod() {
030            return method;
031        }
032    
033        public Object call(Object arguments) {
034            if (arguments instanceof Object[] && ((Object[])arguments).length > 0)
035                return InvokerHelper.invokeMethod(getDelegate(), method, new ParameterArray(arguments));
036            else
037            return InvokerHelper.invokeMethod(getDelegate(), method, arguments);
038        }
039        
040        public MetaClass getMetaClass() {
041            return metaClass;
042        }
043        
044        public void setMetaClass(MetaClass metaClass) {
045            this.metaClass = metaClass;
046        }
047    
048        protected Object doCall(Object arguments) {
049            if (arguments instanceof Object[] && ((Object[])arguments).length > 0)
050                return InvokerHelper.invokeMethod(getDelegate(), method, new ParameterArray(arguments));
051            else
052                return InvokerHelper.invokeMethod(getDelegate(), method, arguments);
053        }
054    }