Search in sources :

Example 36 with ReturnStatement

use of org.codehaus.groovy.ast.stmt.ReturnStatement in project gradle by gradle.

the class RuleVisitor method visitGeneratedClosure.

// Not part of a normal visitor, see ClosureCreationInterceptingVerifier
public static void visitGeneratedClosure(ClassNode node) {
    MethodNode closureCallMethod = AstUtils.getGeneratedClosureImplMethod(node);
    Statement closureCode = closureCallMethod.getCode();
    InputReferences inputs = closureCode.getNodeMetaData(AST_NODE_METADATA_INPUTS_KEY);
    if (inputs != null) {
        SourceLocation sourceLocation = closureCode.getNodeMetaData(AST_NODE_METADATA_LOCATION_KEY);
        node.addInterface(TRANSFORMED_CLOSURE);
        FieldNode inputsField = new FieldNode(INPUTS_FIELD_NAME, Modifier.PRIVATE, POTENTIAL_INPUTS, node, null);
        FieldNode ruleFactoryField = new FieldNode(RULE_FACTORY_FIELD_NAME, Modifier.PRIVATE, RULE_FACTORY, node, null);
        node.addField(inputsField);
        node.addField(ruleFactoryField);
        // Generate makeRule() method
        List<Statement> statements = new ArrayList<Statement>();
        statements.add(new ExpressionStatement(new BinaryExpression(new FieldExpression(inputsField), ASSIGN, new VariableExpression("inputs"))));
        statements.add(new ExpressionStatement(new BinaryExpression(new FieldExpression(ruleFactoryField), ASSIGN, new VariableExpression("ruleFactory"))));
        node.addMethod(new MethodNode("makeRule", Modifier.PUBLIC, ClassHelper.VOID_TYPE, new Parameter[] { new Parameter(POTENTIAL_INPUTS, "inputs"), new Parameter(RULE_FACTORY, "ruleFactory") }, new ClassNode[0], new BlockStatement(statements, new VariableScope())));
        // Generate inputReferences() method
        VariableExpression inputsVar = new VariableExpression("inputs", INPUT_REFERENCES);
        VariableScope methodVarScope = new VariableScope();
        methodVarScope.putDeclaredVariable(inputsVar);
        statements = new ArrayList<Statement>();
        statements.add(new ExpressionStatement(new DeclarationExpression(inputsVar, ASSIGN, new ConstructorCallExpression(INPUT_REFERENCES, new ArgumentListExpression()))));
        for (InputReference inputReference : inputs.getOwnReferences()) {
            statements.add(new ExpressionStatement(new MethodCallExpression(inputsVar, "ownReference", new ArgumentListExpression(new ConstantExpression(inputReference.getPath()), new ConstantExpression(inputReference.getLineNumber())))));
        }
        for (InputReference inputReference : inputs.getNestedReferences()) {
            statements.add(new ExpressionStatement(new MethodCallExpression(inputsVar, "nestedReference", new ArgumentListExpression(new ConstantExpression(inputReference.getPath()), new ConstantExpression(inputReference.getLineNumber())))));
        }
        statements.add(new ReturnStatement(inputsVar));
        node.addMethod(new MethodNode("inputReferences", Modifier.PUBLIC, INPUT_REFERENCES, new Parameter[0], new ClassNode[0], new BlockStatement(statements, methodVarScope)));
        // Generate sourceLocation() method
        statements = new ArrayList<Statement>();
        statements.add(new ReturnStatement(new ConstructorCallExpression(SOURCE_LOCATION, new ArgumentListExpression(Arrays.<Expression>asList(new ConstantExpression(SOURCE_URI_TOKEN), new ConstantExpression(SOURCE_DESC_TOKEN), new ConstantExpression(sourceLocation.getExpression()), new ConstantExpression(sourceLocation.getLineNumber()), new ConstantExpression(sourceLocation.getColumnNumber()))))));
        node.addMethod(new MethodNode("sourceLocation", Modifier.PUBLIC, SOURCE_LOCATION, new Parameter[0], new ClassNode[0], new BlockStatement(statements, new VariableScope())));
    }
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ArrayList(java.util.ArrayList) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement)

Example 37 with ReturnStatement

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

the class VertexEntityTransformation method processClass.

/**
 * Process a class and make it a vertex entity class.
 *
 * @param clazz the class node
 */
private void processClass(ClassNode clazz) {
    // check if class already was processed (has field v)
    if (clazz.getField("v") != null) {
        return;
    }
    // find all classes annotated with @ODocumentEntity
    List<AnnotationNode> entityAnnotations = clazz.getAnnotations(VERTEX_ENTITY_CLASS);
    if (entityAnnotations != null && !entityAnnotations.isEmpty()) {
        Expression entityName = entityAnnotations.get(0).getMember("value");
        Expression typeProperty = entityAnnotations.get(0).getMember("typeProperty");
        if (typeProperty == null) {
            // default value if none given
            typeProperty = new ConstantExpression("_type");
        }
        Expression superEntityName = null;
        FieldNode vertexField = null;
        FieldNode graphField = null;
        ClassNode superClass = clazz.getSuperClass();
        if (superClass != null) {
            List<AnnotationNode> superAnnotations = superClass.getAnnotations(VERTEX_ENTITY_CLASS);
            if (superAnnotations != null && !superAnnotations.isEmpty()) {
                // super class is also a vertex entity
                superEntityName = superAnnotations.get(0).getMember("value");
                // super class must be processed first
                processClass(superClass);
                // use fields from super class
                vertexField = clazz.getField("v");
                graphField = clazz.getField("g");
            } else {
                superClass = null;
            }
        }
        // add the vertex field
        if (vertexField == null) {
            vertexField = clazz.addField("v", Modifier.PROTECTED, VERTEX_CLASS, null);
        }
        // add the graph field
        if (graphField == null) {
            graphField = clazz.addField("g", Modifier.PROTECTED, GRAPH_CLASS, null);
        }
        // add constructor
        clazz.addConstructor(buildVertexGraphConstructor(vertexField, graphField, superClass, typeProperty, entityName));
        Map<String, Expression> initialExpressions = new HashMap<>();
        // get all non-static properties
        List<PropertyNode> properties = AbstractASTTransformUtil.getInstanceProperties(clazz);
        List<PropertyNode> newProperties = new ArrayList<>();
        for (PropertyNode property : properties) {
            // collect initial expressions for create function
            if (property.getField().getInitialExpression() != null) {
                initialExpressions.put(property.getName(), property.getInitialExpression());
            }
            // add static findByX method
            clazz.addMethod(buildFindByMethod(clazz, entityName, typeProperty, property.getName(), property.getType()));
            // add static findUniqueByX method
            clazz.addMethod(buildGetByMethod(clazz, entityName, typeProperty, property.getName(), property.getType()));
            // update property
            property.setGetterBlock(createGetter(property.getName(), vertexField, property.getType(), property.getField().getInitialExpression()));
            property.setSetterBlock(createSetter(property.getName(), vertexField));
            newProperties.add(property);
        }
        // readd updated properties
        for (PropertyNode property : newProperties) {
            readdProperty(clazz, property);
        }
        // add the vertex getter
        // 
        clazz.addMethod(// 
        "getV", // 
        Modifier.PUBLIC, // 
        VERTEX_CLASS, new Parameter[0], new ClassNode[0], new ReturnStatement(new FieldExpression(vertexField)));
        // add the graph getter
        // 
        clazz.addMethod(// 
        "getG", // 
        Modifier.PUBLIC, // 
        GRAPH_CLASS, new Parameter[0], new ClassNode[0], new ReturnStatement(new FieldExpression(graphField)));
        // add the id getter
        clazz.addMethod("getId", Modifier.PUBLIC, ClassHelper.OBJECT_TYPE, new Parameter[0], new ClassNode[0], new ReturnStatement(new MethodCallExpression(new FieldExpression(vertexField), "getId", new ArgumentListExpression())));
        // add delete method
        clazz.addMethod(buildDeleteMethod(vertexField, graphField));
        // add static create method
        clazz.addMethod(buildCreateMethod(clazz, entityName, initialExpressions));
        // add static findAll method
        clazz.addMethod(buildFindAllMethod(clazz, entityName, typeProperty));
        // add static getById method
        clazz.addMethod(buildGetByIdMethod(clazz));
        // add static initGraph method
        clazz.addMethod(buildInitGraphMethod(entityName, superEntityName));
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) HashMap(java.util.HashMap) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArrayList(java.util.ArrayList) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) NotExpression(org.codehaus.groovy.ast.expr.NotExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) PropertyNode(org.codehaus.groovy.ast.PropertyNode) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement)

Example 38 with ReturnStatement

use of org.codehaus.groovy.ast.stmt.ReturnStatement 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)

Example 39 with ReturnStatement

use of org.codehaus.groovy.ast.stmt.ReturnStatement 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 40 with ReturnStatement

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

the class DefaultGrailsDomainClassInjector method injectToStringMethod.

private void injectToStringMethod(ClassNode classNode) {
    final boolean hasToString = /*GrailsASTUtils.*/
    implementsZeroArgMethod(classNode, "toString");
    if (!hasToString) {
        GStringExpression ge = new GStringExpression(classNode.getName() + " : ${id}");
        ge.addString(new ConstantExpression(classNode.getName() + " : "));
        ge.addValue(new VariableExpression("id"));
        Statement s = new ReturnStatement(ge);
        MethodNode mn = new MethodNode("toString", Modifier.PUBLIC, new ClassNode(String.class), new Parameter[0], new ClassNode[0], s);
        //if(LOG.isDebugEnabled()) {
        //    LOG.debug("[GrailsDomainInjector] Adding method [toString()] to class [" + classNode.getName() + "]");
        //}
        classNode.addMethod(mn);
    }
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement)

Aggregations

ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)74 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)44 Statement (org.codehaus.groovy.ast.stmt.Statement)38 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)37 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)35 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)32 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)30 MethodNode (org.codehaus.groovy.ast.MethodNode)28 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)26 Expression (org.codehaus.groovy.ast.expr.Expression)25 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)24 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)23 ClassNode (org.codehaus.groovy.ast.ClassNode)22 Parameter (org.codehaus.groovy.ast.Parameter)22 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)22 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)21 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)18 ArrayList (java.util.ArrayList)17 EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)17 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)15