Skip to content

Commit 306d703

Browse files
committed
De-duplicate project ruleset when loading the project properties
Cherry-Picked. See also #57
1 parent 5b17c4c commit 306d703

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

ReleaseNotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Eclipse Update Site:
1414
### Fixed Issues
1515

1616
* [#52](https://github.com/pmd/pmd-eclipse-plugin/issues/52): Eclipse Internal Error - Out of Memory
17+
* [#57](https://github.com/pmd/pmd-eclipse-plugin/pull/57): De-duplicate project ruleset when loading the project properties
1718

1819
### External Contributions
1920

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@
4343
import java.io.StringWriter;
4444
import java.util.ArrayList;
4545
import java.util.HashMap;
46+
import java.util.HashSet;
4647
import java.util.List;
4748
import java.util.Map;
49+
import java.util.Set;
4850
import javax.xml.bind.DataBindingException;
4951
import javax.xml.bind.JAXBContext;
5052
import javax.xml.bind.JAXBElement;
@@ -102,6 +104,7 @@ private static JAXBContext initJaxbContext() {
102104
* @param project
103105
* a project
104106
*/
107+
@Override
105108
public IProjectProperties loadProjectProperties(final IProject project) throws PropertiesException {
106109
LOG.debug("Loading project properties for project " + project.getName());
107110
try {
@@ -134,6 +137,7 @@ public IProjectProperties loadProjectProperties(final IProject project) throws P
134137
/**
135138
* @see net.sourceforge.pmd.eclipse.runtime.properties.IProjectPropertiesManager#storeProjectProperties(net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties)
136139
*/
140+
@Override
137141
public void storeProjectProperties(IProjectProperties projectProperties) throws PropertiesException {
138142
LOG.debug("Storing project properties for project " + projectProperties.getProject().getName());
139143
try {
@@ -257,14 +261,24 @@ private void fillProjectProperties(IProjectProperties projectProperties, Project
257261
private void setRuleSetFromProperties(IProjectProperties projectProperties, RuleSpecTO[] rules)
258262
throws PropertiesException {
259263
final RuleSet pluginRuleSet = PMDPlugin.getDefault().getPreferencesManager().getRuleSet();
260-
int n = rules == null ? 0 : rules.length;
264+
265+
// de-duplicate rules
266+
Set<String> ruleNamesToAdd = new HashSet<>();
267+
for (RuleSpecTO rule : rules) {
268+
if (!ruleNamesToAdd.add(rule.getName())) {
269+
PMDPlugin.getDefault().logInformation("Duplicated Rule found: " + rule.getName() + ". This rule will be ignored.");
270+
LOG.debug("Duplicated Rule found: " + rule.getName() + ". This rule will be ignored.");
271+
}
272+
}
273+
261274
List<Rule> rulesToAdd = new ArrayList<Rule>();
262-
for (int i = 0; i < n; i++) {
263-
final Rule rule = pluginRuleSet.getRuleByName(rules[i].getName());
275+
for (String ruleName : ruleNamesToAdd) {
276+
Rule rule = pluginRuleSet.getRuleByName(ruleName);
264277
if (rule != null) {
265278
rulesToAdd.add(rule);
266279
} else {
267-
LOG.debug("The rule " + rules[i].getName() + " cannot be found. ignore.");
280+
PMDPlugin.getDefault().logInformation("The rule " + ruleName + " could not be found. The rule will be ignored.");
281+
LOG.debug("The rule " + ruleName + " could not be found. The rule will be ignored.");
268282
}
269283
}
270284

0 commit comments

Comments
 (0)