/**
* Provide a hook so that subclasses can do their own
* caching of classes
+ *
+ * @see #removeCached(String)
*/
protected CtClass getCached(String classname)
{
return (CtClass)classes.get(classname);
}
+ /**
+ * Provide a hook so that subclasses can do their own
+ * caching of classes
+ *
+ * @see #getCached(String)
+ */
+ protected void removeCached(String classname)
+ {
+ classes.remove(classname);
+ }
+
/**
* Creates a class pool.
*
synchronized void classNameChanged(String oldname, CtClass clazz) {
CtClass c = (CtClass)getCached(oldname);
if (c == clazz) // must check this equation
- classes.remove(c);
+ removeCached(oldname);
String newName = clazz.getName();
checkNotFrozen(newName, "the class with the new name is frozen.");
Java bytecode is stored in a binary file called a class file.
Each class file contains one Java class or interface.
-<p>The class <code>Javassist.CtClass</code> is an abstract representation
-of a class file. A <code>CtClass</code> object is a handle for dealing
-with a class file. The following program is a very simple example:
+<p>The class <code>Javassist.CtClass</code> is an abstract
+representation of a class file. A <code>CtClass</code> (compile-time
+class) object is a handle for dealing with a class file. The
+following program is a very simple example:
<ul><pre>
ClassPool pool = ClassPool.getDefault();
mapping between classes and <code>CtClass</code> objects. Javassist
never allows two distinct <code>CtClass</code> objects to represent
the same class. This is a crucial feature to consistent program
-transformaiton.
+transformaiton. If you need, however, you can deal with multiple
+instances of <code>ClassPool</code> at the same time. To create a new
+instance of <code>ClassPool</code>, write the following code:
+
+<ul><pre>
+ClassPool cp = new ClassPool(null);
+cp.appendSystemPath();
+cp.insertClassPath("."); // or something appropriate
+</pre></ul>
+
+<p><code>ClassPool.getDefault()</code> is just a singleton factory
+method provided for convenience.
<p>To modify the definition of a class, the users must first obtain a
reference to the <code>CtClass</code> object representing that class.