Search in sources :

Example 1 with NewArrayTreeImpl

use of org.sonar.java.model.expression.NewArrayTreeImpl in project sonar-java by SonarSource.

the class TreeFactory method newArrayInitializer.

public NewArrayTreeImpl newArrayInitializer(InternalSyntaxToken openBraceToken, Optional<InternalSyntaxToken> optionalComma, Optional<List<Tuple<ExpressionTree, Optional<InternalSyntaxToken>>>> rests, InternalSyntaxToken closeBraceToken) {
    ImmutableList.Builder<ExpressionTree> initializers = ImmutableList.builder();
    ImmutableList.Builder<SyntaxToken> separators = ImmutableList.builder();
    if (optionalComma.isPresent()) {
        separators.add(optionalComma.get());
    }
    if (rests.isPresent()) {
        for (Tuple<ExpressionTree, Optional<InternalSyntaxToken>> rest : rests.get()) {
            initializers.add(rest.first());
            if (rest.second().isPresent()) {
                separators.add(rest.second().get());
            }
        }
    }
    return new NewArrayTreeImpl(ImmutableList.<ArrayDimensionTree>of(), new InitializerListTreeImpl(initializers.build(), separators.build())).completeWithCurlyBraces(openBraceToken, closeBraceToken);
}
Also used : NewArrayTreeImpl(org.sonar.java.model.expression.NewArrayTreeImpl) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken) Optional(com.sonar.sslr.api.typed.Optional) ImmutableList(com.google.common.collect.ImmutableList) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree)

Example 2 with NewArrayTreeImpl

use of org.sonar.java.model.expression.NewArrayTreeImpl in project sonar-java by SonarSource.

the class TreeFactory method newArrayCreatorWithDimension.

public NewArrayTreeImpl newArrayCreatorWithDimension(InternalSyntaxToken openBracketToken, ExpressionTree expression, InternalSyntaxToken closeBracketToken, Optional<List<ArrayAccessExpressionTreeImpl>> arrayAccesses, Optional<List<Tuple<Optional<List<AnnotationTreeImpl>>, Tuple<InternalSyntaxToken, InternalSyntaxToken>>>> dims) {
    ImmutableList.Builder<ArrayDimensionTree> dimensions = ImmutableList.builder();
    dimensions.add(new ArrayDimensionTreeImpl(openBracketToken, expression, closeBracketToken));
    if (arrayAccesses.isPresent()) {
        for (ArrayAccessExpressionTreeImpl arrayAccess : arrayAccesses.get()) {
            dimensions.add(arrayAccess.dimension());
        }
    }
    if (dims.isPresent()) {
        for (Tuple<Optional<List<AnnotationTreeImpl>>, Tuple<InternalSyntaxToken, InternalSyntaxToken>> dim : dims.get()) {
            Tuple<InternalSyntaxToken, InternalSyntaxToken> brackets = dim.second();
            List<AnnotationTreeImpl> annotations = dim.first().or(ImmutableList.<AnnotationTreeImpl>of());
            dimensions.add(new ArrayDimensionTreeImpl(annotations, brackets.first(), null, brackets.second()));
        }
    }
    return new NewArrayTreeImpl(dimensions.build(), InitializerListTreeImpl.emptyList());
}
Also used : NewArrayTreeImpl(org.sonar.java.model.expression.NewArrayTreeImpl) Optional(com.sonar.sslr.api.typed.Optional) ImmutableList(com.google.common.collect.ImmutableList) ArrayDimensionTreeImpl(org.sonar.java.model.ArrayDimensionTreeImpl) AnnotationTreeImpl(org.sonar.java.model.declaration.AnnotationTreeImpl) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) ArrayAccessExpressionTreeImpl(org.sonar.java.model.expression.ArrayAccessExpressionTreeImpl) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken)

Example 3 with NewArrayTreeImpl

use of org.sonar.java.model.expression.NewArrayTreeImpl in project sonar-java by SonarSource.

the class TreeFactory method newExpression.

public ExpressionTree newExpression(InternalSyntaxToken newToken, Optional<List<AnnotationTreeImpl>> annotations, ExpressionTree partial) {
    TypeTree typeTree;
    if (partial.is(Tree.Kind.NEW_CLASS)) {
        NewClassTreeImpl newClassTree = (NewClassTreeImpl) partial;
        newClassTree.completeWithNewKeyword(newToken);
        typeTree = newClassTree.identifier();
    } else {
        NewArrayTreeImpl newArrayTree = (NewArrayTreeImpl) partial;
        newArrayTree.completeWithNewKeyword(newToken);
        typeTree = newArrayTree.type();
    }
    completeTypeTreeWithAnnotations(typeTree, annotations);
    return partial;
}
Also used : ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) NewArrayTreeImpl(org.sonar.java.model.expression.NewArrayTreeImpl) NewClassTreeImpl(org.sonar.java.model.expression.NewClassTreeImpl)

Aggregations

NewArrayTreeImpl (org.sonar.java.model.expression.NewArrayTreeImpl)3 ImmutableList (com.google.common.collect.ImmutableList)2 Optional (com.sonar.sslr.api.typed.Optional)2 InternalSyntaxToken (org.sonar.java.model.InternalSyntaxToken)2 ArrayDimensionTree (org.sonar.plugins.java.api.tree.ArrayDimensionTree)2 ArrayDimensionTreeImpl (org.sonar.java.model.ArrayDimensionTreeImpl)1 AnnotationTreeImpl (org.sonar.java.model.declaration.AnnotationTreeImpl)1 ArrayAccessExpressionTreeImpl (org.sonar.java.model.expression.ArrayAccessExpressionTreeImpl)1 NewClassTreeImpl (org.sonar.java.model.expression.NewClassTreeImpl)1 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)1 ParameterizedTypeTree (org.sonar.plugins.java.api.tree.ParameterizedTypeTree)1 SyntaxToken (org.sonar.plugins.java.api.tree.SyntaxToken)1 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)1