Search in sources :

Example 11 with InvokerInvocationException

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

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)

Example 12 with InvokerInvocationException

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

the class ForTest method testManyParam.

public void testManyParam() 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, "coll1"), new Parameter(ClassHelper.OBJECT_TYPE, "coll2"), new Parameter(ClassHelper.OBJECT_TYPE, "coll3") };
    BlockStatement statement = new BlockStatement();
    statement.addStatement(createPrintlnStatement(new VariableExpression("coll1")));
    statement.addStatement(createPrintlnStatement(new VariableExpression("coll2")));
    statement.addStatement(createPrintlnStatement(new VariableExpression("coll3")));
    classNode.addMethod(new MethodNode("manyParamDemo", 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 many parameters");
    Object[] array = { new Integer(1000 * 1000), "foo-", "bar~" };
    try {
        InvokerHelper.invokeMethod(bean, "manyParamDemo", 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 : InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 13 with InvokerInvocationException

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

the class ForTest method testNonLoop.

public void testNonLoop() 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, "coll") };
    Statement statement = createPrintlnStatement(new VariableExpression("coll"));
    classNode.addMethod(new MethodNode("oneParamDemo", 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 without looping");
    Object value = new Integer(10000);
    try {
        InvokerHelper.invokeMethod(bean, "oneParamDemo", new Object[] { value });
    } 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)

Example 14 with InvokerInvocationException

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

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 15 with InvokerInvocationException

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

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)

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