Skip to content

Commit c72303d

Browse files
committed
Fixes #23: NPE while creating ruleset due to hashcode
1 parent 81fa280 commit c72303d

File tree

12 files changed

+26
-24
lines changed

12 files changed

+26
-24
lines changed

ReleaseNotes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Eclipse Update Site: <https://sourceforge.net/projects/pmd/files/pmd-eclipse/upd
66

77
## ????: 4.0.14.v????
88

9+
* Fixed NPE while creating ruleset due to hashcode ([issue #23](https://github.com/pmd/pmd-eclipse-plugin/issues/23))
10+
911

1012
## 29-April-2017: 4.0.13.v20170429-1921
1113

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/core/RuleSetManagerTest.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
public class RuleSetManagerTest {
5151
private IRuleSetManager ruleSetManager;
5252

53+
private static RuleSet createEmptyTestRuleSet() {
54+
return RuleSetUtil.newEmpty("test-ruleset", "Ruleset for unit testing");
55+
}
56+
5357
/**
5458
* @see junit.framework.TestCase#setUp()
5559
*/
@@ -64,7 +68,7 @@ public void setUp() throws Exception {
6468
*/
6569
@Test
6670
public void testDuplicateRegister() {
67-
final RuleSet ruleSet = RuleSetUtil.newEmpty();
71+
final RuleSet ruleSet = createEmptyTestRuleSet();
6872
this.ruleSetManager.registerRuleSet(ruleSet);
6973
this.ruleSetManager.registerRuleSet(ruleSet);
7074
Assert.assertEquals("Only one rule set should have been registered", 1, this.ruleSetManager.getRegisteredRuleSets().size());
@@ -76,7 +80,7 @@ public void testDuplicateRegister() {
7680
*/
7781
@Test
7882
public void testDuplicateRegisterDefault() {
79-
final RuleSet ruleSet = RuleSetUtil.newEmpty();
83+
final RuleSet ruleSet = createEmptyTestRuleSet();
8084
this.ruleSetManager.registerDefaultRuleSet(ruleSet);
8185
this.ruleSetManager.registerDefaultRuleSet(ruleSet);
8286
Assert.assertEquals("Only one rule set should have been registered", 1, this.ruleSetManager.getDefaultRuleSets().size());
@@ -88,7 +92,7 @@ public void testDuplicateRegisterDefault() {
8892
*/
8993
@Test
9094
public void testDuplicateUnregister() {
91-
final RuleSet ruleSet = RuleSetUtil.newEmpty();
95+
final RuleSet ruleSet = createEmptyTestRuleSet();
9296
this.ruleSetManager.registerRuleSet(ruleSet);
9397

9498
this.ruleSetManager.unregisterRuleSet(ruleSet);
@@ -102,7 +106,7 @@ public void testDuplicateUnregister() {
102106
*/
103107
@Test
104108
public void testDuplicateUnregisterDefault() {
105-
final RuleSet ruleSet = RuleSetUtil.newEmpty();
109+
final RuleSet ruleSet = createEmptyTestRuleSet();
106110
this.ruleSetManager.registerRuleSet(ruleSet);
107111

108112
this.ruleSetManager.unregisterDefaultRuleSet(ruleSet);
@@ -116,7 +120,7 @@ public void testDuplicateUnregisterDefault() {
116120
*/
117121
@Test
118122
public void testRegisterDefaultRuleSet() {
119-
final RuleSet ruleSet = RuleSetUtil.newEmpty();
123+
final RuleSet ruleSet = createEmptyTestRuleSet();
120124
this.ruleSetManager.registerDefaultRuleSet(ruleSet);
121125
Assert.assertEquals("Default RuleSet not registrered!", 1, this.ruleSetManager.getDefaultRuleSets().size());
122126
}
@@ -157,7 +161,7 @@ public void testRegisterNullRuleSet() {
157161
*/
158162
@Test
159163
public void testRegisterRuleSet() {
160-
final RuleSet ruleSet = RuleSetUtil.newEmpty();
164+
final RuleSet ruleSet = createEmptyTestRuleSet();
161165
this.ruleSetManager.registerRuleSet(ruleSet);
162166
Assert.assertEquals("RuleSet not registrered!", 1, this.ruleSetManager.getRegisteredRuleSets().size());
163167
}
@@ -168,7 +172,7 @@ public void testRegisterRuleSet() {
168172
*/
169173
@Test
170174
public void testUnregisterDefaultRuleSet() {
171-
final RuleSet ruleSet = RuleSetUtil.newEmpty();
175+
final RuleSet ruleSet = createEmptyTestRuleSet();
172176
this.ruleSetManager.registerDefaultRuleSet(ruleSet);
173177
Assert.assertEquals("Default RuleSet not registered!", 1, this.ruleSetManager.getDefaultRuleSets().size());
174178

@@ -212,7 +216,7 @@ public void testUnregisterNullRuleSet() {
212216
*/
213217
@Test
214218
public void testUnregisterRuleSet() {
215-
final RuleSet ruleSet = RuleSetUtil.newEmpty();
219+
final RuleSet ruleSet = createEmptyTestRuleSet();
216220
this.ruleSetManager.registerRuleSet(ruleSet);
217221
Assert.assertEquals("RuleSet not registered!", 1, this.ruleSetManager.getRegisteredRuleSets().size());
218222

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/ProjectPropertiesModelTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public void testBug() throws PropertiesException, RuleSetNotFoundException, Core
155155
int ruleCountBefore = projectRuleSet.getRules().size();
156156

157157
// 2. remove a rule (keep its name for assertion)
158-
RuleSet newRuleSet = RuleSetUtil.newEmpty();
158+
RuleSet newRuleSet = RuleSetUtil.newEmpty("test-ruleset", "RuleSet for unit testing");
159159
newRuleSet = RuleSetUtil.addRules(newRuleSet, projectRuleSet.getRules());
160160
final Rule removedRule = newRuleSet.getRuleByName("UnnecessaryParentheses");
161161
newRuleSet = RuleSetUtil.removeRule(newRuleSet, removedRule);

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/ui/properties/UpdateProjectPropertiesCmdTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void testBug() throws CommandException, PropertiesException {
6363
int ruleCountBefore = projectRuleSet.getRules().size();
6464

6565
// 2. remove a rule (keep its name for assertion)
66-
RuleSet newRuleSet = RuleSetUtil.newEmpty();
66+
RuleSet newRuleSet = RuleSetUtil.newEmpty("test-ruleset", "ruleset for unit testing");
6767
newRuleSet = RuleSetUtil.addRules(newRuleSet, projectRuleSet.getRules());
6868
final Rule removedRule = newRuleSet.getRuleByName("UnnecessaryParentheses");
6969
newRuleSet = RuleSetUtil.removeRule(newRuleSet, removedRule);

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/cmd/ReviewCodeCmd.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public Set<IFile> markedFiles() {
132132

133133
private RuleSet currentRules() {
134134
// FIXME
135-
return RuleSetUtil.newEmpty();
135+
return RuleSetUtil.newEmpty(RuleSetUtil.DEFAULT_RULESET_NAME, RuleSetUtil.DEFAULT_RULESET_DESCRIPTION);
136136
}
137137

138138
private Map<Rule, String> misconfiguredRulesIn(RuleSet ruleset) {

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/preferences/impl/PreferencesManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ private RuleSet getRuleSetFromStateLocation() {
465465

466466
// Finally, build a default ruleset
467467
if (preferredRuleSet == null) {
468-
preferredRuleSet = RuleSetUtil.newEmpty("pmd-eclipse", "PMD Plugin preferences rule set");
468+
preferredRuleSet = RuleSetUtil.newEmpty(RuleSetUtil.DEFAULT_RULESET_NAME, RuleSetUtil.DEFAULT_RULESET_DESCRIPTION);
469469

470470
IRuleSetManager ruleSetManager = PMDPlugin.getDefault().getRuleSetManager();
471471
for (RuleSet ruleSet: ruleSetManager.getDefaultRuleSets()) {

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/impl/ProjectPropertiesManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ private void setRuleSetFromProperties(IProjectProperties projectProperties, Rule
258258
}
259259
}
260260

261-
RuleSet ruleSet = RuleSetUtil.newEmpty();
261+
RuleSet ruleSet = RuleSetUtil.newEmpty(RuleSetUtil.DEFAULT_RULESET_NAME, RuleSetUtil.DEFAULT_RULESET_DESCRIPTION);
262262
ruleSet = RuleSetUtil.addRules(ruleSet, rulesToAdd);
263263
ruleSet = RuleSetUtil.setExcludePatterns(ruleSet, pluginRuleSet.getExcludePatterns());
264264
ruleSet = RuleSetUtil.setIncludePatterns(ruleSet, pluginRuleSet.getIncludePatterns());

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/actions/RuleSetUtil.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public static RuleSet newCopyOf(RuleSet original) {
2626
return factory.createRuleSetCopy(original);
2727
}
2828

29+
public static final String DEFAULT_RULESET_NAME = "pmd-eclipse";
30+
public static final String DEFAULT_RULESET_DESCRIPTION = "PMD Plugin preferences rule set";
31+
2932
/**
3033
* This should not really work but the ruleset hands out its
3134
* internal container....oops! :)
@@ -51,13 +54,6 @@ public static RuleSet removeRule(RuleSet ruleSet, Rule removedRule) {
5154
return retainOnly(ruleSet, wantedRules);
5255
}
5356

54-
public static RuleSet newEmpty() {
55-
RuleSetFactory factory = new RuleSetFactory();
56-
Set<String> emptySet = Collections.emptySet();
57-
Set<Rule> emptyRules = Collections.emptySet();
58-
return factory.createNewRuleSet(null, null, null, emptySet, emptySet, emptyRules);
59-
}
60-
6157
public static RuleSet addExcludePatterns(RuleSet ruleSet, Collection<String> activeExclusionPatterns,
6258
Collection<String> buildPathExcludePatterns) {
6359
Set<String> newExcludePatterns = new HashSet<String>(ruleSet.getExcludePatterns());

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/preferences/RuleSetSelectionDialog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ private boolean hasCheckedRules() {
346346
* @return
347347
*/
348348
private RuleSet getSelectedRules() {
349-
RuleSet rs = RuleSetUtil.newEmpty();
349+
RuleSet rs = RuleSetUtil.newEmpty(RuleSetUtil.DEFAULT_RULESET_NAME, RuleSetUtil.DEFAULT_RULESET_DESCRIPTION);
350350
rs = RuleSetUtil.setFileName(rs, selectedRuleSet.getFileName());
351351
rs = RuleSetUtil.addExcludePatterns(rs, selectedRuleSet.getExcludePatterns());
352352
rs = RuleSetUtil.addIncludePatterns(rs, selectedRuleSet.getIncludePatterns());

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/preferences/br/RuleTableManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ private void doImport(RuleSet selectedRuleSet, boolean doByReference) {
475475

476476
try {
477477
if (doByReference) {
478-
RuleSet filteredRS = RuleSetUtil.newEmpty();
478+
RuleSet filteredRS = RuleSetUtil.newEmpty(RuleSetUtil.DEFAULT_RULESET_NAME, RuleSetUtil.DEFAULT_RULESET_DESCRIPTION);
479479
filteredRS = RuleSetUtil.setFileName(filteredRS, selectedRuleSet.getFileName());
480480
filteredRS = RuleSetUtil.addRules(filteredRS, selectedRuleSet.getRules());
481481

0 commit comments

Comments
 (0)