pmd-eclipse: fix #988 Build path exclusions not honored

This commit is contained in:
Andreas Dangel committed 2013-08-11 00:17:31 +02:00
1 parent 90590dce93
commit 4ccb814cf1
5 files changed
+98 -3

No files matched your search

+2
View File
@@ -6,6 +6,7 @@ Eclipse Update Site: http://sourceforge.net/projects/pmd/files/pmd-eclipse/updat
## ????: 4.0.1.v????
* The official update site is now: http://sourceforge.net/projects/pmd/files/pmd-eclipse/update-site/
* Fixed Build path exclusions not honored ([bug #988])
* Fixed right click to add reviewed comment missing ([bug #1052])
* Fixed PMD Eclipse: How to ... documentation missing ([bug #1061])
* Fixed Properties page: "Rule-Selection" should be disabled if project-local config is selected ([bug #1070])
@@ -17,6 +18,7 @@ Eclipse Update Site: http://sourceforge.net/projects/pmd/files/pmd-eclipse/updat
* Fixed PMD Eclipse plugin doesn't analyze project if it has non-existing source folders ([bug #1116])
* Fixed An internal error occurred during: "RenderReport" ([bug #1117])
[bug #988]: https://sourceforge.net/p/pmd/bugs/988/
[bug #1052]: https://sourceforge.net/p/pmd/bugs/1052/
[bug #1061]: https://sourceforge.net/p/pmd/bugs/1061/
[bug #1070]: https://sourceforge.net/p/pmd/bugs/1070/
@@ -55,8 +55,10 @@ import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
@@ -142,10 +144,31 @@ public class PMDPlugin extends AbstractUIPlugin {
String compilerCompliance = jProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
return Language.JAVA.getVersion(compilerCompliance);
}
return null;
}
public static IClasspathEntry buildSourceClassPathEntryFor(IProject project) {
IJavaProject jProject = JavaProjectsByIProject.get(project);
if (jProject == null) {
jProject = JavaCore.create(project);
JavaProjectsByIProject.put(project, jProject);
}
if (jProject.exists()) {
try {
if (jProject.getRawClasspath() != null) {
for (IClasspathEntry entry : jProject.getRawClasspath()) {
if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
return entry;
}
}
}
} catch (JavaModelException e) {
log.error("Couldn't determine source classpath", e);
}
}
return null;
}
private void disposeResources() {
disposeAll(coloursByRGB.values());
@@ -35,6 +35,8 @@
*/
package net.sourceforge.pmd.eclipse.runtime.cmd;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
@@ -71,6 +73,7 @@ import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.MultiRule;
@@ -456,6 +459,8 @@ public class ReviewCodeCmd extends AbstractDefaultCommand {
RuleSetUtil.retainOnly(filteredRuleSet, activeRuleNames);
filteredRuleSet.addExcludePatterns(preferences.activeExclusionPatterns());
filteredRuleSet.addIncludePatterns(preferences.activeInclusionPatterns());
filteredRuleSet.addExcludePatterns(properties.getBuildPathExcludePatterns());
filteredRuleSet.addIncludePatterns(properties.getBuildPathIncludePatterns());
taskScope(filteredRuleSet.getRules().size(), ruleSet.getRules().size());
return filteredRuleSet;
@@ -36,6 +36,7 @@
package net.sourceforge.pmd.eclipse.runtime.properties;
import java.io.File;
import java.util.Set;
import net.sourceforge.pmd.RuleSet;
@@ -169,5 +170,18 @@ public interface IProjectProperties {
*
*/
void sync() throws PropertiesException;
/**
* The exclude patterns determined from the project's build path.
* Note: not persisted.
* @return exclude patterns
*/
Set<String> getBuildPathExcludePatterns();
/**
* The include patterns determined from the project's build path.
* Note: not persisted.
* @return include patterns
*/
Set<String> getBuildPathIncludePatterns();
}
@@ -38,6 +38,9 @@ package net.sourceforge.pmd.eclipse.runtime.properties.impl;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleSet;
@@ -54,6 +57,8 @@ import org.apache.log4j.Logger;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.ui.IWorkingSet;
/**
@@ -78,6 +83,8 @@ public class ProjectPropertiesImpl implements IProjectProperties {
private boolean includeDerivedFiles;
private boolean violationsAsErrors = true;
private boolean fullBuildEnabled = true; // default in case didn't come from properties
private Set<String> buildPathExcludePatterns = new HashSet<String>();
private Set<String> buildPathIncludePatterns = new HashSet<String>();
/**
* The default constructor takes a project as an argument
@@ -87,6 +94,43 @@ public class ProjectPropertiesImpl implements IProjectProperties {
this.project = project;
this.projectPropertiesManager = projectPropertiesManager;
this.projectRuleSet = PMDPlugin.getDefault().getPreferencesManager().getRuleSet();
determineBuildPathIncludesExcludes();
}
/**
* Determines the included and excluded paths configured for the build path of this eclipse project.
*/
private void determineBuildPathIncludesExcludes() {
IClasspathEntry source = PMDPlugin.buildSourceClassPathEntryFor(project);
if (source != null) {
try {
String basePath = new File(project.getWorkspace().getRoot().getLocation().toOSString()
+ java.io.File.separator + source.getPath().toOSString()).getCanonicalPath();
if (!basePath.endsWith(File.separator)) {
basePath += File.separator;
}
if (source.getExclusionPatterns() != null) {
for (IPath path : source.getExclusionPatterns()) {
String pathString = path.toOSString();
if (!pathString.endsWith(File.separator)) {
pathString += File.separator;
}
buildPathExcludePatterns.add(basePath + pathString + ".*");
}
}
if (source.getInclusionPatterns() != null) {
for (IPath path : source.getInclusionPatterns()) {
String pathString = path.toOSString();
if (!pathString.endsWith(File.separator)) {
pathString += File.separator;
}
buildPathIncludePatterns.add(basePath + pathString + ".*");
}
}
} catch (IOException e) {
log.error("Couldn't determine build class path", e);
}
}
}
/**
@@ -360,4 +404,11 @@ public class ProjectPropertiesImpl implements IProjectProperties {
+ " violationsAsErrors: "+violationsAsErrors;
}
public Set<String> getBuildPathExcludePatterns() {
return buildPathExcludePatterns;
}
public Set<String> getBuildPathIncludePatterns() {
return buildPathIncludePatterns;
}
}