Search in sources :

Example 1 with ArrayDimensionTree

use of org.sonar.plugins.java.api.tree.ArrayDimensionTree in project sonar-java by SonarSource.

the class TreeFactory method newArrayCreatorWithInitializer.

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

Example 2 with ArrayDimensionTree

use of org.sonar.plugins.java.api.tree.ArrayDimensionTree 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 3 with ArrayDimensionTree

use of org.sonar.plugins.java.api.tree.ArrayDimensionTree in project sonar-java by SonarSource.

the class JavaTreeModelTest method array_creation_expression.

/**
 * 15.10. Array Creation Expressions
 */
@Test
public void array_creation_expression() {
    NewArrayTree tree = (NewArrayTree) expressionOfReturnStatement("class T { int[][] m() { return new int[][]{{1}, {2, 3}}; } }");
    assertThat(tree.is(Tree.Kind.NEW_ARRAY)).isTrue();
    assertThat(tree.type()).isNotNull();
    assertThat(tree.dimensions()).hasSize(2);
    assertThat(tree.initializers()).hasSize(2);
    assertThat(tree.initializers().separators()).hasSize(1);
    assertThat(tree.newKeyword()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 7);
    ArrayDimensionTree dimension = tree.dimensions().get(0);
    assertThat(dimension.is(Tree.Kind.ARRAY_DIMENSION)).isTrue();
    assertThat(dimension.annotations()).isEmpty();
    assertThat(dimension.openBracketToken().text()).isEqualTo("[");
    assertThat(dimension.expression()).isNull();
    assertThat(dimension.closeBracketToken().text()).isEqualTo("]");
    assertThatChildrenIteratorHasSize(dimension, 2);
    dimension = tree.dimensions().get(1);
    assertThat(dimension.is(Tree.Kind.ARRAY_DIMENSION)).isTrue();
    assertThat(dimension.annotations()).isEmpty();
    assertThat(dimension.openBracketToken().text()).isEqualTo("[");
    assertThat(dimension.expression()).isNull();
    assertThat(dimension.closeBracketToken().text()).isEqualTo("]");
    assertThatChildrenIteratorHasSize(dimension, 2);
    NewArrayTree firstDim = (NewArrayTree) tree.initializers().get(0);
    assertThat(firstDim.initializers()).hasSize(1);
    assertThat(firstDim.initializers().separators()).isEmpty();
    assertThatChildrenIteratorHasSize(firstDim, 3);
    NewArrayTree secondDim = (NewArrayTree) tree.initializers().get(1);
    assertThat(secondDim.initializers()).hasSize(2);
    assertThat(secondDim.initializers().separators()).hasSize(1);
    assertThatChildrenIteratorHasSize(secondDim, 3);
    tree = (NewArrayTree) expressionOfReturnStatement("class T { int[][] m() { return new int[2][2]; } }");
    assertThat(tree.is(Tree.Kind.NEW_ARRAY)).isTrue();
    assertThat(tree.type()).isNotNull();
    assertThat(tree.dimensions()).hasSize(2);
    assertThat(tree.initializers()).isEmpty();
    assertThat(tree.newKeyword()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 5);
    dimension = tree.dimensions().get(0);
    assertThat(dimension.is(Tree.Kind.ARRAY_DIMENSION)).isTrue();
    assertThat(dimension.annotations()).isEmpty();
    assertThat(dimension.openBracketToken().text()).isEqualTo("[");
    assertThat(dimension.expression().is(Tree.Kind.INT_LITERAL)).isTrue();
    assertThat(dimension.closeBracketToken().text()).isEqualTo("]");
    assertThatChildrenIteratorHasSize(dimension, 3);
    tree = (NewArrayTree) expressionOfReturnStatement("class T { int[][] m() { return new int[] @Bar [] {{}, {}}; } }");
    assertThat(tree.is(Tree.Kind.NEW_ARRAY)).isTrue();
    assertThat(tree.type()).isNotNull();
    assertThat(tree.dimensions()).hasSize(2);
    assertThat(tree.initializers()).hasSize(2);
    assertThat(tree.newKeyword()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 7);
    dimension = tree.dimensions().get(0);
    assertThat(dimension.is(Tree.Kind.ARRAY_DIMENSION)).isTrue();
    assertThat(dimension.annotations()).isEmpty();
    assertThat(dimension.openBracketToken().text()).isEqualTo("[");
    assertThat(dimension.expression()).isNull();
    assertThat(dimension.closeBracketToken().text()).isEqualTo("]");
    assertThatChildrenIteratorHasSize(dimension, 2);
    dimension = tree.dimensions().get(1);
    assertThat(dimension.is(Tree.Kind.ARRAY_DIMENSION)).isTrue();
    assertThat(dimension.annotations()).hasSize(1);
    assertThat(dimension.openBracketToken().text()).isEqualTo("[");
    assertThat(dimension.expression()).isNull();
    assertThat(dimension.closeBracketToken().text()).isEqualTo("]");
    assertThatChildrenIteratorHasSize(dimension, 3);
    tree = (NewArrayTree) expressionOfReturnStatement("class T { int[][] m() { return new int[2] @Bar []; } }");
    assertThat(tree.is(Tree.Kind.NEW_ARRAY)).isTrue();
    assertThat(tree.type()).isNotNull();
    assertThat(tree.dimensions()).hasSize(2);
    assertThat(tree.initializers()).isEmpty();
    assertThat(tree.newKeyword()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 5);
    dimension = tree.dimensions().get(0);
    assertThat(dimension.is(Tree.Kind.ARRAY_DIMENSION)).isTrue();
    assertThat(dimension.annotations()).isEmpty();
    assertThat(dimension.openBracketToken().text()).isEqualTo("[");
    assertThat(dimension.expression().is(Tree.Kind.INT_LITERAL)).isTrue();
    assertThat(dimension.closeBracketToken().text()).isEqualTo("]");
    assertThatChildrenIteratorHasSize(dimension, 3);
    dimension = tree.dimensions().get(1);
    assertThat(dimension.is(Tree.Kind.ARRAY_DIMENSION)).isTrue();
    assertThat(dimension.annotations()).hasSize(1);
    assertThat(dimension.openBracketToken().text()).isEqualTo("[");
    assertThat(dimension.expression()).isNull();
    assertThat(dimension.closeBracketToken().text()).isEqualTo("]");
    assertThatChildrenIteratorHasSize(dimension, 3);
    tree = (NewArrayTree) expressionOfReturnStatement("class T { int[] m() { return new int @Foo [2]; } }");
    assertThat(tree.is(Tree.Kind.NEW_ARRAY)).isTrue();
    assertThat(tree.type()).isNotNull();
    assertThat(tree.dimensions()).hasSize(1);
    assertThat(tree.initializers()).isEmpty();
    assertThat(tree.newKeyword()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 4);
    dimension = tree.dimensions().get(0);
    assertThat(dimension.is(Tree.Kind.ARRAY_DIMENSION)).isTrue();
    assertThat(dimension.annotations()).hasSize(1);
    assertThat(dimension.openBracketToken().text()).isEqualTo("[");
    assertThat(dimension.expression().is(Tree.Kind.INT_LITERAL)).isTrue();
    assertThat(dimension.closeBracketToken().text()).isEqualTo("]");
    assertThatChildrenIteratorHasSize(dimension, 4);
    tree = (NewArrayTree) ((VariableTree) firstMethodFirstStatement("class T { void m() { int[] a = {,}; } }")).initializer();
    assertThat(tree.is(Tree.Kind.NEW_ARRAY)).isTrue();
    assertThat(tree.type()).isNull();
    assertThat(tree.dimensions()).isEmpty();
    assertThat(tree.initializers()).isEmpty();
    assertThat(tree.initializers().separators()).hasSize(1);
}
Also used : NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) Test(org.junit.Test)

Example 4 with ArrayDimensionTree

use of org.sonar.plugins.java.api.tree.ArrayDimensionTree in project sonar-java by SonarSource.

the class JavaTreeModelTest method array_access_expression.

/**
 * 15.13. Array Access Expressions
 */
@Test
public void array_access_expression() {
    String code = "class T { T() { return a[42]; } }";
    ArrayAccessExpressionTree tree = (ArrayAccessExpressionTree) expressionOfReturnStatement(code);
    assertThat(tree.is(Tree.Kind.ARRAY_ACCESS_EXPRESSION)).isTrue();
    assertThatChildrenIteratorHasSize(tree, 2);
    assertThat(tree.expression()).isNotNull();
    ArrayDimensionTree dimension = tree.dimension();
    assertThat(dimension).isNotNull();
    assertThat(dimension.openBracketToken().text()).isEqualTo("[");
    assertThat(dimension.expression().is(Tree.Kind.INT_LITERAL)).isTrue();
    assertThat(dimension.closeBracketToken().text()).isEqualTo("]");
    assertThatChildrenIteratorHasSize(dimension, 3);
}
Also used : ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) Test(org.junit.Test)

Example 5 with ArrayDimensionTree

use of org.sonar.plugins.java.api.tree.ArrayDimensionTree 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)

Aggregations

ArrayDimensionTree (org.sonar.plugins.java.api.tree.ArrayDimensionTree)5 ImmutableList (com.google.common.collect.ImmutableList)3 Optional (com.sonar.sslr.api.typed.Optional)3 InternalSyntaxToken (org.sonar.java.model.InternalSyntaxToken)3 Test (org.junit.Test)2 ArrayDimensionTreeImpl (org.sonar.java.model.ArrayDimensionTreeImpl)2 AnnotationTreeImpl (org.sonar.java.model.declaration.AnnotationTreeImpl)2 NewArrayTreeImpl (org.sonar.java.model.expression.NewArrayTreeImpl)2 ArrayAccessExpressionTreeImpl (org.sonar.java.model.expression.ArrayAccessExpressionTreeImpl)1 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)1 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)1 NewArrayTree (org.sonar.plugins.java.api.tree.NewArrayTree)1 SyntaxToken (org.sonar.plugins.java.api.tree.SyntaxToken)1 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)1