Search in sources :

Example 1 with MissingMethodExecutionFailed

use of org.codehaus.groovy.runtime.metaclass.MissingMethodExecutionFailed in project groovy-core by groovy.

the class MetaClassImpl method invokeMissingMethod.

private Object invokeMissingMethod(Object instance, String methodName, Object[] arguments, RuntimeException original, boolean isCallToSuper) {
    if (!isCallToSuper) {
        Class instanceKlazz = instance.getClass();
        if (theClass != instanceKlazz && theClass.isAssignableFrom(instanceKlazz))
            instanceKlazz = theClass;
        Class[] argClasses = MetaClassHelper.castArgumentsToClassArray(arguments);
        MetaMethod method = findMixinMethod(methodName, argClasses);
        if (method != null) {
            onMixinMethodFound(method);
            return method.invoke(instance, arguments);
        }
        method = findMethodInClassHierarchy(instanceKlazz, methodName, argClasses, this);
        if (method != null) {
            onSuperMethodFoundInHierarchy(method);
            return method.invoke(instance, arguments);
        }
        // still not method here, so see if there is an invokeMethod method up the hierarchy
        final Class[] invokeMethodArgs = { String.class, Object[].class };
        method = findMethodInClassHierarchy(instanceKlazz, INVOKE_METHOD_METHOD, invokeMethodArgs, this);
        if (method != null && method instanceof ClosureMetaMethod) {
            onInvokeMethodFoundInHierarchy(method);
            return method.invoke(instance, invokeMethodArgs);
        }
    }
    if (methodMissing != null) {
        try {
            return methodMissing.invoke(instance, new Object[] { methodName, arguments });
        } catch (InvokerInvocationException iie) {
            if (methodMissing instanceof ClosureMetaMethod && iie.getCause() instanceof MissingMethodException) {
                MissingMethodException mme = (MissingMethodException) iie.getCause();
                throw new MissingMethodExecutionFailed(mme.getMethod(), mme.getClass(), mme.getArguments(), mme.isStatic(), mme);
            }
            throw iie;
        } catch (MissingMethodException mme) {
            if (methodMissing instanceof ClosureMetaMethod)
                throw new MissingMethodExecutionFailed(mme.getMethod(), mme.getClass(), mme.getArguments(), mme.isStatic(), mme);
            else
                throw mme;
        }
    } else if (original != null)
        throw original;
    else
        throw new MissingMethodExceptionNoStack(methodName, theClass, arguments, false);
}
Also used : NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) NewMetaMethod(org.codehaus.groovy.runtime.metaclass.NewMetaMethod) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) NewStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) TransformMetaMethod(org.codehaus.groovy.runtime.metaclass.TransformMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) MissingMethodExceptionNoStack(org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack) InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) CachedClass(org.codehaus.groovy.reflection.CachedClass) MissingMethodExecutionFailed(org.codehaus.groovy.runtime.metaclass.MissingMethodExecutionFailed)

Example 2 with MissingMethodExecutionFailed

use of org.codehaus.groovy.runtime.metaclass.MissingMethodExecutionFailed in project groovy by apache.

the class MetaClassImpl method invokeMissingMethod.

private Object invokeMissingMethod(Object instance, String methodName, Object[] arguments, RuntimeException original, boolean isCallToSuper) {
    if (!isCallToSuper) {
        Class instanceKlazz = instance.getClass();
        if (theClass != instanceKlazz && theClass.isAssignableFrom(instanceKlazz))
            instanceKlazz = theClass;
        Class[] argClasses = MetaClassHelper.castArgumentsToClassArray(arguments);
        MetaMethod method = findMixinMethod(methodName, argClasses);
        if (method != null) {
            onMixinMethodFound(method);
            return method.invoke(instance, arguments);
        }
        method = findMethodInClassHierarchy(instanceKlazz, methodName, argClasses, this);
        if (method != null) {
            onSuperMethodFoundInHierarchy(method);
            return method.invoke(instance, arguments);
        }
        // still not method here, so see if there is an invokeMethod method up the hierarchy
        final Class[] invokeMethodArgs = { String.class, Object[].class };
        method = findMethodInClassHierarchy(instanceKlazz, INVOKE_METHOD_METHOD, invokeMethodArgs, this);
        if (method != null && method instanceof ClosureMetaMethod) {
            onInvokeMethodFoundInHierarchy(method);
            return method.invoke(instance, invokeMethodArgs);
        }
    }
    if (methodMissing != null) {
        try {
            return methodMissing.invoke(instance, new Object[] { methodName, arguments });
        } catch (InvokerInvocationException iie) {
            if (methodMissing instanceof ClosureMetaMethod && iie.getCause() instanceof MissingMethodException) {
                MissingMethodException mme = (MissingMethodException) iie.getCause();
                throw new MissingMethodExecutionFailed(mme.getMethod(), mme.getClass(), mme.getArguments(), mme.isStatic(), mme);
            }
            throw iie;
        } catch (MissingMethodException mme) {
            if (methodMissing instanceof ClosureMetaMethod)
                throw new MissingMethodExecutionFailed(mme.getMethod(), mme.getClass(), mme.getArguments(), mme.isStatic(), mme);
            else
                throw mme;
        }
    } else if (original != null)
        throw original;
    else
        throw new MissingMethodExceptionNoStack(methodName, theClass, arguments, false);
}
Also used : NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) NewMetaMethod(org.codehaus.groovy.runtime.metaclass.NewMetaMethod) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) NewStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) TransformMetaMethod(org.codehaus.groovy.runtime.metaclass.TransformMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) MissingMethodExceptionNoStack(org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack) InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) CachedClass(org.codehaus.groovy.reflection.CachedClass) MissingMethodExecutionFailed(org.codehaus.groovy.runtime.metaclass.MissingMethodExecutionFailed)

Example 3 with MissingMethodExecutionFailed

use of org.codehaus.groovy.runtime.metaclass.MissingMethodExecutionFailed in project groovy by apache.

the class InvokerHelper method invokePogoMethod.

static Object invokePogoMethod(Object object, String methodName, Object arguments) {
    GroovyObject groovy = (GroovyObject) object;
    boolean intercepting = groovy instanceof GroovyInterceptable;
    try {
        // if it's a pure interceptable object (even intercepting toString(), clone(), ...)
        if (intercepting) {
            return groovy.invokeMethod(methodName, asUnwrappedArray(arguments));
        }
        //else try a statically typed method or a GDK method
        return groovy.getMetaClass().invokeMethod(object, methodName, asArray(arguments));
    } catch (MissingMethodException e) {
        if (e instanceof MissingMethodExecutionFailed) {
            throw (MissingMethodException) e.getCause();
        } else if (!intercepting && e.getMethod().equals(methodName) && object.getClass() == e.getType()) {
            // in case there's nothing else, invoke the object's own invokeMethod()
            return groovy.invokeMethod(methodName, asUnwrappedArray(arguments));
        } else {
            throw e;
        }
    }
}
Also used : MissingMethodException(groovy.lang.MissingMethodException) GroovyInterceptable(groovy.lang.GroovyInterceptable) GroovyObject(groovy.lang.GroovyObject) MissingMethodExecutionFailed(org.codehaus.groovy.runtime.metaclass.MissingMethodExecutionFailed)

Aggregations

MissingMethodExecutionFailed (org.codehaus.groovy.runtime.metaclass.MissingMethodExecutionFailed)3 CachedClass (org.codehaus.groovy.reflection.CachedClass)2 GeneratedMetaMethod (org.codehaus.groovy.reflection.GeneratedMetaMethod)2 InvokerInvocationException (org.codehaus.groovy.runtime.InvokerInvocationException)2 ClosureMetaMethod (org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod)2 MissingMethodExceptionNoStack (org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack)2 MixinInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)2 NewInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod)2 NewMetaMethod (org.codehaus.groovy.runtime.metaclass.NewMetaMethod)2 NewStaticMetaMethod (org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod)2 TransformMetaMethod (org.codehaus.groovy.runtime.metaclass.TransformMetaMethod)2 GroovyInterceptable (groovy.lang.GroovyInterceptable)1 GroovyObject (groovy.lang.GroovyObject)1 MissingMethodException (groovy.lang.MissingMethodException)1