Search in sources :

Example 31 with MethodTree

use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.

the class MethodMatcherTest method should_fail_if_name_is_not_defined.

@Test
public void should_fail_if_name_is_not_defined() throws Exception {
    MethodMatcher matcher = MethodMatcher.create().withoutParameter();
    MethodTree tree = methodTreeMock("toString", mock(Symbol.TypeSymbol.class));
    exception.expect(IllegalStateException.class);
    matcher.matches(tree);
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Test(org.junit.Test)

Example 32 with MethodTree

use of org.sonar.plugins.java.api.tree.MethodTree 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 33 with MethodTree

use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.

the class JavaTreeModelTest method annotation_method.

@Test
public void annotation_method() {
    MethodTree tree = (MethodTree) firstTypeMember("@interface T { int m() default 0; }");
    assertThat(tree.is(Tree.Kind.METHOD)).isTrue();
    assertThat(tree.returnType()).isNotNull();
    assertThat(tree.simpleName().name()).isEqualTo("m");
    assertThat(tree.parameters()).isEmpty();
    assertThat(tree.throwsClauses()).isEmpty();
    assertThat(tree.block()).isNull();
    assertThat(tree.defaultValue()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 9);
    tree = (MethodTree) firstTypeMember("@interface plop{ public String method(); }");
    assertThat(tree.modifiers().modifiers()).hasSize(1);
    assertThat(tree.modifiers().modifiers().get(0).keyword().text()).isEqualTo("public");
    assertThatChildrenIteratorHasSize(tree, 7);
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Test(org.junit.Test)

Example 34 with MethodTree

use of org.sonar.plugins.java.api.tree.MethodTree 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 35 with MethodTree

use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.

the class JavaTreeModelTest method blocks.

/*
   * 14. Blocks and Statements
   */
/**
 * 14.2. Blocks
 */
@Test
public void blocks() {
    BlockTree tree = ((MethodTree) firstTypeMember("class T { void m() { ; ; } }")).block();
    assertThat(tree.is(Tree.Kind.BLOCK)).isTrue();
    assertThat(tree.openBraceToken().text()).isEqualTo("{");
    assertThat(tree.body()).hasSize(2);
    assertThat(tree.closeBraceToken().text()).isEqualTo("}");
    assertThatChildrenIteratorHasSize(tree, 4);
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) Test(org.junit.Test)

Aggregations

MethodTree (org.sonar.plugins.java.api.tree.MethodTree)143 Test (org.junit.Test)59 Tree (org.sonar.plugins.java.api.tree.Tree)43 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)39 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)34 Symbol (org.sonar.plugins.java.api.semantic.Symbol)31 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)30 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)27 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)23 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)20 Type (org.sonar.plugins.java.api.semantic.Type)19 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)19 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)18 List (java.util.List)16 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)16 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)15 ArrayList (java.util.ArrayList)14 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)14 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)14 JavaFileScannerContext (org.sonar.plugins.java.api.JavaFileScannerContext)12