diff options
author | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2004-04-18 17:32:28 +0000 |
---|---|---|
committer | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2004-04-18 17:32:28 +0000 |
commit | 17ac0d828783b8f3036d259d618f5a8effd898d7 (patch) | |
tree | d8bdea70843a44d9322f97ecadaacb2389035625 | |
parent | 4929508cfb11ea5ba2b3d0ff645e8f60c1e31519 (diff) | |
download | javassist-17ac0d828783b8f3036d259d618f5a8effd898d7.tar.gz javassist-17ac0d828783b8f3036d259d618f5a8effd898d7.zip |
edited for improving runtime performance.
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@88 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
-rw-r--r-- | src/main/javassist/ClassPool.java | 6 | ||||
-rw-r--r-- | src/main/javassist/ClassPoolTail.java | 42 | ||||
-rw-r--r-- | src/main/javassist/CtClassType.java | 20 | ||||
-rw-r--r-- | src/main/javassist/bytecode/ConstPool.java | 34 | ||||
-rw-r--r-- | src/main/javassist/bytecode/LongVector.java | 7 |
5 files changed, 42 insertions, 67 deletions
diff --git a/src/main/javassist/ClassPool.java b/src/main/javassist/ClassPool.java index 3d2b83ab..52909cd2 100644 --- a/src/main/javassist/ClassPool.java +++ b/src/main/javassist/ClassPool.java @@ -440,10 +440,10 @@ public class ClassPool { /* for CtClassType.getClassFile2(). Don't delegate to the parent. */ - byte[] readSource(String classname) - throws NotFoundException, IOException, CannotCompileException + InputStream openClassfile(String classname) + throws NotFoundException { - return source.readSource(classname); + return source.openClassfile(classname); } void writeClassfile(String classname, OutputStream out) diff --git a/src/main/javassist/ClassPoolTail.java b/src/main/javassist/ClassPoolTail.java index 5cfc2933..ef576423 100644 --- a/src/main/javassist/ClassPoolTail.java +++ b/src/main/javassist/ClassPoolTail.java @@ -225,20 +225,6 @@ final class ClassPoolTail { } /** - * @return the contents of the class file. - * @throws NotFoundException if the file could not be found. - */ - byte[] readSource(String classname) - throws NotFoundException, IOException, CannotCompileException - { - byte[] b = readClassfile(classname); - if (b == null) - throw new NotFoundException(classname); - else - return b; - } - - /** * This method does not close the output stream. */ void writeClassfile(String classname, OutputStream out) @@ -276,32 +262,6 @@ final class ClassPoolTail { /** - * Obtains the contents of the class file for the class - * specified by <code>classname</code>. - * - * @param classname a fully-qualified class name - * @return null if the file has not been found. - * @throws NotFoundException if any error is reported by ClassPath. - */ - private byte[] readClassfile(String classname) - throws NotFoundException, IOException - { - InputStream fin = openClassfile(classname); - if (fin == null) - return null; - - byte[] b; - try { - b = readStream(fin); - } - finally { - fin.close(); - } - - return b; - } - - /** * Opens the class file for the class specified by * <code>classname</code>. * @@ -309,7 +269,7 @@ final class ClassPoolTail { * @return null if the file has not been found. * @throws NotFoundException if any error is reported by ClassPath. */ - private InputStream openClassfile(String classname) + InputStream openClassfile(String classname) throws NotFoundException { if (packages.get(classname) != null) diff --git a/src/main/javassist/CtClassType.java b/src/main/javassist/CtClassType.java index c74acbf5..44481553 100644 --- a/src/main/javassist/CtClassType.java +++ b/src/main/javassist/CtClassType.java @@ -19,7 +19,6 @@ import javassist.bytecode.*; import javassist.compiler.Javac; import javassist.compiler.CompileError; import javassist.expr.ExprEditor; -import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.DataInputStream; import java.io.DataOutputStream; @@ -108,11 +107,14 @@ class CtClassType extends CtClass { if (classfile != null) return classfile; + InputStream fin = null; try { - byte[] b = classPool.readSource(getName()); - DataInputStream dis - = new DataInputStream(new ByteArrayInputStream(b)); - return (classfile = new ClassFile(dis)); + fin = classPool.openClassfile(getName()); + if (fin == null) + throw new NotFoundException(getName()); + + classfile = new ClassFile(new DataInputStream(fin)); + return classfile; } catch (NotFoundException e) { throw new RuntimeException(e.toString()); @@ -120,8 +122,12 @@ class CtClassType extends CtClass { catch (IOException e) { throw new RuntimeException(e.toString()); } - catch (CannotCompileException e) { - throw new RuntimeException(e.toString()); + finally { + if (fin != null) + try { + fin.close(); + } + catch (IOException e) {} } } diff --git a/src/main/javassist/bytecode/ConstPool.java b/src/main/javassist/bytecode/ConstPool.java index 8d850cac..d517b531 100644 --- a/src/main/javassist/bytecode/ConstPool.java +++ b/src/main/javassist/bytecode/ConstPool.java @@ -21,7 +21,7 @@ import java.io.ByteArrayOutputStream; import java.io.PrintWriter; import java.io.IOException; import java.util.Map; -import java.util.Hashtable; +import java.util.HashMap; import javassist.CtClass; /** @@ -30,10 +30,9 @@ import javassist.CtClass; public final class ConstPool { LongVector items; int numOfItems; - Hashtable classes; - Hashtable strings; + HashMap classes; + HashMap strings; int thisClassInfo; - private static final int SIZE = 128; /** * <code>CONSTANT_Class</code> @@ -103,7 +102,11 @@ public final class ConstPool { * pool table */ public ConstPool(String thisclass) { - this(); + items = new LongVector(); + numOfItems = 0; + addItem(null); // index 0 is reserved by the JVM. + classes = new HashMap(); + strings = new HashMap(); thisClassInfo = addClassInfo(thisclass); } @@ -113,17 +116,12 @@ public final class ConstPool { * @param in byte stream. */ public ConstPool(DataInputStream in) throws IOException { - this(); - read(in); - } - - private ConstPool() { - items = new LongVector(SIZE); - numOfItems = 0; - addItem(null); // index 0 is reserved by the JVM. - classes = new Hashtable(); - strings = new Hashtable(); + classes = new HashMap(); + strings = new HashMap(); thisClassInfo = 0; + /* read() initializes items and numOfItems, and do addItem(null). + */ + read(in); } /** @@ -861,6 +859,12 @@ public final class ConstPool { private void read(DataInputStream in) throws IOException { int n = in.readUnsignedShort(); + + int size = (n / LongVector.SIZE + 1) * LongVector.SIZE; + items = new LongVector(size); + numOfItems = 0; + addItem(null); // index 0 is reserved by the JVM. + while (--n > 0) { // index 0 is reserved by JVM int tag = readOne(in); if ((tag == LongInfo.tag) || (tag == DoubleInfo.tag)) { diff --git a/src/main/javassist/bytecode/LongVector.java b/src/main/javassist/bytecode/LongVector.java index c4be753e..279bb4fd 100644 --- a/src/main/javassist/bytecode/LongVector.java +++ b/src/main/javassist/bytecode/LongVector.java @@ -16,10 +16,15 @@ package javassist.bytecode; final class LongVector { + static final int SIZE = 128; private int num; private Object[] objects; private LongVector next; + public LongVector() { + this(SIZE); + } + public LongVector(int initialSize) { num = 0; objects = new Object[initialSize]; @@ -34,7 +39,7 @@ final class LongVector { if (p.num < p.objects.length) p.objects[p.num++] = obj; else { - LongVector q = p.next = new LongVector(p.objects.length); + LongVector q = p.next = new LongVector(SIZE); q.objects[q.num++] = obj; } } |