forked from phoedos/pmd
starting to work on type resolution
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@337 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
33
pmd/regress/test/net/sourceforge/pmd/TypeSetTest.java
Normal file
33
pmd/regress/test/net/sourceforge/pmd/TypeSetTest.java
Normal file
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* User: tom
|
||||
* Date: Jul 12, 2002
|
||||
* Time: 8:10:10 PM
|
||||
*/
|
||||
package test.net.sourceforge.pmd;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import net.sourceforge.pmd.TypeSet;
|
||||
|
||||
public class TypeSetTest extends TestCase {
|
||||
public TypeSetTest(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public void testASTCompilationUnitPackage() {
|
||||
TypeSet t = new TypeSet();
|
||||
t.setASTCompilationUnitPackage("java.lang.");
|
||||
assertEquals("java.lang.", t.getASTCompilationUnitPackage());
|
||||
}
|
||||
|
||||
public void testAddImport() {
|
||||
TypeSet t = new TypeSet();
|
||||
t.addImport("java.io.File");
|
||||
}
|
||||
|
||||
public void testFindClass() throws Throwable {
|
||||
TypeSet t = new TypeSet();
|
||||
t.setASTCompilationUnitPackage("net.sourceforge.pmd.");
|
||||
Class clazz = t.findClass("String");
|
||||
assertEquals(String.class, clazz);
|
||||
}
|
||||
}
|
54
pmd/src/net/sourceforge/pmd/TypeSet.java
Normal file
54
pmd/src/net/sourceforge/pmd/TypeSet.java
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* User: tom
|
||||
* Date: Jul 12, 2002
|
||||
* Time: 8:08:53 PM
|
||||
*/
|
||||
package net.sourceforge.pmd;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* Keeps track of the types encountered in a ASTCompilationUnit
|
||||
*/
|
||||
public class TypeSet {
|
||||
|
||||
private String pkg;
|
||||
private Set imports = new HashSet();
|
||||
|
||||
public void setASTCompilationUnitPackage(String pkg) {
|
||||
this.pkg = pkg;
|
||||
}
|
||||
|
||||
public String getASTCompilationUnitPackage() {
|
||||
return pkg;
|
||||
}
|
||||
|
||||
public void addImport(String importString) {
|
||||
imports.add(importString);
|
||||
}
|
||||
|
||||
public Class findClass(String name) throws ClassNotFoundException {
|
||||
// is it explicitly imported?
|
||||
for (Iterator i = imports.iterator(); i.hasNext();) {
|
||||
String importStmt = (String)i.next();
|
||||
if (importStmt.endsWith(name)) {
|
||||
return Class.forName(name);
|
||||
}
|
||||
}
|
||||
// is it in the current package?
|
||||
try {
|
||||
return Class.forName(pkg + name);
|
||||
} catch (ClassNotFoundException cnfe) {
|
||||
}
|
||||
// is it in an implicity imported package - i.e., java.lang?
|
||||
// TODO reference the relevant JLS section
|
||||
try {
|
||||
return Class.forName("java.lang." + name);
|
||||
} catch (ClassNotFoundException cnfe) {
|
||||
}
|
||||
throw new ClassNotFoundException("Type " + name + " not found");
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user