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());
}
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);
}
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);
}
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);
}
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());
}
Aggregations