Java Code Examples for org.codehaus.groovy.ast.expr.DeclarationExpression#getRightExpression()

The following examples show how to use org.codehaus.groovy.ast.expr.DeclarationExpression#getRightExpression() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PicocliScriptASTTransformation.java    From picocli with Apache License 2.0 6 votes vote down vote up
private void changeBaseScriptTypeFromDeclaration(final SourceUnit source, final DeclarationExpression de, final AnnotationNode node) {
    if (de.isMultipleAssignmentDeclaration()) {
        addError("Annotation " + MY_TYPE_NAME + " not supported with multiple assignment notation.", de);
        return;
    }

    ClassNode cNode = de.getDeclaringClass();
    ClassNode baseScriptType = de.getVariableExpression().getType().getPlainNodeReference();
    if (baseScriptType.isScript()) {
        if (!(de.getRightExpression() instanceof EmptyExpression)) {
            addError("Annotation " + MY_TYPE_NAME + " not supported with variable assignment.", de);
            return;
        }
        de.setRightExpression(new VariableExpression("this"));
    } else {
        baseScriptType = BASE_SCRIPT_TYPE;
    }
    Expression value = node.getMember("value");
    if (value != null) {
        addError("Annotation " + MY_TYPE_NAME + " cannot have member 'value' if used on a declaration.", value);
        return;
    }


    changeBaseScriptType(source, de, cNode, baseScriptType, node);
}
 
Example 2
Source File: ResolveVisitor.java    From groovy with Apache License 2.0 6 votes vote down vote up
protected Expression transformDeclarationExpression(final DeclarationExpression de) {
    visitAnnotations(de);
    Expression oldLeft = de.getLeftExpression();
    checkingVariableTypeInDeclaration = true;
    Expression left = transform(oldLeft);
    checkingVariableTypeInDeclaration = false;
    if (left instanceof ClassExpression) {
        ClassExpression ce = (ClassExpression) left;
        addError("you tried to assign a value to the class " + ce.getType().getName(), oldLeft);
        return de;
    }
    Expression right = transform(de.getRightExpression());
    if (right == de.getRightExpression()) {
        fixDeclaringClass(de);
        return de;
    }
    DeclarationExpression newDeclExpr = new DeclarationExpression(left, de.getOperation(), right);
    newDeclExpr.setDeclaringClass(de.getDeclaringClass());
    newDeclExpr.addAnnotations(de.getAnnotations());
    newDeclExpr.copyNodeMetaData(de);
    fixDeclaringClass(newDeclExpr);
    return newDeclExpr;
}
 
Example 3
Source File: SourceURIASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void setScriptURIOnDeclaration(final DeclarationExpression de, final AnnotationNode node) {
    if (de.isMultipleAssignmentDeclaration()) {
        addError("Annotation " + MY_TYPE_NAME + " not supported with multiple assignment notation.", de);
        return;
    }

    if (!(de.getRightExpression() instanceof EmptyExpression)) {
        addError("Annotation " + MY_TYPE_NAME + " not supported with variable assignment.", de);
        return;
    }

    URI uri = getSourceURI(node);

    if (uri == null) {
        addError("Unable to get the URI for the source of this script!", de);
    } else {
        // Set the RHS to '= URI.create("string for this URI")'.
        // That may throw an IllegalArgumentExpression wrapping the URISyntaxException.
        de.setRightExpression(getExpression(uri));
    }
}
 
Example 4
Source File: BaseScriptASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void changeBaseScriptTypeFromDeclaration(final DeclarationExpression de, final AnnotationNode node) {
    if (de.isMultipleAssignmentDeclaration()) {
        addError("Annotation " + MY_TYPE_NAME + " not supported with multiple assignment notation.", de);
        return;
    }

    if (!(de.getRightExpression() instanceof EmptyExpression)) {
        addError("Annotation " + MY_TYPE_NAME + " not supported with variable assignment.", de);
        return;
    }
    Expression value = node.getMember("value");
    if (value != null) {
        addError("Annotation " + MY_TYPE_NAME + " cannot have member 'value' if used on a declaration.", value);
        return;
    }

    ClassNode cNode = de.getDeclaringClass();
    ClassNode baseScriptType = de.getVariableExpression().getType().getPlainNodeReference();
    de.setRightExpression(new VariableExpression("this"));

    changeBaseScriptType(de, cNode, baseScriptType);
}
 
Example 5
Source File: OptimizingStatementWriter.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public void visitDeclarationExpression(final DeclarationExpression expression) {
    Expression rightExpression = expression.getRightExpression();
    rightExpression.visit(this);

    ClassNode leftType = typeChooser.resolveType(expression.getLeftExpression(), node);
    ClassNode rightType = optimizeDivWithIntOrLongTarget(rightExpression, leftType);
    if (rightType == null) rightType = typeChooser.resolveType(rightExpression, node);
    if (isPrimitiveType(leftType) && isPrimitiveType(rightType)) {
        // if right is a constant, then we optimize only if it makes a block complete, so we set a maybe
        if (rightExpression instanceof ConstantExpression) {
            opt.chainCanOptimize(true);
        } else {
            opt.chainShouldOptimize(true);
        }
        addMeta(expression).type = Optional.ofNullable(typeChooser.resolveType(expression, node)).orElse(leftType);
        opt.chainInvolvedType(leftType);
        opt.chainInvolvedType(rightType);
    }
}
 
Example 6
Source File: ASTTransformer.java    From pom-manipulation-ext with Apache License 2.0 5 votes vote down vote up
private void changeBaseScriptTypeFromDeclaration(final DeclarationExpression de, final AnnotationNode node) {
    if (de.isMultipleAssignmentDeclaration()) {
        addError( "Annotation " + getType() + " not supported with multiple assignment notation.", de);
        return;
    }

    if (!(de.getRightExpression() instanceof EmptyExpression)) {
        addError( "Annotation " + getType() + " not supported with variable assignment.", de);
        return;
    }
    Expression value = node.getMember("value");
    if (value != null) {
        addError( "Annotation " + getType() + " cannot have member 'value' if used on a declaration.", value);
        return;
    }

    ClassNode cNode = de.getDeclaringClass();
    ClassNode baseScriptType = de.getVariableExpression().getType().getPlainNodeReference();
    if (baseScriptType.isScript()) {
        if (!(de.getRightExpression() instanceof EmptyExpression)) {
            addError( "Annotation " + getType() + " not supported with variable assignment.", de);
            return;
        }
        de.setRightExpression(new VariableExpression("this"));
    } else {
        if ( type == Type.MAVEN )
        {
            baseScriptType = MAVEN_BASE_SCRIPT_TYPE;
        }
        else
        {
            baseScriptType = GRADLE_BASE_SCRIPT_TYPE;
        }
    }

    changeBaseScriptType(de, cNode, baseScriptType);
}
 
Example 7
Source File: AutoFinalASTTransformation.java    From groovy with Apache License 2.0 4 votes vote down vote up
private void processLocalVariable(DeclarationExpression de, ClassCodeVisitorSupport visitor) {
    if (!isEnabled(de)) return;
    if (de.getRightExpression() instanceof ClosureExpression) {
        visitor.visitDeclarationExpression(de);
    }
}
 
Example 8
Source File: FieldASTTransformation.java    From groovy with Apache License 2.0 4 votes vote down vote up
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);
        // provide setter for CLI Builder purposes unless final
        if (fieldNode.isFinal()) {
            if (!de.getAnnotations(OPTION_TYPE).isEmpty()) {
                addError("Can't have a final field also annotated with @" + OPTION_TYPE.getNameWithoutPackage(), de);
            }
        } else {
            String setterName = getSetterName(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);
    }
}