Skip to content

Commit 036544a

Browse files
committed
Merge branch 'pr-91'
2 parents 1636b3f + 2609a22 commit 036544a

File tree

11 files changed

+159
-0
lines changed

11 files changed

+159
-0
lines changed

ReleaseNotes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ This is a minor release.
1919
The project property "Handle high priority violations as Eclipse errors" now works again.
2020
* The PMD markers are now also visible on the overview ruler. They can be customized or disabled via
2121
workspace preferences, General, Editors, Text Editors, Annotations.
22+
* In PMD's general preferences, there is a new option "Determine applicable file types automatically". This new
23+
option is enabled by default. When enabled, only the files of the languages, for which rules are active,
24+
are considered. This is a fix for [bug #88](https://github.com/pmd/pmd-eclipse-plugin/issues/88).
2225

2326
### Fixed Issues
2427

@@ -27,6 +30,7 @@ This is a minor release.
2730
* [#76](https://github.com/pmd/pmd-eclipse-plugin/issues/76): Global rule management is saved even if cancelled
2831
* [#78](https://github.com/pmd/pmd-eclipse-plugin/issues/78): Project properties cannot be loaded anymore
2932
* [#86](https://github.com/pmd/pmd-eclipse-plugin/issues/86): Add Markers next to Scroll bar
33+
* [#88](https://github.com/pmd/pmd-eclipse-plugin/issues/88): PMD is executed for all file types regardless of active rules
3034
* [#89](https://github.com/pmd/pmd-eclipse-plugin/issues/89): Upgrade to PMD 6.12.0
3135
* [#1359](https://sourceforge.net/p/pmd/bugs/1359/): PMD violations in eclipse should be shown on editor by scrollbar
3236

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import net.sourceforge.pmd.eclipse.EclipseUtils;
2828
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
2929
import net.sourceforge.pmd.eclipse.runtime.PMDRuntimeConstants;
30+
import net.sourceforge.pmd.eclipse.runtime.preferences.IPreferences;
3031
import net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties;
3132
import net.sourceforge.pmd.eclipse.ui.actions.RuleSetUtil;
3233

@@ -174,4 +175,64 @@ public void testReviewCmdNullResource() {
174175
cmd.setResourceDelta(null);
175176
cmd.performExecute();
176177
}
178+
179+
private static Rule findRuleByName(RuleSet ruleset, String ruleName, String language) {
180+
for (Rule rule : ruleset.getRules()) {
181+
if (rule.getName().equals(ruleName) && rule.getLanguage().getTerseName().equals(language)) {
182+
return rule;
183+
}
184+
}
185+
return null;
186+
}
187+
188+
@Test
189+
public void testReviewCmdBasicAllFilesDefault() throws Exception {
190+
// add a second file
191+
EclipseUtils.createTestSourceFile(testProject, "/src/Test.js", "function(arg) { notDeclaredVariable = 1; }");
192+
testProject.refreshLocal(IResource.DEPTH_INFINITE, null);
193+
194+
IProjectProperties projectProperties = PMDPlugin.getDefault().getPropertiesManager().loadProjectProperties(testProject);
195+
RuleSet projectRuleSet = projectProperties.getProjectRuleSet();
196+
Rule emptyCatchBlock = findRuleByName(projectRuleSet, "EmptyCatchBlock", "java");
197+
projectRuleSet = RuleSetUtil.clearRules(projectRuleSet);
198+
projectRuleSet = RuleSetUtil.addRule(projectRuleSet, emptyCatchBlock);
199+
projectProperties.setProjectRuleSet(projectRuleSet);
200+
PMDPlugin.getDefault().getPropertiesManager().storeProjectProperties(projectProperties);
201+
202+
final ReviewCodeCmd cmd = new ReviewCodeCmd();
203+
cmd.addResource(this.testProject);
204+
cmd.performExecute();
205+
cmd.join();
206+
207+
Assert.assertEquals(1, cmd.getFileCount()); // only one file analyzed
208+
}
209+
210+
@Test
211+
public void testReviewCmdBasicAllFiles() throws Exception {
212+
try {
213+
// add a second file
214+
EclipseUtils.createTestSourceFile(testProject, "/src/Test.js", "function(arg) { notDeclaredVariable = 1; }");
215+
testProject.refreshLocal(IResource.DEPTH_INFINITE, null);
216+
217+
PMDPlugin.getDefault().loadPreferences().setDetermineFiletypesAutomatically(false);
218+
219+
IProjectProperties projectProperties = PMDPlugin.getDefault().getPropertiesManager().loadProjectProperties(testProject);
220+
RuleSet projectRuleSet = projectProperties.getProjectRuleSet();
221+
Rule emptyCatchBlock = findRuleByName(projectRuleSet, "EmptyCatchBlock", "java");
222+
projectRuleSet = RuleSetUtil.clearRules(projectRuleSet);
223+
projectRuleSet = RuleSetUtil.addRule(projectRuleSet, emptyCatchBlock);
224+
projectProperties.setProjectRuleSet(projectRuleSet);
225+
PMDPlugin.getDefault().getPropertiesManager().storeProjectProperties(projectProperties);
226+
227+
final ReviewCodeCmd cmd = new ReviewCodeCmd();
228+
cmd.addResource(this.testProject);
229+
cmd.performExecute();
230+
cmd.join();
231+
232+
Assert.assertEquals(2, cmd.getFileCount()); // both files analyzed, although unnecessary, since only
233+
// the one java rule is active and no javascript rules
234+
} finally {
235+
PMDPlugin.getDefault().loadPreferences().setDetermineFiletypesAutomatically(IPreferences.DETERMINE_FILETYPES_AUTOMATICALLY_DEFAULT);
236+
}
237+
}
177238
}

net.sourceforge.pmd.eclipse.plugin.test/src/main/resources/test.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class Test {
99

1010
public void foo() {
1111
try {
12+
// EmptyCatchBlock!!
1213
} catch (Exception e) {
1314

1415
}

net.sourceforge.pmd.eclipse.plugin/messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ preference.pmd.label.check_after_saving = Check code after saving
3030
preference.pmd.label.use_dfa = Enable dataflow anomaly analysis (experimental)
3131
preference.pmd.label.use_project_build_path = Enable using Java Project Build Path. Disable if your Eclipse JVM version is incompatible with .class file versions.
3232
preference.pmd.label.max_violations_pfpr = Maximum reported violations per file per rule
33+
preference.pmd.label.determine_filetypes_automatically = Determine applicable file types automatically (based on rule languages)
3334
preference.pmd.tooltip.max_violations_pfpr = This helps limit report sizes and improves overall performance
3435
preference.pmd.message.invalid_numeric_value = Incorrect numeric value entered
3536
preference.pmd.label.review_pmd_style = Use PMD style (// NOPMD comment)

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.HashSet;
1616
import java.util.Iterator;
1717
import java.util.List;
18+
import java.util.Locale;
1819
import java.util.Map;
1920
import java.util.Set;
2021
import java.util.Stack;
@@ -72,6 +73,7 @@ public class BaseVisitor {
7273
private Map<IFile, Set<MarkerInfo2>> accumulator;
7374
// private PMDEngine pmdEngine;
7475
private RuleSets ruleSets;
76+
private Set<String> fileExtensions;
7577
private int fileCount;
7678
private long pmdDuration;
7779
private IProjectProperties projectProperties;
@@ -225,6 +227,10 @@ public void setRuleSets(final RuleSets ruleSets) {
225227
this.ruleSets = ruleSets;
226228
}
227229

230+
public void setFileExtensions(Set<String> fileExtensions) {
231+
this.fileExtensions = fileExtensions;
232+
}
233+
228234
/**
229235
* @return the number of files that has been processed
230236
*/
@@ -264,6 +270,18 @@ protected final void reviewResource(IResource resource) {
264270
return;
265271
}
266272

273+
if (PMDPlugin.getDefault().loadPreferences().isDetermineFiletypesAutomatically()) {
274+
if (fileExtensions != null) {
275+
if (!fileExtensions.contains(file.getFileExtension().toLowerCase(Locale.ROOT))) {
276+
PMDPlugin.getDefault().logInformation("Skipping file " + file.getName() + " based on file extension");
277+
LOG.debug("Skipping file " + file.getName() + " based on file extension");
278+
return;
279+
}
280+
} else {
281+
LOG.warn("Can't check for file extensions.");
282+
}
283+
}
284+
267285
Reader input = null;
268286
try {
269287
boolean included = isIncluded(file);

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.HashMap;
1010
import java.util.HashSet;
1111
import java.util.List;
12+
import java.util.Locale;
1213
import java.util.Map;
1314
import java.util.Set;
1415

@@ -49,6 +50,7 @@
4950
import net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties;
5051
import net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException;
5152
import net.sourceforge.pmd.eclipse.ui.actions.RuleSetUtil;
53+
import net.sourceforge.pmd.lang.Language;
5254
import net.sourceforge.pmd.util.StringUtil;
5355

5456
/**
@@ -419,6 +421,7 @@ private void processResource(IResource resource) {
419421
}
420422

421423
RuleSets ruleSets = rulesetsFrom(resource);
424+
Set<String> fileExtensions = determineFileExtensions(ruleSets);
422425
// final PMDEngine pmdEngine = getPmdEngineForProject(project);
423426
int targetCount = 0;
424427
if (resource.exists()) {
@@ -432,6 +435,7 @@ private void processResource(IResource resource) {
432435
final ResourceVisitor visitor = new ResourceVisitor();
433436
visitor.setMonitor(getMonitor());
434437
visitor.setRuleSets(ruleSets);
438+
visitor.setFileExtensions(fileExtensions);
435439
// visitor.setPmdEngine(pmdEngine);
436440
visitor.setAccumulator(markersByFile);
437441
visitor.setUseTaskMarker(taskMarker);
@@ -459,6 +463,22 @@ private void processResource(IResource resource) {
459463
}
460464
}
461465

466+
private Set<String> determineFileExtensions(RuleSets ruleSets) {
467+
Set<Language> languages = new HashSet<Language>();
468+
for (Rule rule : ruleSets.getAllRules()) {
469+
languages.add(rule.getLanguage());
470+
}
471+
Set<String> fileExtensions = new HashSet<String>();
472+
for (Language language : languages) {
473+
for (String extension : language.getExtensions()) {
474+
fileExtensions.add(extension.toLowerCase(Locale.ROOT));
475+
}
476+
}
477+
logInfo("Determined applicable file extensions: " + fileExtensions);
478+
LOG.debug("Determined applicable file extensions: " + fileExtensions);
479+
return fileExtensions;
480+
}
481+
462482
/**
463483
* Review an entire project
464484
*/
@@ -576,6 +596,7 @@ private void processResourceDelta() {
576596
+ project.getName());
577597

578598
final RuleSets ruleSets = rulesetsFromResourceDelta();
599+
Set<String> fileExtensions = determineFileExtensions(ruleSets);
579600

580601
// PMDEngine pmdEngine = getPmdEngineForProject(project);
581602
int targetCount = countDeltaElement(resourceDelta);
@@ -587,6 +608,7 @@ private void processResourceDelta() {
587608
DeltaVisitor visitor = new DeltaVisitor();
588609
visitor.setMonitor(getMonitor());
589610
visitor.setRuleSets(ruleSets);
611+
visitor.setFileExtensions(fileExtensions);
590612
// visitor.setPmdEngine(pmdEngine);
591613
visitor.setAccumulator(markersByFile);
592614
visitor.setUseTaskMarker(taskMarker);

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public interface IPreferences {
2929
boolean PMD_CHECK_AFTER_SAVE_DEFAULT = false;
3030
boolean PMD_USE_CUSTOM_PRIORITY_NAMES_DEFAULT = true;
3131
int MAX_VIOLATIONS_PFPR_DEFAULT = 5;
32+
boolean DETERMINE_FILETYPES_AUTOMATICALLY_DEFAULT = true;
3233
String REVIEW_ADDITIONAL_COMMENT_DEFAULT = "by {0} on {1}";
3334
boolean REVIEW_PMD_STYLE_ENABLED_DEFAULT = true;
3435
int MIN_TILE_SIZE_DEFAULT = 25;
@@ -143,6 +144,19 @@ public interface IPreferences {
143144
*/
144145
void setMaxViolationsPerFilePerRule(int maxViolationPerFilePerRule);
145146

147+
/**
148+
* If true: When checking, whether a given file should be analyzed by PMD, take
149+
* the rule's language and the language's file extensions into account.
150+
* @return
151+
*/
152+
boolean isDetermineFiletypesAutomatically();
153+
154+
/**
155+
* Sets whether the rule's language file extensions should be considered or not.
156+
* @param determineFiletypesAutomatically
157+
*/
158+
void setDetermineFiletypesAutomatically(boolean determineFiletypesAutomatically);
159+
146160
/**
147161
* Get the review additional comment. This comment is a text appended to the
148162
* review comment that is inserted into the code when a violation is

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class PreferencesImpl implements IPreferences {
3939
private boolean checkAfterSaveEnabled;
4040
private boolean useCustomPriorityNames;
4141
private int maxViolationsPerFilePerRule;
42+
private boolean determineFiletypesAutomatically;
4243
private String reviewAdditionalComment;
4344
private boolean reviewPmdStyleEnabled;
4445
private int minTileSize;
@@ -146,6 +147,16 @@ public void setMaxViolationsPerFilePerRule(int maxViolationPerFilePerRule) {
146147
this.maxViolationsPerFilePerRule = maxViolationPerFilePerRule;
147148
}
148149

150+
@Override
151+
public boolean isDetermineFiletypesAutomatically() {
152+
return determineFiletypesAutomatically;
153+
}
154+
155+
@Override
156+
public void setDetermineFiletypesAutomatically(boolean determineFiletypesAutomatically) {
157+
this.determineFiletypesAutomatically = determineFiletypesAutomatically;
158+
}
159+
149160
/**
150161
* @see net.sourceforge.pmd.eclipse.runtime.preferences.IPreferences#getReviewAdditionalComment()
151162
*/

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class PreferencesManagerImpl implements IPreferencesManager {
6868
private static final String PMD_VIOLATIONS_OUTLINE_ENABLED = PMDPlugin.PLUGIN_ID + ".pmd_outline_enabled";
6969
private static final String PMD_CHECK_AFTER_SAVE_ENABLED = PMDPlugin.PLUGIN_ID + ".pmd_check_after_save_enabled";
7070
private static final String MAX_VIOLATIONS_PFPR = PMDPlugin.PLUGIN_ID + ".max_violations_pfpr";
71+
private static final String DETERMINE_FILETYPES_AUTOMATICALLY = PMDPlugin.PLUGIN_ID + ".determine_filetypes_automatically";
7172
private static final String REVIEW_ADDITIONAL_COMMENT = PMDPlugin.PLUGIN_ID + ".review_additional_comment";
7273
private static final String REVIEW_PMD_STYLE_ENABLED = PMDPlugin.PLUGIN_ID + ".review_pmd_style_enabled";
7374
private static final String PMD_USE_CUSTOM_PRIORITY_NAMES = PMDPlugin.PLUGIN_ID + ".use_custom_priority_names";
@@ -159,6 +160,7 @@ public IPreferences reloadPreferences() {
159160
loadCheckAfterSaveEnabled();
160161
loadUseCustomPriorityNames();
161162
loadMaxViolationsPerFilePerRule();
163+
loadDetermineFiletypesAutomatically();
162164
loadReviewAdditionalComment();
163165
loadReviewPmdStyleEnabled();
164166
loadMinTileSize();
@@ -231,6 +233,7 @@ public void storePreferences(IPreferences thePreferences) {
231233
storeCheckAfterSaveEnabled();
232234
storeUseCustomPriorityNames();
233235
storeMaxViolationsPerFilePerRule();
236+
storeDetermineFiletypesAutomatically();
234237
storeReviewAdditionalComment();
235238
storeReviewPmdStyleEnabled();
236239
storeMinTileSize();
@@ -305,6 +308,11 @@ private void loadMaxViolationsPerFilePerRule() {
305308
loadPreferencesStore.setDefault(MAX_VIOLATIONS_PFPR, IPreferences.MAX_VIOLATIONS_PFPR_DEFAULT);
306309
preferences.setMaxViolationsPerFilePerRule(loadPreferencesStore.getInt(MAX_VIOLATIONS_PFPR));
307310
}
311+
312+
private void loadDetermineFiletypesAutomatically() {
313+
loadPreferencesStore.setDefault(DETERMINE_FILETYPES_AUTOMATICALLY, IPreferences.DETERMINE_FILETYPES_AUTOMATICALLY_DEFAULT);
314+
preferences.setDetermineFiletypesAutomatically(loadPreferencesStore.getBoolean(DETERMINE_FILETYPES_AUTOMATICALLY));
315+
}
308316

309317
private void loadReviewAdditionalComment() {
310318
loadPreferencesStore.setDefault(REVIEW_ADDITIONAL_COMMENT, IPreferences.REVIEW_ADDITIONAL_COMMENT_DEFAULT);
@@ -486,6 +494,10 @@ private void storeMaxViolationsPerFilePerRule() {
486494
storePreferencesStore.setValue(MAX_VIOLATIONS_PFPR, preferences.getMaxViolationsPerFilePerRule());
487495
}
488496

497+
private void storeDetermineFiletypesAutomatically() {
498+
storePreferencesStore.setValue(DETERMINE_FILETYPES_AUTOMATICALLY, preferences.isDetermineFiletypesAutomatically());
499+
}
500+
489501
private void storeReviewAdditionalComment() {
490502
storePreferencesStore.setValue(REVIEW_ADDITIONAL_COMMENT, preferences.getReviewAdditionalComment());
491503
}

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/nls/StringKeys.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class StringKeys {
4141
public static final String PREF_GENERAL_LABEL_USE_DFA = "preference.pmd.label.use_dfa";
4242
public static final String PREF_GENERAL_LABEL_USE_PROJECT_BUILD_PATH = "preference.pmd.label.use_project_build_path";
4343
public static final String PREF_GENERAL_LABEL_MAX_VIOLATIONS_PFPR = "preference.pmd.label.max_violations_pfpr";
44+
public static final String PREF_GENERAL_LABEL_DETERMINE_FILETYPES_AUTOMATICALLY = "preference.pmd.label.determine_filetypes_automatically";
4445
public static final String PREF_GENERAL_TOOLTIP_MAX_VIOLATIONS_PFPR = "preference.pmd.tooltip.max_violations_pfpr";
4546
public static final String PREF_GENERAL_MESSAGE_INVALID_NUMERIC_VALUE = "preference.pmd.message.invalid_numeric_value";
4647
public static final String PREF_GENERAL_REVIEW_PMD_STYLE = "preference.pmd.label.review_pmd_style";

0 commit comments

Comments
 (0)