working on a CPD GUI

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@632 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2002-08-06 19:59:45 +00:00
parent 58e022db8e
commit ca2827eae8
8 changed files with 165 additions and 46 deletions

View File

@ -21,7 +21,8 @@ public class CPDTest extends TestCase{
CPD cpd = new CPD();
cpd.add("1", "public class Foo {}");
cpd.add("2", "public class Bar {}");
cpd.go(2);
cpd.setMinimumTileSize(2);
cpd.go();
Results results = cpd.getResults();
Iterator i = results.getOccurrences(new Tile(getHelloTokens()));
assertTrue(i.hasNext());
@ -38,7 +39,8 @@ public class CPDTest extends TestCase{
CPD cpd = new CPD();
cpd.add("1", "public class Foo {}");
cpd.add("2", "public class Bar {}");
cpd.go(2);
cpd.setMinimumTileSize(2);
cpd.go();
Results results = cpd.getResults();
Iterator i = results.getOccurrences(new Tile(getHelloTokens()));
assertTrue(i.hasNext());

View File

@ -18,25 +18,13 @@ public class CPD {
}
}
public interface Listener {
public void update(String msg);
}
public static class ListenerImpl implements Listener {
public void update(String msg) {
System.out.println(msg);
}
}
public static class NullListener implements Listener {
public void update(String msg) {}
}
private TokenSets tokenSets = new TokenSets();
private CPDListener listener = new CPDNullListener();
private Results results;
private Listener listener = new NullListener();
private int minimumTileSize;
public void addListener(Listener listener) {
public void setListener(CPDListener listener) {
this.listener = listener;
}
@ -50,6 +38,38 @@ public class CPD {
tokenSets.add(ts);
}
public void addAllInDirectory(String dir) throws IOException {
addDirectory(dir, false);
}
public void addRecursively(String dir) throws IOException {
addDirectory(dir, true);
}
private void addDirectory(String dir, boolean recurse) throws IOException {
File root = new File(dir);
List list = new ArrayList();
scanDirectory(root, list, recurse);
add(list);
}
public void setMinimumTileSize(int tileSize) {
minimumTileSize = tileSize;
}
private void scanDirectory(File dir, List list, boolean recurse) {
FilenameFilter filter = new JavaFileOrDirectoryFilter();
String[] possibles = dir.list(filter);
for (int i=0; i<possibles.length; i++) {
File tmp = new File(dir + System.getProperty("file.separator") + possibles[i]);
if (recurse && tmp.isDirectory()) {
scanDirectory(tmp, list, true);
} else {
list.add(new File(dir + System.getProperty("file.separator") + possibles[i]));
}
}
}
public void add(String id, String input) throws IOException {
Tokenizer t = new JavaTokensTokenizer();
TokenList ts = new TokenList(id);
@ -63,7 +83,7 @@ public class CPD {
}
}
public void go(int minimumTileSize) {
public void go() {
listener.update("Starting to process " + tokenSets.size() + " files");
GST gst = new GST(this.tokenSets, minimumTileSize);
results = gst.crunch(listener);
@ -90,7 +110,7 @@ public class CPD {
public static void main(String[] args) {
CPD cpd = new CPD();
//cpd.addListener(new ListenerImpl());
cpd.addListener(new NullListener());
cpd.setListener(new CPDNullListener());
try {
/*
cpd.add("1", "public class Foo {}");
@ -107,14 +127,15 @@ public class CPD {
*/
//cpd.add(findFilesRecursively("c:\\data\\pmd\\pmd-cpd\\src\\net\\sourceforge\\pmd\\cpd"));
//cpd.add(new File("c:\\data\\cougaar\\core\\src\\org\\cougaar\\core\\adaptivity\\PlayHelper.java"));
cpd.add(findFilesRecursively("c:\\data\\cougaar\\core\\src\\org\\cougaar\\core\\adaptivity\\"));
cpd.addRecursively("c:\\data\\cougaar\\core\\src\\org\\cougaar\\core\\adaptivity\\");
//cpd.add(findFilesRecursively("c:\\data\\cougaar\\core\\src\\org\\"));
} catch (IOException ioe) {
ioe.printStackTrace();
return;
}
long start = System.currentTimeMillis();
cpd.go(26);
cpd.setMinimumTileSize(26);
cpd.go();
System.out.println((System.currentTimeMillis() - start));
Results results = cpd.getResults();
for (Iterator i = results.getTiles(); i.hasNext();) {
@ -129,23 +150,4 @@ public class CPD {
}
}
private static List findFilesRecursively(String dir) {
File root = new File(dir);
List list = new ArrayList();
scanDirectory(root, list, true);
return list;
}
private static void scanDirectory(File dir, List list, boolean recurse) {
FilenameFilter filter = new JavaFileOrDirectoryFilter();
String[] possibles = dir.list(filter);
for (int i=0; i<possibles.length; i++) {
File tmp = new File(dir + System.getProperty("file.separator") + possibles[i]);
if (recurse && tmp.isDirectory()) {
scanDirectory(tmp, list, true);
} else {
list.add(new File(dir + System.getProperty("file.separator") + possibles[i]));
}
}
}
}

View File

@ -0,0 +1,10 @@
/*
* User: tom
* Date: Aug 6, 2002
* Time: 2:40:22 PM
*/
package net.sourceforge.pmd.cpd;
public interface CPDListener {
public void update(String msg);
}

View File

@ -0,0 +1,13 @@
/*
* User: tom
* Date: Aug 6, 2002
* Time: 2:41:22 PM
*/
package net.sourceforge.pmd.cpd;
public class CPDListenerImpl implements CPDListener{
public void update(String msg) {
System.out.println(msg);
}
}

View File

@ -0,0 +1,10 @@
/*
* User: tom
* Date: Aug 6, 2002
* Time: 2:41:43 PM
*/
package net.sourceforge.pmd.cpd;
public class CPDNullListener implements CPDListener{
public void update(String msg) {}
}

View File

@ -20,7 +20,7 @@ public class GST {
this.tokenSets = tokenSets;
}
public Results crunch(CPD.Listener listener) {
public Results crunch(CPDListener listener) {
Results results = new Results();
Occurrences occ =new Occurrences(tokenSets, listener);
@ -52,10 +52,10 @@ public class GST {
}
public Results crunch() {
return crunch(new CPD.NullListener());
return crunch(new CPDNullListener());
}
private void expandTile(Occurrences oldOcc, Occurrences newOcc, Tile tile, CPD.Listener listener) {
private void expandTile(Occurrences oldOcc, Occurrences newOcc, Tile tile, CPDListener listener) {
Tile newTile = null;
for (Iterator i = oldOcc.getOccurrences(tile); i.hasNext();) {
TokenEntry tok = (TokenEntry)i.next();

View File

@ -0,0 +1,82 @@
/*
* User: tom
* Date: Aug 6, 2002
* Time: 2:45:54 PM
*/
package net.sourceforge.pmd.cpd;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.io.IOException;
import java.util.Iterator;
public class GUI implements CPDListener {
public static void main(String[] args) {
new GUI();
}
private class GoListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
go();
}
}
private JTextField rootDirectoryField= new JTextField("c:\\data\\pmd\\pmd\\src\\net\\sourceforge\\pmd\\cpd\\");
private JTextField minimumLengthField= new JTextField("50");
private JCheckBox recurseCheckbox = new JCheckBox("Recurse?", true);
public GUI() {
JFrame f = new JFrame("PMD Cut and Paste Detector");
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new GridLayout(4,2));
inputPanel.add(new JLabel("Enter a root src directory"));
inputPanel.add(rootDirectoryField);
inputPanel.add(new JLabel("Enter a minimum tile size"));
inputPanel.add(minimumLengthField);
inputPanel.add(recurseCheckbox);
JButton goButton = new JButton("Go");
goButton.addActionListener(new GoListener());
inputPanel.add(goButton);
f.getContentPane().add(inputPanel);
f.getContentPane().setSize(600,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.show();
}
private void go() {
try {
CPD cpd = new CPD();
if (recurseCheckbox.isSelected()) {
cpd.addRecursively(rootDirectoryField.getText());
} else {
cpd.addAllInDirectory(rootDirectoryField.getText());
}
cpd.setListener(this);
cpd.setMinimumTileSize(Integer.parseInt(minimumLengthField.getText()));
cpd.go();
Results results = cpd.getResults();
for (Iterator i = results.getTiles(); i.hasNext();) {
Tile tile = (Tile)i.next();
System.out.println("=============================================================");
System.out.println("A " + cpd.getLineCountFor(tile) + " line (" + tile.getTokenCount() + " tokens) duplication in these files:");
for (Iterator j = cpd.getResults().getOccurrences(tile); j.hasNext();) {
TokenEntry tok = (TokenEntry)j.next();
System.out.println(tok.getBeginLine() + "\t" + tok.getTokenSrcID());
}
System.out.println(cpd.getImage(tile));
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public void update(String msg) {
System.out.println(msg);
}
}

View File

@ -15,10 +15,10 @@ public class Occurrences {
private Map tileToOccurrenceMap = new HashMap();
public Occurrences(TokenSets tss) {
this(tss, new CPD.NullListener());
this(tss, new CPDNullListener());
}
public Occurrences(TokenSets tss, CPD.Listener listener) {
public Occurrences(TokenSets tss, CPDListener listener) {
for (Iterator j = tss.iterator();j.hasNext();) {
TokenList ts = (TokenList)j.next();
listener.update("Adding token set " + ts.getID() + " to the initial frequency table");