From 4ccb814cf14fd18f72932618f29004c8a102493b Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sun, 11 Aug 2013 00:17:31 +0200 Subject: [PATCH] pmd-eclipse: fix #988 Build path exclusions not honored --- pmd-eclipse-plugin/ReleaseNotes.md | 2 + .../pmd/eclipse/plugin/PMDPlugin.java | 27 +++++++++- .../eclipse/runtime/cmd/ReviewCodeCmd.java | 5 ++ .../properties/IProjectProperties.java | 16 +++++- .../impl/ProjectPropertiesImpl.java | 51 +++++++++++++++++++ 5 files changed, 98 insertions(+), 3 deletions(-) diff --git a/pmd-eclipse-plugin/ReleaseNotes.md b/pmd-eclipse-plugin/ReleaseNotes.md index 892f626455..017d8b5914 100644 --- a/pmd-eclipse-plugin/ReleaseNotes.md +++ b/pmd-eclipse-plugin/ReleaseNotes.md @@ -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/ diff --git a/pmd-eclipse-plugin/net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/plugin/PMDPlugin.java b/pmd-eclipse-plugin/net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/plugin/PMDPlugin.java index 6ab0fdd10a..814ccbe81d 100644 --- a/pmd-eclipse-plugin/net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/plugin/PMDPlugin.java +++ b/pmd-eclipse-plugin/net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/plugin/PMDPlugin.java @@ -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()); diff --git a/pmd-eclipse-plugin/net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/cmd/ReviewCodeCmd.java b/pmd-eclipse-plugin/net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/cmd/ReviewCodeCmd.java index fcc5772962..6428636d73 100644 --- a/pmd-eclipse-plugin/net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/cmd/ReviewCodeCmd.java +++ b/pmd-eclipse-plugin/net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/cmd/ReviewCodeCmd.java @@ -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; diff --git a/pmd-eclipse-plugin/net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/IProjectProperties.java b/pmd-eclipse-plugin/net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/IProjectProperties.java index 7eae75d1f0..a709dee85e 100644 --- a/pmd-eclipse-plugin/net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/IProjectProperties.java +++ b/pmd-eclipse-plugin/net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/IProjectProperties.java @@ -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 getBuildPathExcludePatterns(); + + /** + * The include patterns determined from the project's build path. + * Note: not persisted. + * @return include patterns + */ + Set getBuildPathIncludePatterns(); } diff --git a/pmd-eclipse-plugin/net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/impl/ProjectPropertiesImpl.java b/pmd-eclipse-plugin/net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/impl/ProjectPropertiesImpl.java index 63f4242e89..f3c038b027 100644 --- a/pmd-eclipse-plugin/net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/impl/ProjectPropertiesImpl.java +++ b/pmd-eclipse-plugin/net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/impl/ProjectPropertiesImpl.java @@ -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 buildPathExcludePatterns = new HashSet(); + private Set buildPathIncludePatterns = new HashSet(); /** * 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 getBuildPathExcludePatterns() { + return buildPathExcludePatterns; + } + + public Set getBuildPathIncludePatterns() { + return buildPathIncludePatterns; + } } \ No newline at end of file