diff options
author | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2012-02-11 03:12:41 +0000 |
---|---|---|
committer | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2012-02-11 03:12:41 +0000 |
commit | 0564d29d853bb18655c35405c74a421e2e550cef (patch) | |
tree | 00807f83c18461432cc5ef800f697c961cedf6e9 /src/main/javassist | |
parent | b83522334f4abe2d9d3f83d0b6949fabd984cf75 (diff) | |
download | javassist-0564d29d853bb18655c35405c74a421e2e550cef.tar.gz javassist-0564d29d853bb18655c35405c74a421e2e550cef.zip |
fixed JASSIST-150
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@611 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
Diffstat (limited to 'src/main/javassist')
-rw-r--r-- | src/main/javassist/ClassPool.java | 12 | ||||
-rw-r--r-- | src/main/javassist/ClassPoolTail.java | 16 | ||||
-rw-r--r-- | src/main/javassist/compiler/MemberCodeGen.java | 2 | ||||
-rw-r--r-- | src/main/javassist/compiler/MemberResolver.java | 74 |
4 files changed, 50 insertions, 54 deletions
diff --git a/src/main/javassist/ClassPool.java b/src/main/javassist/ClassPool.java index dbbb99e5..2c467fb2 100644 --- a/src/main/javassist/ClassPool.java +++ b/src/main/javassist/ClassPool.java @@ -331,17 +331,21 @@ public class ClassPool { } /** - * Records a name that never exists. + * Records a class name that never exists. * For example, a package name can be recorded by this method. * This would improve execution performance - * since <code>get()</code> does not search the class path at all + * since <code>get()</code> quickly throw an exception + * without searching the class path at all * if the given name is an invalid name recorded by this method. * Note that searching the class path takes relatively long time. * - * @param name a class name (separeted by dot). + * <p>The current implementation of this method performs nothing. + * + * @param name an invalid class name (separeted by dots). + * @deprecated */ public void recordInvalidClassName(String name) { - source.recordInvalidClassName(name); + // source.recordInvalidClassName(name); } /** diff --git a/src/main/javassist/ClassPoolTail.java b/src/main/javassist/ClassPoolTail.java index 72470ed0..2fd952b7 100644 --- a/src/main/javassist/ClassPoolTail.java +++ b/src/main/javassist/ClassPoolTail.java @@ -178,11 +178,9 @@ final class JarClassPath implements ClassPath { final class ClassPoolTail { protected ClassPathList pathList; - private Hashtable packages; // should be synchronized. public ClassPoolTail() { pathList = null; - packages = new Hashtable(); } public String toString() { @@ -270,14 +268,6 @@ final class ClassPoolTail { } /** - * You can record "System" so that java.lang.System can be quickly - * found although "System" is not a package name. - */ - public void recordInvalidClassName(String name) { - packages.put(name, name); - } - - /** * This method does not close the output stream. */ void writeClassfile(String classname, OutputStream out) @@ -325,9 +315,6 @@ final class ClassPoolTail { InputStream openClassfile(String classname) throws NotFoundException { - if (packages.get(classname) != null) - return null; // not found - ClassPathList list = pathList; InputStream ins = null; NotFoundException error = null; @@ -361,9 +348,6 @@ final class ClassPoolTail { * @return null if the class file could not be found. */ public URL find(String classname) { - if (packages.get(classname) != null) - return null; - ClassPathList list = pathList; URL url = null; while (list != null) { diff --git a/src/main/javassist/compiler/MemberCodeGen.java b/src/main/javassist/compiler/MemberCodeGen.java index d4cd254a..d5c61bd2 100644 --- a/src/main/javassist/compiler/MemberCodeGen.java +++ b/src/main/javassist/compiler/MemberCodeGen.java @@ -504,7 +504,6 @@ public class MemberCodeGen extends CodeGen { exprType = CLASS; arrayDim = 0; className = nfe.getField(); // JVM-internal - resolver.recordPackage(className); isStatic = true; } @@ -1078,7 +1077,6 @@ public class MemberCodeGen extends CodeGen { Symbol fname = (Symbol)e.oprand2(); String cname = nfe.getField(); f = resolver.lookupFieldByJvmName2(cname, fname, expr); - resolver.recordPackage(cname); resultStatic = true; return f; } diff --git a/src/main/javassist/compiler/MemberResolver.java b/src/main/javassist/compiler/MemberResolver.java index 164ca008..f35b0ef6 100644 --- a/src/main/javassist/compiler/MemberResolver.java +++ b/src/main/javassist/compiler/MemberResolver.java @@ -16,6 +16,8 @@ package javassist.compiler; +import java.util.Hashtable; +import java.util.WeakHashMap; import java.util.List; import java.util.Iterator; import javassist.*; @@ -37,22 +39,6 @@ public class MemberResolver implements TokenId { throw new CompileError("fatal"); } - /** - * @param jvmClassName a class name. Not a package name. - */ - public void recordPackage(String jvmClassName) { - String classname = jvmToJavaName(jvmClassName); - for (;;) { - int i = classname.lastIndexOf('.'); - if (i > 0) { - classname = classname.substring(0, i); - classPool.recordInvalidClassName(classname); - } - else - break; - } - } - public static class Method { public CtClass declaring; public MethodInfo info; @@ -276,6 +262,7 @@ public class MemberResolver implements TokenId { * Only used by fieldAccess() in MemberCodeGen and TypeChecker. * * @param jvmClassName a JVM class name. e.g. java/lang/String + * @see #lookupClass(String, boolean) */ public CtField lookupFieldByJvmName2(String jvmClassName, Symbol fieldSym, ASTree expr) throws NoFieldException @@ -406,12 +393,44 @@ public class MemberResolver implements TokenId { public CtClass lookupClass(String name, boolean notCheckInner) throws CompileError { + Hashtable cache = getInvalidNames(); + Object found = cache.get(name); + if (found == INVALID) + throw new CompileError("no such class: " + name); + else if (found != null) + return (CtClass)found; + + CtClass cc = null; try { - return lookupClass0(name, notCheckInner); + cc = lookupClass0(name, notCheckInner); } catch (NotFoundException e) { - return searchImports(name); + cc = searchImports(name); } + + cache.put(name, cc); + return cc; + } + + private static final String INVALID = "<invalid>"; + private static WeakHashMap invalidNamesMap = new WeakHashMap(); + private Hashtable invalidNames = null; + + private Hashtable getInvalidNames() { + Hashtable ht = invalidNames; + if (ht == null) { + synchronized (MemberResolver.class) { + ht = (Hashtable)invalidNamesMap.get(classPool); + if (ht == null) { + ht = new Hashtable(); + invalidNamesMap.put(classPool, ht); + } + } + + invalidNames = ht; + } + + return ht; } private CtClass searchImports(String orgName) @@ -423,28 +442,19 @@ public class MemberResolver implements TokenId { 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; + return classPool.get(fqName); } catch (NotFoundException e) { - classPool.recordInvalidClassName(fqName); try { - if (pac.endsWith("." + orgName)) { - CtClass cc = classPool.get(pac); - // if the class is found, - classPool.recordInvalidClassName(orgName); - return cc; - } - } - catch (NotFoundException e2) { - classPool.recordInvalidClassName(pac); + if (pac.endsWith("." + orgName)) + return classPool.get(pac); } + catch (NotFoundException e2) {} } } } + getInvalidNames().put(orgName, INVALID); throw new CompileError("no such class: " + orgName); } |