
org.sonar.java.checks.IdenticalCasesInSwitchCheck Maven / Gradle / Ivy
/*
* SonarQube Java
* Copyright (C) 2012-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonar.java.checks;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.sonar.check.Rule;
import org.sonar.java.model.LineUtils;
import org.sonar.java.model.SyntacticEquivalence;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.JavaFileScannerContext;
import org.sonar.plugins.java.api.tree.BlockTree;
import org.sonar.plugins.java.api.tree.CaseGroupTree;
import org.sonar.plugins.java.api.tree.IfStatementTree;
import org.sonar.plugins.java.api.tree.StatementTree;
import org.sonar.plugins.java.api.tree.SwitchTree;
import org.sonar.plugins.java.api.tree.Tree;
@Rule(key = "S1871")
public class IdenticalCasesInSwitchCheck extends IssuableSubscriptionVisitor {
@Override
public List nodesToVisit() {
return Arrays.asList(Tree.Kind.SWITCH_STATEMENT, Tree.Kind.SWITCH_EXPRESSION, Tree.Kind.IF_STATEMENT);
}
@Override
public void visitNode(Tree node) {
if (node.is(Tree.Kind.SWITCH_STATEMENT, Tree.Kind.SWITCH_EXPRESSION)) {
SwitchTree switchTree = (SwitchTree) node;
Map> identicalBranches = checkSwitchStatement(switchTree);
boolean allBranchesSame = allBranchesSame(identicalBranches, switchTree.cases().size());
boolean allBranchesSameWithoutDefault = allBranchesSame && !hasDefaultClause(switchTree);
if (!allBranchesSame || allBranchesSameWithoutDefault) {
identicalBranches.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));
}
}
protected static boolean allBranchesSame(Map extends Tree, ? extends Set extends Tree>> identicalBranches, int size) {
return identicalBranches.keySet().size() == 1 && identicalBranchesSize(identicalBranches) == size - 1;
}
private static long identicalBranchesSize(Map extends Tree, ? extends Set extends Tree>> identicalBranches) {
return identicalBranches.values().stream().flatMap(Collection::stream).count();
}
private static boolean isTrivialCase(List body) {
return body.size() == 1 || (body.size() == 2 && body.get(1).is(Tree.Kind.BREAK_STATEMENT));
}
protected Map> checkSwitchStatement(SwitchTree node) {
Map> identicalBranches = new HashMap<>();
int index = 0;
List cases = node.cases();
Set duplicates = new HashSet<>();
for (CaseGroupTree caseGroupTree : cases) {
index++;
if (duplicates.contains(caseGroupTree)) {
continue;
}
for (int i = index; i < cases.size(); i++) {
CaseGroupTree caseI = cases.get(i);
if (SyntacticEquivalence.areEquivalent(caseGroupTree.body(), caseI.body())) {
duplicates.add(caseI);
identicalBranches.computeIfAbsent(caseGroupTree, k -> new HashSet<>()).add(caseI);
}
}
}
return identicalBranches;
}
protected static class IfElseChain {
Map> branches = new HashMap<>();
int totalBranchCount;
}
protected static IfElseChain checkIfStatement(IfStatementTree node) {
IfElseChain ifElseChain = new IfElseChain();
ifElseChain.totalBranchCount = 1;
List allBranches = new ArrayList<>();
allBranches.add(node.thenStatement());
StatementTree elseStatement = node.elseStatement();
while (elseStatement != null && elseStatement.is(Tree.Kind.IF_STATEMENT)) {
IfStatementTree ifStatement = (IfStatementTree) elseStatement;
allBranches.add(ifStatement.thenStatement());
elseStatement = ifStatement.elseStatement();
}
if (elseStatement != null) {
allBranches.add(elseStatement);
}
return collectIdenticalBranches(allBranches);
}
private static IfElseChain collectIdenticalBranches(List allBranches) {
IfElseChain ifElseChain = new IfElseChain();
Set duplicates = new HashSet<>();
for (int i = 0; i < allBranches.size(); i++) {
if (duplicates.contains(allBranches.get(i))) {
continue;
}
for (int j = i + 1; j < allBranches.size(); j++) {
StatementTree statement1 = allBranches.get(i);
StatementTree statement2 = allBranches.get(j);
if (SyntacticEquivalence.areEquivalentIncludingSameVariables(statement1, statement2)) {
duplicates.add(statement2);
ifElseChain.branches.computeIfAbsent(statement1, k -> new HashSet<>()).add(statement2);
}
}
}
ifElseChain.totalBranchCount = allBranches.size();
return ifElseChain;
}
private void reportIdenticalIfChainBranches(Map> identicalBranches, int totalBranchCount, boolean withElseClause) {
boolean allBranchesSame = allBranchesSame(identicalBranches, totalBranchCount);
boolean allBranchesSameWithoutElse = allBranchesSame && !withElseClause;
if (!allBranchesSame || allBranchesSameWithoutElse) {
identicalBranches.forEach((first, others) -> {
if (!isTrivialIfStatement(first) || allBranchesSameWithoutElse) {
others.forEach(other -> createIssue(other, issueMessage("branch", first), first));
}
});
}
}
private static boolean isTrivialIfStatement(StatementTree node) {
return !node.is(Tree.Kind.BLOCK) || ((BlockTree) node).body().size() <= 1;
}
protected static boolean hasDefaultClause(SwitchTree switchStatement) {
return switchStatement.cases().stream()
.flatMap(caseGroupTree -> caseGroupTree.labels().stream())
.anyMatch(caseLabelTree -> "default".equals(caseLabelTree.caseOrDefaultKeyword().text()));
}
protected static boolean hasElseClause(IfStatementTree ifStatement) {
StatementTree elseStatement = ifStatement.elseStatement();
while (elseStatement != null && elseStatement.is(Tree.Kind.IF_STATEMENT)) {
elseStatement = ((IfStatementTree) elseStatement).elseStatement();
}
return elseStatement != null;
}
private void createIssue(Tree node, String message, Tree secondary) {
reportIssue(node, message, Collections.singletonList(new JavaFileScannerContext.Location("Original", secondary)), null);
}
private static String issueMessage(String type, Tree node) {
return String.format("This %s's code block is the same as the block for the %s on line %d.", type, type, LineUtils.startLine(node));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy