Search in sources :

Example 16 with InvokerInvocationException

use of org.codehaus.groovy.runtime.InvokerInvocationException in project groovy by apache.

the class TupleListTest method assertIterate.

protected void assertIterate(String methodName, Expression listExpression) throws Exception {
    ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
    classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));
    classNode.addProperty(new PropertyNode("bar", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null));
    Statement loopStatement = createPrintlnStatement(new VariableExpression("i"));
    BlockStatement block = new BlockStatement();
    block.addStatement(new ExpressionStatement(new DeclarationExpression(new VariableExpression("list"), Token.newSymbol("=", 0, 0), listExpression)));
    block.addStatement(new ForStatement(new Parameter(ClassHelper.DYNAMIC_TYPE, "i"), new VariableExpression("list"), loopStatement));
    classNode.addMethod(new MethodNode(methodName, ACC_PUBLIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, block));
    Class fooClass = loadClass(classNode);
    assertTrue("Loaded a new class", fooClass != null);
    Object bean = fooClass.newInstance();
    assertTrue("Managed to create bean", bean != null);
    System.out.println("################ Now about to invoke method");
    try {
        InvokerHelper.invokeMethod(bean, methodName, null);
    } catch (InvokerInvocationException e) {
        System.out.println("Caught: " + e.getCause());
        e.getCause().printStackTrace();
        fail("Should not have thrown an exception");
    }
    System.out.println("################ Done");
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement)

Example 17 with InvokerInvocationException

use of org.codehaus.groovy.runtime.InvokerInvocationException in project groovy by apache.

the class GroovyShell method runRunnable.

private static 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)

Example 18 with InvokerInvocationException

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

use of org.codehaus.groovy.runtime.InvokerInvocationException in project groovy by apache.

the class GStringTest method testConstructor.

public void testConstructor() throws Exception {
    ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
    //Statement printStatement = createPrintlnStatement(new VariableExpression("str"));
    // simulate "Hello ${user}!"
    GStringExpression compositeStringExpr = new GStringExpression("hello ${user}!");
    compositeStringExpr.addString(new ConstantExpression("Hello "));
    compositeStringExpr.addValue(new VariableExpression("user"));
    compositeStringExpr.addString(new ConstantExpression("!"));
    BlockStatement block = new BlockStatement();
    block.addStatement(new ExpressionStatement(new DeclarationExpression(new VariableExpression("user"), Token.newSymbol("=", -1, -1), new ConstantExpression("World"))));
    block.addStatement(new ExpressionStatement(new DeclarationExpression(new VariableExpression("str"), Token.newSymbol("=", -1, -1), compositeStringExpr)));
    block.addStatement(new ExpressionStatement(new MethodCallExpression(VariableExpression.THIS_EXPRESSION, "println", new VariableExpression("str"))));
    block.addStatement(new ExpressionStatement(new DeclarationExpression(new VariableExpression("text"), Token.newSymbol("=", -1, -1), new MethodCallExpression(new VariableExpression("str"), "toString", MethodCallExpression.NO_ARGUMENTS))));
    block.addStatement(new AssertStatement(new BooleanExpression(new BinaryExpression(new VariableExpression("text"), Token.newSymbol("==", -1, -1), new ConstantExpression("Hello World!"))), // TODO FIX if empty, AssertionWriter fails because source text is null
    new ConstantExpression("Assertion failed")));
    classNode.addMethod(new MethodNode("stringDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, block));
    Class fooClass = loadClass(classNode);
    assertTrue("Loaded a new class", fooClass != null);
    Object bean = fooClass.newInstance();
    assertTrue("Managed to create bean", bean != null);
    try {
        InvokerHelper.invokeMethod(bean, "stringDemo", null);
    } catch (InvokerInvocationException e) {
        System.out.println("Caught: " + e.getCause());
        e.getCause().printStackTrace();
        fail("Should not have thrown an exception");
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) MethodNode(org.codehaus.groovy.ast.MethodNode) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement)

Example 20 with InvokerInvocationException

use of org.codehaus.groovy.runtime.InvokerInvocationException in project groovy by apache.

the class ForTest method testLoop.

public void testLoop() throws Exception {
    ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
    classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));
    Parameter[] parameters = { new Parameter(ClassHelper.OBJECT_TYPE.makeArray(), "coll") };
    Statement loopStatement = createPrintlnStatement(new VariableExpression("i"));
    ForStatement statement = new ForStatement(new Parameter(ClassHelper.OBJECT_TYPE, "i"), new VariableExpression("coll"), loopStatement);
    classNode.addMethod(new MethodNode("iterateDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, parameters, ClassNode.EMPTY_ARRAY, statement));
    Class fooClass = loadClass(classNode);
    assertTrue("Loaded a new class", fooClass != null);
    Object bean = fooClass.newInstance();
    assertTrue("Managed to create bean", bean != null);
    System.out.println("################ Now about to invoke a method with looping");
    Object[] array = { new Integer(1234), "abc", "def" };
    try {
        InvokerHelper.invokeMethod(bean, "iterateDemo", new Object[] { array });
    } catch (InvokerInvocationException e) {
        System.out.println("Caught: " + e.getCause());
        e.getCause().printStackTrace();
        fail("Should not have thrown an exception");
    }
    System.out.println("################ Done");
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement)

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