Search in sources :

Example 1 with ArrayTypeTree

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;
}
Also used : VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree)

Example 2 with ArrayTypeTree

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);
}
Also used : VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ImportClauseTree(org.sonar.plugins.java.api.tree.ImportClauseTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) SynchronizedStatementTree(org.sonar.plugins.java.api.tree.SynchronizedStatementTree) StaticInitializerTree(org.sonar.plugins.java.api.tree.StaticInitializerTree) TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) CaseLabelTree(org.sonar.plugins.java.api.tree.CaseLabelTree) UnionTypeTree(org.sonar.plugins.java.api.tree.UnionTypeTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) ContinueStatementTree(org.sonar.plugins.java.api.tree.ContinueStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) PackageDeclarationTree(org.sonar.plugins.java.api.tree.PackageDeclarationTree) WildcardTree(org.sonar.plugins.java.api.tree.WildcardTree) LabeledStatementTree(org.sonar.plugins.java.api.tree.LabeledStatementTree) BreakStatementTree(org.sonar.plugins.java.api.tree.BreakStatementTree) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) ParenthesizedTree(org.sonar.plugins.java.api.tree.ParenthesizedTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) SwitchStatementTree(org.sonar.plugins.java.api.tree.SwitchStatementTree) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) AssertStatementTree(org.sonar.plugins.java.api.tree.AssertStatementTree) EnumConstantTree(org.sonar.plugins.java.api.tree.EnumConstantTree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ImportTree(org.sonar.plugins.java.api.tree.ImportTree) PrimitiveTypeTree(org.sonar.plugins.java.api.tree.PrimitiveTypeTree) ModifierKeywordTree(org.sonar.plugins.java.api.tree.ModifierKeywordTree) TypeParameterTree(org.sonar.plugins.java.api.tree.TypeParameterTree) CaseGroupTree(org.sonar.plugins.java.api.tree.CaseGroupTree) Tree(org.sonar.plugins.java.api.tree.Tree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) EmptyStatementTree(org.sonar.plugins.java.api.tree.EmptyStatementTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) InstanceOfTree(org.sonar.plugins.java.api.tree.InstanceOfTree) CatchTree(org.sonar.plugins.java.api.tree.CatchTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Test(org.junit.Test)

Example 3 with ArrayTypeTree

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);
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree) Test(org.junit.Test)

Example 4 with ArrayTypeTree

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);
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree) Test(org.junit.Test)

Example 5 with ArrayTypeTree

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();
    }
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree)

Aggregations

ArrayTypeTree (org.sonar.plugins.java.api.tree.ArrayTypeTree)12 Test (org.junit.Test)9 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)8 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)8 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)6 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)4 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)4 ArrayDimensionTree (org.sonar.plugins.java.api.tree.ArrayDimensionTree)4 AssertStatementTree (org.sonar.plugins.java.api.tree.AssertStatementTree)4 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)4 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)4 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)4 BreakStatementTree (org.sonar.plugins.java.api.tree.BreakStatementTree)4 CaseGroupTree (org.sonar.plugins.java.api.tree.CaseGroupTree)4 CaseLabelTree (org.sonar.plugins.java.api.tree.CaseLabelTree)4 CatchTree (org.sonar.plugins.java.api.tree.CatchTree)4 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)4 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)4 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)4 ContinueStatementTree (org.sonar.plugins.java.api.tree.ContinueStatementTree)4