forked from phoedos/pmd
Checking in some Java 5 changes
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4993 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -113,7 +113,7 @@ public class AbstractRuleTest extends TestCase {
|
||||
public void testRuleExclusion() {
|
||||
MyRule r = new MyRule();
|
||||
RuleContext ctx = new RuleContext();
|
||||
Map m = new HashMap();
|
||||
Map<Integer, String> m = new HashMap<Integer, String>();
|
||||
m.put(new Integer(5), "");
|
||||
ctx.setReport(new Report());
|
||||
ctx.excludeLines(m);
|
||||
|
@ -29,6 +29,8 @@ import net.sourceforge.pmd.RuleSet;
|
||||
import net.sourceforge.pmd.RuleViolation;
|
||||
import net.sourceforge.pmd.TargetJDK1_4;
|
||||
import net.sourceforge.pmd.ast.JavaParser;
|
||||
import net.sourceforge.pmd.ast.ASTCompilationUnit;
|
||||
|
||||
import test.net.sourceforge.pmd.testframework.MockRule;
|
||||
|
||||
import java.io.StringReader;
|
||||
@ -170,7 +172,7 @@ public class RuleSetTest extends TestCase {
|
||||
protected void verifyRuleSet(RuleSet IUT, int size, Set values) throws Throwable {
|
||||
|
||||
RuleContext context = new RuleContext();
|
||||
Set reportedValues = new HashSet();
|
||||
Set<RuleViolation> reportedValues = new HashSet<RuleViolation>();
|
||||
context.setReport(new Report());
|
||||
IUT.apply(makeCompilationUnits(), context);
|
||||
|
||||
@ -193,7 +195,7 @@ public class RuleSetTest extends TestCase {
|
||||
|
||||
|
||||
protected List makeCompilationUnits() throws Throwable {
|
||||
List RC = new ArrayList();
|
||||
List<ASTCompilationUnit> RC = new ArrayList<ASTCompilationUnit>();
|
||||
JavaParser parser = (new TargetJDK1_4()).createParser(new StringReader(javaCode));
|
||||
RC.add(parser.CompilationUnit());
|
||||
return RC;
|
||||
|
@ -46,7 +46,7 @@ public class MatchAlgorithmTest extends TestCase {
|
||||
TokenEntry.clearImages();
|
||||
tokenizer.tokenize(sourceCode, tokens);
|
||||
assertEquals(41, tokens.size());
|
||||
Map codeMap = new HashMap();
|
||||
Map<String, SourceCode> codeMap = new HashMap<String, SourceCode>();
|
||||
codeMap.put("Foo.java", sourceCode);
|
||||
|
||||
MatchAlgorithm matchAlgorithm = new MatchAlgorithm(codeMap, tokens, 5);
|
||||
@ -74,7 +74,7 @@ public class MatchAlgorithmTest extends TestCase {
|
||||
Tokens tokens = new Tokens();
|
||||
TokenEntry.clearImages();
|
||||
tokenizer.tokenize(sourceCode, tokens);
|
||||
Map codeMap = new HashMap();
|
||||
Map<String, SourceCode> codeMap = new HashMap<String, SourceCode>();
|
||||
codeMap.put("Foo.java", sourceCode);
|
||||
|
||||
MatchAlgorithm matchAlgorithm = new MatchAlgorithm(codeMap, tokens, 5);
|
||||
|
@ -39,7 +39,7 @@ public class XMLRendererTest extends TestCase {
|
||||
|
||||
public void test_one_dupe() {
|
||||
Renderer renderer = new XMLRenderer();
|
||||
List list = new ArrayList();
|
||||
List<Match> list = new ArrayList<Match>();
|
||||
Match match = new Match(75, new TokenEntry("public", "/var/Foo.java", 48), new TokenEntry("stuff", "/var/Foo.java", 73));
|
||||
match.setLineCount(6);
|
||||
match.setSourceCodeSlice("code fragment");
|
||||
@ -73,7 +73,7 @@ public class XMLRendererTest extends TestCase {
|
||||
|
||||
public void testRender_MultipleMatch() {
|
||||
Renderer renderer = new XMLRenderer();
|
||||
List list = new ArrayList();
|
||||
List<Match> list = new ArrayList<Match>();
|
||||
Match match1 = new Match(75, new TokenEntry("public", "/var/Foo.java", 48), new TokenEntry("void", "/var/Foo.java", 73));
|
||||
match1.setLineCount(6);
|
||||
match1.setSourceCodeSlice("code fragment");
|
||||
|
@ -62,7 +62,7 @@ public class MatchesFunctionTest extends TestCase implements Node {
|
||||
private Object tryRegexp(String exp) throws FunctionCallException, NoSuchMethodException {
|
||||
MatchesFunction function = new MatchesFunction();
|
||||
List list = new ArrayList();
|
||||
List attrs = new ArrayList();
|
||||
List<Attribute> attrs = new ArrayList<Attribute>();
|
||||
attrs.add(new Attribute(this, "matches", getClass().getMethod("getValue", new Class[0])));
|
||||
list.add(attrs);
|
||||
list.add(exp);
|
||||
|
@ -130,7 +130,7 @@ public class StatisticalRuleTest extends TestCase {
|
||||
IUT.addDataPoint(points[i]);
|
||||
}
|
||||
} else {
|
||||
List lPoints = new ArrayList();
|
||||
List<DataPoint> lPoints = new ArrayList<DataPoint>();
|
||||
for (int i = 0; i < POINTS; i++) {
|
||||
points[i] = new DataPoint();
|
||||
points[i].setScore(1.0 * i);
|
||||
@ -147,7 +147,7 @@ public class StatisticalRuleTest extends TestCase {
|
||||
|
||||
Collections.shuffle(lPoints);
|
||||
for (int i = 0; i < POINTS; i++) {
|
||||
IUT.addDataPoint((DataPoint) lPoints.get(i));
|
||||
IUT.addDataPoint(lPoints.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,7 +54,7 @@ public class AcceptanceTest extends STBBaseTst {
|
||||
System.out.println(TEST_FIELD);
|
||||
parseCode(TEST_FIELD);
|
||||
ASTVariableDeclaratorId declaration = acu.findChildrenOfType(ASTVariableDeclaratorId.class).get(0);
|
||||
NameOccurrence no = (NameOccurrence)declaration.getUsages().iterator().next();
|
||||
NameOccurrence no = declaration.getUsages().iterator().next();
|
||||
SimpleNode location = no.getLocation();
|
||||
System.out.println("variable " + declaration.getImage() + " is used here: " + location.getImage());
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ public class ImageFinderFunctionTest extends TestCase {
|
||||
}
|
||||
|
||||
public void testSeveralImages() {
|
||||
List imgs = new ArrayList();
|
||||
List<String> imgs = new ArrayList<String>();
|
||||
imgs.add("Foo.foo");
|
||||
imgs.add("foo");
|
||||
ImageFinderFunction f = new ImageFinderFunction(imgs);
|
||||
|
@ -82,7 +82,7 @@ public class TypeSetTest extends TestCase {
|
||||
}
|
||||
|
||||
public void testExplicitImportResolver() throws Throwable {
|
||||
Set imports = new HashSet();
|
||||
Set<String> imports = new HashSet<String>();
|
||||
imports.add("java.io.File");
|
||||
TypeSet.Resolver r = new TypeSet.ExplicitImportResolver(imports);
|
||||
assertEquals(File.class, r.resolve("File"));
|
||||
@ -108,7 +108,7 @@ public class TypeSetTest extends TestCase {
|
||||
}
|
||||
|
||||
public void testImportOnDemandResolverPass() throws Throwable {
|
||||
Set imports = new HashSet();
|
||||
Set<String> imports = new HashSet<String>();
|
||||
imports.add("java.io.*");
|
||||
imports.add("java.util.*");
|
||||
TypeSet.Resolver r = new TypeSet.ImportOnDemandResolver(imports);
|
||||
@ -117,7 +117,7 @@ public class TypeSetTest extends TestCase {
|
||||
}
|
||||
|
||||
public void testImportOnDemandResolverFail() throws Throwable {
|
||||
Set imports = new HashSet();
|
||||
Set<String> imports = new HashSet<String>();
|
||||
imports.add("java.io.*");
|
||||
imports.add("java.util.*");
|
||||
TypeSet.Resolver r = new TypeSet.ImportOnDemandResolver(imports);
|
||||
|
@ -18,7 +18,7 @@ public class VariableNameDeclarationTest extends STBBaseTst {
|
||||
parseCode(TEST1);
|
||||
List nodes = acu.findChildrenOfType(ASTVariableDeclaratorId.class);
|
||||
Scope s = ((ASTVariableDeclaratorId) nodes.get(0)).getScope();
|
||||
VariableNameDeclaration decl = (VariableNameDeclaration) s.getVariableDeclarations().keySet().iterator().next();
|
||||
VariableNameDeclaration decl = s.getVariableDeclarations().keySet().iterator().next();
|
||||
assertEquals("bar", decl.getImage());
|
||||
assertEquals(3, decl.getNode().getBeginLine());
|
||||
}
|
||||
@ -36,37 +36,37 @@ public class VariableNameDeclarationTest extends STBBaseTst {
|
||||
|
||||
public void testIsArray() {
|
||||
parseCode(TEST3);
|
||||
VariableNameDeclaration decl = (VariableNameDeclaration) (acu.findChildrenOfType(ASTVariableDeclaratorId.class).get(0)).getScope().getVariableDeclarations().keySet().iterator().next();
|
||||
VariableNameDeclaration decl = acu.findChildrenOfType(ASTVariableDeclaratorId.class).get(0).getScope().getVariableDeclarations().keySet().iterator().next();
|
||||
assertTrue(decl.isArray());
|
||||
}
|
||||
|
||||
public void testPrimitiveType() {
|
||||
parseCode(TEST1);
|
||||
VariableNameDeclaration decl = (VariableNameDeclaration) (acu.findChildrenOfType(ASTVariableDeclaratorId.class).get(0)).getScope().getVariableDeclarations().keySet().iterator().next();
|
||||
VariableNameDeclaration decl = acu.findChildrenOfType(ASTVariableDeclaratorId.class).get(0).getScope().getVariableDeclarations().keySet().iterator().next();
|
||||
assertTrue(decl.isPrimitiveType());
|
||||
}
|
||||
|
||||
public void testArrayIsReferenceType() {
|
||||
parseCode(TEST3);
|
||||
VariableNameDeclaration decl = (VariableNameDeclaration) (acu.findChildrenOfType(ASTVariableDeclaratorId.class).get(0)).getScope().getVariableDeclarations().keySet().iterator().next();
|
||||
VariableNameDeclaration decl = acu.findChildrenOfType(ASTVariableDeclaratorId.class).get(0).getScope().getVariableDeclarations().keySet().iterator().next();
|
||||
assertTrue(decl.isReferenceType());
|
||||
}
|
||||
|
||||
public void testPrimitiveTypeImage() {
|
||||
parseCode(TEST3);
|
||||
VariableNameDeclaration decl = (VariableNameDeclaration) (acu.findChildrenOfType(ASTVariableDeclaratorId.class).get(0)).getScope().getVariableDeclarations().keySet().iterator().next();
|
||||
VariableNameDeclaration decl = acu.findChildrenOfType(ASTVariableDeclaratorId.class).get(0).getScope().getVariableDeclarations().keySet().iterator().next();
|
||||
assertEquals("int", decl.getTypeImage());
|
||||
}
|
||||
|
||||
public void testRefTypeImage() {
|
||||
parseCode(TEST4);
|
||||
VariableNameDeclaration decl = (VariableNameDeclaration) (acu.findChildrenOfType(ASTVariableDeclaratorId.class).get(0)).getScope().getVariableDeclarations().keySet().iterator().next();
|
||||
VariableNameDeclaration decl = acu.findChildrenOfType(ASTVariableDeclaratorId.class).get(0).getScope().getVariableDeclarations().keySet().iterator().next();
|
||||
assertEquals("String", decl.getTypeImage());
|
||||
}
|
||||
|
||||
public void testParamTypeImage() {
|
||||
parseCode(TEST5);
|
||||
VariableNameDeclaration decl = (VariableNameDeclaration) (acu.findChildrenOfType(ASTVariableDeclaratorId.class).get(0)).getScope().getVariableDeclarations().keySet().iterator().next();
|
||||
VariableNameDeclaration decl = acu.findChildrenOfType(ASTVariableDeclaratorId.class).get(0).getScope().getVariableDeclarations().keySet().iterator().next();
|
||||
assertEquals("String", decl.getTypeImage());
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,6 @@ package test.net.sourceforge.pmd.symboltable;
|
||||
import junit.framework.TestCase;
|
||||
import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
|
||||
import net.sourceforge.pmd.ast.SimpleJavaNode;
|
||||
import net.sourceforge.pmd.symboltable.NameDeclaration;
|
||||
import net.sourceforge.pmd.symboltable.NameOccurrence;
|
||||
import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
|
||||
import net.sourceforge.pmd.symboltable.VariableUsageFinderFunction;
|
||||
@ -19,14 +18,14 @@ public class VariableUsageFinderFunctionTest extends TestCase {
|
||||
public void testLookingForUsed() {
|
||||
ASTVariableDeclaratorId variableDeclarationIdNode = new ASTVariableDeclaratorId(1);
|
||||
variableDeclarationIdNode.setImage("x");
|
||||
NameDeclaration nameDeclaration = new VariableNameDeclaration(variableDeclarationIdNode);
|
||||
List nameOccurrences = new ArrayList();
|
||||
VariableNameDeclaration nameDeclaration = new VariableNameDeclaration(variableDeclarationIdNode);
|
||||
List<NameOccurrence> nameOccurrences = new ArrayList<NameOccurrence>();
|
||||
nameOccurrences.add(new NameOccurrence(new SimpleJavaNode(2), "x"));
|
||||
|
||||
Map declarations = new HashMap();
|
||||
Map<VariableNameDeclaration, List<NameOccurrence>> declarations = new HashMap<VariableNameDeclaration, List<NameOccurrence>>();
|
||||
declarations.put(nameDeclaration, nameOccurrences);
|
||||
|
||||
List vars = new ArrayList();
|
||||
List<VariableNameDeclaration> vars = new ArrayList<VariableNameDeclaration>();
|
||||
vars.add(nameDeclaration);
|
||||
|
||||
VariableUsageFinderFunction f = new VariableUsageFinderFunction(declarations);
|
||||
|
@ -423,7 +423,7 @@ public abstract class AbstractRule extends JavaParserVisitorAdapter implements R
|
||||
* @param node the node which will be searched
|
||||
*/
|
||||
protected final String getDeclaringType(SimpleNode node) {
|
||||
ASTClassOrInterfaceDeclaration c = (ASTClassOrInterfaceDeclaration) node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
|
||||
ASTClassOrInterfaceDeclaration c = node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
|
||||
if (c != null)
|
||||
return c.getImage();
|
||||
return null;
|
||||
|
@ -30,7 +30,7 @@ public interface PropertyDescriptor extends Comparable<PropertyDescriptor> {
|
||||
* Denotes the value datatype.
|
||||
* @return Class
|
||||
*/
|
||||
Class type();
|
||||
Class<?> type();
|
||||
/**
|
||||
* If the property is multi-valued, i.e. an array of strings, then this
|
||||
* returns the maximum number permitted. Unary property rule properties
|
||||
|
@ -77,8 +77,8 @@ public class ASTFieldDeclaration extends AccessNode implements Dimensionable {
|
||||
if (jjtGetParent().jjtGetParent() instanceof ASTEnumBody) {
|
||||
return false;
|
||||
}
|
||||
ASTClassOrInterfaceDeclaration n = (ASTClassOrInterfaceDeclaration)getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
|
||||
return n instanceof ASTClassOrInterfaceDeclaration && n.isInterface();
|
||||
ASTClassOrInterfaceDeclaration n = getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
|
||||
return n != null && n.isInterface();
|
||||
}
|
||||
|
||||
public boolean isArray() {
|
||||
|
@ -58,7 +58,7 @@ public class ASTMethodDeclaration extends AccessNode {
|
||||
}
|
||||
|
||||
public boolean isInterfaceMember() {
|
||||
ASTClassOrInterfaceDeclaration clz = (ASTClassOrInterfaceDeclaration) getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
|
||||
ASTClassOrInterfaceDeclaration clz = getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
|
||||
return clz != null && clz.isInterface();
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
package net.sourceforge.pmd.ast;
|
||||
|
||||
import net.sourceforge.pmd.symboltable.NameOccurrence;
|
||||
import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
|
||||
|
||||
import java.util.List;
|
||||
@ -34,8 +35,8 @@ public class ASTVariableDeclaratorId extends SimpleJavaNode {
|
||||
nameDeclaration = decl;
|
||||
}
|
||||
|
||||
public List getUsages() {
|
||||
return (List) getScope().getVariableDeclarations().get(nameDeclaration);
|
||||
public List<NameOccurrence> getUsages() {
|
||||
return getScope().getVariableDeclarations().get(nameDeclaration);
|
||||
}
|
||||
|
||||
public void bumpArrayDepth() {
|
||||
|
@ -134,12 +134,12 @@ public abstract class SimpleNode implements Node {
|
||||
* @param parentType class which you want to find.
|
||||
* @return Node of type parentType. Returns null if none found.
|
||||
*/
|
||||
public <T> Node getFirstParentOfType(Class<T> parentType) {
|
||||
public <T> T getFirstParentOfType(Class<T> parentType) {
|
||||
Node parentNode = jjtGetParent();
|
||||
while (parentNode != null && parentNode.getClass() != parentType) {
|
||||
parentNode = parentNode.jjtGetParent();
|
||||
}
|
||||
return parentNode;
|
||||
return (T) parentNode;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -15,7 +15,7 @@ import java.util.Set;
|
||||
|
||||
public class CPD {
|
||||
|
||||
private Map source = new HashMap();
|
||||
private Map<String, SourceCode> source = new HashMap<String, SourceCode>();
|
||||
private CPDListener listener = new CPDNullListener();
|
||||
private Tokens tokens = new Tokens();
|
||||
private int minimumTileSize;
|
||||
@ -47,7 +47,7 @@ public class CPD {
|
||||
matchAlgorithm.findMatches();
|
||||
}
|
||||
|
||||
public Iterator getMatches() {
|
||||
public Iterator<Match> getMatches() {
|
||||
return matchAlgorithm.matches();
|
||||
}
|
||||
|
||||
@ -63,9 +63,9 @@ public class CPD {
|
||||
addDirectory(dir, true);
|
||||
}
|
||||
|
||||
public void add(List files) throws IOException {
|
||||
for (Iterator i = files.iterator(); i.hasNext();) {
|
||||
add(files.size(), (File) i.next());
|
||||
public void add(List<File> files) throws IOException {
|
||||
for (File f: files) {
|
||||
add(files.size(), f);
|
||||
}
|
||||
}
|
||||
|
||||
@ -78,7 +78,7 @@ public class CPD {
|
||||
add(finder.findFilesFrom(dir, language.getFileFilter(), recurse));
|
||||
}
|
||||
|
||||
private Set current = new HashSet();
|
||||
private Set<String> current = new HashSet<String>();
|
||||
|
||||
private void add(int fileCount, File file) throws IOException {
|
||||
|
||||
|
@ -13,9 +13,9 @@ public class FileFinder {
|
||||
private FilenameFilter filter;
|
||||
private static final String FILE_SEP = System.getProperty("file.separator");
|
||||
|
||||
public List findFilesFrom(String dir, FilenameFilter filter, boolean recurse) {
|
||||
public List<File> findFilesFrom(String dir, FilenameFilter filter, boolean recurse) {
|
||||
this.filter = filter;
|
||||
List files = new ArrayList();
|
||||
List<File> files = new ArrayList<File>();
|
||||
scanDirectory(new File(dir), files, recurse);
|
||||
return files;
|
||||
}
|
||||
@ -23,7 +23,7 @@ public class FileFinder {
|
||||
/**
|
||||
* Implements a tail recursive file scanner
|
||||
*/
|
||||
private void scanDirectory(File dir, List list, boolean recurse) {
|
||||
private void scanDirectory(File dir, List<File> list, boolean recurse) {
|
||||
String[] candidates = dir.list(filter);
|
||||
if (candidates == null) {
|
||||
return;
|
||||
|
@ -232,7 +232,7 @@ public class GUI implements CPDListener {
|
||||
private JFrame frame;
|
||||
private boolean trimLeadingWhitespace;
|
||||
|
||||
private List matches = new ArrayList();
|
||||
private List<Match> matches = new ArrayList<Match>();
|
||||
|
||||
private void addSaveOptionsTo(JMenu menu) {
|
||||
|
||||
@ -382,7 +382,7 @@ public class GUI implements CPDListener {
|
||||
private void populateResultArea() {
|
||||
int[] selectionIndices = resultsTable.getSelectedRows();
|
||||
TableModel model = resultsTable.getModel();
|
||||
List selections = new ArrayList(selectionIndices.length);
|
||||
List<Object> selections = new ArrayList<Object>(selectionIndices.length);
|
||||
for (int i=0; i<selectionIndices.length; i++) {
|
||||
selections.add(model.getValueAt(selectionIndices[i], 99));
|
||||
}
|
||||
@ -463,14 +463,14 @@ public class GUI implements CPDListener {
|
||||
|
||||
private String setLabelFor(Match match) {
|
||||
|
||||
Set sourceIDs = new HashSet(match.getMarkCount());
|
||||
for (Iterator occurrences = match.iterator(); occurrences.hasNext();) {
|
||||
sourceIDs.add( ((TokenEntry) occurrences.next()).getTokenSrcID());
|
||||
Set<String> sourceIDs = new HashSet<String>(match.getMarkCount());
|
||||
for (Iterator<TokenEntry> occurrences = match.iterator(); occurrences.hasNext();) {
|
||||
sourceIDs.add(occurrences.next().getTokenSrcID());
|
||||
}
|
||||
String label;
|
||||
|
||||
if (sourceIDs.size() == 1) {
|
||||
String sourceId = (String)sourceIDs.iterator().next();
|
||||
String sourceId = sourceIDs.iterator().next();
|
||||
int separatorPos = sourceId.lastIndexOf(File.separatorChar);
|
||||
label = "..." + sourceId.substring(separatorPos);
|
||||
} else {
|
||||
@ -542,10 +542,10 @@ public class GUI implements CPDListener {
|
||||
cpd.go();
|
||||
t.stop();
|
||||
|
||||
matches = new ArrayList();
|
||||
matches = new ArrayList<Match>();
|
||||
Match match;
|
||||
for (Iterator i = cpd.getMatches(); i.hasNext();) {
|
||||
match = (Match)i.next();
|
||||
for (Iterator<Match> i = cpd.getMatches(); i.hasNext();) {
|
||||
match = i.next();
|
||||
setLabelFor(match);
|
||||
matches.add(match);
|
||||
}
|
||||
|
@ -16,18 +16,18 @@ public class MatchAlgorithm {
|
||||
private int lastHash;
|
||||
private int lastMod = 1;
|
||||
|
||||
private List matches;
|
||||
private Map source;
|
||||
private List<Match> matches;
|
||||
private Map<String, SourceCode> source;
|
||||
private Tokens tokens;
|
||||
private List code;
|
||||
private List<TokenEntry> code;
|
||||
private CPDListener cpdListener;
|
||||
private int min;
|
||||
|
||||
public MatchAlgorithm(Map sourceCode, Tokens tokens, int min) {
|
||||
public MatchAlgorithm(Map<String, SourceCode> sourceCode, Tokens tokens, int min) {
|
||||
this(sourceCode, tokens, min, new CPDNullListener());
|
||||
}
|
||||
|
||||
public MatchAlgorithm(Map sourceCode, Tokens tokens, int min, CPDListener listener) {
|
||||
public MatchAlgorithm(Map<String, SourceCode> sourceCode, Tokens tokens, int min, CPDListener listener) {
|
||||
this.source = sourceCode;
|
||||
this.tokens = tokens;
|
||||
this.code = tokens.getTokens();
|
||||
@ -42,12 +42,12 @@ public class MatchAlgorithm {
|
||||
this.cpdListener = listener;
|
||||
}
|
||||
|
||||
public Iterator matches() {
|
||||
public Iterator<Match> matches() {
|
||||
return matches.iterator();
|
||||
}
|
||||
|
||||
public TokenEntry tokenAt(int offset, TokenEntry m) {
|
||||
return (TokenEntry) code.get(offset + m.getIndex());
|
||||
return code.get(offset + m.getIndex());
|
||||
}
|
||||
|
||||
public int getMinimumTileSize() {
|
||||
@ -56,30 +56,31 @@ public class MatchAlgorithm {
|
||||
|
||||
public void findMatches() {
|
||||
cpdListener.phaseUpdate(CPDListener.HASH);
|
||||
Map markGroups = hash();
|
||||
Map<TokenEntry, Object> markGroups = hash();
|
||||
|
||||
cpdListener.phaseUpdate(CPDListener.MATCH);
|
||||
MatchCollector matchCollector = new MatchCollector(this);
|
||||
for (Iterator i = markGroups.values().iterator(); i.hasNext();) {
|
||||
for (Iterator<Object> i = markGroups.values().iterator(); i.hasNext();) {
|
||||
Object o = i.next();
|
||||
if (o instanceof List) {
|
||||
Collections.reverse((List) o);
|
||||
matchCollector.collect((List) o);
|
||||
List<TokenEntry> l = (List<TokenEntry>) o;
|
||||
|
||||
Collections.reverse(l);
|
||||
matchCollector.collect(l);
|
||||
}
|
||||
i.remove();
|
||||
}
|
||||
cpdListener.phaseUpdate(CPDListener.GROUPING);
|
||||
matches = matchCollector.getMatches();
|
||||
matchCollector = null;
|
||||
for (Iterator i = matches.iterator(); i.hasNext();) {
|
||||
Match match = (Match) i.next();
|
||||
for (Iterator occurrences = match.iterator(); occurrences.hasNext();) {
|
||||
TokenEntry mark = (TokenEntry) occurrences.next();
|
||||
for (Match match: matches) {
|
||||
for (Iterator<TokenEntry> occurrences = match.iterator(); occurrences.hasNext();) {
|
||||
TokenEntry mark = occurrences.next();
|
||||
match.setLineCount(tokens.getLineCount(mark, match));
|
||||
if (!occurrences.hasNext()) {
|
||||
int start = mark.getBeginLine();
|
||||
int end = start + match.getLineCount() - 1;
|
||||
SourceCode sourceCode = (SourceCode) source.get(mark.getTokenSrcID());
|
||||
SourceCode sourceCode = source.get(mark.getTokenSrcID());
|
||||
match.setSourceCodeSlice(sourceCode.getSlice(start, end));
|
||||
}
|
||||
}
|
||||
@ -87,10 +88,10 @@ public class MatchAlgorithm {
|
||||
cpdListener.phaseUpdate(CPDListener.DONE);
|
||||
}
|
||||
|
||||
private Map hash() {
|
||||
Map markGroups = new HashMap(tokens.size());
|
||||
private Map<TokenEntry, Object> hash() {
|
||||
Map<TokenEntry, Object> markGroups = new HashMap<TokenEntry, Object>(tokens.size());
|
||||
for (int i = code.size() - 1; i >= 0; i--) {
|
||||
TokenEntry token = (TokenEntry) code.get(i);
|
||||
TokenEntry token = code.get(i);
|
||||
if (token != TokenEntry.EOF) {
|
||||
int last = tokenAt(min, token).getIdentifier();
|
||||
lastHash = MOD * lastHash + token.getIdentifier() - lastMod * last;
|
||||
@ -102,18 +103,18 @@ public class MatchAlgorithm {
|
||||
if (o == null) {
|
||||
markGroups.put(token, token);
|
||||
} else if (o instanceof TokenEntry) {
|
||||
List l = new ArrayList();
|
||||
l.add(o);
|
||||
List<TokenEntry> l = new ArrayList<TokenEntry>();
|
||||
l.add((TokenEntry) o);
|
||||
l.add(token);
|
||||
markGroups.put(token, l);
|
||||
} else {
|
||||
List l = (List) o;
|
||||
List<TokenEntry> l = (List<TokenEntry>) o;
|
||||
l.add(token);
|
||||
}
|
||||
} else {
|
||||
lastHash = 0;
|
||||
for (int end = Math.max(0, i - min + 1); i > end; i--) {
|
||||
token = (TokenEntry) code.get(i - 1);
|
||||
token = code.get(i - 1);
|
||||
lastHash = MOD * lastHash + token.getIdentifier();
|
||||
if (token == TokenEntry.EOF) {
|
||||
break;
|
||||
|
@ -22,12 +22,12 @@ public class MatchCollector {
|
||||
this.ma = ma;
|
||||
}
|
||||
|
||||
public void collect(List marks) {
|
||||
public void collect(List<TokenEntry> marks) {
|
||||
//first get a pairwise collection of all maximal matches
|
||||
for (int i = 0; i < marks.size() - 1; i++) {
|
||||
TokenEntry mark1 = (TokenEntry) marks.get(i);
|
||||
TokenEntry mark1 = marks.get(i);
|
||||
for (int j = i + 1; j < marks.size(); j++) {
|
||||
TokenEntry mark2 = (TokenEntry) marks.get(j);
|
||||
TokenEntry mark2 = marks.get(j);
|
||||
int diff = mark1.getIndex() - mark2.getIndex();
|
||||
if (-diff < ma.getMinimumTileSize()) {
|
||||
continue;
|
||||
@ -50,7 +50,7 @@ public class MatchCollector {
|
||||
}
|
||||
}
|
||||
|
||||
public List getMatches() {
|
||||
public List<Match> getMatches() {
|
||||
List<Match> matchList = new ArrayList<Match>(startMap.values());
|
||||
Collections.sort(matchList);
|
||||
Set<Match.MatchCode> matchSet = new HashSet<Match.MatchCode>();
|
||||
@ -66,8 +66,8 @@ public class MatchCollector {
|
||||
break;
|
||||
}
|
||||
TokenEntry mark2 = null;
|
||||
for (Iterator iter = match2.getMarkSet().iterator(); iter.hasNext();) {
|
||||
mark2 = (TokenEntry) iter.next();
|
||||
for (Iterator<TokenEntry> iter = match2.getMarkSet().iterator(); iter.hasNext();) {
|
||||
mark2 = iter.next();
|
||||
if (mark2 != mark1) {
|
||||
break;
|
||||
}
|
||||
|
@ -9,18 +9,18 @@ import java.util.List;
|
||||
|
||||
public class Tokens {
|
||||
|
||||
private List tokens = new ArrayList();
|
||||
private List<TokenEntry> tokens = new ArrayList<TokenEntry>();
|
||||
|
||||
public void add(TokenEntry tokenEntry) {
|
||||
this.tokens.add(tokenEntry);
|
||||
}
|
||||
|
||||
public Iterator iterator() {
|
||||
public Iterator<TokenEntry> iterator() {
|
||||
return tokens.iterator();
|
||||
}
|
||||
|
||||
private TokenEntry get(int index) {
|
||||
return (TokenEntry) tokens.get(index);
|
||||
return tokens.get(index);
|
||||
}
|
||||
|
||||
public int size() {
|
||||
@ -35,7 +35,7 @@ public class Tokens {
|
||||
return endTok.getBeginLine() - mark.getBeginLine() + 1;
|
||||
}
|
||||
|
||||
public List getTokens() {
|
||||
public List<TokenEntry> getTokens() {
|
||||
return tokens;
|
||||
}
|
||||
|
||||
|
@ -190,7 +190,7 @@ public class Linker {
|
||||
}
|
||||
|
||||
if (n.isType(NodeType.LABEL_LAST_STATEMENT)) {
|
||||
SimpleNode parentNode = (SimpleNode) n.getSimpleNode().getFirstParentOfType(ASTLabeledStatement.class);
|
||||
SimpleNode parentNode = n.getSimpleNode().getFirstParentOfType(ASTLabeledStatement.class);
|
||||
if (parentNode == null) {
|
||||
break;
|
||||
} else {
|
||||
|
@ -28,7 +28,7 @@ public class SequenceChecker {
|
||||
private static class Status {
|
||||
public static final int ROOT = -1;
|
||||
|
||||
private List nextSteps = new ArrayList();
|
||||
private List<Status> nextSteps = new ArrayList<Status>();
|
||||
private int type;
|
||||
private boolean lastStep;
|
||||
|
||||
@ -48,8 +48,8 @@ public class SequenceChecker {
|
||||
|
||||
public Status step(int type) {
|
||||
for (int i = 0; i < this.nextSteps.size(); i++) {
|
||||
if (type == ((Status) nextSteps.get(i)).type) {
|
||||
return (Status) nextSteps.get(i);
|
||||
if (type == nextSteps.get(i).type) {
|
||||
return nextSteps.get(i);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
@ -58,17 +58,15 @@ public class VariableAccessVisitor extends JavaParserVisitorAdapter {
|
||||
private List<VariableAccess> markUsages(IDataFlowNode inode) {
|
||||
// undefinitions was once a field... seems like it works fine as a local
|
||||
List<VariableAccess> undefinitions = new ArrayList<VariableAccess>();
|
||||
Set variableDeclarations = collectDeclarations(inode);
|
||||
for (Iterator i = variableDeclarations.iterator(); i.hasNext();) {
|
||||
Map declarations = (Map) i.next();
|
||||
for (Iterator j = declarations.entrySet().iterator(); j.hasNext();) {
|
||||
Map.Entry entry = (Map.Entry) j.next();
|
||||
VariableNameDeclaration vnd = (VariableNameDeclaration) entry.getKey();
|
||||
Set<Map<VariableNameDeclaration, List<NameOccurrence>>> variableDeclarations = collectDeclarations(inode);
|
||||
for (Map<VariableNameDeclaration, List<NameOccurrence>> declarations: variableDeclarations) {
|
||||
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry: declarations.entrySet()) {
|
||||
VariableNameDeclaration vnd = entry.getKey();
|
||||
|
||||
if (vnd.getAccessNodeParent() instanceof ASTFormalParameter) {
|
||||
// add definition for parameters
|
||||
addVariableAccess(
|
||||
(SimpleNode)vnd.getNode().getFirstParentOfType(ASTFormalParameters.class),
|
||||
vnd.getNode().getFirstParentOfType(ASTFormalParameters.class),
|
||||
new VariableAccess(VariableAccess.DEFINITION, vnd.getImage()),
|
||||
inode.getFlow());
|
||||
} else if (vnd.getAccessNodeParent().getFirstChildOfType(ASTVariableInitializer.class) != null) {
|
||||
@ -80,17 +78,17 @@ public class VariableAccessVisitor extends JavaParserVisitorAdapter {
|
||||
}
|
||||
undefinitions.add(new VariableAccess(VariableAccess.UNDEFINITION, vnd.getImage()));
|
||||
|
||||
for (Iterator k = ((List) entry.getValue()).iterator(); k.hasNext();) {
|
||||
addAccess(k, inode);
|
||||
for (NameOccurrence occurrence: entry.getValue()) {
|
||||
addAccess(occurrence, inode);
|
||||
}
|
||||
}
|
||||
}
|
||||
return undefinitions;
|
||||
}
|
||||
|
||||
private Set collectDeclarations(IDataFlowNode inode) {
|
||||
Set decls = new HashSet();
|
||||
Map varDecls;
|
||||
private Set<Map<VariableNameDeclaration, List<NameOccurrence>>> collectDeclarations(IDataFlowNode inode) {
|
||||
Set<Map<VariableNameDeclaration, List<NameOccurrence>>> decls = new HashSet<Map<VariableNameDeclaration, List<NameOccurrence>>>();
|
||||
Map<VariableNameDeclaration, List<NameOccurrence>> varDecls;
|
||||
for (int i = 0; i < inode.getFlow().size(); i++) {
|
||||
IDataFlowNode n = inode.getFlow().get(i);
|
||||
if (n instanceof StartOrEndDataFlowNode) {
|
||||
@ -104,8 +102,7 @@ public class VariableAccessVisitor extends JavaParserVisitorAdapter {
|
||||
return decls;
|
||||
}
|
||||
|
||||
private void addAccess(Iterator k, IDataFlowNode inode) {
|
||||
NameOccurrence occurrence = (NameOccurrence) k.next();
|
||||
private void addAccess(NameOccurrence occurrence, IDataFlowNode inode) {
|
||||
if (occurrence.isOnLeftHandSide()) {
|
||||
this.addVariableAccess(occurrence.getLocation(), new VariableAccess(VariableAccess.DEFINITION, occurrence.getImage()), inode.getFlow());
|
||||
} else if (occurrence.isOnRightHandSide() || (!occurrence.isOnLeftHandSide() && !occurrence.isOnRightHandSide())) {
|
||||
@ -131,7 +128,7 @@ public class VariableAccessVisitor extends JavaParserVisitorAdapter {
|
||||
Iterator childrenIterator = children.iterator();
|
||||
while (childrenIterator.hasNext()) {
|
||||
if (node.equals(childrenIterator.next())) {
|
||||
List v = new ArrayList();
|
||||
List<VariableAccess> v = new ArrayList<VariableAccess>();
|
||||
v.add(va);
|
||||
inode.setVariableAccess(v);
|
||||
return;
|
||||
|
@ -210,7 +210,7 @@ public abstract class AbstractPMDProperty implements PropertyDescriptor {
|
||||
return "Value is not an array of type: " + type();
|
||||
}
|
||||
|
||||
Class arrayType = value.getClass().getComponentType();
|
||||
Class<?> arrayType = value.getClass().getComponentType();
|
||||
if (arrayType == null || !arrayType.isAssignableFrom(type())) {
|
||||
return "Value is not an array of type: " + type();
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ public class BooleanProperty extends AbstractScalarProperty {
|
||||
* @return Class
|
||||
* @see net.sourceforge.pmd.PropertyDescriptor#type()
|
||||
*/
|
||||
public Class type() {
|
||||
public Class<Boolean> type() {
|
||||
return Boolean.class;
|
||||
}
|
||||
|
||||
|
@ -76,7 +76,7 @@ public class CharacterProperty extends AbstractPMDProperty {
|
||||
* @return Class
|
||||
* @see net.sourceforge.pmd.PropertyDescriptor#type()
|
||||
*/
|
||||
public Class type() {
|
||||
public Class<Character> type() {
|
||||
return Character.class;
|
||||
}
|
||||
|
||||
|
@ -50,7 +50,7 @@ public class DoubleProperty extends AbstractScalarProperty {
|
||||
* @return Class
|
||||
* @see net.sourceforge.pmd.PropertyDescriptor#type()
|
||||
*/
|
||||
public Class type() {
|
||||
public Class<Double> type() {
|
||||
return Double.class;
|
||||
}
|
||||
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user