Search in sources :

Example 6 with InvokerInvocationException

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

the class MetaClassImpl method invokeMissingProperty.

/**
     * Invoke a missing property on the given object with the given arguments.
     *
     * @param instance The object the method should be invoked on.
     * @param propertyName The name of the property to invoke.
     * @param optionalValue The (optional) new value for the property
     * @param isGetter Wether the method is a getter
     *
     * @return The result of the method invocation.
     */
public Object invokeMissingProperty(Object instance, String propertyName, Object optionalValue, boolean isGetter) {
    Class theClass = instance instanceof Class ? (Class) instance : instance.getClass();
    CachedClass superClass = theCachedClass;
    while (superClass != null && superClass != ReflectionCache.OBJECT_CLASS) {
        final MetaBeanProperty property = findPropertyInClassHierarchy(propertyName, superClass);
        if (property != null) {
            onSuperPropertyFoundInHierarchy(property);
            if (!isGetter) {
                property.setProperty(instance, optionalValue);
                return null;
            } else {
                return property.getProperty(instance);
            }
        }
        superClass = superClass.getCachedSuperClass();
    }
    // got here to property not found, look for getProperty or setProperty overrides
    if (isGetter) {
        final Class[] getPropertyArgs = { String.class };
        final MetaMethod method = findMethodInClassHierarchy(instance.getClass(), GET_PROPERTY_METHOD, getPropertyArgs, this);
        if (method != null && method instanceof ClosureMetaMethod) {
            onGetPropertyFoundInHierarchy(method);
            return method.invoke(instance, new Object[] { propertyName });
        }
    } else {
        final Class[] setPropertyArgs = { String.class, Object.class };
        final MetaMethod method = findMethodInClassHierarchy(instance.getClass(), SET_PROPERTY_METHOD, setPropertyArgs, this);
        if (method != null && method instanceof ClosureMetaMethod) {
            onSetPropertyFoundInHierarchy(method);
            return method.invoke(instance, new Object[] { propertyName, optionalValue });
        }
    }
    try {
        if (!(instance instanceof Class)) {
            if (isGetter && propertyMissingGet != null) {
                return propertyMissingGet.invoke(instance, new Object[] { propertyName });
            } else {
                if (propertyMissingSet != null)
                    return propertyMissingSet.invoke(instance, new Object[] { propertyName, optionalValue });
            }
        }
    } catch (InvokerInvocationException iie) {
        boolean shouldHandle = isGetter && propertyMissingGet != null;
        if (!shouldHandle)
            shouldHandle = !isGetter && propertyMissingSet != null;
        if (shouldHandle && iie.getCause() instanceof MissingPropertyException) {
            throw (MissingPropertyException) iie.getCause();
        }
        throw iie;
    }
    if (instance instanceof Class && theClass != Class.class) {
        final MetaProperty metaProperty = InvokerHelper.getMetaClass(Class.class).hasProperty(instance, propertyName);
        if (metaProperty != null)
            if (isGetter)
                return metaProperty.getProperty(instance);
            else {
                metaProperty.setProperty(instance, optionalValue);
                return null;
            }
    }
    throw new MissingPropertyExceptionNoStack(propertyName, theClass);
}
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) MissingPropertyExceptionNoStack(org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack) InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) CachedClass(org.codehaus.groovy.reflection.CachedClass) CachedClass(org.codehaus.groovy.reflection.CachedClass) GetMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty) GetBeanMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty)

Example 7 with InvokerInvocationException

use of org.codehaus.groovy.runtime.InvokerInvocationException in project intellij-community by JetBrains.

the class GroovyDslScript method addGdslMembers.

private CustomMembersHolder addGdslMembers(GroovyClassDescriptor descriptor, final PsiType psiType) {
    final ProcessingContext ctx = new ProcessingContext();
    ctx.put(GdslUtil.INITIAL_CONTEXT, descriptor);
    try {
        if (!isApplicable(executor, descriptor, ctx)) {
            return CustomMembersHolder.EMPTY;
        }
        return executor.processVariants(descriptor, ctx, psiType);
    } catch (InvokerInvocationException e) {
        Throwable cause = e.getCause();
        if (cause instanceof ProcessCanceledException) {
            throw (ProcessCanceledException) cause;
        }
        if (cause instanceof OutOfMemoryError) {
            throw (OutOfMemoryError) cause;
        }
        handleDslError(e);
    } catch (ProcessCanceledException | OutOfMemoryError e) {
        throw e;
    } catch (Throwable e) {
        // To handle exceptions in definition script
        handleDslError(e);
    }
    return CustomMembersHolder.EMPTY;
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 8 with InvokerInvocationException

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

the class MetaClassImpl method invokeMethod.

/**
     * <p>Invokes a method on the given receiver for the specified arguments. The sender is the class that invoked the method on the object.
     * The MetaClass will attempt to establish the method to invoke based on the name and arguments provided.
     *
     * <p>The isCallToSuper and fromInsideClass help the Groovy runtime perform optimisations on the call to go directly
     * to the super class if necessary
     *
     * @param sender The java.lang.Class instance that invoked the method
     * @param object The object which the method was invoked on
     * @param methodName The name of the method
     * @param originalArguments The arguments to the method
     * @param isCallToSuper Whether the method is a call to a super class method
     * @param fromInsideClass Whether the call was invoked from the inside or the outside of the class
     *
     * @return The return value of the method
     *
     * @see MetaClass#invokeMethod(Class, Object, String, Object[], boolean, boolean)
     */
public Object invokeMethod(Class sender, Object object, String methodName, Object[] originalArguments, boolean isCallToSuper, boolean fromInsideClass) {
    checkInitalised();
    if (object == null) {
        throw new NullPointerException("Cannot invoke method: " + methodName + " on null object");
    }
    final Object[] arguments = originalArguments == null ? EMPTY_ARGUMENTS : originalArguments;
    //        final Class[] argClasses = MetaClassHelper.convertToTypeArray(arguments);
    //
    //        unwrap(arguments);
    MetaMethod method = null;
    if (CLOSURE_CALL_METHOD.equals(methodName) && object instanceof GeneratedClosure) {
        method = getMethodWithCaching(sender, "doCall", arguments, isCallToSuper);
    }
    if (method == null) {
        method = getMethodWithCaching(sender, methodName, arguments, isCallToSuper);
    }
    MetaClassHelper.unwrap(arguments);
    if (method == null)
        method = tryListParamMetaMethod(sender, methodName, isCallToSuper, arguments);
    final boolean isClosure = object instanceof Closure;
    if (isClosure) {
        final Closure closure = (Closure) object;
        final Object owner = closure.getOwner();
        if (CLOSURE_CALL_METHOD.equals(methodName) || CLOSURE_DO_CALL_METHOD.equals(methodName)) {
            final Class objectClass = object.getClass();
            if (objectClass == MethodClosure.class) {
                final MethodClosure mc = (MethodClosure) object;
                methodName = mc.getMethod();
                final Class ownerClass = owner instanceof Class ? (Class) owner : owner.getClass();
                final MetaClass ownerMetaClass = registry.getMetaClass(ownerClass);
                return ownerMetaClass.invokeMethod(ownerClass, owner, methodName, arguments, false, false);
            } else if (objectClass == CurriedClosure.class) {
                final CurriedClosure cc = (CurriedClosure) object;
                // change the arguments for an uncurried call
                final Object[] curriedArguments = cc.getUncurriedArguments(arguments);
                final Class ownerClass = owner instanceof Class ? (Class) owner : owner.getClass();
                final MetaClass ownerMetaClass = registry.getMetaClass(ownerClass);
                return ownerMetaClass.invokeMethod(owner, methodName, curriedArguments);
            }
            if (method == null)
                invokeMissingMethod(object, methodName, arguments);
        }
        final Object delegate = closure.getDelegate();
        final boolean isClosureNotOwner = owner != closure;
        final int resolveStrategy = closure.getResolveStrategy();
        final Class[] argClasses = MetaClassHelper.convertToTypeArray(arguments);
        switch(resolveStrategy) {
            case Closure.TO_SELF:
                method = closure.getMetaClass().pickMethod(methodName, argClasses);
                if (method != null)
                    return method.invoke(closure, arguments);
                break;
            case Closure.DELEGATE_ONLY:
                if (method == null && delegate != closure && delegate != null) {
                    MetaClass delegateMetaClass = lookupObjectMetaClass(delegate);
                    method = delegateMetaClass.pickMethod(methodName, argClasses);
                    if (method != null)
                        return delegateMetaClass.invokeMethod(delegate, methodName, originalArguments);
                    else if (delegate != closure && (delegate instanceof GroovyObject)) {
                        return invokeMethodOnGroovyObject(methodName, originalArguments, delegate);
                    }
                }
                break;
            case Closure.OWNER_ONLY:
                if (method == null && owner != closure) {
                    MetaClass ownerMetaClass = lookupObjectMetaClass(owner);
                    return ownerMetaClass.invokeMethod(owner, methodName, originalArguments);
                }
                break;
            case Closure.DELEGATE_FIRST:
                if (method == null && delegate != closure && delegate != null) {
                    MetaClass delegateMetaClass = lookupObjectMetaClass(delegate);
                    method = delegateMetaClass.pickMethod(methodName, argClasses);
                    if (method != null)
                        return delegateMetaClass.invokeMethod(delegate, methodName, originalArguments);
                }
                if (method == null && owner != closure) {
                    MetaClass ownerMetaClass = lookupObjectMetaClass(owner);
                    method = ownerMetaClass.pickMethod(methodName, argClasses);
                    if (method != null)
                        return ownerMetaClass.invokeMethod(owner, methodName, originalArguments);
                }
                if (method == null && resolveStrategy != Closure.TO_SELF) {
                    // still no methods found, test if delegate or owner are GroovyObjects
                    // and invoke the method on them if so.
                    MissingMethodException last = null;
                    if (delegate != closure && (delegate instanceof GroovyObject)) {
                        try {
                            return invokeMethodOnGroovyObject(methodName, originalArguments, delegate);
                        } catch (MissingMethodException mme) {
                            if (last == null)
                                last = mme;
                        }
                    }
                    if (isClosureNotOwner && (owner instanceof GroovyObject)) {
                        try {
                            return invokeMethodOnGroovyObject(methodName, originalArguments, owner);
                        } catch (MissingMethodException mme) {
                            last = mme;
                        }
                    }
                    if (last != null)
                        return invokeMissingMethod(object, methodName, originalArguments, last, isCallToSuper);
                }
                break;
            default:
                if (method == null && owner != closure) {
                    MetaClass ownerMetaClass = lookupObjectMetaClass(owner);
                    method = ownerMetaClass.pickMethod(methodName, argClasses);
                    if (method != null)
                        return ownerMetaClass.invokeMethod(owner, methodName, originalArguments);
                }
                if (method == null && delegate != closure && delegate != null) {
                    MetaClass delegateMetaClass = lookupObjectMetaClass(delegate);
                    method = delegateMetaClass.pickMethod(methodName, argClasses);
                    if (method != null)
                        return delegateMetaClass.invokeMethod(delegate, methodName, originalArguments);
                }
                if (method == null && resolveStrategy != Closure.TO_SELF) {
                    // still no methods found, test if delegate or owner are GroovyObjects
                    // and invoke the method on them if so.
                    MissingMethodException last = null;
                    if (isClosureNotOwner && (owner instanceof GroovyObject)) {
                        try {
                            return invokeMethodOnGroovyObject(methodName, originalArguments, owner);
                        } catch (MissingMethodException mme) {
                            if (methodName.equals(mme.getMethod())) {
                                if (last == null)
                                    last = mme;
                            } else {
                                throw mme;
                            }
                        } catch (InvokerInvocationException iie) {
                            if (iie.getCause() instanceof MissingMethodException) {
                                MissingMethodException mme = (MissingMethodException) iie.getCause();
                                if (methodName.equals(mme.getMethod())) {
                                    if (last == null)
                                        last = mme;
                                } else {
                                    throw iie;
                                }
                            } else
                                throw iie;
                        }
                    }
                    if (delegate != closure && (delegate instanceof GroovyObject)) {
                        try {
                            return invokeMethodOnGroovyObject(methodName, originalArguments, delegate);
                        } catch (MissingMethodException mme) {
                            last = mme;
                        } catch (InvokerInvocationException iie) {
                            if (iie.getCause() instanceof MissingMethodException) {
                                last = (MissingMethodException) iie.getCause();
                            } else
                                throw iie;
                        }
                    }
                    if (last != null)
                        return invokeMissingMethod(object, methodName, originalArguments, last, isCallToSuper);
                }
        }
    }
    if (method != null) {
        return method.doMethodInvoke(object, arguments);
    } else {
        return invokePropertyOrMissing(object, methodName, originalArguments, fromInsideClass, isCallToSuper);
    }
}
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) MethodClosure(org.codehaus.groovy.runtime.MethodClosure) GeneratedClosure(org.codehaus.groovy.runtime.GeneratedClosure) CurriedClosure(org.codehaus.groovy.runtime.CurriedClosure) ConvertedClosure(org.codehaus.groovy.runtime.ConvertedClosure) InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) MethodClosure(org.codehaus.groovy.runtime.MethodClosure) GeneratedClosure(org.codehaus.groovy.runtime.GeneratedClosure) CurriedClosure(org.codehaus.groovy.runtime.CurriedClosure) CachedClass(org.codehaus.groovy.reflection.CachedClass)

Example 9 with InvokerInvocationException

use of org.codehaus.groovy.runtime.InvokerInvocationException 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 10 with InvokerInvocationException

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

the class GroovyShell method runRunnable.

private Object runRunnable(Class scriptClass, String[] args) {
    Constructor constructor = null;
    Runnable runnable = null;
    Throwable reason = null;
    try {
        // first, fetch the constructor taking String[] as parameter
        constructor = scriptClass.getConstructor(new Class[] { (new String[] {}).getClass() });
        try {
            // instantiate a runnable and run it
            runnable = (Runnable) constructor.newInstance(new Object[] { args });
        } catch (Throwable t) {
            reason = t;
        }
    } catch (NoSuchMethodException e1) {
        try {
            // otherwise, find the default constructor
            constructor = scriptClass.getConstructor(new Class[] {});
            try {
                // instantiate a runnable and run it
                runnable = (Runnable) constructor.newInstance();
            } catch (InvocationTargetException ite) {
                throw new InvokerInvocationException(ite.getTargetException());
            } catch (Throwable t) {
                reason = t;
            }
        } catch (NoSuchMethodException nsme) {
            reason = nsme;
        }
    }
    if (constructor != null && runnable != null) {
        runnable.run();
    } else {
        throw new GroovyRuntimeException("This script or class was runnable but could not be run. ", reason);
    }
    return null;
}
Also used : Constructor(java.lang.reflect.Constructor) InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

InvokerInvocationException (org.codehaus.groovy.runtime.InvokerInvocationException)20 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)10 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)6 ForStatement (org.codehaus.groovy.ast.stmt.ForStatement)6 Statement (org.codehaus.groovy.ast.stmt.Statement)6 CachedClass (org.codehaus.groovy.reflection.CachedClass)6 GeneratedMetaMethod (org.codehaus.groovy.reflection.GeneratedMetaMethod)6 ClosureMetaMethod (org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod)6 MixinInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)6 NewInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod)6 NewMetaMethod (org.codehaus.groovy.runtime.metaclass.NewMetaMethod)6 NewStaticMetaMethod (org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod)6 TransformMetaMethod (org.codehaus.groovy.runtime.metaclass.TransformMetaMethod)6 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)4 MethodClosure (org.codehaus.groovy.runtime.MethodClosure)3 Constructor (java.lang.reflect.Constructor)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ClassNode (org.codehaus.groovy.ast.ClassNode)2 MethodNode (org.codehaus.groovy.ast.MethodNode)2 AssertStatement (org.codehaus.groovy.ast.stmt.AssertStatement)2