From 06862eaa4f3e3ad33bc8f09f04fdb8503477f29a Mon Sep 17 00:00:00 2001 From: Tom Copeland Date: Sat, 13 Jul 2002 00:24:19 +0000 Subject: [PATCH] starting to work on type resolution git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@337 51baf565-9d33-0410-a72c-fc3788e3496d --- .../test/net/sourceforge/pmd/TypeSetTest.java | 33 ++++++++++++ pmd/src/net/sourceforge/pmd/TypeSet.java | 54 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 pmd/regress/test/net/sourceforge/pmd/TypeSetTest.java create mode 100644 pmd/src/net/sourceforge/pmd/TypeSet.java diff --git a/pmd/regress/test/net/sourceforge/pmd/TypeSetTest.java b/pmd/regress/test/net/sourceforge/pmd/TypeSetTest.java new file mode 100644 index 0000000000..b496834e93 --- /dev/null +++ b/pmd/regress/test/net/sourceforge/pmd/TypeSetTest.java @@ -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); + } +} diff --git a/pmd/src/net/sourceforge/pmd/TypeSet.java b/pmd/src/net/sourceforge/pmd/TypeSet.java new file mode 100644 index 0000000000..9a2a8c400e --- /dev/null +++ b/pmd/src/net/sourceforge/pmd/TypeSet.java @@ -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"); + } + +}