if (fieldsCache == null) {
List list = getClassFile2().getFields();
int n = list.size();
+ CtMember allFields = null;
+ CtField tail = null;
for (int i = 0; i < n; ++i) {
FieldInfo finfo = (FieldInfo)list.get(i);
- fieldsCache = CtMember.append(fieldsCache,
- new CtField(finfo, this));
+ CtField newTail = new CtField(finfo, this);
+ allFields = CtMember.append(allFields, tail, newTail);
+ tail = newTail;
}
+
+ fieldsCache = allFields;
}
return fieldsCache;
if (constructorsCache == null) {
List list = getClassFile2().getMethods();
int n = list.size();
+ CtMember allConstructors = null;
+ CtConstructor tail = null;
for (int i = 0; i < n; ++i) {
MethodInfo minfo = (MethodInfo)list.get(i);
- if (minfo.isConstructor())
- constructorsCache
- = CtMember.append(constructorsCache,
- new CtConstructor(minfo, this));
+ if (minfo.isConstructor()) {
+ CtConstructor newTail = new CtConstructor(minfo, this);
+ allConstructors = CtMember.append(allConstructors, tail, newTail);
+ tail = newTail;
+ }
}
+
+ constructorsCache = allConstructors;
}
return constructorsCache;
if (methodsCache == null) {
List list = getClassFile2().getMethods();
int n = list.size();
+ CtMember allMethods = null;
+ CtMethod tail = null;
for (int i = 0; i < n; ++i) {
MethodInfo minfo = (MethodInfo)list.get(i);
- if (minfo.isMethod())
- methodsCache = CtMember.append(methodsCache,
- new CtMethod(minfo, this));
+ if (minfo.isMethod()) {
+ CtMethod newTail = new CtMethod(minfo, this);
+ allMethods = CtMember.append(allMethods, tail, newTail);
+ tail = newTail;
+ }
}
+
+ methodsCache = allMethods;
}
return methodsCache;
must contain all the instances of <code>CtClass</code> all the time of
program execution.
+<a name="avoidmemory">
<h4>Avoid out of memory</h4>
+</a>
<p>
This specification of <code>ClassPool</code> may cause huge memory
default <code>ClassPool</code> returned by
<code>ClassPool.getDefault()</code> does.
Note that <code>ClassPool.getDefault()</code> is a singleton factory method
-provided for convenience. Therefore, the default <code>ClassPool</code>
-is never garbage-collected.
+provided for convenience. It creates a <code>ClassPool</code> object in
+the same way shown above although it keeps a single instance of
+<code>ClassPool</code> and reuses it.
+A <code>ClassPool</code> object returned by <code>getDefault()</code>
+does not have a special role. <code>getDefault()</code> is a convenience
+method.
+
<h4>Cascaded ClassPools.</h4>
<p>
-<code>ClassPool</code> objects can be cascaded like
+<em>If a program is running on a web application server,</em>
+creating multiple instances of <code>ClassPool</code> might be necessary;
+an instance of <code>ClassPool</code> should be created
+for each class loader (i.e. container).
+The program should create a <code>ClassPool</code> object by not calling
+<code>getDefault()</code> but a constructor of <code>ClassPool</code>.
+
+<p>
+Multiple <code>ClassPool</code> objects can be cascaded like
<code>java.lang.ClassLoader</code>. For example,
<ul><pre>
<p>To create another copy of the default instance of
<code>ClassPool</code>, which is returned by
<code>ClassPool.getDefault()</code>, execute the following code
-snippet (this code was already shown above):
+snippet (this code was already <a href="#avoidmemory">shown above</a>):
<ul><pre>
ClassPool cp = new ClassPool();