Search in sources :

Example 51 with BlockStatement

use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy by apache.

the class MethodTest method testMethods.

public void testMethods() throws Exception {
    ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
    classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));
    Statement statementA = new ReturnStatement(new ConstantExpression("calledA"));
    Statement statementB = new ReturnStatement(new ConstantExpression("calledB"));
    Statement emptyStatement = new BlockStatement();
    classNode.addMethod(new MethodNode("a", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statementA));
    classNode.addMethod(new MethodNode("b", ACC_PUBLIC, null, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statementB));
    classNode.addMethod(new MethodNode("noReturnMethodA", ACC_PUBLIC, null, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, emptyStatement));
    classNode.addMethod(new MethodNode("noReturnMethodB", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, emptyStatement));
    classNode.addMethod(new MethodNode("c", ACC_PUBLIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, emptyStatement));
    Class fooClass = loadClass(classNode);
    assertTrue("Loaded a new class", fooClass != null);
    Object bean = fooClass.newInstance();
    assertTrue("Created instance of class: " + bean, bean != null);
    assertCallMethod(bean, "a", "calledA");
    assertCallMethod(bean, "b", "calledB");
    assertCallMethod(bean, "noReturnMethodA", null);
    assertCallMethod(bean, "noReturnMethodB", null);
    assertCallMethod(bean, "c", null);
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 52 with BlockStatement

use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy by apache.

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 53 with BlockStatement

use of org.codehaus.groovy.ast.stmt.BlockStatement in project hale by halestudio.

the class VertexEntityTransformation method buildFindAllMethod.

/**
 * Create a static method to retrieve all objects in a graph.
 *
 * @param clazz the entity class node
 * @param entityName the entity name
 * @param typeProperty the name of the property holding the entity name in a
 *            vertex
 * @return the method
 */
private MethodNode buildFindAllMethod(ClassNode clazz, Expression entityName, Expression typeProperty) {
    clazz = ClassHelper.make(clazz.getName());
    ClassNode returnType = ITERABLE_CLASS.getPlainNodeReference();
    // add generic type argument
    returnType.setGenericsTypes(new GenericsType[] { new GenericsType(clazz) });
    BlockStatement code = new BlockStatement();
    // initialize graph
    code.addStatement(callInitGraph(clazz, new VariableExpression("graph")));
    /*
		 * def vertices = VertexEntityDelegates.findAllDelegate(graph,
		 * entityName, typeProperty)
		 */
    VariableExpression vertices = new VariableExpression("vertices");
    ArgumentListExpression args = new ArgumentListExpression();
    args.addExpression(new VariableExpression("graph"));
    args.addExpression(entityName);
    args.addExpression(typeProperty);
    code.addStatement(AbstractASTTransformUtil.declStatement(vertices, new StaticMethodCallExpression(VE_DELEGATES_CLASS, VertexEntityDelegates.METHOD_FIND_ALL, args)));
    /*
		 * return new IterableDelegate(vertices, EntityClass, graph)
		 */
    ArgumentListExpression createDelegateArgs = new ArgumentListExpression();
    createDelegateArgs.addExpression(vertices);
    createDelegateArgs.addExpression(new ClassExpression(clazz));
    createDelegateArgs.addExpression(new VariableExpression("graph"));
    code.addStatement(new ReturnStatement(new ConstructorCallExpression(VE_ITERABLE_DELEGATE_CLASS, createDelegateArgs)));
    return new MethodNode("findAll", Modifier.STATIC | Modifier.PUBLIC, returnType, new Parameter[] { new Parameter(GRAPH_CLASS, "graph") }, new ClassNode[0], code);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) GenericsType(org.codehaus.groovy.ast.GenericsType) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) Parameter(org.codehaus.groovy.ast.Parameter) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Example 54 with BlockStatement

use of org.codehaus.groovy.ast.stmt.BlockStatement in project hale by halestudio.

the class VertexEntityTransformation method createSetter.

private Statement createSetter(String name, FieldNode vertexField) {
    BlockStatement block = new BlockStatement();
    VariableExpression value = new VariableExpression("value");
    /*
		 * v.setProperty(name, value)
		 */
    ArgumentListExpression args = new ArgumentListExpression();
    args.addExpression(new ConstantExpression(name));
    args.addExpression(value);
    Statement ifNotNull = new ExpressionStatement(new MethodCallExpression(new FieldExpression(vertexField), "setProperty", args));
    /*
		 * v.removeProperty(name)
		 */
    Statement ifNull = new ExpressionStatement(new MethodCallExpression(new FieldExpression(vertexField), "removeProperty", new ArgumentListExpression(new ConstantExpression(name))));
    block.addStatement(new IfStatement(AbstractASTTransformUtil.equalsNullExpr(value), ifNull, ifNotNull));
    return block;
}
Also used : IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression)

Example 55 with BlockStatement

use of org.codehaus.groovy.ast.stmt.BlockStatement in project hale by halestudio.

the class VertexEntityTransformation method buildGetByMethod.

/**
 * Create a static method to retrieve an object by property value.
 *
 * @param clazz the entity class node
 * @param entityName the entity name
 * @param typeProperty the name of the property holding the entity name in a
 *            vertex
 * @param propertyName the property name
 * @param propertyType the property type
 * @return the method
 */
private MethodNode buildGetByMethod(ClassNode clazz, Expression entityName, Expression typeProperty, String propertyName, ClassNode propertyType) {
    clazz = ClassHelper.make(clazz.getName());
    propertyType = ClassHelper.make(propertyType.getName());
    String methodName = "getBy" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
    BlockStatement code = new BlockStatement();
    // initialize graph
    code.addStatement(callInitGraph(clazz, new VariableExpression("graph")));
    /*
		 * def vertex = VertexEntityDelegates.findUniqueByDelegate(graph,
		 * entityName, typeProperty, propertyName, value)
		 */
    VariableExpression vertex = new VariableExpression("vertex");
    ArgumentListExpression args = new ArgumentListExpression();
    args.addExpression(new VariableExpression("graph"));
    args.addExpression(entityName);
    args.addExpression(typeProperty);
    args.addExpression(new ConstantExpression(propertyName));
    args.addExpression(new VariableExpression("value"));
    code.addStatement(AbstractASTTransformUtil.declStatement(vertex, new StaticMethodCallExpression(VE_DELEGATES_CLASS, VertexEntityDelegates.METHOD_GET_BY, args)));
    /*
		 * return new EntityClass(vertex, graph)
		 */
    Statement returnEntity = new ReturnStatement(new ConstructorCallExpression(clazz, new ArgumentListExpression(vertex, new VariableExpression("graph"))));
    // return null
    Statement returnNull = new ReturnStatement(new ConstantExpression(null));
    // if (vertex == null) ... else ...
    code.addStatement(new IfStatement(AbstractASTTransformUtil.equalsNullExpr(vertex), returnNull, returnEntity));
    return new MethodNode(methodName, Modifier.STATIC | Modifier.PUBLIC, clazz, new Parameter[] { new Parameter(GRAPH_CLASS, "graph"), new Parameter(propertyType, "value") }, new ClassNode[] { VE_NON_UNIQUE_EXCEPTION }, code);
}
Also used : IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) MethodNode(org.codehaus.groovy.ast.MethodNode) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) Parameter(org.codehaus.groovy.ast.Parameter) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Aggregations

BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)212 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)94 Statement (org.codehaus.groovy.ast.stmt.Statement)87 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)76 Expression (org.codehaus.groovy.ast.expr.Expression)75 MethodNode (org.codehaus.groovy.ast.MethodNode)64 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)62 Parameter (org.codehaus.groovy.ast.Parameter)58 ClassNode (org.codehaus.groovy.ast.ClassNode)54 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)52 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)51 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)45 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)45 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)44 FieldNode (org.codehaus.groovy.ast.FieldNode)38 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)37 EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)33 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)33 ArrayList (java.util.ArrayList)32 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)28