Skip to content

Commit 7fe6a1f

Browse files
committed
Rule set result nodes should show number of violations per severity level #113
1 parent c3de97e commit 7fe6a1f

File tree

13 files changed

+140
-57
lines changed

13 files changed

+140
-57
lines changed

src/main/java/com/intellij/plugins/bodhi/pmd/PMDInvoker.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
import com.intellij.openapi.wm.ToolWindow;
1717
import com.intellij.openapi.wm.ToolWindowManager;
1818
import com.intellij.plugins.bodhi.pmd.core.PMDResultCollector;
19-
import com.intellij.plugins.bodhi.pmd.tree.PMDBranchNode;
20-
import com.intellij.plugins.bodhi.pmd.tree.PMDRootNode;
21-
import com.intellij.plugins.bodhi.pmd.tree.PMDRuleSetEntryNode;
19+
import com.intellij.plugins.bodhi.pmd.tree.*;
2220

2321
import java.io.File;
2422
import java.util.LinkedList;
@@ -177,13 +175,13 @@ public void run() {
177175
// sort rules by priority, rule and suppressed nodes are comparable
178176
resultRuleNodes.sort(null);
179177

180-
if (resultRuleNodes.size() != 0) {
178+
if (!resultRuleNodes.isEmpty()) {
181179
String ruleSetName = PMDUtil.getBareFileNameFromPath(ruleSetPath);
182180
String desc = PMDResultCollector.getRuleSetDescription(ruleSetPath);
183-
PMDBranchNode ruleSetNode = resultPanel.addCreateBranchNodeAtRoot(ruleSetName);
181+
PMDRuleSetNode ruleSetNode = resultPanel.addCreateRuleSetNodeAtRoot(ruleSetName);
184182
ruleSetNode.setToolTip(desc);
185183
//Add all rule nodes to the tree
186-
for (PMDBranchNode resultRuleNode : resultRuleNodes) {
184+
for (PMDRuleSetEntryNode resultRuleNode : resultRuleNodes) {
187185
resultPanel.addNode(ruleSetNode, resultRuleNode);
188186
}
189187
rootNode.calculateCounts();

src/main/java/com/intellij/plugins/bodhi/pmd/PMDResultPanel.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,13 +391,13 @@ private Editor openEditor(HasPositionInFile finding) {
391391
}
392392

393393
/**
394-
* Adds a branch node to the tree as a direct child of the root, and return it
394+
* Adds a rule set node to the tree as a direct child of the root, and return it
395395
*
396396
* @param name the rule name
397397
* @return the created rule set node
398398
*/
399-
public PMDBranchNode addCreateBranchNodeAtRoot(String name) {
400-
return (PMDBranchNode)addNode(rootNode, new PMDBranchNode(name));
399+
public PMDRuleSetNode addCreateRuleSetNodeAtRoot(String name) {
400+
return (PMDRuleSetNode)addNode(rootNode, new PMDRuleSetNode(name));
401401
}
402402

403403
/**

src/main/java/com/intellij/plugins/bodhi/pmd/core/PMDResultCollector.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public static RuleSet loadRuleSet(String path) throws InvalidRuleSetException {
200200
Thread.currentThread().setContextClassLoader(PMDResultCollector.class.getClassLoader());
201201
try {
202202
RuleSet rs = new RuleSetLoader().loadFromResource(path);
203-
if (rs.getRules().size() != 0) {
203+
if (!rs.getRules().isEmpty()) {
204204
return rs;
205205
}
206206
} catch (RuleSetLoadException e) {
@@ -223,7 +223,7 @@ public InvalidRuleSetException(final Throwable cause) {
223223

224224
private static class PMDResultAsTreeRenderer extends AbstractIncrementingRenderer {
225225

226-
private final Map<RuleKey, PMDRuleBranchNode> ruleKeyToNodeMap;
226+
private final Map<RuleKey, PMDRuleNode> ruleKeyToNodeMap;
227227
private final List<PMDRuleSetEntryNode> pmdRuleResultNodes;
228228
private final PMDErrorBranchNode processingErrorsNode;
229229
private final Set<String> filesWithError = new HashSet<>();
@@ -243,15 +243,15 @@ public void renderFileViolations(Iterator<RuleViolation> violations) {
243243
PMDResultCollector.report.addRuleViolation(iRuleViolation);
244244
Rule rule = iRuleViolation.getRule();
245245
RuleKey key = new RuleKey(rule);
246-
PMDRuleBranchNode ruleNode = ruleKeyToNodeMap.get(key);
246+
PMDRuleNode ruleNode = ruleKeyToNodeMap.get(key);
247247
if (ruleNode == null) {
248-
ruleNode = nodeFactory.createRuleBranchNode(rule);
248+
ruleNode = nodeFactory.createRuleNode(rule);
249249
ruleNode.setToolTip(rule.getDescription());
250250
ruleKeyToNodeMap.put(key, ruleNode);
251251
}
252252
ruleNode.add(nodeFactory.createViolationLeafNode(new PMDViolation(iRuleViolation)));
253253
}
254-
for (PMDRuleBranchNode ruleNode : ruleKeyToNodeMap.values()) {
254+
for (PMDRuleNode ruleNode : ruleKeyToNodeMap.values()) {
255255
if (ruleNode.getChildCount() > 0 && !pmdRuleResultNodes.contains(ruleNode)) {
256256
pmdRuleResultNodes.add(ruleNode);
257257
}

src/main/java/com/intellij/plugins/bodhi/pmd/handlers/PMDCheckinHandler.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515
import com.intellij.plugins.bodhi.pmd.PMDResultPanel;
1616
import com.intellij.plugins.bodhi.pmd.PMDUtil;
1717
import com.intellij.plugins.bodhi.pmd.core.PMDResultCollector;
18-
import com.intellij.plugins.bodhi.pmd.tree.PMDBranchNode;
19-
import com.intellij.plugins.bodhi.pmd.tree.PMDRootNode;
20-
import com.intellij.plugins.bodhi.pmd.tree.PMDRuleSetEntryNode;
21-
import com.intellij.plugins.bodhi.pmd.tree.PMDTreeNodeFactory;
18+
import com.intellij.plugins.bodhi.pmd.tree.*;
2219
import com.intellij.util.PairConsumer;
2320
import com.intellij.util.ui.UIUtil;
2421
import org.apache.commons.lang.StringUtils;
@@ -103,18 +100,18 @@ public ReturnResult beforeCheckin(@Nullable CommitExecutor executor,
103100

104101
PMDResultCollector.clearReport();
105102

106-
List<PMDBranchNode> ruleSetResultNodes = new ArrayList<>();
103+
List<PMDRuleSetNode> ruleSetResultNodes = new ArrayList<>();
107104
for (String ruleSetPath : plugin.getCustomRuleSetPaths()) {
108-
PMDBranchNode ruleSetResultNode = scanFiles(ruleSetPath, plugin);
105+
PMDRuleSetNode ruleSetResultNode = scanFiles(ruleSetPath, plugin);
109106
if (ruleSetResultNode != null) {
110107
ruleSetResultNodes.add(ruleSetResultNode);
111108
}
112109
}
113110
return processScanResults(ruleSetResultNodes, project);
114111
}
115112

116-
private PMDBranchNode scanFiles(String ruleSetPath, PMDProjectComponent plugin) {
117-
PMDBranchNode ruleSetResultNode = null;
113+
private PMDRuleSetNode scanFiles(String ruleSetPath, PMDProjectComponent plugin) {
114+
PMDRuleSetNode ruleSetResultNode = null;
118115
PMDResultCollector collector = new PMDResultCollector();
119116
List<File> files = new ArrayList<>(checkinProjectPanel.getFiles());
120117

@@ -125,18 +122,18 @@ private PMDBranchNode scanFiles(String ruleSetPath, PMDProjectComponent plugin)
125122
return ruleSetResultNode;
126123
}
127124

128-
private PMDBranchNode createRuleSetNodeWithResults(String ruleSetPath, List<PMDRuleSetEntryNode> ruleResultNodes) {
125+
private PMDRuleSetNode createRuleSetNodeWithResults(String ruleSetPath, List<PMDRuleSetEntryNode> ruleResultNodes) {
129126
ruleSetPath = PMDUtil.getFileNameFromPath(ruleSetPath) + ";" + ruleSetPath;
130-
PMDBranchNode ruleSetNode = PMDTreeNodeFactory.getInstance().createBranchNode(ruleSetPath);
127+
PMDRuleSetNode ruleSetNode = PMDTreeNodeFactory.getInstance().createRuleSetNode(ruleSetPath);
131128

132-
for (PMDBranchNode ruleResultNode : ruleResultNodes) {
129+
for (PMDRuleSetEntryNode ruleResultNode : ruleResultNodes) {
133130
ruleSetNode.add(ruleResultNode);
134131
}
135132
return ruleSetNode;
136133
}
137134

138135
@NotNull
139-
private ReturnResult processScanResults(List<PMDBranchNode> ruleSetResultNodes, Project project) {
136+
private ReturnResult processScanResults(List<PMDRuleSetNode> ruleSetResultNodes, Project project) {
140137
int violations = toViolations(ruleSetResultNodes);
141138
if (violations > 0) {
142139
int answer = promptUser(project, violations);
@@ -151,9 +148,9 @@ private ReturnResult processScanResults(List<PMDBranchNode> ruleSetResultNodes,
151148
return ReturnResult.COMMIT;
152149
}
153150

154-
private int toViolations(List<PMDBranchNode> ruleSetResultNodes) {
151+
private int toViolations(List<PMDRuleSetNode> ruleSetResultNodes) {
155152
int violations = 0;
156-
for (PMDBranchNode ruleSetResultNode : ruleSetResultNodes) {
153+
for (PMDRuleSetNode ruleSetResultNode : ruleSetResultNodes) {
157154
violations += ruleSetResultNode.getViolationCount();
158155
}
159156
return violations;
@@ -168,7 +165,7 @@ private int promptUser(Project project, int violations) {
168165
message("handler.before.checkin.error.title"), buttons, 0, UIUtil.getWarningIcon());
169166
}
170167

171-
private void showToolWindow(List<PMDBranchNode> ruleSetResultNodes, Project project) {
168+
private void showToolWindow(List<PMDRuleSetNode> ruleSetResultNodes, Project project) {
172169
PMDProjectComponent plugin = project.getComponent(PMDProjectComponent.class);
173170
PMDResultPanel resultPanel = plugin.getResultPanel();
174171
plugin.setupToolWindow();

src/main/java/com/intellij/plugins/bodhi/pmd/tree/BasePMDNode.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,6 @@ protected PMDResultPanel getRootResultPanel() {
4848
public abstract int getSuppressedCount();
4949

5050
public abstract int getErrorCount();
51+
52+
public abstract int getSevViolationCount(Severity sev);
5153
}

src/main/java/com/intellij/plugins/bodhi/pmd/tree/PMDBranchNode.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.intellij.plugins.bodhi.pmd.tree;
22

33
import javax.swing.tree.TreeNode;
4+
import java.util.EnumMap;
45
import java.util.Enumeration;
6+
import java.util.Map;
7+
58
import static com.intellij.ui.SimpleTextAttributes.GRAYED_ATTRIBUTES;
69

710
/**
@@ -13,13 +16,14 @@
1316
* @author bodhi
1417
* @version 1.2
1518
*/
16-
public class PMDBranchNode extends BasePMDNode {
19+
public abstract class PMDBranchNode extends BasePMDNode {
1720

1821
private final String nodeName;
1922
private String toolTip;
2023
private int violationCount = 0;
2124
private int suppressedCount = 0;
2225
private int errorCount = 0;
26+
private final Map<Severity, Integer> sevToViolationCount = new EnumMap<>(Severity.class);
2327

2428
/**
2529
* Create a node with the given value as node name.
@@ -30,14 +34,25 @@ public PMDBranchNode(String nodeName) {
3034
this.nodeName = nodeName;
3135
}
3236

37+
/**
38+
* initialize violations counts for all severities with 0
39+
*/
40+
private void initSevToViolationCount() {
41+
for (Severity sev : Severity.values()) {
42+
sevToViolationCount.put(sev, 0);
43+
}
44+
}
45+
3346
/**
3447
* Calculate the number of violations, suppressed violations and processing errors from the child nodes.
48+
* In addition, the number of violation counts per severity level.
3549
* The leaf nodes will answer with 0 or 1, and the branch nodes will aggregate/calculate recursively.
3650
*/
37-
public void calculateCounts() {
51+
public synchronized void calculateCounts() {
3852
violationCount = 0;
3953
suppressedCount = 0;
4054
errorCount = 0;
55+
initSevToViolationCount();
4156
Enumeration<TreeNode> children = children();
4257
while (children.hasMoreElements()) {
4358
Object child = children.nextElement();
@@ -49,6 +64,9 @@ public void calculateCounts() {
4964
violationCount += node.getViolationCount();
5065
suppressedCount += node.getSuppressedCount();
5166
errorCount += node.getErrorCount();
67+
for (Severity sev : Severity.values()) {
68+
sevToViolationCount.put(sev, sevToViolationCount.get(sev) + node.getSevViolationCount(sev));
69+
}
5270
}
5371
}
5472
}
@@ -67,7 +85,7 @@ public String getNodeName() {
6785
*
6886
* @return the violation count
6987
*/
70-
public int getViolationCount() {
88+
public synchronized int getViolationCount() {
7189
if (violationCount == 0) {
7290
calculateCounts();
7391
}
@@ -79,7 +97,7 @@ public int getViolationCount() {
7997
*
8098
* @return the violation count
8199
*/
82-
public int getSuppressedCount() {
100+
public synchronized int getSuppressedCount() {
83101
return suppressedCount;
84102
}
85103

@@ -88,10 +106,15 @@ public int getSuppressedCount() {
88106
*
89107
* @return the violation count
90108
*/
91-
public int getErrorCount() {
109+
public synchronized int getErrorCount() {
92110
return errorCount;
93111
}
94112

113+
@Override
114+
public synchronized int getSevViolationCount(Severity sev) {
115+
return sevToViolationCount.get(sev);
116+
}
117+
95118
/**
96119
* Sets the tooltip.
97120
*
@@ -105,7 +128,7 @@ public String getToolTip() {
105128
return toolTip;
106129
}
107130

108-
public void render(PMDCellRenderer cellRenderer, boolean expanded) {
131+
public synchronized void render(PMDCellRenderer cellRenderer, boolean expanded) {
109132
cellRenderer.append(getNodeName());
110133
if (violationCount > 0 ) {
111134
cellRenderer.append(getCountMsg("violation", violationCount), GRAYED_ATTRIBUTES);

src/main/java/com/intellij/plugins/bodhi/pmd/tree/PMDErrorBranchNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
*
66
* @author jborgers
77
*/
8-
public class PMDErrorBranchNode extends PMDBranchNode{
8+
public class PMDErrorBranchNode extends PMDBranchNode {
99
public PMDErrorBranchNode(String name) {
1010
super(name);
1111
}
1212

1313
@Override
14-
public void render(PMDCellRenderer cellRenderer, boolean expanded) {
14+
public synchronized void render(PMDCellRenderer cellRenderer, boolean expanded) {
1515
cellRenderer.setIcon(Severity.BLOCKER.getIcon());
1616
super.render(cellRenderer, expanded);
1717
}

src/main/java/com/intellij/plugins/bodhi/pmd/tree/PMDLeafNode.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public int getErrorCount() {
3333
return 0;
3434
}
3535

36+
@Override
37+
public int getSevViolationCount(Severity sev) {
38+
return 0;
39+
}
40+
3641
/**
3742
* Open editor and select/navigate to the correct line and column in the file.
3843
*

src/main/java/com/intellij/plugins/bodhi/pmd/tree/PMDRuleBranchNode.java renamed to src/main/java/com/intellij/plugins/bodhi/pmd/tree/PMDRuleNode.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
* @author jborgers
1414
*/
15-
public class PMDRuleBranchNode extends PMDRuleSetEntryNode {
15+
public class PMDRuleNode extends PMDRuleSetEntryNode {
1616

1717
private final RulePriority priority;
1818
private final RuleKey ruleKey;
@@ -23,14 +23,14 @@ public class PMDRuleBranchNode extends PMDRuleSetEntryNode {
2323
*
2424
* @param rule The PMD rule to set.
2525
*/
26-
public PMDRuleBranchNode(Rule rule) {
26+
public PMDRuleNode(Rule rule) {
2727
super(rule.getName());
2828
priority = rule.getPriority();
2929
this.ruleKey = new RuleKey(rule);
3030
}
3131

3232
@Override
33-
public void render(PMDCellRenderer cellRenderer, boolean expanded) {
33+
public synchronized void render(PMDCellRenderer cellRenderer, boolean expanded) {
3434
cellRenderer.setIconForRulePriority(priority);
3535
super.render(cellRenderer, expanded);
3636
}
@@ -39,7 +39,7 @@ public void render(PMDCellRenderer cellRenderer, boolean expanded) {
3939
public boolean equals(Object o) {
4040
if (this == o) return true;
4141
if (o == null || getClass() != o.getClass()) return false;
42-
PMDRuleBranchNode that = (PMDRuleBranchNode) o;
42+
PMDRuleNode that = (PMDRuleNode) o;
4343
return Objects.equals(ruleKey, that.ruleKey);
4444
}
4545

@@ -50,8 +50,8 @@ public int hashCode() {
5050

5151
@Override
5252
public int compareTo(@NotNull PMDRuleSetEntryNode o) {
53-
if (o instanceof PMDRuleBranchNode) {
54-
return ruleKey.compareTo(((PMDRuleBranchNode) o).ruleKey);
53+
if (o instanceof PMDRuleNode) {
54+
return ruleKey.compareTo(((PMDRuleNode) o).ruleKey);
5555
}
5656
return -1; // always before suppressed
5757
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.intellij.plugins.bodhi.pmd.tree;
2+
3+
import com.intellij.ui.SimpleTextAttributes;
4+
import static com.intellij.ui.SimpleTextAttributes.GRAYED_ATTRIBUTES;
5+
import static com.intellij.ui.SimpleTextAttributes.STYLE_PLAIN;
6+
7+
public class PMDRuleSetNode extends PMDBranchNode {
8+
/**
9+
* Create a node with the given value as node name.
10+
*
11+
* @param nodeName The node name to set.
12+
*/
13+
public PMDRuleSetNode(String nodeName) {
14+
super(nodeName);
15+
}
16+
17+
@Override
18+
public synchronized void render(PMDCellRenderer cellRenderer, boolean expanded) {
19+
cellRenderer.append(getNodeName());
20+
if (getViolationCount() > 0 ) {
21+
cellRenderer.append(" (" + getViolationCount() + " violation" + ((getViolationCount() != 1) ? "s: " : ": "), GRAYED_ATTRIBUTES);
22+
boolean first = true;
23+
for (Severity sev : Severity.values()) {
24+
int count = getSevViolationCount(sev);
25+
if (count > 0) {
26+
if (!first) {
27+
cellRenderer.append(" + ", GRAYED_ATTRIBUTES);
28+
}
29+
cellRenderer.append(Integer.toString(count), new SimpleTextAttributes(STYLE_PLAIN, sev.getColor()));
30+
first = false;
31+
}
32+
}
33+
cellRenderer.append(")", GRAYED_ATTRIBUTES);
34+
}
35+
if (getSuppressedCount() > 0) {
36+
cellRenderer.append(getCountMsg("suppressed violation", getSuppressedCount()), GRAYED_ATTRIBUTES);
37+
}
38+
if (getErrorCount() > 0) {
39+
cellRenderer.append(getCountMsg("processing error", getErrorCount()), GRAYED_ATTRIBUTES);
40+
}
41+
if (getViolationCount() == 0 && getSuppressedCount() == 0 && getErrorCount() == 0) {
42+
cellRenderer.append(getCountMsg("violation", getViolationCount()), GRAYED_ATTRIBUTES);
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)