use of org.sonar.plugins.java.api.tree.SwitchStatementTree in project sonar-java by SonarSource.
the class IdenticalCasesInSwitchCheck method visitNode.
@Override
public void visitNode(Tree node) {
if (node.is(Tree.Kind.SWITCH_STATEMENT)) {
SwitchStatementTree switchStatement = (SwitchStatementTree) node;
Multimap<CaseGroupTree, CaseGroupTree> identicalBranches = checkSwitchStatement(switchStatement);
boolean allBranchesSame = allBranchesSame(identicalBranches, switchStatement.cases().size());
boolean allBranchesSameWithoutDefault = allBranchesSame && !hasDefaultClause(switchStatement);
if (!allBranchesSame || allBranchesSameWithoutDefault) {
identicalBranches.asMap().forEach((first, others) -> {
if (!isTrivialCase(first.body()) || allBranchesSameWithoutDefault) {
others.forEach(other -> createIssue(other, issueMessage("case", first), first));
}
});
}
} else if (node.is(Tree.Kind.IF_STATEMENT) && !node.parent().is(Tree.Kind.IF_STATEMENT)) {
IfStatementTree ifStatement = (IfStatementTree) node;
IfElseChain ifElseChain = checkIfStatement(ifStatement);
reportIdenticalIfChainBranches(ifElseChain.branches, ifElseChain.totalBranchCount, hasElseClause(ifStatement));
}
}
use of org.sonar.plugins.java.api.tree.SwitchStatementTree in project sonar-java by SonarSource.
the class SwitchCaseWithoutBreakCheck method intentionalFallThrough.
private static boolean intentionalFallThrough(SwitchStatementTree switchStatement, CaseGroupTree caseGroup) {
FallThroughCommentVisitor visitor = new FallThroughCommentVisitor();
// Check first token of next case group when comment is last element of case group it is attached to next group.
CaseGroupTree nextCaseGroup = switchStatement.cases().get(switchStatement.cases().indexOf(caseGroup) + 1);
List<Tree> treesToScan = ImmutableList.<Tree>builder().addAll(caseGroup.body()).add(nextCaseGroup.firstToken()).build();
visitor.scan(treesToScan);
return visitor.hasComment;
}
use of org.sonar.plugins.java.api.tree.SwitchStatementTree in project sonar-java by SonarSource.
the class SwitchCaseTooBigCheck method visitNode.
@Override
public void visitNode(Tree tree) {
SwitchStatementTree switchStatementTree = (SwitchStatementTree) tree;
LinesOfCodeVisitor locVisitor = new LinesOfCodeVisitor();
switchStatementTree.cases().forEach(cgt -> {
int lines = cgt.body().stream().mapToInt(locVisitor::linesOfCode).sum();
if (lines > max) {
reportIssue(cgt.labels().get(cgt.labels().size() - 1), "Reduce this switch case number of lines from " + lines + " to at most " + max + ", for example by extracting code into methods.");
}
});
}
use of org.sonar.plugins.java.api.tree.SwitchStatementTree in project sonar-java by SonarSource.
the class SwitchDefaultLastCaseCheck method visitNode.
@Override
public void visitNode(Tree tree) {
SwitchStatementTree switchStatementTree = (SwitchStatementTree) tree;
getDefaultLabelAtWrongPosition(switchStatementTree).ifPresent(defaultLabel -> reportIssue(defaultLabel, "Move this default to the end of the switch."));
}
use of org.sonar.plugins.java.api.tree.SwitchStatementTree in project sonar-java by SonarSource.
the class SwitchWithTooManyCasesCheck method visitNode.
@Override
public void visitNode(Tree tree) {
SwitchStatementTree switchStatementTree = (SwitchStatementTree) tree;
if (isSwitchOverEnum(switchStatementTree)) {
return;
}
List<CaseGroupTree> cases = switchStatementTree.cases();
int size = cases.size();
if (size > maximumCases) {
List<JavaFileScannerContext.Location> secondary = new ArrayList<>();
for (CaseGroupTree element : cases) {
secondary.add(new JavaFileScannerContext.Location("+1", element.labels().get(0)));
}
reportIssue(switchStatementTree.switchKeyword(), "Reduce the number of non-empty switch cases from " + size + " to at most " + maximumCases + ".", secondary, null);
}
}
Aggregations