Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2003-2007 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.codehaus.groovy.runtime.callsite;
 
 
POJO call site meta class - cached method - cached

Author(s):
Alex Tkachman
 
 public class PojoMetaMethodSite extends MetaMethodSite {
 
     protected final int version;
 
     public PojoMetaMethodSite(CallSite siteMetaClassImpl metaClassMetaMethod metaMethodClass params[]) {
         super(sitemetaClassmetaMethodparams);
          = metaClass.getVersion();
     }
 
     public Object invoke(Object receiverObject[] argsthrows Throwable {
         MetaClassHelper.unwrap(args);
         return .doMethodInvoke(receiver,  args);
     }
 
     public Object call(Object receiverObject[] argsthrows Throwable {
         if(checkCall(receiverargs))
           return invoke(receiver,args);
         else
           return CallSiteArray.defaultCall(thisreceiverargs);
     }
 
     protected final boolean checkPojoMetaClass() {
         return .get() == 0
             && ((MetaClassImpl)).getVersion() == ;
     }
 
     protected final boolean checkCall(Object receiverObject[] args) {
         try {
             return receiver.getClass() == .getTheClass() // meta class match receiver
                && checkPojoMetaClass()
                && MetaClassHelper.sameClasses(args);
         }
         catch (NullPointerException e) {
             if (receiver == null)
               return checkCall(NullObject.getNullObject(), args);
 
             throw e;
         }
     }
 
     protected final boolean checkCall(Object receiver) {
         try {
             return receiver.getClass() == .getTheClass() // meta class match receiver
                && checkPojoMetaClass()
                && MetaClassHelper.sameClasses();
         } catch (NullPointerException e) {
             if (receiver == null)
               return checkCall(NullObject.getNullObject());
 
             throw e;
         }
     }
 
     protected final boolean checkCall(Object receiverObject arg1) {
         try {
             return receiver.getClass() == .getTheClass() // meta class match receiver
                && checkPojoMetaClass()
                && MetaClassHelper.sameClasses(arg1);
         } catch (NullPointerException e) {
             if (receiver == null)
               return checkCall(NullObject.getNullObject(), arg1);
 
             throw e;
         }
    }
    protected final boolean checkCall(Object receiverObject arg1Object arg2) {
        try {
            return receiver.getClass() == .getTheClass() // meta class match receiver
               && checkPojoMetaClass()
               && MetaClassHelper.sameClasses(arg1arg2);
        } catch (NullPointerException e) {
            if (receiver == null)
              return checkCall(NullObject.getNullObject(), arg1arg2);
            throw e;
        }
    }
    protected final boolean checkCall(Object receiverObject arg1Object arg2Object arg3) {
        try {
            return receiver.getClass() == .getTheClass() // meta class match receiver
               && checkPojoMetaClass()
               && MetaClassHelper.sameClasses(arg1arg2arg3);
        } catch (NullPointerException e) {
            if (receiver == null)
              return checkCall(NullObject.getNullObject(), arg1arg2arg3);
            throw e;
        }
    }
    protected final boolean checkCall(Object receiverObject arg1Object arg2Object arg3Object arg4) {
        try {
            return receiver.getClass() == .getTheClass() // meta class match receiver
               && checkPojoMetaClass()
               && MetaClassHelper.sameClasses(arg1arg2arg3arg4);
        } catch (NullPointerException e) {
            if (receiver == null)
              return checkCall(NullObject.getNullObject(), arg1arg2arg3arg4);
            throw e;
        }
    }
    public static CallSite createPojoMetaMethodSite(CallSite siteMetaClassImpl metaClassMetaMethod metaMethodClass[] paramsObject receiverObject[] args) {
        if (metaMethod instanceof CallSiteAwareMetaMethod) {
            return ((CallSiteAwareMetaMethod)metaMethod).createPojoCallSite(sitemetaClassmetaMethodparamsreceiverargs);
        }
        if (metaMethod.getClass() == CachedMethod.class)
          return createCachedMethodSite (sitemetaClass, (CachedMethodmetaMethodparamsargs);
        return createNonAwareCallSite(sitemetaClassmetaMethodparamsargs);
    }
    public static CallSite createCachedMethodSite(CallSite siteMetaClassImpl metaClassCachedMethod metaMethodClass[] paramsObject[] args) {
        if (metaMethod.correctArguments(args) == args) {
            if (noWrappers(args)) {
                if (noCoerce(metaMethod,args))
                    return new PojoCachedMethodSiteNoUnwrap(sitemetaClassmetaMethodparams);
                else
                    return metaMethod.createPojoMetaMethodSite(sitemetaClassparams);
            }
        }
        return new PojoCachedMethodSite(sitemetaClassmetaMethodparams);
    }
    public static CallSite createNonAwareCallSite(CallSite siteMetaClassImpl metaClassMetaMethod metaMethodClass[] paramsObject[] args) {
        if (metaMethod.correctArguments(args) == args) {
            if (noWrappers(args)) {
                if (noCoerce(metaMethod,args))
                    return new PojoMetaMethodSiteNoUnwrap(sitemetaClassmetaMethodparams);
                else
                    return new PojoMetaMethodSiteNoUnwrapNoCoerce(sitemetaClassmetaMethodparams);
            }
        }
        return new PojoMetaMethodSite(sitemetaClassmetaMethodparams);
    }
    public static class PojoCachedMethodSite extends PojoMetaMethodSite {
        final Method reflect;
        public PojoCachedMethodSite(CallSite siteMetaClassImpl metaClassMetaMethod metaMethodClass[] params) {
            super(sitemetaClassmetaMethodparams);
             = ((CachedMethod)metaMethod).setAccessible();
        }
        public Object invoke(Object receiverObject[] argsthrows Throwable {
            MetaClassHelper.unwrap(args);
            args = .coerceArgumentsToClasses(args);
            try {
                return .invoke(receiverargs);
            } catch (InvocationTargetException e) {
                Throwable cause = e.getCause();
                if (cause instanceof GroovyRuntimeException) {
                    throw ScriptBytecodeAdapter.unwrap ((GroovyRuntimeExceptioncause);
                } else {
                    throw cause;
                }
            }
        }
    }
    public static class PojoCachedMethodSiteNoUnwrap extends PojoCachedMethodSite {
        public PojoCachedMethodSiteNoUnwrap(CallSite siteMetaClassImpl metaClassMetaMethod metaMethodClass params[]) {
            super(sitemetaClassmetaMethodparams);
        }
        public final Object invoke(Object receiverObject[] argsthrows Throwable {
            args = .coerceArgumentsToClasses(args);
            try {
                return .invoke(receiverargs);
            } catch (InvocationTargetException e) {
                Throwable cause = e.getCause();
                if (cause instanceof GroovyRuntimeException) {
                    throw ScriptBytecodeAdapter.unwrap ((GroovyRuntimeExceptioncause);
                } else {
                    throw cause;
                }
            }
        }
    }
    public static class PojoCachedMethodSiteNoUnwrapNoCoerce extends PojoCachedMethodSite {
        public PojoCachedMethodSiteNoUnwrapNoCoerce(CallSite siteMetaClassImpl metaClassMetaMethod metaMethodClass params[]) {
            super(sitemetaClassmetaMethodparams);
        }
        public final Object invoke(Object receiverObject[] argsthrows Throwable {
            try {
                return .invoke(receiverargs);
            } catch (InvocationTargetException e) {
                Throwable cause = e.getCause();
                if (cause instanceof GroovyRuntimeException) {
                    throw ScriptBytecodeAdapter.unwrap ((GroovyRuntimeExceptioncause);
                } else {
                    throw cause;
                }
            }
        }
    }

    
Call site where we know there is no need to unwrap arguments
    public static class PojoMetaMethodSiteNoUnwrap extends PojoMetaMethodSite {
        public PojoMetaMethodSiteNoUnwrap(CallSite siteMetaClassImpl metaClassMetaMethod metaMethodClass params[]) {
            super(sitemetaClassmetaMethodparams);
        }
        public final Object invoke(Object receiverObject[] argsthrows Throwable {
            try {
                return .doMethodInvoke(receiver,  args);
            } catch (GroovyRuntimeException gre) {
                throw ScriptBytecodeAdapter.unwrap(gre);
            }
        }
    }

    
Call site where we know there is no need neither unwrap nor coerce arguments
    public static class PojoMetaMethodSiteNoUnwrapNoCoerce extends PojoMetaMethodSite {
        public PojoMetaMethodSiteNoUnwrapNoCoerce(CallSite siteMetaClassImpl metaClassMetaMethod metaMethodClass params[]) {
            super(sitemetaClassmetaMethodparams);
        }
        public final Object invoke(Object receiverObject[] argsthrows Throwable {
            try {
                return .invoke(receiver,  args);
            } catch (GroovyRuntimeException gre) {
                throw ScriptBytecodeAdapter.unwrap(gre);
            }
        }
    }
New to GrepCode? Check out our FAQ X