use of org.sonar.plugins.java.api.tree.StatementTree in project sonar-java by SonarSource.
the class ReassignmentFinderTest method parameter_with_usage.
@Test
public void parameter_with_usage() throws Exception {
String code = newCode("int foo(boolean test) {", " if (test) {}", " return test;", "}");
MethodTree method = methodTree(code);
List<StatementTree> statements = method.block().body();
assertThatLastReassignmentsOfReturnedVariableIsEqualTo(statements, null);
}
use of org.sonar.plugins.java.api.tree.StatementTree in project sonar-java by SonarSource.
the class NonNullSetToNullCheck method callsThisConstructor.
private static boolean callsThisConstructor(MethodTree constructor) {
List<StatementTree> body = constructor.block().body();
if (body.isEmpty()) {
return false;
}
StatementTree firstStatement = body.get(0);
if (!firstStatement.is(Tree.Kind.EXPRESSION_STATEMENT)) {
return false;
}
ExpressionTree expression = ((ExpressionStatementTree) firstStatement).expression();
if (!expression.is(Tree.Kind.METHOD_INVOCATION)) {
return false;
}
ExpressionTree methodSelect = ((MethodInvocationTree) expression).methodSelect();
return methodSelect.is(Tree.Kind.IDENTIFIER) && "this".equals(((IdentifierTree) methodSelect).name());
}
use of org.sonar.plugins.java.api.tree.StatementTree in project sonar-java by SonarSource.
the class MethodIdenticalImplementationsCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
ClassTree classTree = (ClassTree) tree;
List<MethodWithUsedVariables> methods = classTree.members().stream().filter(member -> member.is(Tree.Kind.METHOD)).map(MethodTree.class::cast).filter(methodTree -> isDuplicateCandidate(methodTree, classTree)).map(MethodWithUsedVariables::new).collect(Collectors.toList());
if (methods.size() <= 1) {
return;
}
Set<MethodTree> reported = new HashSet<>();
for (int i = 0; i < methods.size(); i++) {
MethodWithUsedVariables methodWithVariables = methods.get(i);
MethodTree method = methodWithVariables.method;
SyntaxToken methodIdentifier = method.simpleName().identifierToken();
List<StatementTree> methodBody = method.block().body();
methods.stream().skip(i + 1L).filter(otherMethodWithVariables -> !reported.contains(otherMethodWithVariables.method)).filter(otherMethodWithVariables -> !methodIdentifier.text().equals(otherMethodWithVariables.method.simpleName().name())).filter(otherMethodWithVariables -> SyntacticEquivalence.areEquivalent(methodBody, otherMethodWithVariables.method.block().body())).filter(methodWithVariables::isUsingSameVariablesWithSameTypes).forEach(otherMethodWithVariables -> {
MethodTree otherMethod = otherMethodWithVariables.method;
reportIssue(otherMethod.simpleName(), String.format(ISSUE_MSG, methodIdentifier.text(), methodIdentifier.line()), Collections.singletonList(new JavaFileScannerContext.Location("original implementation", methodIdentifier)), null);
reported.add(otherMethod);
});
}
}
use of org.sonar.plugins.java.api.tree.StatementTree in project sonar-java by SonarSource.
the class MissingCurlyBracesCheck method checkIfStatement.
private void checkIfStatement(IfStatementTree ifStmt) {
checkStatement(ifStmt.ifKeyword(), ifStmt.thenStatement());
StatementTree elseStmt = ifStmt.elseStatement();
if (elseStmt != null && !elseStmt.is(Tree.Kind.IF_STATEMENT)) {
checkStatement(ifStmt.elseKeyword(), elseStmt);
}
}
use of org.sonar.plugins.java.api.tree.StatementTree in project sonar-java by SonarSource.
the class MethodOnlyCallsSuperCheck method isUselessSuperCall.
private static boolean isUselessSuperCall(MethodTree methodTree) {
ExpressionTree callToSuper = null;
StatementTree statementTree = methodTree.block().body().get(0);
if (returnsVoid(methodTree) && statementTree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
callToSuper = ((ExpressionStatementTree) statementTree).expression();
} else if (statementTree.is(Tree.Kind.RETURN_STATEMENT)) {
callToSuper = ((ReturnStatementTree) statementTree).expression();
}
return callToSuper != null && isCallToSuper(methodTree, callToSuper) && sameVisibility(methodTree.symbol(), ((MethodInvocationTree) callToSuper).symbol());
}
Aggregations