Example usage for org.eclipse.jdt.core.dom ArrayInitializer expressions

List of usage examples for org.eclipse.jdt.core.dom ArrayInitializer expressions

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom ArrayInitializer expressions.

Prototype

ASTNode.NodeList expressions

To view the source code for org.eclipse.jdt.core.dom ArrayInitializer expressions.

Click Source Link

Document

The list of expressions (element type: Expression ).

Usage

From source file:at.bestsolution.fxide.jdt.corext.dom.ASTFlattener.java

License:Open Source License

@Override
public boolean visit(ArrayInitializer node) {
    this.fBuffer.append("{");//$NON-NLS-1$
    for (Iterator<Expression> it = node.expressions().iterator(); it.hasNext();) {
        Expression e = it.next();
        e.accept(this);
        if (it.hasNext()) {
            this.fBuffer.append(",");//$NON-NLS-1$
        }//from  w w  w  .ja v  a2 s  . co m
    }
    this.fBuffer.append("}");//$NON-NLS-1$
    return false;
}

From source file:boa.datagen.util.Java7Visitor.java

License:Apache License

@Override
public boolean visit(ArrayInitializer node) {
    boa.types.Ast.Expression.Builder b = boa.types.Ast.Expression.newBuilder();
    //      b.setPosition(pos.build());
    b.setKind(boa.types.Ast.Expression.ExpressionKind.ARRAYINIT);
    for (Object e : node.expressions()) {
        ((org.eclipse.jdt.core.dom.Expression) e).accept(this);
        if (expressions.empty()) {
            boa.types.Ast.Expression.Builder eb = boa.types.Ast.Expression.newBuilder();
            //         eb.setPosition(pos.build()); // FIXME
            eb.setKind(boa.types.Ast.Expression.ExpressionKind.ANNOTATION);
            eb.setAnnotation(modifiers.pop());
            b.addExpressions(eb.build());
        } else {//ww w  .j  a v  a2s  .  c  om
            b.addExpressions(expressions.pop());
        }
    }
    expressions.push(b.build());
    return false;
}

From source file:ca.ecliptical.pde.internal.ds.AnnotationProcessor.java

License:Open Source License

private void reportProblem(Annotation annotation, String member, int valueIndex,
        Collection<DSAnnotationProblem> problems, String message, String... args) {
    if (errorLevel.isNone())
        return;/*from w  w  w  .j  av  a 2  s.  co m*/

    Expression memberValue = annotation;
    if (annotation.isNormalAnnotation() && member != null) {
        NormalAnnotation na = (NormalAnnotation) annotation;
        for (Object value : na.values()) {
            MemberValuePair pair = (MemberValuePair) value;
            if (member.equals(pair.getName().getIdentifier())) {
                memberValue = pair.getValue();
                break;
            }
        }
    } else if (annotation.isSingleMemberAnnotation()) {
        SingleMemberAnnotation sma = (SingleMemberAnnotation) annotation;
        memberValue = sma.getValue();
    }

    int start = memberValue.getStartPosition();
    int length = memberValue.getLength();

    if (valueIndex >= 0 && memberValue instanceof ArrayInitializer) {
        ArrayInitializer ai = (ArrayInitializer) memberValue;
        if (valueIndex < ai.expressions().size()) {
            Expression element = (Expression) ai.expressions().get(valueIndex);
            start = element.getStartPosition();
            length = element.getLength();
        }
    }

    if (start >= 0) {
        DSAnnotationProblem problem = new DSAnnotationProblem(errorLevel.isError(), message, args);
        problem.setSourceStart(start);
        problem.setSourceEnd(start + length - 1);
        problems.add(problem);
    }
}

From source file:ch.acanda.eclipse.pmd.java.resolution.SuppressWarningsQuickFix.java

License:Open Source License

@SuppressWarnings("unchecked")
private ArrayInitializer createArrayInitializer(final Expression value) {
    final AST ast = value.getAST();
    final ArrayInitializer array;
    if (value instanceof ArrayInitializer) {
        array = createArrayInitializerAndCopyExpressions(ast, (ArrayInitializer) value);

    } else {//from   w  ww  . j a  v a  2s.c o  m
        array = (ArrayInitializer) ast.createInstance(ArrayInitializer.class);
        array.expressions().add(ASTUtil.copy(value));
    }

    array.expressions().add(createPMDLiteralValue(ast));
    return array;
}

From source file:ch.acanda.eclipse.pmd.java.resolution.SuppressWarningsQuickFix.java

License:Open Source License

@SuppressWarnings("unchecked")
private ArrayInitializer createArrayInitializerAndCopyExpressions(final AST ast,
        final ArrayInitializer existingArray) {
    final ArrayInitializer array;
    array = (ArrayInitializer) ast.createInstance(ArrayInitializer.class);
    final List<Expression> expressions = array.expressions();
    final List<Expression> existingExpressions = existingArray.expressions();
    for (final Expression existingExpression : existingExpressions) {
        expressions.add(ASTUtil.copy(existingExpression));
    }//from  ww w .ja  v a 2s .  c  o  m
    return array;
}

From source file:coloredide.utils.CopiedNaiveASTFlattener.java

License:Open Source License

public boolean visit(ArrayInitializer node) {
    this.buffer.append("{");//$NON-NLS-1$
    for (Iterator it = node.expressions().iterator(); it.hasNext();) {
        Expression e = (Expression) it.next();
        e.accept(this);
        if (it.hasNext()) {
            this.buffer.append(",");//$NON-NLS-1$
        }//from w w  w. ja v  a2  s.  co m
    }
    this.buffer.append("}");//$NON-NLS-1$
    return false;
}

From source file:com.android.ide.eclipse.adt.internal.lint.AddSuppressAnnotation.java

License:Open Source License

@SuppressWarnings({ "rawtypes" }) // Java AST API has raw types
private MultiTextEdit addSuppressAnnotation(IDocument document, ICompilationUnit compilationUnit,
        BodyDeclaration declaration) throws CoreException {
    List modifiers = declaration.modifiers();
    SingleMemberAnnotation existing = null;
    for (Object o : modifiers) {
        if (o instanceof SingleMemberAnnotation) {
            SingleMemberAnnotation annotation = (SingleMemberAnnotation) o;
            String type = annotation.getTypeName().getFullyQualifiedName();
            if (type.equals(FQCN_SUPPRESS_LINT) || type.endsWith(SUPPRESS_LINT)) {
                existing = annotation;//from  w w w .  jav a  2  s. c o  m
                break;
            }
        }
    }

    ImportRewrite importRewrite = ImportRewrite.create(compilationUnit, true);
    String local = importRewrite.addImport(FQCN_SUPPRESS_LINT);
    AST ast = declaration.getAST();
    ASTRewrite rewriter = ASTRewrite.create(ast);
    if (existing == null) {
        SingleMemberAnnotation newAnnotation = ast.newSingleMemberAnnotation();
        newAnnotation.setTypeName(ast.newSimpleName(local));
        StringLiteral value = ast.newStringLiteral();
        value.setLiteralValue(mId);
        newAnnotation.setValue(value);
        ListRewrite listRewrite = rewriter.getListRewrite(declaration, declaration.getModifiersProperty());
        listRewrite.insertFirst(newAnnotation, null);
    } else {
        Expression existingValue = existing.getValue();
        if (existingValue instanceof StringLiteral) {
            StringLiteral stringLiteral = (StringLiteral) existingValue;
            if (mId.equals(stringLiteral.getLiteralValue())) {
                // Already contains the id
                return null;
            }
            // Create a new array initializer holding the old string plus the new id
            ArrayInitializer array = ast.newArrayInitializer();
            StringLiteral old = ast.newStringLiteral();
            old.setLiteralValue(stringLiteral.getLiteralValue());
            array.expressions().add(old);
            StringLiteral value = ast.newStringLiteral();
            value.setLiteralValue(mId);
            array.expressions().add(value);
            rewriter.set(existing, VALUE_PROPERTY, array, null);
        } else if (existingValue instanceof ArrayInitializer) {
            // Existing array: just append the new string
            ArrayInitializer array = (ArrayInitializer) existingValue;
            List expressions = array.expressions();
            if (expressions != null) {
                for (Object o : expressions) {
                    if (o instanceof StringLiteral) {
                        if (mId.equals(((StringLiteral) o).getLiteralValue())) {
                            // Already contains the id
                            return null;
                        }
                    }
                }
            }
            StringLiteral value = ast.newStringLiteral();
            value.setLiteralValue(mId);
            ListRewrite listRewrite = rewriter.getListRewrite(array, EXPRESSIONS_PROPERTY);
            listRewrite.insertLast(value, null);
        } else {
            assert false : existingValue;
            return null;
        }
    }

    TextEdit importEdits = importRewrite.rewriteImports(new NullProgressMonitor());
    TextEdit annotationEdits = rewriter.rewriteAST(document, null);

    // Apply to the document
    MultiTextEdit edit = new MultiTextEdit();
    // Create the edit to change the imports, only if
    // anything changed
    if (importEdits.hasChildren()) {
        edit.addChild(importEdits);
    }
    edit.addChild(annotationEdits);

    return edit;
}

From source file:com.android.ide.eclipse.auidt.internal.lint.AddSuppressAnnotation.java

License:Open Source License

@SuppressWarnings({ "rawtypes" }) // Java AST API has raw types
private MultiTextEdit addSuppressAnnotation(IDocument document, ICompilationUnit compilationUnit,
        BodyDeclaration declaration) throws CoreException {
    List modifiers = declaration.modifiers();
    SingleMemberAnnotation existing = null;
    for (Object o : modifiers) {
        if (o instanceof SingleMemberAnnotation) {
            SingleMemberAnnotation annotation = (SingleMemberAnnotation) o;
            String type = annotation.getTypeName().getFullyQualifiedName();
            if (type.equals(FQCN_SUPPRESS_LINT) || type.endsWith(SUPPRESS_LINT)) {
                existing = annotation;//from   w  ww  .ja  v a2s  . c  om
                break;
            }
        }
    }

    ImportRewrite importRewrite = ImportRewrite.create(compilationUnit, true);
    String local = importRewrite.addImport(FQCN_SUPPRESS_LINT);
    AST ast = declaration.getAST();
    ASTRewrite rewriter = ASTRewrite.create(ast);
    if (existing == null) {
        SingleMemberAnnotation newAnnotation = ast.newSingleMemberAnnotation();
        newAnnotation.setTypeName(ast.newSimpleName(local));
        StringLiteral value = ast.newStringLiteral();
        value.setLiteralValue(mId);
        newAnnotation.setValue(value);
        ListRewrite listRewrite = rewriter.getListRewrite(declaration, declaration.getModifiersProperty());
        listRewrite.insertFirst(newAnnotation, null);
    } else {
        Expression existingValue = existing.getValue();
        if (existingValue instanceof StringLiteral) {
            // Create a new array initializer holding the old string plus the new id
            ArrayInitializer array = ast.newArrayInitializer();
            StringLiteral old = ast.newStringLiteral();
            StringLiteral stringLiteral = (StringLiteral) existingValue;
            old.setLiteralValue(stringLiteral.getLiteralValue());
            array.expressions().add(old);
            StringLiteral value = ast.newStringLiteral();
            value.setLiteralValue(mId);
            array.expressions().add(value);
            rewriter.set(existing, VALUE_PROPERTY, array, null);
        } else if (existingValue instanceof ArrayInitializer) {
            // Existing array: just append the new string
            ArrayInitializer array = (ArrayInitializer) existingValue;
            StringLiteral value = ast.newStringLiteral();
            value.setLiteralValue(mId);
            ListRewrite listRewrite = rewriter.getListRewrite(array, EXPRESSIONS_PROPERTY);
            listRewrite.insertLast(value, null);
        } else {
            assert false : existingValue;
            return null;
        }
    }

    TextEdit importEdits = importRewrite.rewriteImports(new NullProgressMonitor());
    TextEdit annotationEdits = rewriter.rewriteAST(document, null);

    // Apply to the document
    MultiTextEdit edit = new MultiTextEdit();
    // Create the edit to change the imports, only if
    // anything changed
    if (importEdits.hasChildren()) {
        edit.addChild(importEdits);
    }
    edit.addChild(annotationEdits);

    return edit;
}

From source file:com.bsiag.eclipse.jdt.java.formatter.linewrap.WrapPreparator.java

License:Open Source License

@Override
public boolean visit(ArrayInitializer node) {
    List<Expression> expressions = node.expressions();
    if (!expressions.isEmpty()) {
        for (Expression expression : expressions)
            this.wrapIndexes.add(this.tm.firstIndexIn(expression, -1));
        this.wrapParentIndex = this.tm.firstIndexBefore(expressions.get(0), TokenNameLBRACE);
        this.wrapGroupEnd = this.tm.lastIndexIn(node, -1);
        handleWrap(this.options.alignment_for_expressions_in_array_initializer, node);
    }/*  w  w  w  . j a va2s .  c  o  m*/
    if (!this.options.join_wrapped_lines
            && !this.options.insert_new_line_before_closing_brace_in_array_initializer) {
        // if there is a line break before the closing brace, formatter should treat it as a valid wrap to preserve
        int closingBraceIndex = this.tm.lastIndexIn(node, TokenNameRBRACE);
        Token closingBrace = this.tm.get(closingBraceIndex);
        if (this.tm.countLineBreaksBetween(this.tm.get(closingBraceIndex - 1), closingBrace) == 1) {
            int openingBraceIndex = this.tm.firstIndexIn(node, TokenNameLBRACE);
            closingBrace.setWrapPolicy(
                    new WrapPolicy(0, openingBraceIndex, this.currentDepth, 1, true, false, -1, false));
        }
    }
    return true;
}

From source file:com.bsiag.eclipse.jdt.java.formatter.SpacePreparator.java

License:Open Source License

@Override
public boolean visit(ArrayInitializer node) {
    int openingBraceIndex = this.tm.firstIndexIn(node, TokenNameLBRACE);
    int closingBraceIndex = this.tm.lastIndexIn(node, TokenNameRBRACE);
    Token lastToken = this.tm.get(closingBraceIndex - 1);
    if (lastToken.tokenType == TokenNameLBRACE) {
        handleToken(this.tm.get(openingBraceIndex),
                this.options.insert_space_before_opening_brace_in_array_initializer
                        && !(node.getParent() instanceof ArrayInitializer)
                        && !(node.getParent() instanceof SingleMemberAnnotation),
                this.options.insert_space_between_empty_braces_in_array_initializer);
    } else {//w w  w . j  a va2s.  c o  m
        boolean endsWithComma = lastToken.tokenType == TokenNameCOMMA;
        handleToken(this.tm.get(openingBraceIndex),
                this.options.insert_space_before_opening_brace_in_array_initializer
                        && !(node.getParent() instanceof ArrayInitializer)
                        && !(node.getParent() instanceof SingleMemberAnnotation),
                this.options.insert_space_after_opening_brace_in_array_initializer
                        && !(endsWithComma && node.expressions().isEmpty()));
        handleCommas(node.expressions(), this.options.insert_space_before_comma_in_array_initializer,
                this.options.insert_space_after_comma_in_array_initializer);
        if (endsWithComma) {
            handleToken(lastToken, this.options.insert_space_before_comma_in_array_initializer, false); //this.options.insert_space_after_comma_in_array_initializer);
        }
        handleToken(this.tm.get(closingBraceIndex),
                this.options.insert_space_before_closing_brace_in_array_initializer
                        && !(endsWithComma && node.expressions().isEmpty()),
                false);
    }
    return true;
}