Search in sources :

Example 21 with FieldExpression

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

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 createAssignmentToField(rightExpression, operation, leftFieldName);
            }
        }
        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 = new PropertyExpression(receiver, "class");
            }
            String method = Traits.helperSetterName(fn);
            MethodCallExpression mce = new MethodCallExpression(receiver, method, new ArgumentListExpression(super.transform(rightExpression)));
            mce.setSourcePosition(exp);
            mce.setImplicitThis(false);
            markDynamicCall(mce, staticField, isStatic);
            return mce;
        }
    }
    Expression leftTransform = transform(leftExpression);
    Expression rightTransform = transform(rightExpression);
    Expression ret = exp instanceof DeclarationExpression ? new DeclarationExpression(leftTransform, operation, rightTransform) : new BinaryExpression(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) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) 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) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) 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 22 with FieldExpression

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

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.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 23 with FieldExpression

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

the class ClassNode method positionStmtsAfterEnumInitStmts.

public void positionStmtsAfterEnumInitStmts(List<Statement> staticFieldStatements) {
    MethodNode constructor = getOrAddStaticConstructorNode();
    Statement statement = constructor.getCode();
    if (statement instanceof BlockStatement) {
        BlockStatement block = (BlockStatement) statement;
        // add given statements for explicitly declared static fields just after enum-special fields
        // are found - the $VALUES binary expression marks the end of such fields.
        List<Statement> blockStatements = block.getStatements();
        ListIterator<Statement> litr = blockStatements.listIterator();
        while (litr.hasNext()) {
            Statement stmt = litr.next();
            if (stmt instanceof ExpressionStatement && ((ExpressionStatement) stmt).getExpression() instanceof BinaryExpression) {
                BinaryExpression bExp = (BinaryExpression) ((ExpressionStatement) stmt).getExpression();
                if (bExp.getLeftExpression() instanceof FieldExpression) {
                    FieldExpression fExp = (FieldExpression) bExp.getLeftExpression();
                    if (fExp.getFieldName().equals("$VALUES")) {
                        for (Statement tmpStmt : staticFieldStatements) {
                            litr.add(tmpStmt);
                        }
                    }
                }
            }
        }
    }
}
Also used : BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression)

Example 24 with FieldExpression

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

the class TraitReceiverTransformer method transform.

@Override
public Expression transform(final Expression exp) {
    ClassNode weavedType = weaved.getOriginType();
    if (exp instanceof BinaryExpression) {
        return transformBinaryExpression((BinaryExpression) exp, weavedType);
    } else if (exp instanceof StaticMethodCallExpression) {
        StaticMethodCallExpression call = (StaticMethodCallExpression) exp;
        ClassNode ownerType = call.getOwnerType();
        if (traitClass.equals(ownerType)) {
            MethodCallExpression mce = callX(varX(weaved), call.getMethod(), transform(call.getArguments()));
            mce.setSafe(false);
            mce.setSpreadSafe(false);
            mce.setImplicitThis(false);
            mce.setSourcePosition(exp);
            return mce;
        }
    } else if (exp instanceof MethodCallExpression) {
        MethodCallExpression mce = (MethodCallExpression) exp;
        String obj = mce.getObjectExpression().getText();
        if (mce.isImplicitThis() || "this".equals(obj)) {
            return transformMethodCallOnThis(mce);
        } else if ("super".equals(obj)) {
            return transformSuperMethodCall(mce);
        }
    } else if (exp instanceof FieldExpression) {
        FieldNode fn = ((FieldExpression) exp).getField();
        return transformFieldReference(exp, fn, fn.isStatic());
    } else if (exp instanceof VariableExpression) {
        VariableExpression vexp = (VariableExpression) exp;
        Variable accessedVariable = vexp.getAccessedVariable();
        if (accessedVariable instanceof FieldNode || accessedVariable instanceof PropertyNode) {
            if (knownFields.contains(vexp.getName())) {
                boolean isStatic = Modifier.isStatic(accessedVariable.getModifiers());
                return transformFieldReference(exp, accessedVariable instanceof FieldNode ? (FieldNode) accessedVariable : ((PropertyNode) accessedVariable).getField(), isStatic);
            } else {
                PropertyExpression propertyExpression = propX(varX(weaved), vexp.getName());
                propertyExpression.getProperty().setSourcePosition(exp);
                return propertyExpression;
            }
        } else if (accessedVariable instanceof DynamicVariable && !inClosure) {
            // GROOVY-9386
            PropertyExpression propertyExpression = propX(varX(weaved), vexp.getName());
            propertyExpression.getProperty().setSourcePosition(exp);
            return propertyExpression;
        }
        if (vexp.isThisExpression()) {
            VariableExpression variableExpression = varX(weaved);
            variableExpression.setSourcePosition(exp);
            return variableExpression;
        }
        if (vexp.isSuperExpression()) {
            throwSuperError(vexp);
        }
    } else if (exp instanceof PropertyExpression) {
        PropertyExpression pexp = (PropertyExpression) exp;
        String obj = pexp.getObjectExpression().getText();
        if (pexp.isImplicitThis() || "this".equals(obj)) {
            String propName = pexp.getPropertyAsString();
            if (knownFields.contains(propName)) {
                FieldNode fn = new FieldNode(propName, 0, ClassHelper.OBJECT_TYPE, weavedType, null);
                return transformFieldReference(exp, fn, false);
            }
        }
    } else if (exp instanceof ClosureExpression) {
        MethodCallExpression mce = callX(exp, "rehydrate", args(varX(weaved), varX(weaved), varX(weaved)));
        mce.setImplicitThis(false);
        mce.setSourcePosition(exp);
        boolean oldInClosure = inClosure;
        inClosure = true;
        ((ClosureExpression) exp).getCode().visit(this);
        inClosure = oldInClosure;
        // The rewrite we do is causing some troubles with type checking, which will
        // not be able to perform closure parameter type inference
        // so we store the replacement, which will be done *after* type checking.
        exp.putNodeMetaData(TraitASTTransformation.POST_TYPECHECKING_REPLACEMENT, mce);
        return exp;
    }
    // TODO: unary expressions (field++, field+=, ...)
    return super.transform(exp);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) Variable(org.codehaus.groovy.ast.Variable) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) FieldNode(org.codehaus.groovy.ast.FieldNode) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) PropertyNode(org.codehaus.groovy.ast.PropertyNode) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 25 with FieldExpression

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

the class Verifier method moveOptimizedConstantsInitialization.

private static boolean moveOptimizedConstantsInitialization(final ClassNode node) {
    if (node.isInterface() && !Traits.isTrait(node))
        return false;
    String name = SWAP_INIT;
    int mods = ACC_STATIC | ACC_SYNTHETIC | ACC_PUBLIC;
    BlockStatement methodCode = block(new SwapInitStatement());
    boolean swapInitRequired = false;
    for (FieldNode fn : node.getFields()) {
        if (!fn.isStatic() || !fn.isSynthetic() || !fn.getName().startsWith("$const$"))
            continue;
        if (fn.getInitialExpression() == null)
            continue;
        final FieldExpression fe = fieldX(fn);
        if (fn.getType().equals(ClassHelper.REFERENCE_TYPE))
            fe.setUseReferenceDirectly(true);
        ConstantExpression init = (ConstantExpression) fn.getInitialExpression();
        init = constX(init.getValue(), true);
        Statement statement = stmt(binX(fe, Token.newSymbol(Types.ASSIGN, fn.getLineNumber(), fn.getColumnNumber()), init));
        fn.setInitialValueExpression(null);
        methodCode.addStatement(statement);
        swapInitRequired = true;
    }
    if (swapInitRequired) {
        node.addSyntheticMethod(name, mods, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, methodCode);
    }
    return swapInitRequired;
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) 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