Search in sources :

Example 1 with DeclarationExpression

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

the class BinaryExpressionTransformer method optimizeConstantInitialization.

private static DeclarationExpression optimizeConstantInitialization(final BinaryExpression originalDeclaration, final Token operation, final ConstantExpression constant, final Expression leftExpression, final ClassNode declarationType) {
    ConstantExpression cexp = new ConstantExpression(convertConstant((Number) constant.getValue(), ClassHelper.getWrapper(declarationType)), true);
    cexp.setType(declarationType);
    cexp.setSourcePosition(constant);
    DeclarationExpression result = new DeclarationExpression(leftExpression, operation, cexp);
    result.setSourcePosition(originalDeclaration);
    result.copyNodeMetaData(originalDeclaration);
    return result;
}
Also used : ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression)

Example 2 with DeclarationExpression

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

the class FieldASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    sourceUnit = source;
    if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) {
        throw new GroovyBugError("Internal error: expecting [AnnotationNode, AnnotatedNode] but got: " + Arrays.asList(nodes));
    }
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(node.getClassNode()))
        return;
    if (parent instanceof DeclarationExpression) {
        DeclarationExpression de = (DeclarationExpression) parent;
        ClassNode cNode = de.getDeclaringClass();
        if (!cNode.isScript()) {
            addError("Annotation " + MY_TYPE_NAME + " can only be used within a Script.", parent);
            return;
        }
        candidate = de;
        // GROOVY-4548: temp fix to stop CCE until proper support is added
        if (de.isMultipleAssignmentDeclaration()) {
            addError("Annotation " + MY_TYPE_NAME + " not supported with multiple assignment notation.", parent);
            return;
        }
        VariableExpression ve = de.getVariableExpression();
        variableName = ve.getName();
        // set owner null here, it will be updated by addField
        fieldNode = new FieldNode(variableName, ve.getModifiers(), ve.getType(), null, de.getRightExpression());
        fieldNode.setSourcePosition(de);
        cNode.addField(fieldNode);
        String setterName = "set" + MetaClassHelper.capitalize(variableName);
        cNode.addMethod(setterName, ACC_PUBLIC | ACC_SYNTHETIC, ClassHelper.VOID_TYPE, params(param(ve.getType(), variableName)), ClassNode.EMPTY_ARRAY, block(stmt(assignX(propX(varX("this"), variableName), varX(variableName)))));
        // GROOVY-4833 : annotations that are not Groovy transforms should be transferred to the generated field
        // GROOVY-6112 : also copy acceptable Groovy transforms
        final List<AnnotationNode> annotations = de.getAnnotations();
        for (AnnotationNode annotation : annotations) {
            // GROOVY-6337 HACK: in case newly created field is @Lazy
            if (annotation.getClassNode().equals(LAZY_TYPE)) {
                LazyASTTransformation.visitField(this, annotation, fieldNode);
            }
            final ClassNode annotationClassNode = annotation.getClassNode();
            if (notTransform(annotationClassNode) || acceptableTransform(annotation)) {
                fieldNode.addAnnotation(annotation);
            }
        }
        super.visitClass(cNode);
        // GROOVY-5207 So that Closures can see newly added fields
        // (not super efficient for a very large class with many @Fields but we chose simplicity
        // and understandability of this solution over more complex but efficient alternatives)
        VariableScopeVisitor scopeVisitor = new VariableScopeVisitor(source);
        scopeVisitor.visitClass(cNode);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) GroovyBugError(org.codehaus.groovy.GroovyBugError) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) VariableScopeVisitor(org.codehaus.groovy.classgen.VariableScopeVisitor)

Example 3 with DeclarationExpression

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

the class SourceURIASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(node.getClassNode()))
        return;
    if (parent instanceof DeclarationExpression) {
        setScriptURIOnDeclaration((DeclarationExpression) parent, node);
    } else if (parent instanceof FieldNode) {
        setScriptURIOnField((FieldNode) parent, node);
    } else {
        addError("Expected to find the annotation " + MY_TYPE_NAME + " on an declaration statement.", parent);
    }
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression)

Example 4 with DeclarationExpression

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

the class FinalVariableAnalyzer method visitBinaryExpression.

@Override
public void visitBinaryExpression(final BinaryExpression expression) {
    boolean assignment = StaticTypeCheckingSupport.isAssignment(expression.getOperation().getType());
    boolean isDeclaration = expression instanceof DeclarationExpression;
    Expression leftExpression = expression.getLeftExpression();
    Expression rightExpression = expression.getRightExpression();
    if (isDeclaration && leftExpression instanceof VariableExpression) {
        VariableExpression var = (VariableExpression) leftExpression;
        if (Modifier.isFinal(var.getModifiers())) {
            declaredFinalVariables.add(var);
        }
    }
    leftExpression.visit(this);
    inAssignment = assignment;
    rightExpression.visit(this);
    inAssignment = false;
    if (assignment) {
        if (leftExpression instanceof Variable) {
            boolean uninitialized = isDeclaration && rightExpression == EmptyExpression.INSTANCE;
            recordAssignment((Variable) leftExpression, isDeclaration, uninitialized, false, expression);
        } else if (leftExpression instanceof TupleExpression) {
            TupleExpression te = (TupleExpression) leftExpression;
            for (Expression next : te.getExpressions()) {
                if (next instanceof Variable) {
                    recordAssignment((Variable) next, isDeclaration, false, false, next);
                }
            }
        }
    }
}
Also used : Variable(org.codehaus.groovy.ast.Variable) PrefixExpression(org.codehaus.groovy.ast.expr.PrefixExpression) PostfixExpression(org.codehaus.groovy.ast.expr.PostfixExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) 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) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 5 with DeclarationExpression

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

Aggregations

DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)31 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)24 Expression (org.codehaus.groovy.ast.expr.Expression)21 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)18 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)17 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)16 ClassNode (org.codehaus.groovy.ast.ClassNode)15 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)15 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)13 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)11 FieldNode (org.codehaus.groovy.ast.FieldNode)11 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)11 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)11 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)10 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)10 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)10 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)10 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)9 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)9 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)9