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

The following examples show how to use org.codehaus.groovy.ast.expr.DeclarationExpression#isMultipleAssignmentDeclaration() . 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: VariableFinderVisitor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void visitDeclarationExpression(DeclarationExpression expression) {
    // if we are in the same block we check position, if it occurs after
    // current position we ignore it
    if (blocks.isEmpty()
            && expression.getLineNumber() >= 0 && expression.getColumnNumber() >= 0
            && path.getLineNumber() >= 0 && path.getColumnNumber() >= 0
            && (expression.getLineNumber() > path.getLineNumber()
            || (expression.getLineNumber() == path.getLineNumber() && expression.getColumnNumber() >= path.getColumnNumber()))) {
        return;
    }

    if (!expression.isMultipleAssignmentDeclaration()) {
        VariableExpression variableExpression = expression.getVariableExpression();
        if (variableExpression.getAccessedVariable() != null) {
            String name = variableExpression.getAccessedVariable().getName();
            variables.put(name, variableExpression.getAccessedVariable());
        }
    }
    // perhaps we could visit just declaration or do nothing
    super.visitDeclarationExpression(expression);
}
 
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: ClassCompletionVerifier.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public void visitDeclarationExpression(DeclarationExpression expression) {
    super.visitDeclarationExpression(expression);
    if (expression.isMultipleAssignmentDeclaration()) return;
    checkInvalidDeclarationModifier(expression, ACC_ABSTRACT, "abstract");
    checkInvalidDeclarationModifier(expression, ACC_NATIVE, "native");
    checkInvalidDeclarationModifier(expression, ACC_PRIVATE, "private");
    checkInvalidDeclarationModifier(expression, ACC_PROTECTED, "protected");
    checkInvalidDeclarationModifier(expression, ACC_PUBLIC, "public");
    checkInvalidDeclarationModifier(expression, ACC_STATIC, "static");
    checkInvalidDeclarationModifier(expression, ACC_STRICT, "strictfp");
    checkInvalidDeclarationModifier(expression, ACC_SYNCHRONIZED, "synchronized");
    checkInvalidDeclarationModifier(expression, ACC_TRANSIENT, "transient");
    checkInvalidDeclarationModifier(expression, ACC_VOLATILE, "volatile");
    if (expression.getVariableExpression().getOriginType().equals(VOID_TYPE)) {
        addError("The variable '" + expression.getVariableExpression().getName() + "' has invalid type void", expression);
    }
}
 
Example 6
Source File: FindTypeUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static OffsetRange getDeclarationExpressionRange(DeclarationExpression expression, BaseDocument doc, int cursorOffset) {
    OffsetRange range;
    if (!expression.isMultipleAssignmentDeclaration()) {
        range = getVariableRange(expression.getVariableExpression(), doc, cursorOffset);
    } else {
        range = getRange(expression.getTupleExpression(), doc, cursorOffset);
    }
    
    return range;
}
 
Example 7
Source File: VariableScopeVisitor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void visitDeclarationExpression(DeclarationExpression expression) {
    ClassNode visitedType;
    if (!expression.isMultipleAssignmentDeclaration()) {
        visitedType = expression.getVariableExpression().getType();
    } else {
        visitedType = expression.getTupleExpression().getType();
    }

    if (FindTypeUtils.isCaretOnClassNode(path, doc, cursorOffset)) {
        addOccurrences(visitedType, (ClassNode) FindTypeUtils.findCurrentNode(path, doc, cursorOffset));
    }
    super.visitDeclarationExpression(expression);
}
 
Example 8
Source File: VariableScopeVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visitDeclarationExpression(final DeclarationExpression expression) {
    visitAnnotations(expression);
    // visit right side first to prevent the use of a variable before its declaration
    expression.getRightExpression().visit(this);

    if (expression.isMultipleAssignmentDeclaration()) {
        TupleExpression list = expression.getTupleExpression();
        for (Expression listExpression : list.getExpressions()) {
            declare((VariableExpression) listExpression);
        }
    } else {
        declare(expression.getVariableExpression());
    }
}
 
Example 9
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 10
Source File: GroovyASTUtils.java    From groovy-language-server with Apache License 2.0 4 votes vote down vote up
public static ASTNode getDefinition(ASTNode node, boolean strict, ASTNodeVisitor astVisitor) {
    if (node == null) {
        return null;
    }
    ASTNode parentNode = astVisitor.getParent(node);
    if (node instanceof ExpressionStatement) {
        ExpressionStatement statement = (ExpressionStatement) node;
        node = statement.getExpression();
    }
    if (node instanceof ClassNode) {
        return tryToResolveOriginalClassNode((ClassNode) node, strict, astVisitor);
    } else if (node instanceof ConstructorCallExpression) {
        ConstructorCallExpression callExpression = (ConstructorCallExpression) node;
        return GroovyASTUtils.getMethodFromCallExpression(callExpression, astVisitor);
    } else if (node instanceof DeclarationExpression) {
        DeclarationExpression declExpression = (DeclarationExpression) node;
        if (!declExpression.isMultipleAssignmentDeclaration()) {
            ClassNode originType = declExpression.getVariableExpression().getOriginType();
            return tryToResolveOriginalClassNode(originType, strict, astVisitor);
        }
    } else if (node instanceof ClassExpression) {
        ClassExpression classExpression = (ClassExpression) node;
        return tryToResolveOriginalClassNode(classExpression.getType(), strict, astVisitor);
    } else if (node instanceof ImportNode) {
        ImportNode importNode = (ImportNode) node;
        return tryToResolveOriginalClassNode(importNode.getType(), strict, astVisitor);
    } else if (node instanceof MethodNode) {
        return node;
    } else if (node instanceof ConstantExpression && parentNode != null) {
        if (parentNode instanceof MethodCallExpression) {
            MethodCallExpression methodCallExpression = (MethodCallExpression) parentNode;
            return GroovyASTUtils.getMethodFromCallExpression(methodCallExpression, astVisitor);
        } else if (parentNode instanceof PropertyExpression) {
            PropertyExpression propertyExpression = (PropertyExpression) parentNode;
            PropertyNode propNode = GroovyASTUtils.getPropertyFromExpression(propertyExpression, astVisitor);
            if (propNode != null) {
                return propNode;
            }
            return GroovyASTUtils.getFieldFromExpression(propertyExpression, astVisitor);
        }
    } else if (node instanceof VariableExpression) {
        VariableExpression variableExpression = (VariableExpression) node;
        Variable accessedVariable = variableExpression.getAccessedVariable();
        if (accessedVariable instanceof ASTNode) {
            return (ASTNode) accessedVariable;
        }
        // DynamicVariable is not an ASTNode, so skip it
        return null;
    } else if (node instanceof Variable) {
        return node;
    }
    return null;
}
 
Example 11
Source File: ElementUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Returns type for the given ASTNode. For example if FieldNode is passed
 * as a parameter, it returns type of the given field etc. If the Method call
 * is passed as a parameter, the method tried to interfere proper type and return it
 *
 * @param node where we want to know declared type
 * @return type of the given node
 * @throws IllegalStateException if an implementation is missing for the given ASTNode type
 */
public static ClassNode getType(ASTNode node) {
    if (node instanceof FakeASTNode) {
        node = ((FakeASTNode) node).getOriginalNode();
    }

    if (node instanceof ClassNode) {
        ClassNode clazz = ((ClassNode) node);
        if (clazz.getComponentType() != null) {
            return clazz.getComponentType();
        } else {
            return clazz;
        }
    } else if (node instanceof AnnotationNode) {
        return ((AnnotationNode) node).getClassNode();
    } else if (node instanceof FieldNode) {
        return ((FieldNode) node).getType();
    } else if (node instanceof PropertyNode) {
        return ((PropertyNode) node).getType();
    } else if (node instanceof MethodNode) {
        return ((MethodNode) node).getReturnType();
    } else if (node instanceof Parameter) {
       return ((Parameter) node).getType();
    } else if (node instanceof ForStatement) {
        return ((ForStatement) node).getVariableType();
    } else if (node instanceof CatchStatement) {
        return ((CatchStatement) node).getVariable().getOriginType();
    } else if (node instanceof ImportNode) {
        return ((ImportNode) node).getType();
    } else if (node instanceof ClassExpression) {
        return ((ClassExpression) node).getType();
    } else if (node instanceof VariableExpression) {
        return ((VariableExpression) node).getType();
    } else if (node instanceof DeclarationExpression) {
        DeclarationExpression declaration = ((DeclarationExpression) node);
        if (declaration.isMultipleAssignmentDeclaration()) {
            return declaration.getTupleExpression().getType();
        } else {
            return declaration.getVariableExpression().getType();
        }
    } else if (node instanceof ConstructorCallExpression) {
        return ((ConstructorCallExpression) node).getType();
    } else if (node instanceof ArrayExpression) {
        return ((ArrayExpression) node).getElementType();
    }
    throw new IllegalStateException("Not implemented yet - GroovyRefactoringElement.getType() needs to be improve!"); // NOI18N
}
 
Example 12
Source File: ElementUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static String getNameWithoutPackage(ASTNode node) {
    if (node instanceof FakeASTNode) {
        node = ((FakeASTNode) node).getOriginalNode();
    }

    String name = null;
    if (node instanceof ClassNode) {
        name = ((ClassNode) node).getNameWithoutPackage();
    } else if (node instanceof AnnotationNode) {
        return ((AnnotationNode) node).getText();
    } else if (node instanceof MethodNode) {
        name = ((MethodNode) node).getName();
        if ("<init>".equals(name)) { // NOI18N
            name = getDeclaringClassNameWithoutPackage(node);
        }
    } else if (node instanceof FieldNode) {
        name = ((FieldNode) node).getName();
    } else if (node instanceof PropertyNode) {
        name = ((PropertyNode) node).getName();
    } else if (node instanceof Parameter) {
        name = ((Parameter) node).getName();
    } else if (node instanceof ForStatement) {
        name = ((ForStatement) node).getVariableType().getNameWithoutPackage();
    } else if (node instanceof CatchStatement) {
        name = ((CatchStatement) node).getVariable().getName();
    } else if (node instanceof ImportNode) {
        name = ((ImportNode) node).getType().getNameWithoutPackage();
    } else if (node instanceof ClassExpression) {
        name = ((ClassExpression) node).getType().getNameWithoutPackage();
    } else if (node instanceof VariableExpression) {
        name = ((VariableExpression) node).getName();
    } else if (node instanceof DeclarationExpression) {
        DeclarationExpression declaration = ((DeclarationExpression) node);
        if (declaration.isMultipleAssignmentDeclaration()) {
            name = declaration.getTupleExpression().getType().getNameWithoutPackage();
        } else {
            name = declaration.getVariableExpression().getType().getNameWithoutPackage();
        }
    } else if (node instanceof ConstantExpression) {
        name = ((ConstantExpression) node).getText();
    } else if (node instanceof MethodCallExpression) {
        name = ((MethodCallExpression) node).getMethodAsString();
    } else if (node instanceof ConstructorCallExpression) {
        name = ((ConstructorCallExpression) node).getType().getNameWithoutPackage();
    } else if (node instanceof ArrayExpression) {
        name = ((ArrayExpression) node).getElementType().getNameWithoutPackage();
    }


    if (name != null) {
        return normalizeTypeName(name, null);
    }
    throw new IllegalStateException("Not implemented yet - GroovyRefactoringElement.getName() needs to be improve for type: " + node.getClass().getSimpleName()); // NOI18N
}
 
Example 13
Source File: ElementUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static ClassNode getDeclaringClass(ASTNode node) {
    if (node instanceof ClassNode) {
        return (ClassNode) node;
    } else if (node instanceof AnnotationNode) {
        return ((AnnotationNode) node).getClassNode();
    } else if (node instanceof MethodNode) {
        return ((MethodNode) node).getDeclaringClass();
    } else if (node instanceof FieldNode) {
        return ((FieldNode) node).getDeclaringClass();
    } else if (node instanceof PropertyNode) {
        return ((PropertyNode) node).getDeclaringClass();
    } else if (node instanceof Parameter) {
        return ((Parameter) node).getDeclaringClass();
    } else if (node instanceof ForStatement) {
        return ((ForStatement) node).getVariableType().getDeclaringClass();
    } else if (node instanceof CatchStatement) {
        return ((CatchStatement) node).getVariable().getDeclaringClass();
    } else if (node instanceof ImportNode) {
        return ((ImportNode) node).getDeclaringClass();
    } else if (node instanceof ClassExpression) {
        return ((ClassExpression) node).getType().getDeclaringClass();
    } else if (node instanceof VariableExpression) {
        return ((VariableExpression) node).getDeclaringClass();
    } else if (node instanceof DeclarationExpression) {
        DeclarationExpression declaration = ((DeclarationExpression) node);
        if (declaration.isMultipleAssignmentDeclaration()) {
            return declaration.getTupleExpression().getDeclaringClass();
        } else {
            return declaration.getVariableExpression().getDeclaringClass();
        }
    } else if (node instanceof ConstantExpression) {
        return ((ConstantExpression) node).getDeclaringClass();
    } else if (node instanceof MethodCallExpression) {
        return ((MethodCallExpression) node).getType();
    } else if (node instanceof ConstructorCallExpression) {
        return ((ConstructorCallExpression) node).getType();
    } else if (node instanceof ArrayExpression) {
        return ((ArrayExpression) node).getDeclaringClass();
    }

    throw new IllegalStateException("Not implemented yet - GroovyRefactoringElement.getDeclaringClass() ..looks like the type: " + node.getClass().getName() + " isn't handled at the moment!"); // NOI18N
}
 
Example 14
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);
    }
}