use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class CompareToResultTestCheck method isIdentifierContainingCompareToResult.
private static boolean isIdentifierContainingCompareToResult(IdentifierTree identifier) {
Symbol variableSymbol = identifier.symbol();
if (!variableSymbol.isVariableSymbol()) {
return false;
}
VariableTree variableDefinition = ((Symbol.VariableSymbol) variableSymbol).declaration();
if (variableDefinition != null) {
ExpressionTree initializer = variableDefinition.initializer();
if (initializer != null && initializer.is(Tree.Kind.METHOD_INVOCATION) && variableSymbol.owner().isMethodSymbol()) {
MethodTree method = ((Symbol.MethodSymbol) variableSymbol.owner()).declaration();
return method != null && COMPARE_TO.matches((MethodInvocationTree) initializer) && !isReassigned(variableSymbol, method);
}
}
return false;
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class ConstantMethodCheck method visitNode.
@Override
public void visitNode(Tree tree) {
MethodTree methodTree = (MethodTree) tree;
BlockTree body = methodTree.block();
if (!methodTree.modifiers().annotations().isEmpty() || ModifiersUtils.hasModifier(methodTree.modifiers(), Modifier.DEFAULT)) {
return;
}
if (Boolean.FALSE.equals(methodTree.isOverriding()) && body != null && body.body().size() == 1) {
StatementTree uniqueStatement = body.body().get(0);
if (uniqueStatement.is(Kind.RETURN_STATEMENT)) {
ExpressionTree returnedExpression = ((ReturnStatementTree) uniqueStatement).expression();
if (isConstant(returnedExpression)) {
reportIssue(returnedExpression, "Remove this method and declare a constant for this value.");
}
}
}
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class CognitiveComplexityMethodCheck method visitNode.
@Override
public void visitNode(Tree tree) {
MethodTree method = (MethodTree) tree;
CognitiveComplexityVisitor.Result result = CognitiveComplexityVisitor.methodComplexity(method);
int total = result.complexity;
if (total > max) {
reportIssue(method.simpleName(), "Refactor this method to reduce its Cognitive Complexity from " + total + " to the " + max + " allowed.", result.locations, total - max);
}
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class CallSuperInTestCaseCheck method visitNode.
@Override
public void visitNode(Tree tree) {
MethodTree methodTree = (MethodTree) tree;
Symbol.MethodSymbol methodSymbol = methodTree.symbol();
boolean isMethodInJunit3 = isWithinJunit3TestCase(methodSymbol) && isSetUpOrTearDown(methodSymbol);
if (isMethodInJunit3 && requiresSuperCall(methodSymbol) && !callSuperOnOverride(methodTree.block(), methodSymbol)) {
reportIssue(methodTree.simpleName(), String.format("Add a \"super.%s()\" call to this method.", methodSymbol.name()));
}
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class SyntaxTreeNameFinderTest method testCatchParameter.
@Test
public void testCatchParameter() {
MethodTree tree = buildSyntaxTree("public void test() {try {} catch (Exception ex) {} }");
assertThat(SyntaxTreeNameFinder.getName(tree)).isEqualTo("ex");
}
Aggregations