Search in sources :

Example 16 with FieldExpression

use of org.codehaus.groovy.ast.expr.FieldExpression in project hale by halestudio.

the class VertexEntityTransformation method createGetter.

private Statement createGetter(String name, FieldNode vertexField, ClassNode propertyType, Expression initialExpression) {
    BlockStatement block = new BlockStatement();
    // def tmp
    VariableExpression tmpValue = new VariableExpression("tmp");
    /*
		 * > tmp = v.getProperty(name)
		 */
    ArgumentListExpression args = new ArgumentListExpression();
    args.addExpression(new ConstantExpression(name));
    block.addStatement(AbstractASTTransformUtil.declStatement(tmpValue, new MethodCallExpression(new FieldExpression(vertexField), "getProperty", args)));
    if (ClassHelper.isPrimitiveType(propertyType) && initialExpression != null) {
        // if the class is a primitive, we must do a null check here
        // if (tmp == null) return <initial-value>
        block.addStatement(new IfStatement(AbstractASTTransformUtil.equalsNullExpr(tmpValue), new ReturnStatement(initialExpression), new EmptyStatement()));
    }
    block.addStatement(new ReturnStatement(tmpValue));
    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) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) 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 17 with FieldExpression

use of org.codehaus.groovy.ast.expr.FieldExpression in project hale by halestudio.

the class VertexEntityTransformation method buildVertexGraphConstructor.

/**
 * Create a constructor taking a Vertex and a Graph as an argument,
 * assigning them to the vertex and graph fields.
 *
 * @param vertexField the vertex field
 * @param graphField the graph field
 * @param superClass the vertex entity super class or <code>null</code>
 * @param typeProperty the expression specifying the name of the type
 *            property
 * @param entityName the expression specifying the entity name
 * @return a constructor taking a Vertex as an argument
 */
private ConstructorNode buildVertexGraphConstructor(FieldNode vertexField, FieldNode graphField, ClassNode superClass, Expression typeProperty, Expression entityName) {
    BlockStatement block = new BlockStatement();
    // parameter vertex
    VariableExpression vertex = new VariableExpression("vertex");
    // parameter graph
    VariableExpression graph = new VariableExpression("graph");
    if (superClass != null) {
        // super(vertex, graph)
        block.addStatement(new ExpressionStatement(new ConstructorCallExpression(ClassNode.SUPER, new ArgumentListExpression(vertex, graph))));
    } else {
        // > this.v = vertex
        block.addStatement(AbstractASTTransformUtil.assignStatement(new FieldExpression(vertexField), new VariableExpression("vertex")));
        // > this.g = graph
        block.addStatement(AbstractASTTransformUtil.assignStatement(new FieldExpression(graphField), new VariableExpression("graph")));
    }
    // vertex.setProperty(typeProperty, entityName)
    Statement notOrientStatement = new ExpressionStatement(new MethodCallExpression(vertex, "setProperty", new ArgumentListExpression(new Expression[] { typeProperty, entityName })));
    // > if (!(graph instanceof OrientGraph))
    // > this.v.setProperty(typeProperty, entityName)
    block.addStatement(new IfStatement(new NotExpression(AbstractASTTransformUtil.isInstanceOf(graph, ORIENT_GRAPH_CLASS)), notOrientStatement, new EmptyStatement()));
    return new ConstructorNode(Modifier.PUBLIC, new Parameter[] { new Parameter(VERTEX_CLASS, "vertex"), new Parameter(GRAPH_CLASS, "graph") }, new ClassNode[0], block);
}
Also used : ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) 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) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) NotExpression(org.codehaus.groovy.ast.expr.NotExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) Parameter(org.codehaus.groovy.ast.Parameter)

Example 18 with FieldExpression

use of org.codehaus.groovy.ast.expr.FieldExpression in project hale by halestudio.

the class VertexEntityTransformation method buildDeleteMethod.

/**
 * Create a method the removes the entity from the graph.
 *
 * @param vertexField the vertex field
 * @param graphField the graph field
 * @return the delete method
 */
private MethodNode buildDeleteMethod(FieldNode vertexField, FieldNode graphField) {
    BlockStatement code = new BlockStatement();
    // > g.removeVertex(v)
    code.addStatement(new ExpressionStatement(new MethodCallExpression(new FieldExpression(graphField), "removeVertex", new ArgumentListExpression(new FieldExpression(vertexField)))));
    // reset graph field
    // > g = null
    code.addStatement(AbstractASTTransformUtil.assignStatement(new FieldExpression(graphField), new ConstantExpression(null)));
    return new MethodNode("delete", Modifier.PUBLIC, ClassHelper.VOID_TYPE, new Parameter[0], new ClassNode[0], code);
}
Also used : StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression)

Example 19 with FieldExpression

use of org.codehaus.groovy.ast.expr.FieldExpression in project groovy-core by groovy.

the class Verifier method addFieldInitialization.

protected void addFieldInitialization(List list, List staticList, FieldNode fieldNode, boolean isEnumClassNode, List initStmtsAfterEnumValuesInit, Set explicitStaticPropsInEnum) {
    Expression expression = fieldNode.getInitialExpression();
    if (expression != null) {
        final FieldExpression fe = new FieldExpression(fieldNode);
        if (fieldNode.getType().equals(ClassHelper.REFERENCE_TYPE) && ((fieldNode.getModifiers() & Opcodes.ACC_SYNTHETIC) != 0)) {
            fe.setUseReferenceDirectly(true);
        }
        ExpressionStatement statement = new ExpressionStatement(new BinaryExpression(fe, Token.newSymbol(Types.EQUAL, fieldNode.getLineNumber(), fieldNode.getColumnNumber()), expression));
        if (fieldNode.isStatic()) {
            // GROOVY-3311: pre-defined constants added by groovy compiler for numbers/characters should be
            // initialized first so that code dependent on it does not see their values as empty
            Expression initialValueExpression = fieldNode.getInitialValueExpression();
            if (initialValueExpression instanceof ConstantExpression) {
                ConstantExpression cexp = (ConstantExpression) initialValueExpression;
                cexp = transformToPrimitiveConstantIfPossible(cexp);
                if (fieldNode.isFinal() && ClassHelper.isStaticConstantInitializerType(cexp.getType()) && cexp.getType().equals(fieldNode.getType())) {
                    // GROOVY-5150: primitive type constants will be initialized directly
                    return;
                }
                staticList.add(0, statement);
            } else {
                staticList.add(statement);
            }
            // to avoid double initialization in case of several constructors
            fieldNode.setInitialValueExpression(null);
            /*
                 * If it is a statement for an explicitly declared static field inside an enum, store its
                 * reference. For enums, they need to be handled differently as such init statements should
                 * come after the enum values have been initialized inside <clinit> block. GROOVY-3161.
                 */
            if (isEnumClassNode && explicitStaticPropsInEnum.contains(fieldNode.getName())) {
                initStmtsAfterEnumValuesInit.add(statement);
            }
        } else {
            list.add(statement);
        }
    }
}
Also used : BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) 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) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression)

Example 20 with FieldExpression

use of org.codehaus.groovy.ast.expr.FieldExpression in project groovy-core by groovy.

the class TraitASTTransformation method processProperty.

/**
 * Mostly copied from the {@link Verifier} class but does *not* generate bytecode
 *
 * @param cNode
 * @param node
 */
private static void processProperty(final ClassNode cNode, PropertyNode node) {
    String name = node.getName();
    FieldNode field = node.getField();
    int propNodeModifiers = node.getModifiers();
    String getterName = "get" + Verifier.capitalize(name);
    String setterName = "set" + Verifier.capitalize(name);
    // GROOVY-3726: clear volatile, transient modifiers so that they don't get applied to methods
    if ((propNodeModifiers & Modifier.VOLATILE) != 0) {
        propNodeModifiers = propNodeModifiers - Modifier.VOLATILE;
    }
    if ((propNodeModifiers & Modifier.TRANSIENT) != 0) {
        propNodeModifiers = propNodeModifiers - Modifier.TRANSIENT;
    }
    Statement getterBlock = node.getGetterBlock();
    if (getterBlock == null) {
        MethodNode getter = cNode.getGetterMethod(getterName);
        if (getter == null && ClassHelper.boolean_TYPE == node.getType()) {
            String secondGetterName = "is" + Verifier.capitalize(name);
            getter = cNode.getGetterMethod(secondGetterName);
        }
        if (!node.isPrivate() && methodNeedsReplacement(cNode, getter)) {
            getterBlock = new ExpressionStatement(new FieldExpression(field));
        }
    }
    Statement setterBlock = node.getSetterBlock();
    if (setterBlock == null) {
        // 2nd arg false below: though not usual, allow setter with non-void return type
        MethodNode setter = cNode.getSetterMethod(setterName, false);
        if (!node.isPrivate() && (propNodeModifiers & ACC_FINAL) == 0 && methodNeedsReplacement(cNode, setter)) {
            setterBlock = new ExpressionStatement(new BinaryExpression(new FieldExpression(field), Token.newSymbol(Types.EQUAL, 0, 0), new VariableExpression("value")));
        }
    }
    if (getterBlock != null) {
        MethodNode getter = new MethodNode(getterName, propNodeModifiers, node.getType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, getterBlock);
        getter.setSynthetic(true);
        cNode.addMethod(getter);
        if (ClassHelper.boolean_TYPE == node.getType() || ClassHelper.Boolean_TYPE == node.getType()) {
            String secondGetterName = "is" + Verifier.capitalize(name);
            MethodNode secondGetter = new MethodNode(secondGetterName, propNodeModifiers, node.getType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, getterBlock);
            secondGetter.setSynthetic(true);
            cNode.addMethod(secondGetter);
        }
    }
    if (setterBlock != null) {
        Parameter[] setterParameterTypes = { new Parameter(node.getType(), "value") };
        VariableExpression var = (VariableExpression) ((BinaryExpression) ((ExpressionStatement) setterBlock).getExpression()).getRightExpression();
        var.setAccessedVariable(setterParameterTypes[0]);
        MethodNode setter = new MethodNode(setterName, propNodeModifiers, ClassHelper.VOID_TYPE, setterParameterTypes, ClassNode.EMPTY_ARRAY, setterBlock);
        setter.setSynthetic(true);
        cNode.addMethod(setter);
    }
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) MethodNode(org.codehaus.groovy.ast.MethodNode) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) Parameter(org.codehaus.groovy.ast.Parameter) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression)

Aggregations

FieldExpression (org.codehaus.groovy.ast.expr.FieldExpression)28 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)17 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)17 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)16 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)15 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)15 FieldNode (org.codehaus.groovy.ast.FieldNode)13 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)13 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)13 ClassNode (org.codehaus.groovy.ast.ClassNode)12 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)11 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)10 Statement (org.codehaus.groovy.ast.stmt.Statement)10 Expression (org.codehaus.groovy.ast.expr.Expression)8 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)7 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)7 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)7 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)7 MethodNode (org.codehaus.groovy.ast.MethodNode)6 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)6