diff options
author | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2005-06-16 17:52:38 +0000 |
---|---|---|
committer | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2005-06-16 17:52:38 +0000 |
commit | ee8265e79f1e6c4f399abbca73c3174e68e83cc0 (patch) | |
tree | 127c48495db95d9be9125a3479ca49fa38db83a6 /src/main/javassist | |
parent | c4db11b46b7011f0c6279d552326428a8ab86b76 (diff) | |
download | javassist-ee8265e79f1e6c4f399abbca73c3174e68e83cc0.tar.gz javassist-ee8265e79f1e6c4f399abbca73c3174e68e83cc0.zip |
Turning the automatic pruning on by default.
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@181 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
Diffstat (limited to 'src/main/javassist')
-rw-r--r-- | src/main/javassist/ClassPool.java | 27 | ||||
-rw-r--r-- | src/main/javassist/CtClass.java | 83 | ||||
-rw-r--r-- | src/main/javassist/CtClassType.java | 32 | ||||
-rw-r--r-- | src/main/javassist/bytecode/ClassFile.java | 1 |
4 files changed, 97 insertions, 46 deletions
diff --git a/src/main/javassist/ClassPool.java b/src/main/javassist/ClassPool.java index 05b0bb82..452f2e6f 100644 --- a/src/main/javassist/ClassPool.java +++ b/src/main/javassist/ClassPool.java @@ -73,6 +73,33 @@ public class ClassPool { */ public boolean childFirstLookup = false; + /** + * Turning the automatic pruning on/off. + * + * <p>If this field is true, <code>CtClass</code> objects are + * automatically pruned by default when <code>toBytecode()</code> etc. + * are called. The automatic pruning can be turned on/off individually + * for each <code>CtClass</code> object. + * + * <p>The initial value is true. + * + * @see CtClass#prune() + * @see CtClass#stopPruning(boolean) + */ + public static boolean doPruning = true; + + /* releaseUnmodifiedClassFile was introduced for avoiding a bug + of JBoss AOP. So the value should be true except for JBoss AOP. + */ + + /** + * If true, unmodified and not-recently-used class files are + * periodically released for saving memory. + * + * <p>The initial value is true. + */ + public static boolean releaseUnmodifiedClassFile = true; + protected ClassPoolTail source; protected ClassPool parent; protected Hashtable classes; // should be synchronous diff --git a/src/main/javassist/CtClass.java b/src/main/javassist/CtClass.java index 67378862..eabdb891 100644 --- a/src/main/javassist/CtClass.java +++ b/src/main/javassist/CtClass.java @@ -266,33 +266,6 @@ public abstract class CtClass { } /** - * Disallows (or allows) pruning the data structure on memory - * when this <code>CtClass</code> object is converted into a class file. - * Pruning saves memory space since a <code>ClassPool</code> holds - * all instances of <code>CtClass</code> - * all the time of program execution. - * However, pruning discards the data representing part of the - * class definition, such as method bodies. - * Therefore, once it is pruned, <code>toBytecode()</code>, - * <code>writeFile()</code>, or <code>toClass()</code> cannot - * be called again. - * - * <p>Initially, pruning is allowed. - * - * @param stop disallow pruning if true. Otherwise, allow. - * @see #detach() - * @see #toBytecode() - * @see #toClass() - * @see #writeFile() - */ - public void stopPruning(boolean stop) {} - - /* Called by get() in ClassPool. - * CtClassType overrides this method. - */ - void incGetCounter() {} - - /** * Returns <code>true</code> if this object represents a primitive * Java type: boolean, byte, char, short, int, long, float, double, * or void. @@ -1007,6 +980,55 @@ public abstract class CtClass { } /** + * Disallows (or allows) automatically pruning this <code>CtClass</code> + * object. + * + * <p> + * Javassist can automatically prune a <code>CtClass</code> object + * when <code>toBytecode()</code> (or <code>toClass()</code>, + * <code>writeFile()</code>) is called. + * Since a <code>ClassPool</code> holds all instances of <code>CtClass</code> + * even after <code>toBytecode()</code> (or <code>toClass()</code>, + * <code>writeFile()</code>) is called, pruning may significantly + * save memory consumption. + * + * <p>If <code>ClassPool.doPruning</code> is true, the automatic pruning + * is on by default. Otherwise, it is off. + * + * @param stop disallow pruning if true. Otherwise, allow. + * + * @see #detach() + * @see #prune() + * @see ClassPool#doPruning + */ + public void stopPruning(boolean stop) {} + + /** + * Discards unnecessary data, in particuar, <code>CodeAttribute</code>s + * (method bodies) of the class, + * to minimize the memory footprint. + * After calling this method, the class is read only. + * It cannot be modified any more. + * Furthermore, <code>toBytecode()</code>, + * <code>writeFile()</code>, <code>toClass()</code>, + * or <code>instrument()</code> cannot be called. + * However, the method names and signatures in the class etc. + * are still accessible. + * + * @see #toBytecode() + * @see #toClass() + * @see #writeFile() + * + * @see #stopPruning(boolean) + */ + public void prune() {} + + /* Called by get() in ClassPool. + * CtClassType overrides this method. + */ + void incGetCounter() {} + + /** * Converts this class to a class file. * Once this method is called, further modifications are not * possible any more. @@ -1112,13 +1134,6 @@ public abstract class CtClass { } /** - * Discards unnecessary data to minimize the memory footprint. - * After calling this method, the class is read only. - * It cannot be modified any more. - */ - public void prune() {} - - /** * Converts this class to a class file. * Once this method is called, further modifications are not * possible any more. diff --git a/src/main/javassist/CtClassType.java b/src/main/javassist/CtClassType.java index 6b1ad068..44014641 100644 --- a/src/main/javassist/CtClassType.java +++ b/src/main/javassist/CtClassType.java @@ -66,7 +66,7 @@ class CtClassType extends CtClass { private Hashtable hiddenMethods; // must be synchronous private int uniqueNumberSeed; - private boolean doPruning = false; + private boolean doPruning = ClassPool.doPruning; int getCounter; private static int readCounter = 0; private static final int READ_THRESHOLD = 100; // see getClassFile2() @@ -173,12 +173,11 @@ class CtClassType extends CtClass { if (classfile != null) return classfile; - /* - if (readCounter++ > READ_THRESHOLD) { - doCompaction(); + if (readCounter++ > READ_THRESHOLD + && ClassPool.releaseUnmodifiedClassFile) { + releaseClassFiles(); readCounter = 0; } - */ InputStream fin = null; try { @@ -215,7 +214,12 @@ class CtClassType extends CtClass { */ void incGetCounter() { ++getCounter; } - private void doCompaction() { + /** + * Releases the class files and cached CtBehaviors + * of the CtClasses that have not been recently used + * if they are unmodified. + */ + private void releaseClassFiles() { Enumeration e = classPool.classes.elements(); while (e.hasMoreElements()) { Object obj = e.nextElement(); @@ -963,13 +967,17 @@ class CtClassType extends CtClass { } } - public void prune() { - if (wasPruned) - return; + /** + * @see javassist.CtClass#prune() + * @see javassist.CtClass#stopPruning(boolean) + */ + public void prune() { + if (wasPruned) + return; - wasPruned = wasFrozen = true; - getClassFile2().prune(); - } + wasPruned = wasFrozen = true; + getClassFile2().prune(); + } public void toBytecode(DataOutputStream out) throws CannotCompileException, IOException diff --git a/src/main/javassist/bytecode/ClassFile.java b/src/main/javassist/bytecode/ClassFile.java index 4a8fa4ab..9fd3c8e4 100644 --- a/src/main/javassist/bytecode/ClassFile.java +++ b/src/main/javassist/bytecode/ClassFile.java @@ -373,6 +373,7 @@ public final class ClassFile { /** * Returns the names of the interfaces implemented by the class. + * The returned array is read only. */ public String[] getInterfaces() { if (cachedInterfaces != null) |