Make CPD "working set aware"

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4372 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Philippe Herlin
2006-05-02 18:34:23 +00:00
parent 85127f6948
commit 931af6b057
3 changed files with 71 additions and 16 deletions

View File

@ -1,6 +1,8 @@
package net.sourceforge.pmd.eclipse.cpd; package net.sourceforge.pmd.eclipse.cpd;
import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.Set;
import name.herlin.command.CommandException; import name.herlin.command.CommandException;
import net.sourceforge.pmd.cpd.SimpleRenderer; import net.sourceforge.pmd.cpd.SimpleRenderer;
@ -26,6 +28,9 @@ import org.eclipse.ui.IWorkbenchPart;
* @version $Revision$ * @version $Revision$
* *
* $Log$ * $Log$
* Revision 1.2 2006/05/02 18:34:23 phherlin
* Make CPD "working set aware"
*
* Revision 1.1 2005/05/31 23:04:11 phherlin * Revision 1.1 2005/05/31 23:04:11 phherlin
* Fix Bug 1190624: refactor CPD integration * Fix Bug 1190624: refactor CPD integration
* *
@ -49,6 +54,7 @@ import org.eclipse.ui.IWorkbenchPart;
public class CPDCheckProjectAction implements IObjectActionDelegate { public class CPDCheckProjectAction implements IObjectActionDelegate {
private static Log log = LogFactory.getLog("net.sourceforge.pmd.eclipse.CPDCheckProjectAction"); private static Log log = LogFactory.getLog("net.sourceforge.pmd.eclipse.CPDCheckProjectAction");
private IWorkbenchPart targetPart; private IWorkbenchPart targetPart;
private Set projects = new HashSet();
/** /**
* @see org.eclipse.ui.IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart) * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)

View File

@ -3,6 +3,9 @@ package net.sourceforge.pmd.eclipse.cpd;
import java.io.IOException; import java.io.IOException;
import net.sourceforge.pmd.cpd.CPD; import net.sourceforge.pmd.cpd.CPD;
import net.sourceforge.pmd.eclipse.model.ModelException;
import net.sourceforge.pmd.eclipse.model.ModelFactory;
import net.sourceforge.pmd.eclipse.model.ProjectPropertiesModel;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -10,6 +13,8 @@ import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceVisitor; import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.ResourceWorkingSetFilter;
/** /**
* A visitor to process IFile resource against CPD * A visitor to process IFile resource against CPD
@ -19,17 +24,17 @@ import org.eclipse.core.runtime.CoreException;
* @version $Revision$ * @version $Revision$
* *
* $Log$ * $Log$
* Revision 1.1 2005/05/31 23:04:11 phherlin * Revision 1.2 2006/05/02 18:34:23 phherlin
* Fix Bug 1190624: refactor CPD integration * Make CPD "working set aware"
* * Revision 1.1 2005/05/31 23:04:11 phherlin Fix Bug 1190624: refactor CPD integration
* Revision 1.4 2003/05/19 22:26:07 phherlin *
* Updating PMD engine to v1.05 * Revision 1.4 2003/05/19 22:26:07 phherlin Updating PMD engine to v1.05 Fixing CPD usage to conform to new engine implementation
* Fixing CPD usage to conform to new engine implementation *
*
*/ */
public class CPDVisitor implements IResourceVisitor { public class CPDVisitor implements IResourceVisitor {
private static Log log = LogFactory.getLog("net.sourceforge.pmd.eclipse.CPDVisitor"); private static Log log = LogFactory.getLog("net.sourceforge.pmd.eclipse.CPDVisitor");
private CPD cpd; private CPD cpd;
private boolean includeDerivedFiles;
/** /**
* Constructor for CPDVisitor. * Constructor for CPDVisitor.
@ -40,26 +45,58 @@ public class CPDVisitor implements IResourceVisitor {
} }
/** /**
* @see org.eclipse.core.resources.IResourceVisitor#visit(IResource) * @param includeDerivedFiles The includeDerivedFiles to set.
* Add java files into the CPD object */
public void setIncludeDerivedFiles(boolean includeDerivedFiles) {
this.includeDerivedFiles = includeDerivedFiles;
}
/**
* @see org.eclipse.core.resources.IResourceVisitor#visit(IResource) Add java files into the CPD object
*/ */
public boolean visit(IResource resource) throws CoreException { public boolean visit(IResource resource) throws CoreException {
log.debug("CPD Visiting " + resource.getName()); log.debug("CPD Visiting " + resource.getName());
boolean result = true; boolean result = true;
if ((resource instanceof IFile)
&& (((IFile) resource).getFileExtension() != null) if (resource instanceof IFile) {
&& ((IFile) resource).getFileExtension().equals("java")) { IFile file = (IFile) resource;
try { try {
log.debug("Add file " + resource.getName()); if ((((IFile) resource).getFileExtension() != null)
cpd.add(((IFile) resource).getLocation().toFile()); && ((IFile) resource).getFileExtension().equals("java")
&& (isFileInWorkingSet(file) && (this.includeDerivedFiles || (!this.includeDerivedFiles && !file
.isDerived())))) {
log.debug("Add file " + resource.getName());
cpd.add(((IFile) resource).getLocation().toFile());
result = false;
}
} catch (IOException e) { } catch (IOException e) {
log.warn("IOException when adding file " + resource.getName() + " to CPD. Continuing.", e); log.warn("IOException when adding file " + resource.getName() + " to CPD. Continuing.", e);
} catch (ModelException e) {
log.warn("ModelException when adding file " + resource.getName() + " to CPD. Continuing.", e);
} }
result = false;
} }
return result; return result;
} }
/**
* Test if a file is in the PMD working set
*
* @param file
* @return true if the file should be checked
*/
private boolean isFileInWorkingSet(final IFile file) throws ModelException {
boolean fileInWorkingSet = true;
final ProjectPropertiesModel model = ModelFactory.getFactory().getProperiesModelForProject(file.getProject());
final IWorkingSet workingSet = model.getProjectWorkingSet();
if (workingSet != null) {
final ResourceWorkingSetFilter filter = new ResourceWorkingSetFilter();
filter.setWorkingSet(workingSet);
fileInWorkingSet = filter.select(null, null, file);
}
return fileInWorkingSet;
}
} }

View File

@ -47,6 +47,9 @@ import net.sourceforge.pmd.cpd.Renderer;
import net.sourceforge.pmd.eclipse.PMDPlugin; import net.sourceforge.pmd.eclipse.PMDPlugin;
import net.sourceforge.pmd.eclipse.PMDPluginConstants; import net.sourceforge.pmd.eclipse.PMDPluginConstants;
import net.sourceforge.pmd.eclipse.cmd.AbstractDefaultCommand; import net.sourceforge.pmd.eclipse.cmd.AbstractDefaultCommand;
import net.sourceforge.pmd.eclipse.model.ModelException;
import net.sourceforge.pmd.eclipse.model.ModelFactory;
import net.sourceforge.pmd.eclipse.model.ProjectPropertiesModel;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -63,6 +66,9 @@ import org.eclipse.core.runtime.CoreException;
* @version $Revision$ * @version $Revision$
* *
* $Log$ * $Log$
* Revision 1.2 2006/05/02 18:34:23 phherlin
* Make CPD "working set aware"
*
* Revision 1.1 2005/05/31 23:04:11 phherlin * Revision 1.1 2005/05/31 23:04:11 phherlin
* Fix Bug 1190624: refactor CPD integration * Fix Bug 1190624: refactor CPD integration
* *
@ -121,6 +127,9 @@ public class DetectCutAndPasteCmd extends AbstractDefaultCommand {
} catch (IOException e) { } catch (IOException e) {
log.debug("IO Exception: " + e.getMessage(), e); log.debug("IO Exception: " + e.getMessage(), e);
throw new CommandException(e); throw new CommandException(e);
} catch (ModelException e) {
log.debug("Model Exception: " + e.getMessage(), e);
throw new CommandException(e);
} finally { } finally {
this.setTerminated(true); this.setTerminated(true);
} }
@ -171,11 +180,14 @@ public class DetectCutAndPasteCmd extends AbstractDefaultCommand {
* @return matches an iterator to CPD matches * @return matches an iterator to CPD matches
* @throws CoreException * @throws CoreException
*/ */
private Iterator detectCutAndPaste() throws CoreException { private Iterator detectCutAndPaste() throws CoreException, ModelException {
log.debug("Searching for project files"); log.debug("Searching for project files");
final int minTileSize = PMDPlugin.getDefault().getPreferenceStore().getInt(PMDPlugin.MIN_TILE_SIZE_PREFERENCE); final int minTileSize = PMDPlugin.getDefault().getPreferenceStore().getInt(PMDPlugin.MIN_TILE_SIZE_PREFERENCE);
final ProjectPropertiesModel model = ModelFactory.getFactory().getProperiesModelForProject(project);
final CPD cpd = new CPD(minTileSize, new LanguageFactory().createLanguage(LanguageFactory.JAVA_KEY)); final CPD cpd = new CPD(minTileSize, new LanguageFactory().createLanguage(LanguageFactory.JAVA_KEY));
final CPDVisitor visitor = new CPDVisitor(cpd); final CPDVisitor visitor = new CPDVisitor(cpd);
visitor.setIncludeDerivedFiles(model.isIncludeDerivedFiles());
this.project.accept(visitor); this.project.accept(visitor);
log.debug("Performing CPD"); log.debug("Performing CPD");