use of org.sonar.plugins.java.api.tree.ArrayTypeTree in project sonar-java by SonarSource.
the class MethodTreeImpl method isParameterStringArray.
private boolean isParameterStringArray() {
VariableTree variableTree = parameters.get(0);
boolean result = false;
if (variableTree.type().is(Tree.Kind.ARRAY_TYPE)) {
ArrayTypeTree arrayTypeTree = (ArrayTypeTree) variableTree.type();
result = arrayTypeTree.type().symbolType().isClass() && "String".equals(arrayTypeTree.type().symbolType().name());
}
return result;
}
use of org.sonar.plugins.java.api.tree.ArrayTypeTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method interface_field.
@Test
public void interface_field() {
List<Tree> declarations = firstType("interface T { public int f1 = 42, f2[] = { 13 }; }").members();
assertThat(declarations).hasSize(2);
VariableTree tree = (VariableTree) declarations.get(0);
assertThat(tree.is(Tree.Kind.VARIABLE)).isTrue();
assertThat(tree.modifiers().modifiers()).hasSize(1);
assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.PUBLIC);
assertThat(tree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThat(tree.simpleName().name()).isEqualTo("f1");
assertThat(tree.initializer()).isNotNull();
assertThatChildrenIteratorHasSize(tree, 6);
tree = (VariableTree) declarations.get(1);
assertThat(tree.is(Tree.Kind.VARIABLE)).isTrue();
assertThat(tree.modifiers().modifiers()).hasSize(1);
assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.PUBLIC);
assertThat(tree.type()).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBrackets((ArrayTypeTree) tree.type());
assertThat(tree.simpleName().name()).isEqualTo("f2");
assertThat(tree.initializer()).isNotNull();
assertThatChildrenIteratorHasSize(tree, 6);
}
use of org.sonar.plugins.java.api.tree.ArrayTypeTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method class_method.
@Test
public void class_method() {
MethodTree tree = (MethodTree) firstTypeMember("class T { public int m(int p[][]){} }");
assertThat(tree.is(Tree.Kind.METHOD)).isTrue();
assertThat(tree.parameters()).hasSize(1);
assertThat(tree.parameters().get(0).type()).isInstanceOf(ArrayTypeTree.class);
assertThatChildrenIteratorHasSize(tree, 8);
assertThat(((ArrayTypeTree) tree.parameters().get(0).type()).type()).isInstanceOf(ArrayTypeTree.class);
tree = (MethodTree) firstTypeMember("class T { public <T> int m(@Annotate int p1, int... p2) throws Exception1, Exception2 {} }");
assertThat(tree.is(Tree.Kind.METHOD)).isTrue();
assertThat(tree.modifiers().modifiers()).hasSize(1);
assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.PUBLIC);
assertThat(tree.typeParameters()).isNotEmpty();
assertThat(tree.returnType()).isNotNull();
assertThat(tree.simpleName().name()).isEqualTo("m");
assertThat(tree.parameters()).hasSize(2);
assertThat(tree.parameters().get(0).type()).isInstanceOf(PrimitiveTypeTree.class);
assertThat(tree.parameters().get(0).modifiers().annotations()).hasSize(1);
assertThat(tree.parameters().get(1).type()).isInstanceOf(ArrayTypeTree.class);
assertThat(tree.parameters().get(1).endToken()).isNull();
assertThat(tree.throwsClauses()).hasSize(2);
assertThat(tree.throwsClauses().separators()).hasSize(1);
assertThat(tree.block()).isNotNull();
assertThat(tree.defaultValue()).isNull();
assertThatChildrenIteratorHasSize(tree, 11);
// void method
tree = (MethodTree) firstTypeMember("class T { public void m(int p) throws Exception1, Exception2 {} }");
assertThat(tree.is(Tree.Kind.METHOD)).isTrue();
assertThat(tree.modifiers().modifiers()).hasSize(1);
assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.PUBLIC);
assertThat(tree.typeParameters()).isEmpty();
assertThat(tree.returnType()).isNotNull();
assertThat(tree.simpleName().name()).isEqualTo("m");
assertThat(tree.parameters()).hasSize(1);
assertThat(tree.parameters().get(0).endToken()).isNull();
assertThat(tree.throwsClauses()).hasSize(2);
assertThat(tree.block()).isNotNull();
assertThat(tree.defaultValue()).isNull();
assertThatChildrenIteratorHasSize(tree, 10);
tree = (MethodTree) firstTypeMember("class T { public int[] m(int p1, int... p2)[] throws Exception1, Exception2 {} }");
assertThat(tree.is(Tree.Kind.METHOD)).isTrue();
assertThat(tree.modifiers().modifiers()).hasSize(1);
assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.PUBLIC);
assertThat(tree.returnType()).isNotNull();
assertThat(tree.returnType().is(Tree.Kind.ARRAY_TYPE)).isTrue();
assertThat(((ArrayTypeTree) tree.returnType()).type().is(Kind.ARRAY_TYPE)).isTrue();
assertThat(((ArrayTypeTree) ((ArrayTypeTree) tree.returnType()).type()).type().is(Kind.PRIMITIVE_TYPE)).isTrue();
assertThat(tree.simpleName().name()).isEqualTo("m");
assertThat(tree.parameters()).hasSize(2);
assertThat(tree.parameters().get(0).type()).isInstanceOf(PrimitiveTypeTree.class);
assertThat(tree.parameters().get(1).type()).isInstanceOf(ArrayTypeTree.class);
assertThat(tree.throwsClauses()).hasSize(2);
assertThat(tree.throwsClauses().separators()).hasSize(1);
assertThat(tree.block()).isNotNull();
assertThat(tree.defaultValue()).isNull();
assertThatChildrenIteratorHasSize(tree, 11);
tree = (MethodTree) firstTypeMember("class T { public int m()[] { return null; } }");
assertThat(tree.is(Tree.Kind.METHOD)).isTrue();
assertThat(tree.parameters()).isEmpty();
assertThat(tree.returnType().is(Kind.ARRAY_TYPE)).isTrue();
assertThat(tree.returnType().is(Kind.PRIMITIVE_TYPE)).isFalse();
assertThatChildrenIteratorHasSize(tree, 7);
}
use of org.sonar.plugins.java.api.tree.ArrayTypeTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method array_formal_parameter.
@Test
public void array_formal_parameter() {
MethodTree method;
VariableTree variable;
ArrayTypeTree arrayTypeTree, childArrayTypeTree;
method = (MethodTree) firstTypeMember("interface T { void m(int[] a); }");
variable = method.parameters().get(0);
// 1+2, as empty modifiers are always included
assertThatChildrenIteratorHasSize(variable, 3);
assertThat(variable.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) variable.type();
assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThatArrayTypeHasBrackets(arrayTypeTree);
assertThatChildrenIteratorHasSize(arrayTypeTree, 3);
method = (MethodTree) firstTypeMember("interface T { void m(int... a); }");
variable = method.parameters().get(0);
assertThatChildrenIteratorHasSize(variable, 3);
assertThat(variable.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) variable.type();
assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThatArrayTypeHasEllipsis(arrayTypeTree);
assertThatChildrenIteratorHasSize(arrayTypeTree, 2);
method = (MethodTree) firstTypeMember("interface T { void m(int @Foo ... a); }");
variable = method.parameters().get(0);
assertThatChildrenIteratorHasSize(variable, 3);
assertThat(variable.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) variable.type();
assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThatArrayTypeHasEllipsisAndAnnotations(arrayTypeTree, 1);
assertThatChildrenIteratorHasSize(arrayTypeTree, 3);
method = (MethodTree) firstTypeMember("interface T { void m(int @Foo ... a[]); }");
variable = method.parameters().get(0);
assertThatChildrenIteratorHasSize(variable, 3);
assertThat(variable.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) variable.type();
assertThatArrayTypeHasBrackets(arrayTypeTree);
assertThatChildrenIteratorHasSize(arrayTypeTree, 3);
childArrayTypeTree = (ArrayTypeTree) arrayTypeTree.type();
assertThat(childArrayTypeTree).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasEllipsisAndAnnotations(childArrayTypeTree, 1);
assertThatChildrenIteratorHasSize(childArrayTypeTree, 3);
assertThat(childArrayTypeTree.ellipsisToken().column() < arrayTypeTree.openBracketToken().column()).isTrue();
assertThat(childArrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
method = (MethodTree) firstTypeMember("interface T { void m(int @Foo [] a); }");
assertThatChildrenIteratorHasSize(variable, 3);
variable = method.parameters().get(0);
assertThat(variable.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) variable.type();
assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 1);
assertThatChildrenIteratorHasSize(arrayTypeTree, 4);
method = (MethodTree) firstTypeMember("interface T { void m(int @Foo @bar [] a); }");
variable = method.parameters().get(0);
assertThatChildrenIteratorHasSize(variable, 3);
assertThat(variable.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) variable.type();
assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 2);
assertThatChildrenIteratorHasSize(arrayTypeTree, 5);
method = (MethodTree) firstTypeMember("interface T { void m(int[] @Foo [] a); }");
variable = method.parameters().get(0);
assertThatChildrenIteratorHasSize(variable, 3);
assertThat(variable.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) variable.type();
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("interface T { void m(int[] a[]); }");
variable = method.parameters().get(0);
assertThatChildrenIteratorHasSize(variable, 3);
assertThat(variable.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) variable.type();
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("interface T { void m(int[] a @Foo []); }");
variable = method.parameters().get(0);
assertThatChildrenIteratorHasSize(variable, 3);
assertThat(variable.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) variable.type();
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 ArrayDesignatorAfterTypeCheck method visitNode.
@Override
public void visitNode(Tree tree) {
MethodTree methodTree = (MethodTree) tree;
TypeTree returnType = methodTree.returnType();
SyntaxToken identifierToken = methodTree.simpleName().identifierToken();
while (returnType.is(Tree.Kind.ARRAY_TYPE)) {
ArrayTypeTree arrayTypeTree = (ArrayTypeTree) returnType;
SyntaxToken openBracketToken = arrayTypeTree.openBracketToken();
if (isInvalidPosition(openBracketToken, identifierToken)) {
reportIssue(openBracketToken, "Move the array designators \"[]\" to the end of the return type.");
break;
}
returnType = arrayTypeTree.type();
}
}
Aggregations