use of org.sonar.plugins.java.api.tree.ArrayTypeTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method array_method_return_type.
@Test
public void array_method_return_type() {
MethodTree method;
ArrayTypeTree arrayTypeTree, childArrayTypeTree;
method = (MethodTree) firstTypeMember("class T { int[] m(); }");
assertThat(method.returnType()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) method.returnType();
assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThatArrayTypeHasBrackets(arrayTypeTree);
assertThatChildrenIteratorHasSize(arrayTypeTree, 3);
method = (MethodTree) firstTypeMember("class T { int @Foo [] m(); }");
assertThat(method.returnType()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) method.returnType();
assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 1);
assertThatChildrenIteratorHasSize(arrayTypeTree, 4);
method = (MethodTree) firstTypeMember("class T { int @Foo @bar [] m(); }");
assertThat(method.returnType()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) method.returnType();
assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 2);
assertThatChildrenIteratorHasSize(arrayTypeTree, 5);
method = (MethodTree) firstTypeMember("class T { int[] @Foo [] m(); }");
assertThat(method.returnType()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) method.returnType();
assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 1);
assertThatChildrenIteratorHasSize(arrayTypeTree, 4);
childArrayTypeTree = (ArrayTypeTree) arrayTypeTree.type();
assertThat(childArrayTypeTree).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBrackets(childArrayTypeTree);
assertThatChildrenIteratorHasSize(childArrayTypeTree, 3);
assertThat(childArrayTypeTree.openBracketToken().column() < arrayTypeTree.openBracketToken().column()).isTrue();
assertThat(childArrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
method = (MethodTree) firstTypeMember("class T { int[] m()[]; }");
assertThat(method.returnType()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) method.returnType();
assertThatArrayTypeHasBrackets(arrayTypeTree);
assertThatChildrenIteratorHasSize(arrayTypeTree, 3);
childArrayTypeTree = (ArrayTypeTree) arrayTypeTree.type();
assertThat(childArrayTypeTree).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBrackets(childArrayTypeTree);
assertThatChildrenIteratorHasSize(childArrayTypeTree, 3);
assertThat(childArrayTypeTree.openBracketToken().column() < arrayTypeTree.openBracketToken().column()).isTrue();
assertThat(childArrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
method = (MethodTree) firstTypeMember("class T { int[] m() @Foo []; }");
assertThat(method.returnType()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) method.returnType();
assertThat(arrayTypeTree).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 1);
assertThatChildrenIteratorHasSize(arrayTypeTree, 4);
childArrayTypeTree = (ArrayTypeTree) arrayTypeTree.type();
assertThat(childArrayTypeTree).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBrackets(childArrayTypeTree);
assertThatChildrenIteratorHasSize(childArrayTypeTree, 3);
assertThat(childArrayTypeTree.openBracketToken().column() < arrayTypeTree.openBracketToken().column()).isTrue();
assertThat(childArrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
}
use of org.sonar.plugins.java.api.tree.ArrayTypeTree in project sonar-java by SonarSource.
the class ArrayDesignatorOnVariableCheck method visitNode.
@Override
public void visitNode(Tree tree) {
VariableTree variableTree = (VariableTree) tree;
TypeTree type = variableTree.type();
SyntaxToken identifierToken = variableTree.simpleName().identifierToken();
while (type.is(Tree.Kind.ARRAY_TYPE)) {
ArrayTypeTree arrayTypeTree = (ArrayTypeTree) type;
SyntaxToken arrayDesignatorToken = arrayTypeTree.ellipsisToken();
if (arrayDesignatorToken == null) {
arrayDesignatorToken = arrayTypeTree.openBracketToken();
}
if (isInvalidPosition(arrayDesignatorToken, identifierToken)) {
reportIssue(arrayDesignatorToken, "Move the array designator from the variable to the type.");
break;
}
type = arrayTypeTree.type();
}
}
Aggregations