diff options
author | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2005-08-18 15:22:38 +0000 |
---|---|---|
committer | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2005-08-18 15:22:38 +0000 |
commit | 2d60b1690e44dd33553e28618c7c0b25e4b34d9f (patch) | |
tree | b9a6b9c069e8ed045258c00d25708156401a838c /src | |
parent | ff329de74b48a2a8431024ae1097abbdac60aa8f (diff) | |
download | javassist-2d60b1690e44dd33553e28618c7c0b25e4b34d9f.tar.gz javassist-2d60b1690e44dd33553e28618c7c0b25e4b34d9f.zip |
modified the compiler to support "import".
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@195 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
Diffstat (limited to 'src')
-rw-r--r-- | src/main/javassist/ClassPool.java | 45 | ||||
-rw-r--r-- | src/main/javassist/CtClass.java | 2 | ||||
-rw-r--r-- | src/main/javassist/compiler/MemberResolver.java | 36 |
3 files changed, 71 insertions, 12 deletions
diff --git a/src/main/javassist/ClassPool.java b/src/main/javassist/ClassPool.java index 452f2e6f..e8b288e5 100644 --- a/src/main/javassist/ClassPool.java +++ b/src/main/javassist/ClassPool.java @@ -22,6 +22,8 @@ import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.util.Hashtable; +import java.util.Iterator; +import java.util.ArrayList; import javassist.bytecode.Descriptor; /** @@ -111,6 +113,8 @@ public class ClassPool { private static final int INIT_HASH_SIZE = 191; + private ArrayList importedPackages; + /** * Creates a root class pool. No parent class pool is specified. */ @@ -136,6 +140,7 @@ public class ClassPool { } this.cflow = null; + clearImportedPackages(); } /** @@ -214,6 +219,46 @@ public class ClassPool { } /** + * Record a package name so that the Javassist compiler searches + * the package to resolve a class name. + * Don't record the <code>java.lang</code> package, which has + * been implicitly recorded by default. + * + * <p>Note that <code>get()</code> in <code>ClassPool</code> does + * not search the recorded package. Only the compiler searches it. + * + * @param packageName the package name. + * It must not include the last '.' (dot). + * For example, "java.util" is valid but "java.util." is wrong. + * @since 3.1 + */ + public void importPackage(String packageName) { + importedPackages.add(packageName); + } + + /** + * Clear all the package names recorded by <code>importPackage()</code>. + * The <code>java.lang</code> package is not removed. + * + * @see #importPackage(String) + * @since 3.1 + */ + public void clearImportedPackages() { + importedPackages = new ArrayList(); + importedPackages.add("java.lang"); + } + + /** + * Returns all the package names recorded by <code>importPackage()</code>. + * + * @see #importPackage(String) + * @since 3.1 + */ + public Iterator getImportedPackages() { + return importedPackages.iterator(); + } + + /** * Records a name that never exists. * For example, a package name can be recorded by this method. * This would improve execution performance diff --git a/src/main/javassist/CtClass.java b/src/main/javassist/CtClass.java index eabdb891..b2d6ff10 100644 --- a/src/main/javassist/CtClass.java +++ b/src/main/javassist/CtClass.java @@ -235,6 +235,7 @@ public abstract class CtClass { * and thus it cannot be modified any more. * * @see #defrost() + * @see #detach() */ public boolean isFrozen() { return true; } @@ -260,6 +261,7 @@ public abstract class CtClass { * * @see #isFrozen() * @see #stopPruning(boolean) + * @see #detach() */ public void defrost() { throw new RuntimeException("cannot defrost " + getName()); diff --git a/src/main/javassist/compiler/MemberResolver.java b/src/main/javassist/compiler/MemberResolver.java index fafbe22b..f931c9de 100644 --- a/src/main/javassist/compiler/MemberResolver.java +++ b/src/main/javassist/compiler/MemberResolver.java @@ -16,6 +16,7 @@ package javassist.compiler; import java.util.List; +import java.util.Iterator; import javassist.*; import javassist.bytecode.*; import javassist.compiler.ast.*; @@ -354,21 +355,32 @@ public class MemberResolver implements TokenId { try { return lookupClass0(name, notCheckInner); } - catch (NotFoundException e) {} - if (name.indexOf('.') < 0) { - String jlangName = "java.lang." + name; - try { - CtClass cc = classPool.get(jlangName); - // if java.lang... is found, then - classPool.recordInvalidClassName(name); - return cc; - } - catch (NotFoundException e) { - classPool.recordInvalidClassName(jlangName); + catch (NotFoundException e) { + return searchImports(name); + } + } + + private CtClass searchImports(String orgName) + throws CompileError + { + if (orgName.indexOf('.') < 0) { + Iterator it = classPool.getImportedPackages(); + while (it.hasNext()) { + String pac = (String)it.next(); + String fqName = pac + '.' + orgName; + try { + CtClass cc = classPool.get(fqName); + // if the class is found, + classPool.recordInvalidClassName(orgName); + return cc; + } + catch (NotFoundException e) { + classPool.recordInvalidClassName(fqName); + } } } - throw new CompileError("no such class: " + name); + throw new CompileError("no such class: " + orgName); } private CtClass lookupClass0(String classname, boolean notCheckInner) |