-
Notifications
You must be signed in to change notification settings - Fork 681
/
SwitchWithTooManyCasesCheck.java
74 lines (66 loc) · 2.62 KB
/
SwitchWithTooManyCasesCheck.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* 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 GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* 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 GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.java.checks;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.JavaFileScannerContext;
import org.sonar.plugins.java.api.semantic.Type;
import org.sonar.plugins.java.api.tree.CaseGroupTree;
import org.sonar.plugins.java.api.tree.SwitchTree;
import org.sonar.plugins.java.api.tree.Tree;
@Rule(key = "S1479")
public class SwitchWithTooManyCasesCheck extends IssuableSubscriptionVisitor {
private static final int DEFAULT_MAXIMUM_CASES = 30;
@RuleProperty(
key = "maximum",
description = "Maximum number of case",
defaultValue = "" + DEFAULT_MAXIMUM_CASES)
public int maximumCases = DEFAULT_MAXIMUM_CASES;
@Override
public List<Tree.Kind> nodesToVisit() {
return Arrays.asList(Tree.Kind.SWITCH_STATEMENT, Tree.Kind.SWITCH_EXPRESSION);
}
@Override
public void visitNode(Tree tree) {
SwitchTree switchTree = (SwitchTree) tree;
if (isSwitchOverEnum(switchTree)) {
return;
}
List<CaseGroupTree> cases = switchTree.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(switchTree.switchKeyword(),
String.format("Reduce the number of non-empty switch cases from %d to at most %d.", size, maximumCases),
secondary, null);
}
}
private static boolean isSwitchOverEnum(SwitchTree switchStatementTree) {
Type type = switchStatementTree.expression().symbolType();
return type.symbol().isEnum();
}
}