Save This Page
Home » Groovy-1.7.0 » org.codehaus » groovy » runtime » callsite » [javadoc | source]
    1   /*
    2    * Copyright 2003-2007 the original author or authors.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
   10    * Unless required by applicable law or agreed to in writing, software
   11    * distributed under the License is distributed on an "AS IS" BASIS,
   12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13    * See the License for the specific language governing permissions and
   14    * limitations under the License.
   15    */
   16   package org.codehaus.groovy.runtime.callsite;
   17   
   18   import groovy.lang.GroovyObject;
   19   import groovy.lang.GroovyRuntimeException;
   20   import groovy.lang.MetaClass;
   21   import groovy.lang.MissingMethodException;
   22   
   23   import org.codehaus.groovy.runtime.ScriptBytecodeAdapter;
   24   import org.codehaus.groovy.runtime.metaclass.MissingMethodExecutionFailed;
   25   
   26   /**
   27    *
   28    * @author Alex Tkachman
   29    */
   30   public class PogoMetaClassSite extends MetaClassSite {
   31       public PogoMetaClassSite(CallSite site, MetaClass metaClass) {
   32           super(site, metaClass);
   33       }
   34   
   35       public final Object call(Object receiver, Object[] args) throws Throwable {
   36           if (checkCall(receiver)) {
   37               try {
   38                   try {
   39                       return metaClass.invokeMethod(receiver, name, args);
   40                   } catch (MissingMethodException e) {
   41                       if (e instanceof MissingMethodExecutionFailed) {
   42                           throw (MissingMethodException)e.getCause();
   43                       } else if (receiver.getClass() == e.getType() && e.getMethod().equals(name)) {
   44                           // in case there's nothing else, invoke the object's own invokeMethod()
   45                           return ((GroovyObject)receiver).invokeMethod(name, args);
   46                       } else {
   47                           throw e;
   48                       }
   49                   }
   50               } catch (GroovyRuntimeException gre) {
   51                   throw ScriptBytecodeAdapter.unwrap(gre);
   52               }
   53           } else {
   54             return CallSiteArray.defaultCall(this, receiver, args);
   55           }
   56       }
   57   
   58       protected final boolean checkCall(Object receiver) {
   59           return receiver instanceof GroovyObject && ((GroovyObject)receiver).getMetaClass() == metaClass;
   60       }
   61   
   62       public final Object callCurrent(GroovyObject receiver, Object[] args) throws Throwable {
   63           if (checkCall(receiver)) {
   64               try {
   65                   try {
   66                       return metaClass.invokeMethod(receiver, name, args);
   67                   } catch (MissingMethodException e) {
   68                       if (e instanceof MissingMethodExecutionFailed) {
   69                           throw (MissingMethodException)e.getCause();
   70                       } else if (receiver.getClass() == e.getType() && e.getMethod().equals(name)) {
   71                           // in case there's nothing else, invoke the object's own invokeMethod()
   72                           return ((GroovyObject)receiver).invokeMethod(name, args);
   73                       } else {
   74                           throw e;
   75                       }
   76                   }
   77               } catch (GroovyRuntimeException gre) {
   78                   throw ScriptBytecodeAdapter.unwrap(gre);
   79               }
   80           } else {
   81             return CallSiteArray.defaultCallCurrent(this, receiver, args);
   82           }
   83       }
   84   }

Save This Page
Home » Groovy-1.7.0 » org.codehaus » groovy » runtime » callsite » [javadoc | source]