*/
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
throw new RuntimeException("cannot defrost " + getName());
}
- /**
- * 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,
cp.cacheCtClass(getName(), obj, false);
}
+ /**
+ * 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
}
}
- /**
- * 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
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()
if (classfile != null)
return classfile;
- /*
- if (readCounter++ > READ_THRESHOLD) {
- doCompaction();
+ if (readCounter++ > READ_THRESHOLD
+ && ClassPool.releaseUnmodifiedClassFile) {
+ releaseClassFiles();
readCounter = 0;
}
- */
InputStream fin = null;
try {
*/
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();
}
}
- 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