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);
}
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);
}
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);
}
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();
}
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;
}
Aggregations