org.codehaus.groovy.ast.expr.DeclarationExpression Java Examples

The following examples show how to use org.codehaus.groovy.ast.expr.DeclarationExpression. 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: 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 #2
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 #3
Source File: TryWithResourcesASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
private ExpressionStatement makeVariableDeclarationFinal(ExpressionStatement variableDeclaration) {
    if (!asBoolean(variableDeclaration)) {
        return variableDeclaration;
    }

    if (!(variableDeclaration.getExpression() instanceof DeclarationExpression)) {
        throw new IllegalArgumentException("variableDeclaration is not a declaration statement");
    }

    DeclarationExpression declarationExpression = (DeclarationExpression) variableDeclaration.getExpression();
    if (!(declarationExpression.getLeftExpression() instanceof VariableExpression)) {
        throw astBuilder.createParsingFailedException("The expression statement is not a variable delcaration statement", variableDeclaration);
    }

    VariableExpression variableExpression = (VariableExpression) declarationExpression.getLeftExpression();
    variableExpression.setModifiers(variableExpression.getModifiers() | Opcodes.ACC_FINAL);

    return variableDeclaration;
}
 
Example #4
Source File: GroovydocVisitor.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public void visitDeclarationExpression(DeclarationExpression expression) {
    if (currentClassDoc.isScript()) {
        if (hasAnno(expression, "Field")) {
            VariableExpression varx = expression.getVariableExpression();
            SimpleGroovyFieldDoc field = new SimpleGroovyFieldDoc(varx.getName(), currentClassDoc);
            field.setType(new SimpleGroovyType(makeType(varx.getType())));
            int mods = varx.getModifiers();
            processModifiers(field, varx, mods);
            boolean isProp = (mods & (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED)) == 0;
            if (isProp) {
                currentClassDoc.addProperty(field);
            } else {
                currentClassDoc.add(field);
            }
        }
    }
    super.visitDeclarationExpression(expression);
}
 
Example #5
Source File: NewifyASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
public void visit(ASTNode[] nodes, SourceUnit source) {
    this.source = source;
    if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) {
        internalError("Expecting [AnnotationNode, AnnotatedClass] but got: " + Arrays.asList(nodes));
    }

    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(node.getClassNode())) {
        internalError("Transformation called from wrong annotation: " + node.getClassNode().getName());
    }

    final boolean autoFlag = determineAutoFlag(node.getMember("auto"));
    final Expression classNames = node.getMember("value");
    final Pattern cnPattern = determineClassNamePattern(node.getMember("pattern"));

    if (parent instanceof ClassNode) {
        newifyClass((ClassNode) parent, autoFlag, determineClasses(classNames, false), cnPattern);
    } else if (parent instanceof MethodNode || parent instanceof FieldNode) {
        newifyMethodOrField(parent, autoFlag, determineClasses(classNames, false), cnPattern);
    } else if (parent instanceof DeclarationExpression) {
        newifyDeclaration((DeclarationExpression) parent, autoFlag, determineClasses(classNames, true), cnPattern);
    }
}
 
Example #6
Source File: NewifyASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void newifyDeclaration(DeclarationExpression de, boolean autoFlag, ListExpression list, final Pattern cnPattern) {
    ClassNode cNode = de.getDeclaringClass();
    candidate = de;
    final ListExpression oldClassesToNewify = classesToNewify;
    final boolean oldAuto = auto;
    final Pattern oldCnPattern = classNamePattern;

    classesToNewify = list;
    auto = autoFlag;
    classNamePattern = cnPattern;

    super.visitClass(cNode);

    classesToNewify = oldClassesToNewify;
    auto = oldAuto;
    classNamePattern = oldCnPattern;
}
 
Example #7
Source File: GenericsUtils.java    From groovy with Apache License 2.0 6 votes vote down vote up
public static ClassNode[] parseClassNodesFromString(final String option, final SourceUnit sourceUnit, final CompilationUnit compilationUnit, final MethodNode mn, final ASTNode usage) {
    try {
        ModuleNode moduleNode = ParserPlugin.buildAST("Dummy<" + option + "> dummy;", compilationUnit.getConfiguration(), compilationUnit.getClassLoader(), null);
        DeclarationExpression dummyDeclaration = (DeclarationExpression) ((ExpressionStatement) moduleNode.getStatementBlock().getStatements().get(0)).getExpression();

        // the returned node is DummyNode<Param1, Param2, Param3, ...)
        ClassNode dummyNode = dummyDeclaration.getLeftExpression().getType();
        GenericsType[] dummyNodeGenericsTypes = dummyNode.getGenericsTypes();
        if (dummyNodeGenericsTypes == null) {
            return null;
        }
        ClassNode[] signature = new ClassNode[dummyNodeGenericsTypes.length];
        for (int i = 0, n = dummyNodeGenericsTypes.length; i < n; i += 1) {
            final GenericsType genericsType = dummyNodeGenericsTypes[i];
            signature[i] = resolveClassNode(sourceUnit, compilationUnit, mn, usage, genericsType.getType());
        }
        return signature;
    } catch (Exception | LinkageError e) {
        sourceUnit.addError(new IncorrectTypeHintException(mn, e, usage.getLineNumber(), usage.getColumnNumber()));
    }
    return null;
}
 
Example #8
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 #9
Source File: FinalVariableAnalyzer.java    From groovy with Apache License 2.0 6 votes vote down vote up
@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) {
        recordFinalVars(leftExpression);
    }
    // visit RHS first for expressions like a = b = 0
    inAssignmentRHS = assignment;
    rightExpression.visit(this);
    inAssignmentRHS = false;
    leftExpression.visit(this);
    if (assignment) {
        recordAssignments(expression, isDeclaration, leftExpression, rightExpression);
    }
}
 
Example #10
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 #11
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 #12
Source File: ASTTransformer.java    From pom-manipulation-ext with Apache License 2.0 6 votes vote down vote up
public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    if (MAVEN_TYPE.equals( node.getClassNode()) || GRADLE_TYPE.equals( node.getClassNode() ))
    {
        type = ( MAVEN_TYPE.equals( node.getClassNode()) ) ? Type.MAVEN : Type.GRADLE;

        if ( parent instanceof DeclarationExpression )
        {
            changeBaseScriptTypeFromDeclaration( (DeclarationExpression) parent, node );
        }
        else if ( parent instanceof ImportNode || parent instanceof PackageNode )
        {
            changeBaseScriptTypeFromPackageOrImport( source, parent, node );
        }
        else if ( parent instanceof ClassNode )
        {
            changeBaseScriptTypeFromClass( (ClassNode) parent );
        }
    }
}
 
Example #13
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 #14
Source File: UnknownElementsIndexerTest.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void should_not_add_to_unknonwn_variables_a_variable_declared_but_not_in_the_process_scope() throws Exception {
    final BonitaScriptGroovyCompilationUnit groovyCompilationUnit = mock(BonitaScriptGroovyCompilationUnit.class, RETURNS_DEEP_STUBS);

    final List<Statement> statements = new ArrayList<Statement>();
    statements.add(new ExpressionStatement(
            new DeclarationExpression(new VariableExpression("declaredVar"), Token.NULL, new VariableExpression("something"))));
    statements.add(new ReturnStatement(new VariableExpression("declaredVar")));
    final VariableScope variableScope = new VariableScope();
    variableScope.putDeclaredVariable(new VariableExpression("declaredVar"));
    final BlockStatement blockStatement = new BlockStatement(statements, variableScope);

    when(groovyCompilationUnit.getModuleNode().getStatementBlock()).thenReturn(blockStatement);
    final UnknownElementsIndexer unknownElementsIndexer = new UnknownElementsIndexer(groovyCompilationUnit);

    unknownElementsIndexer.run(new NullProgressMonitor());

    assertThat(unknownElementsIndexer.getUnknownVaraibles()).isEmpty();
}
 
Example #15
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 #16
Source File: UnknownElementsIndexerTest.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void should_add_to_overriden_variables_a_variable_declared_and_already_in_the_process_scope() throws Exception {
    final BonitaScriptGroovyCompilationUnit groovyCompilationUnit = mock(BonitaScriptGroovyCompilationUnit.class, RETURNS_DEEP_STUBS);
    Map<String, ScriptVariable> context = new HashMap<String, ScriptVariable>();
    context.put("declaredVar", null);
    when(groovyCompilationUnit.getContext()).thenReturn(context);
    
    final List<Statement> statements = new ArrayList<Statement>();
    statements.add(new ExpressionStatement(
            new DeclarationExpression(new VariableExpression("declaredVar"), Token.NULL, new VariableExpression("something"))));
    statements.add(new ReturnStatement(new VariableExpression("declaredVar")));
    final VariableScope variableScope = new VariableScope();
    variableScope.putDeclaredVariable(new VariableExpression("declaredVar"));
    final BlockStatement blockStatement = new BlockStatement(statements, variableScope);

    when(groovyCompilationUnit.getModuleNode().getStatementBlock()).thenReturn(blockStatement);
    final UnknownElementsIndexer unknownElementsIndexer = new UnknownElementsIndexer(groovyCompilationUnit);

    unknownElementsIndexer.run(new NullProgressMonitor());

    assertThat(unknownElementsIndexer.getOverridenVariables()).containsExactly(entry("declaredVar", new Position(0)));
}
 
Example #17
Source File: SourceURIASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example #18
Source File: AutoFinalASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void process(ASTNode[] nodes, final ClassCodeVisitorSupport visitor) {
    candidate = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(node.getClassNode())) return;

    if (candidate instanceof ClassNode) {
        processClass((ClassNode) candidate, visitor);
    } else if (candidate instanceof MethodNode) {
        processConstructorOrMethod((MethodNode) candidate, visitor);
    } else if (candidate instanceof FieldNode) {
        processField((FieldNode) candidate, visitor);
    } else if (candidate instanceof DeclarationExpression) {
        processLocalVariable((DeclarationExpression) candidate, visitor);
    }
}
 
Example #19
Source File: PicocliScriptASTTransformation.java    From picocli with Apache License 2.0 5 votes vote down vote up
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) {
        changeBaseScriptTypeFromDeclaration(source, (DeclarationExpression) parent, node);
    } else if (parent instanceof ImportNode || parent instanceof PackageNode) {
        changeBaseScriptTypeFromPackageOrImport(source, parent, node);
    } else if (parent instanceof ClassNode) {
        changeBaseScriptTypeFromClass(source, (ClassNode) parent, node);
    }
}
 
Example #20
Source File: BaseScriptASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
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) {
        changeBaseScriptTypeFromDeclaration((DeclarationExpression) parent, node);
    } else if (parent instanceof ImportNode || parent instanceof PackageNode) {
        changeBaseScriptTypeFromPackageOrImport(source, parent, node);
    } else if (parent instanceof ClassNode) {
        changeBaseScriptTypeFromClass((ClassNode) parent, node);
    }
}
 
Example #21
Source File: BinaryExpressionTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static DeclarationExpression optimizeConstantInitialization(final BinaryExpression originalDeclaration, final Token operation, final ConstantExpression constant, final Expression leftExpression, final ClassNode declarationType) {
    Expression cexp = constX(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;
}
 
Example #22
Source File: FieldASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public Expression transform(Expression expr) {
    if (expr == null) return null;
    if (expr instanceof DeclarationExpression) {
        DeclarationExpression de = (DeclarationExpression) expr;
        if (de.getLeftExpression() == candidate.getLeftExpression()) {
            if (insideScriptBody) {
                // TODO make EmptyExpression work
                // partially works but not if only thing in script
                // return EmptyExpression.INSTANCE;
                return nullX();
            }
            addError("Annotation " + MY_TYPE_NAME + " can only be used within a Script body.", expr);
            return expr;
        }
    } else if (insideScriptBody && expr instanceof VariableExpression && currentClosure != null) {
        VariableExpression ve = (VariableExpression) expr;
        if (ve.getName().equals(variableName)) {
            adjustToClassVar(ve);
            return ve;
        }
    } else if (currentAIC != null && expr instanceof ArgumentListExpression) {
        // if a match is found, the compiler will have already set up aic constructor to hav
        // an argument which isn't needed since we'll be accessing the field; we must undo it
        Expression skip = null;
        List<Expression> origArgList = ((ArgumentListExpression) expr).getExpressions();
        for (int i = 0; i < origArgList.size(); i++) {
            Expression arg = origArgList.get(i);
            if (matchesCandidate(arg)) {
                skip = arg;
                adjustConstructorAndFields(i, currentAIC.getType());
                break;
            }
        }
        if (skip != null) {
            return adjustedArgList(skip, origArgList);
        }
    }
    return expr.transformExpression(this);
}
 
Example #23
Source File: TryCatchStatement.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void addResource(ExpressionStatement resourceStatement) {
    Expression resourceExpression = resourceStatement.getExpression();
    if (!(resourceExpression instanceof DeclarationExpression || resourceExpression instanceof VariableExpression)) {
        throw new GroovyBugError("resourceStatement should be a variable declaration statement or a variable");
    }

    resourceExpression.putNodeMetaData(IS_RESOURCE, Boolean.TRUE);

    resourceStatements.add(resourceStatement);
}
 
Example #24
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 #25
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 #26
Source File: VariablesVisitorTest.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void should_add_decalred_expression_with_position() throws Exception {
    final VariablesVisitor variablesVisitor = new VariablesVisitor(new VariableScope());

    final DeclarationExpression declarationExpression = new DeclarationExpression(new VariableExpression("myVar"), Token.NULL,
            new VariableExpression("anotherVar"));
    declarationExpression.setStart(42);
    variablesVisitor.visitDeclarationExpression(declarationExpression);

    assertThat(variablesVisitor.getDeclaredExpressions()).containsExactly(entry("myVar", new Position(42)));
}
 
Example #27
Source File: ResolveVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public Expression transform(final Expression exp) {
    if (exp == null) return null;
    Expression ret;
    if (exp instanceof VariableExpression) {
        ret = transformVariableExpression((VariableExpression) exp);
    } else if (exp.getClass() == PropertyExpression.class) {
        ret = transformPropertyExpression((PropertyExpression) exp);
    } else if (exp instanceof DeclarationExpression) {
        ret = transformDeclarationExpression((DeclarationExpression) exp);
    } else if (exp instanceof BinaryExpression) {
        ret = transformBinaryExpression((BinaryExpression) exp);
    } else if (exp instanceof MethodCallExpression) {
        ret = transformMethodCallExpression((MethodCallExpression) exp);
    } else if (exp instanceof ClosureExpression) {
        ret = transformClosureExpression((ClosureExpression) exp);
    } else if (exp instanceof ConstructorCallExpression) {
        ret = transformConstructorCallExpression((ConstructorCallExpression) exp);
    } else if (exp instanceof AnnotationConstantExpression) {
        ret = transformAnnotationConstantExpression((AnnotationConstantExpression) exp);
    } else {
        resolveOrFail(exp.getType(), exp);
        ret = exp.transformExpression(this);
    }
    if (ret != null && ret != exp) {
        ret.setSourcePosition(exp);
    }
    return ret;
}
 
Example #28
Source File: MarkupBuilderCodeTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void extractModelTypesFromStatement(final Statement code, final Map<String, ClassNode> model) {
    if (code instanceof BlockStatement) {
        BlockStatement block = (BlockStatement) code;
        for (Statement statement : block.getStatements()) {
            extractModelTypesFromStatement(statement, model);
        }
    } else if (code instanceof ExpressionStatement) {
        Expression expression = ((ExpressionStatement) code).getExpression();
        if (expression instanceof DeclarationExpression) {
            VariableExpression var = ((DeclarationExpression) expression).getVariableExpression();
            model.put(var.getName(), var.getOriginType());
        }
    }
}
 
Example #29
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 #30
Source File: GroovyDeclarationFinder.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private String getFqNameForNode(ASTNode node) {
    if (node instanceof DeclarationExpression) {
        return ((BinaryExpression) node).getLeftExpression().getType().getName();
    } else if (node instanceof Expression) {
        return ((Expression) node).getType().getName();
    } else if (node instanceof PropertyNode) {
        return ((PropertyNode) node).getField().getType().getName();
    } else if (node instanceof FieldNode) {
        return ((FieldNode) node).getType().getName();
    } else if (node instanceof Parameter) {
        return ((Parameter) node).getType().getName();
    }

    return "";
}