Updating PMD engine to v1.05

Fixing CPD usage to conform to new engine implementation


git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1932 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Philippe Herlin
2003-05-19 22:26:07 +00:00
parent 37faf93973
commit 885f0a09fd
2 changed files with 75 additions and 111 deletions

View File

@ -1,6 +1,9 @@
package net.sourceforge.pmd.eclipse;
import net.sourceforge.pmd.cpd.CPD;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceVisitor;
@ -15,12 +18,14 @@ import org.eclipse.jface.dialogs.MessageDialog;
* @version $Revision$
*
* $Log$
* Revision 1.3 2003/03/18 23:28:36 phherlin
* *** keyword substitution change ***
* 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
*
*/
public class CPDVisitor implements IResourceVisitor {
CPD cpd;
private static Log log = LogFactory.getLog("net.sourceforge.pmd.eclipse.CPDVisitor");
private CPD cpd;
/**
* Constructor for CPDVisitor.
@ -35,11 +40,13 @@ public class CPDVisitor implements IResourceVisitor {
* Add java files into the CPD object
*/
public boolean visit(IResource resource) throws CoreException {
log.debug("CPD Visiting " + resource);
boolean result = true;
if ((resource instanceof IFile)
&& (((IFile) resource).getFileExtension() != null)
&& ((IFile) resource).getFileExtension().equals("java")) {
try {
log.debug("CPD adding file " + resource.getName());
cpd.add(((IFile) resource).getLocation().toFile());
} catch (Exception e) {
MessageDialog.openError(null, "CPD", e.toString());

View File

@ -1,26 +1,28 @@
package net.sourceforge.pmd.eclipse.actions;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import net.sourceforge.pmd.cpd.CPD;
import net.sourceforge.pmd.cpd.CPDListener;
import net.sourceforge.pmd.cpd.Results;
import net.sourceforge.pmd.cpd.Tile;
import net.sourceforge.pmd.cpd.TokenEntry;
import net.sourceforge.pmd.cpd.LanguageFactory;
import net.sourceforge.pmd.cpd.Mark;
import net.sourceforge.pmd.cpd.Match;
import net.sourceforge.pmd.eclipse.CPDReportWindow;
import net.sourceforge.pmd.eclipse.CPDVisitor;
import net.sourceforge.pmd.eclipse.PMDPlugin;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
@ -32,12 +34,14 @@ import org.eclipse.ui.IWorkbenchPart;
* @version $Revision$
*
* $Log$
* Revision 1.4 2003/03/18 23:28:36 phherlin
* *** keyword substitution change ***
* Revision 1.5 2003/05/19 22:26:07 phherlin
* Updating PMD engine to v1.05
* Fixing CPD usage to conform to new engine implementation
*
*/
public class CPDCheckProjectAction implements IObjectActionDelegate, IRunnableWithProgress {
IWorkbenchPart targetPart;
private static Log log = LogFactory.getLog("net.sourceforge.pmd.eclipse.CPDCheckProjectAction");
private IWorkbenchPart targetPart;
/**
* Constructor for CPDCheckProjectAction.
@ -55,45 +59,65 @@ public class CPDCheckProjectAction implements IObjectActionDelegate, IRunnableWi
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
CPD cpd = new CPD();
//cpd.setListener(new CPDActivityListener(monitor)); //for some reason CPD dies when using this listener
int minTileSize = PMDPlugin.getDefault().getPreferenceStore().getInt(PMDPlugin.MIN_TILE_SIZE_PREFERENCE);
log.debug("Instantiating CPD for Java language with tile size to " + minTileSize);
CPD cpd = new CPD(minTileSize, new LanguageFactory().createLanguage(LanguageFactory.JAVA_KEY));
PMDPlugin.getDefault().getPreferenceStore().setDefault(PMDPlugin.MIN_TILE_SIZE_PREFERENCE, 25);
cpd.setMinimumTileSize(PMDPlugin.getDefault().getPreferenceStore().getInt(PMDPlugin.MIN_TILE_SIZE_PREFERENCE));
CPDVisitor visitor = new CPDVisitor(cpd);
Object sel = targetPart.getSite().getSelectionProvider();
CPDReportWindow crw = new CPDReportWindow(targetPart.getSite().getShell());
crw.create();
if (sel instanceof TreeViewer) {
TreeViewer tv = (TreeViewer) sel;
ISelection sel2 = tv.getSelection();
if (sel2 instanceof StructuredSelection) {
monitor.beginTask("Searhing for files...", IProgressMonitor.UNKNOWN);
StructuredSelection ss = (StructuredSelection) sel2;
for (Iterator iter = ss.iterator(); iter.hasNext();) {
Object obj = iter.next();
if (obj instanceof IProject) {
((IProject) obj).accept(visitor);
}
Object sel = targetPart.getSite().getSelectionProvider().getSelection();
if (sel instanceof IStructuredSelection) {
monitor.beginTask("Searhing for files...", IProgressMonitor.UNKNOWN);
StructuredSelection ss = (StructuredSelection) sel;
for (Iterator iter = ss.iterator(); iter.hasNext();) {
Object obj = iter.next();
if (obj instanceof IProject) {
((IProject) obj).accept(visitor);
} else if (obj instanceof IJavaProject) {
IResource resource = ((IJavaProject) obj).getResource();
resource.accept(visitor);
}
monitor.beginTask("Running Cut & Paste Detector...", IProgressMonitor.UNKNOWN);
cpd.go();
monitor.beginTask("Building result set...", IProgressMonitor.UNKNOWN);
Results results = cpd.getResults();
for (Iterator iter = results.getTiles(); iter.hasNext();) {
Tile tile = (Tile) iter.next();
int dups = results.getOccurrenceCountFor(tile);
crw.addEntry(String.valueOf(dups) + " duplicates found.\n");
for (Iterator iter2 = results.getOccurrences(tile); iter2.hasNext();) {
TokenEntry te = (TokenEntry) iter2.next();
crw.addEntry("\t" + te.getTokenSrcID() + ": " + te.getBeginLine() + "\n");
}
crw.addEntry("\n");
}
monitor.done();
crw.open();
crw.getShell().setSize(500, 500);
}
log.debug("CPD start");
monitor.beginTask("Running Cut & Paste Detector...", IProgressMonitor.UNKNOWN);
cpd.go();
monitor.beginTask("Building result set...", IProgressMonitor.UNKNOWN);
log.debug("CPD stop");
Iterator iter = cpd.getMatches();
while (iter.hasNext()) {
Match match = (Match) iter.next();
crw.addEntry("=====================================\n");
crw.addEntry(
"Found a "
+ match.getLineCount()
+ " line ("
+ match.getTokenCount()
+ " tokens) duplication in the following files :\n");
log.debug(
"Found a "
+ match.getLineCount()
+ " line ("
+ match.getTokenCount()
+ " tokens) duplication in the following files :");
for (Iterator iter2 = match.iterator(); iter2.hasNext();) {
Mark mark = (Mark) iter2.next();
crw.addEntry("\tStarting at line " + mark.getBeginLine() + " of " + mark.getTokenSrcID() + "\n");
log.debug(" Starting at line " + mark.getBeginLine() + " of " + mark.getTokenSrcID());
}
crw.addEntry("\n");
crw.addEntry("-------------------------------------\n");
crw.addEntry(match.getSourceCodeSlice() + "\n");
crw.addEntry("=====================================\n");
}
monitor.done();
crw.open();
crw.getShell().setSize(800, 600);
}
} catch (Exception e) {
MessageDialog.openError(null, "Error running CPD", e.toString());
@ -124,71 +148,4 @@ public class CPDCheckProjectAction implements IObjectActionDelegate, IRunnableWi
public void selectionChanged(IAction action, ISelection selection) {
}
class CPDActivityListener implements CPDListener {
IProgressMonitor monitor;
private boolean firstFile = true;
private boolean firstToken = true;
private boolean firstTile = true;
private int tokenTracker = 0;
private static final int TOKEN_LIMIT = 100;
public CPDActivityListener(IProgressMonitor monitor) {
this.monitor = monitor;
}
/**
* @see net.sourceforge.pmd.cpd.CPDListener#addedFile(int, File)
*/
public boolean addedFile(int fileCount, File file) {
if (firstFile) {
monitor.beginTask("Adding Files", fileCount);
firstFile = false;
}
if (file != null) {
monitor.subTask(file.getName());
}
monitor.worked(1);
return (file != null);
}
/**
* @see net.sourceforge.pmd.cpd.CPDListener#addingTokens(int, int, String)
*/
public boolean addingTokens(int tokenSetCount, int doneSoFar, String tokenSrcId) {
if (firstToken) {
monitor.beginTask("Adding Tokens", tokenSetCount);
firstToken = false;
}
if (tokenSrcId != null && tokenTracker++ == TOKEN_LIMIT) {
monitor.subTask(tokenSrcId);
tokenTracker = 0;
monitor.worked(TOKEN_LIMIT);
}
return true;
}
/**
* @see net.sourceforge.pmd.cpd.CPDListener#update(String)
*/
public boolean update(String arg0) {
return true;
}
/**
* @see net.sourceforge.pmd.cpd.CPDListener#addedNewTile(Tile, int, int)
*/
public boolean addedNewTile(Tile tile, int tilesSoFar, int totalTiles) {
if (firstTile) {
monitor.beginTask("Adding Tiles", totalTiles);
firstTile = false;
}
if (tile != null) {
monitor.subTask(tile.getImage());
}
monitor.worked(1);
return true;
}
}
}