use of org.sonar.plugins.java.api.tree.BlockTree in project sonar-java by SonarSource.
the class OverwrittenKeyCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
ListMultimap<CollectionAndKey, Tree> usedKeys = ArrayListMultimap.create();
for (StatementTree statementTree : ((BlockTree) tree).body()) {
CollectionAndKey mapPut = isMapPut(statementTree);
if (mapPut != null) {
usedKeys.put(mapPut, mapPut.keyTree);
} else {
CollectionAndKey arrayAssignment = isArrayAssignment(statementTree);
if (arrayAssignment != null) {
if (arrayAssignment.collectionOnRHS()) {
usedKeys.clear();
}
usedKeys.put(arrayAssignment, arrayAssignment.keyTree);
} else {
// sequence of setting collection values is interrupted
reportOverwrittenKeys(usedKeys);
usedKeys.clear();
}
}
}
reportOverwrittenKeys(usedKeys);
}
use of org.sonar.plugins.java.api.tree.BlockTree in project sonar-java by SonarSource.
the class MethodTooBigCheck method visitNode.
@Override
public void visitNode(Tree tree) {
MethodTree methodTree = (MethodTree) tree;
BlockTree block = methodTree.block();
if (block != null) {
int lines = new LinesOfCodeVisitor().linesOfCode(block);
if (lines > max) {
reportIssue(methodTree.simpleName(), "This method has " + lines + " lines, which is greater than the " + max + " lines authorized. Split it into smaller methods.");
}
}
}
use of org.sonar.plugins.java.api.tree.BlockTree 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.BlockTree in project sonar-java by SonarSource.
the class SyntaxTreeNameFinderTest method testMemberSelectOnMethodInvocation.
@Test
public void testMemberSelectOnMethodInvocation() {
MethodTree tree = buildSyntaxTree("public void test() {int i = checkForNullMethod().length;}");
BlockTree block = tree.block();
StatementTree statementTree = block.body().get(0);
assertThat(SyntaxTreeNameFinder.getName(statementTree)).isEqualTo("checkForNullMethod");
}
use of org.sonar.plugins.java.api.tree.BlockTree in project sonar-java by SonarSource.
the class SyntaxTreeNameFinderTest method testSwitch.
@Test
public void testSwitch() {
MethodTree tree = buildSyntaxTree("public void test() {int i; switch (i) { case 0: break;}}");
BlockTree block = tree.block();
StatementTree statementTree = block.body().get(1);
assertThat(SyntaxTreeNameFinder.getName(statementTree)).isEqualTo("i");
}
Aggregations