Search in sources :

Example 11 with FieldExpression

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

the class IfElseTest method testLoop.

public void testLoop() 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));
    classNode.addProperty(new PropertyNode("result", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null));
    BooleanExpression expression = new BooleanExpression(new BinaryExpression(new FieldExpression(new FieldNode("bar", ACC_PRIVATE, ClassHelper.STRING_TYPE, classNode, ConstantExpression.NULL)), Token.newSymbol("==", 0, 0), new ConstantExpression("abc")));
    Statement trueStatement = new ExpressionStatement(new BinaryExpression(new FieldExpression(new FieldNode("result", ACC_PRIVATE, ClassHelper.STRING_TYPE, classNode, ConstantExpression.NULL)), Token.newSymbol("=", 0, 0), new ConstantExpression("worked")));
    Statement falseStatement = createPrintlnStatement(new ConstantExpression("false"));
    IfStatement statement = new IfStatement(expression, trueStatement, falseStatement);
    classNode.addMethod(new MethodNode("ifDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statement));
    Class fooClass = loadClass(classNode);
    assertTrue("Loaded a new class", fooClass != null);
    Object bean = fooClass.getDeclaredConstructor().newInstance();
    assertTrue("Managed to create bean", bean != null);
    assertSetProperty(bean, "bar", "abc");
    System.out.println("################ Now about to invoke method");
    Object[] array = {};
    InvokerHelper.invokeMethod(bean, "ifDemo", array);
    System.out.println("################ Done");
    assertGetProperty(bean, "result", "worked");
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement)

Example 12 with FieldExpression

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

the class TraitReceiverTransformer method transformBinaryExpression.

private Expression transformBinaryExpression(final BinaryExpression exp, final ClassNode weavedType) {
    Expression leftExpression = exp.getLeftExpression();
    Expression rightExpression = exp.getRightExpression();
    Token operation = exp.getOperation();
    if (operation.getText().equals("=")) {
        String leftFieldName = null;
        // it's an assignment
        if (leftExpression instanceof VariableExpression && ((VariableExpression) leftExpression).getAccessedVariable() instanceof FieldNode) {
            leftFieldName = ((VariableExpression) leftExpression).getAccessedVariable().getName();
        } else if (leftExpression instanceof FieldExpression) {
            leftFieldName = ((FieldExpression) leftExpression).getFieldName();
        } else if (leftExpression instanceof PropertyExpression && (((PropertyExpression) leftExpression).isImplicitThis() || "this".equals(((PropertyExpression) leftExpression).getObjectExpression().getText()))) {
            leftFieldName = ((PropertyExpression) leftExpression).getPropertyAsString();
            FieldNode fn = tryGetFieldNode(weavedType, leftFieldName);
            if (fieldHelper == null || fn == null && !fieldHelper.hasPossibleMethod(Traits.helperSetterName(new FieldNode(leftFieldName, 0, ClassHelper.OBJECT_TYPE, weavedType, null)), rightExpression)) {
                return binX(propX(varX(weaved), leftFieldName), operation, transform(rightExpression));
            }
        }
        if (leftFieldName != null) {
            FieldNode fn = weavedType.getDeclaredField(leftFieldName);
            FieldNode staticField = tryGetFieldNode(weavedType, leftFieldName);
            if (fn == null) {
                fn = new FieldNode(leftFieldName, 0, ClassHelper.OBJECT_TYPE, weavedType, null);
            }
            Expression receiver = createFieldHelperReceiver();
            boolean isStatic = staticField != null && staticField.isStatic();
            if (fn.isStatic()) {
                // DO NOT USE isStatic variable here!
                receiver = propX(receiver, "class");
            }
            String method = Traits.helperSetterName(fn);
            MethodCallExpression mce = callX(receiver, method, args(super.transform(rightExpression)));
            mce.setImplicitThis(false);
            mce.setSourcePosition(exp);
            markDynamicCall(mce, staticField, isStatic);
            return mce;
        }
    }
    Expression leftTransform = transform(leftExpression);
    Expression rightTransform = transform(rightExpression);
    Expression ret = exp instanceof DeclarationExpression ? new DeclarationExpression(leftTransform, operation, rightTransform) : binX(leftTransform, operation, rightTransform);
    ret.setSourcePosition(exp);
    ret.copyNodeMetaData(exp);
    return ret;
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) 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) 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) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) Token(org.codehaus.groovy.syntax.Token) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression)

Example 13 with FieldExpression

use of org.codehaus.groovy.ast.expr.FieldExpression 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 14 with FieldExpression

use of org.codehaus.groovy.ast.expr.FieldExpression 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 15 with FieldExpression

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

the class TestMixinTransformation method addJunitRuleFields.

protected void addJunitRuleFields(ClassNode classNode) {
    if (classNode.getField(JUNIT_ADAPTER_FIELD_NAME) != null) {
        return;
    }
    ClassNode junitAdapterType = ClassHelper.make(TestRuntimeJunitAdapter.class);
    FieldNode junitAdapterFieldNode = classNode.addField(JUNIT_ADAPTER_FIELD_NAME, Modifier.STATIC, junitAdapterType, new ConstructorCallExpression(junitAdapterType, MethodCallExpression.NO_ARGUMENTS));
    boolean spockTest = isSpockTest(classNode);
    FieldNode staticRuleFieldNode = classNode.addField(RULE_FIELD_NAME_BASE + "StaticClassRule", Modifier.PRIVATE | Modifier.STATIC, ClassHelper.make(TestRule.class), new MethodCallExpression(new FieldExpression(junitAdapterFieldNode), "newClassRule", new ClassExpression(classNode)));
    AnnotationNode classRuleAnnotation = new AnnotationNode(ClassHelper.make(ClassRule.class));
    if (spockTest) {
        // @ClassRule must be added to @Shared field in spock
        FieldNode spockSharedRuleFieldNode = classNode.addField(RULE_FIELD_NAME_BASE + "SharedClassRule", Modifier.PUBLIC, ClassHelper.make(TestRule.class), new FieldExpression(staticRuleFieldNode));
        spockSharedRuleFieldNode.addAnnotation(classRuleAnnotation);
        spockSharedRuleFieldNode.addAnnotation(new AnnotationNode(ClassHelper.make(Shared.class)));
        addSpockFieldMetadata(spockSharedRuleFieldNode, 0);
    } else {
        staticRuleFieldNode.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
        staticRuleFieldNode.addAnnotation(classRuleAnnotation);
    }
    FieldNode ruleFieldNode = classNode.addField(RULE_FIELD_NAME_BASE + "Rule", Modifier.PUBLIC, ClassHelper.make(TestRule.class), new MethodCallExpression(new FieldExpression(junitAdapterFieldNode), "newRule", new VariableExpression("this")));
    ruleFieldNode.addAnnotation(new AnnotationNode(ClassHelper.make(Rule.class)));
    if (spockTest) {
        addSpockFieldMetadata(ruleFieldNode, 0);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) TestRule(org.junit.rules.TestRule) FieldNode(org.codehaus.groovy.ast.FieldNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ClassRule(org.junit.ClassRule) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) 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