Search in sources :

Example 26 with MethodTree

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

the class ReassignmentFinderTest method parameter.

@Test
public void parameter() throws Exception {
    String code = newCode("int foo(int a) {", "  return a;", "}");
    MethodTree method = methodTree(code);
    List<StatementTree> statements = method.block().body();
    assertThatLastReassignmentsOfReturnedVariableIsEqualTo(statements, null);
}
Also used : IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Test(org.junit.Test)

Example 27 with MethodTree

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

the class MethodMatcherTest method should_fail_if_parameters_are_not_defined.

@Test
public void should_fail_if_parameters_are_not_defined() throws Exception {
    MethodMatcher matcher = MethodMatcher.create().name("toString");
    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 28 with MethodTree

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

the class MethodMatcherTest method test_copy.

@Test
public void test_copy() throws Exception {
    MethodMatcher vanilla = MethodMatcher.create().typeDefinition("Test").name("f").withoutParameter();
    MethodMatcher copyInt = vanilla.copy().addParameter("int");
    MethodMatcher copyString = vanilla.copy().addParameter("java.lang.String");
    Map<MethodMatcher, List<Integer>> matches = new HashMap<>();
    matches.put(vanilla, new ArrayList<>());
    matches.put(copyInt, new ArrayList<>());
    matches.put(copyString, new ArrayList<>());
    JavaCheckVerifier.verifyNoIssue("src/test/files/matcher/Copy.java", new IssuableSubscriptionVisitor() {

        @Override
        public List<Tree.Kind> nodesToVisit() {
            return Collections.singletonList(Tree.Kind.METHOD);
        }

        @Override
        public void visitNode(Tree tree) {
            MethodTree methodTree = (MethodTree) tree;
            matches.forEach((matcher, list) -> {
                if (matcher.matches(methodTree)) {
                    list.add(methodTree.firstToken().line());
                }
            });
        }
    });
    assertThat(matches.get(vanilla)).containsExactly(3);
    assertThat(matches.get(copyInt)).containsExactly(5);
    assertThat(matches.get(copyString)).containsExactly(7);
}
Also used : SubscriptionVisitor(org.sonar.java.ast.visitors.SubscriptionVisitor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) JavaAstScanner(org.sonar.java.ast.JavaAstScanner) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) ExpectedException(org.junit.rules.ExpectedException) Nullable(javax.annotation.Nullable) VisitorsBridge(org.sonar.java.model.VisitorsBridge) Test(org.junit.Test) JavaTree(org.sonar.java.model.JavaTree) Tree(org.sonar.plugins.java.api.tree.Tree) Mockito.when(org.mockito.Mockito.when) File(java.io.File) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) List(java.util.List) Rule(org.junit.Rule) JavaCheckVerifier(org.sonar.java.se.JavaCheckVerifier) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Collections(java.util.Collections) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Mockito.mock(org.mockito.Mockito.mock) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) HashMap(java.util.HashMap) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) JavaTree(org.sonar.java.model.JavaTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Test(org.junit.Test)

Example 29 with MethodTree

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

the class MethodMatcherTest method does_not_match_without_enclosingClass.

@Test
public void does_not_match_without_enclosingClass() throws Exception {
    MethodMatcher matcher = MethodMatcher.create().name("toString").withoutParameter();
    Symbol.MethodSymbol symbol = mock(Symbol.MethodSymbol.class);
    when(symbol.enclosingClass()).thenReturn(null);
    MethodTree tree = mock(MethodTree.class);
    when(tree.symbol()).thenReturn(symbol);
    assertThat(matcher.matches(tree)).isFalse();
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) Test(org.junit.Test)

Example 30 with MethodTree

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

the class MethodMatcherTest method methodTreeMock.

private MethodTree methodTreeMock(String methodName, @Nullable Symbol.TypeSymbol enclosingClass) {
    Symbol.MethodSymbol methodSymbol = mock(Symbol.MethodSymbol.class);
    when(methodSymbol.isMethodSymbol()).thenReturn(true);
    when(methodSymbol.name()).thenReturn(methodName);
    when(methodSymbol.enclosingClass()).thenReturn(enclosingClass);
    MethodTree tree = mock(MethodTree.class);
    when(tree.symbol()).thenReturn(methodSymbol);
    return tree;
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol)

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