Search in sources :

Example 86 with MethodTree

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

the class UndocumentedApiCheck method isAccessor.

private boolean isAccessor(Tree tree) {
    if (!classTrees.isEmpty() && tree.is(Tree.Kind.METHOD)) {
        MethodTree methodTree = (MethodTree) tree;
        String name = methodTree.simpleName().name();
        return (setterPattern.matcher(name).matches() && methodTree.parameters().size() == 1) || (getterPattern.matcher(name).matches() && methodTree.parameters().isEmpty());
    }
    return false;
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 87 with MethodTree

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

the class JavaParserTest method parent_link_should_be_computed.

@Test
public void parent_link_should_be_computed() {
    CompilationUnitTree cut = (CompilationUnitTree) JavaParser.createParser().parse("class A { void foo() {} }");
    ClassTree classTree = (ClassTree) cut.types().get(0);
    MethodTree method = (MethodTree) classTree.members().get(0);
    assertThat(method.parent()).isSameAs(classTree);
    assertThat(classTree.parent()).isSameAs(cut);
    assertThat(cut.parent()).isNull();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Test(org.junit.Test)

Example 88 with MethodTree

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

the class JavaTreeModelTest method enum_constructor.

@Test
public void enum_constructor() {
    MethodTree tree = (MethodTree) firstType("enum T { ; T(int p1, int... p2) throws Exception1, Exception2 {} }").members().get(1);
    assertThat(tree.is(Tree.Kind.CONSTRUCTOR)).isTrue();
    assertThat(tree.returnType()).isNull();
    assertThat(tree.simpleName().name()).isEqualTo("T");
    assertThat(tree.parameters()).hasSize(2);
    assertThat(tree.parameters().get(0).type()).isInstanceOf(PrimitiveTypeTree.class);
    assertThatArrayTypeHasEllipsis((ArrayTypeTree) tree.parameters().get(1).type());
    assertThat(tree.throwsClauses()).hasSize(2);
    assertThat(tree.throwsClauses().separators()).hasSize(1);
    assertThat(tree.block()).isNotNull();
    assertThat(tree.defaultValue()).isNull();
    assertThatChildrenIteratorHasSize(tree, 10);
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Test(org.junit.Test)

Example 89 with MethodTree

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

the class JavaTreeModelTest method enum_method.

@Test
public void enum_method() {
    MethodTree tree = (MethodTree) firstType("enum T { ; int m(int p1, int... p2) throws Exception1, Exception2 {} }").members().get(1);
    assertThat(tree.is(Tree.Kind.METHOD)).isTrue();
    assertThat(tree.returnType()).isNotNull();
    assertThat(tree.simpleName().name()).isEqualTo("m");
    assertThat(tree.parameters()).hasSize(2);
    assertThat(tree.parameters().get(0).type()).isInstanceOf(PrimitiveTypeTree.class);
    assertThatArrayTypeHasEllipsis((ArrayTypeTree) tree.parameters().get(1).type());
    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) firstType("enum T { ; void m(int p) throws Exception1, Exception2; }").members().get(1);
    assertThat(tree.is(Tree.Kind.METHOD)).isTrue();
    assertThat(tree.returnType()).isNotNull();
    assertThat(tree.simpleName().name()).isEqualTo("m");
    assertThat(tree.parameters()).hasSize(1);
    assertThat(tree.throwsClauses()).hasSize(2);
    assertThat(tree.throwsClauses().separators()).hasSize(1);
    assertThat(tree.block()).isNull();
    assertThat(tree.defaultValue()).isNull();
    assertThatChildrenIteratorHasSize(tree, 10);
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Test(org.junit.Test)

Example 90 with MethodTree

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

the class JavaTreeModelTest method interface_method.

@Test
public void interface_method() {
    MethodTree tree = (MethodTree) firstTypeMember("interface T { <T> int m(int p1, int... p2) throws Exception1, Exception2; }");
    assertThat(tree.is(Tree.Kind.METHOD)).isTrue();
    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(1).type()).isInstanceOf(ArrayTypeTree.class);
    assertThat(tree.throwsClauses()).hasSize(2);
    assertThat(tree.throwsClauses().separators()).hasSize(1);
    assertThat(tree.block()).isNull();
    assertThat(tree.defaultValue()).isNull();
    assertThatChildrenIteratorHasSize(tree, 11);
    // void method
    tree = (MethodTree) firstTypeMember("interface T { void m(int p) throws Exception1, Exception2; }");
    assertThat(tree.is(Tree.Kind.METHOD)).isTrue();
    assertThat(tree.typeParameters()).isEmpty();
    assertThat(tree.returnType()).isNotNull();
    assertThat(tree.simpleName().name()).isEqualTo("m");
    assertThat(tree.parameters()).hasSize(1);
    assertThat(tree.throwsClauses()).hasSize(2);
    assertThat(tree.throwsClauses().separators()).hasSize(1);
    assertThat(tree.block()).isNull();
    assertThat(tree.defaultValue()).isNull();
    assertThatChildrenIteratorHasSize(tree, 10);
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) 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