Search in sources :

Example 11 with ArrayTypeTree

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

Example 12 with ArrayTypeTree

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