Repository navigation
Expand file tree
/
Copy pathSeveralBreakOrContinuePerLoopCheck.java
More file actions
151 lines (131 loc) · 4.53 KB
/
Copy pathSeveralBreakOrContinuePerLoopCheck.java
File metadata and controls
151 lines (131 loc) · 4.53 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* SonarQube Java
* Copyright (C) SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* You can redistribute and/or modify this program under the terms of
* the Sonar Source-Available License Version 1, as published by SonarSource Sàrl.
*
* 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 org.sonar.check.Rule;
import org.sonar.plugins.java.api.JavaFileScanner;
import org.sonar.plugins.java.api.JavaFileScannerContext;
import org.sonar.plugins.java.api.tree.BaseTreeVisitor;
import org.sonar.plugins.java.api.tree.BreakStatementTree;
import org.sonar.plugins.java.api.tree.ContinueStatementTree;
import org.sonar.plugins.java.api.tree.DoWhileStatementTree;
import org.sonar.plugins.java.api.tree.ForEachStatement;
import org.sonar.plugins.java.api.tree.ForStatementTree;
import org.sonar.plugins.java.api.tree.SwitchStatementTree;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.WhileStatementTree;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
@Rule(key = "S135")
public class SeveralBreakOrContinuePerLoopCheck extends BaseTreeVisitor implements JavaFileScanner {
private final Deque<List<Tree>> breakAndContinueCounter = new ArrayDeque<>();
private final Deque<Boolean> currentScopeIsSwitch = new ArrayDeque<>();
private int loopCount;
private JavaFileScannerContext context;
@Override
public void scanFile(JavaFileScannerContext context) {
this.context = context;
loopCount = 0;
breakAndContinueCounter.clear();
currentScopeIsSwitch.clear();
try {
scan(context.getTree());
} finally {
breakAndContinueCounter.clear();
currentScopeIsSwitch.clear();
}
}
@Override
public void visitForStatement(ForStatementTree tree) {
enterLoop();
super.visitForStatement(tree);
leaveLoop(tree.forKeyword());
}
@Override
public void visitForEachStatement(ForEachStatement tree) {
enterLoop();
super.visitForEachStatement(tree);
leaveLoop(tree.forKeyword());
}
@Override
public void visitWhileStatement(WhileStatementTree tree) {
enterLoop();
super.visitWhileStatement(tree);
leaveLoop(tree.whileKeyword());
}
@Override
public void visitDoWhileStatement(DoWhileStatementTree tree) {
enterLoop();
super.visitDoWhileStatement(tree);
leaveLoop(tree.doKeyword());
}
@Override
public void visitBreakStatement(BreakStatementTree tree) {
if (isInLoop() && !isInSwitch()) {
incrementBreakCounter(tree);
}
super.visitBreakStatement(tree);
}
@Override
public void visitContinueStatement(ContinueStatementTree tree) {
if (isInLoop()) {
incrementBreakCounter(tree);
}
super.visitContinueStatement(tree);
}
private boolean isInLoop() {
return loopCount > 0;
}
private boolean isInSwitch() {
return currentScopeIsSwitch.peek();
}
private void incrementBreakCounter(Tree tree) {
if (!breakAndContinueCounter.isEmpty()) {
breakAndContinueCounter.peek().add(tree);
}
}
@Override
public void visitSwitchStatement(SwitchStatementTree tree) {
currentScopeIsSwitch.push(true);
super.visitSwitchStatement(tree);
currentScopeIsSwitch.pop();
}
private void enterLoop() {
loopCount++;
breakAndContinueCounter.push(new ArrayList<>());
currentScopeIsSwitch.push(false);
}
private void leaveLoop(Tree primaryLocationTree) {
List<Tree> breakAndContinues = new ArrayList<>();
if (!breakAndContinueCounter.isEmpty()) {
breakAndContinues = breakAndContinueCounter.pop();
}
if (breakAndContinues.size() > 1) {
int effortToFix = breakAndContinues.size() - 1;
List<JavaFileScannerContext.Location> secondaryLocations = breakAndContinues.stream()
.map(t -> new JavaFileScannerContext.Location(String.format("\"%s\" statement.", t.is(Tree.Kind.BREAK_STATEMENT) ? "break" : "continue"), t))
.toList();
context.reportIssue(this, primaryLocationTree,
"Reduce the total number of break and continue statements in this loop to use at most one.",
secondaryLocations,
effortToFix);
}
loopCount--;
currentScopeIsSwitch.pop();
}
}