aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/main/javassist/CtClassType.java34
-rw-r--r--src/main/javassist/CtMember.java10
-rw-r--r--tutorial/tutorial.html23
3 files changed, 54 insertions, 13 deletions
diff --git a/src/main/javassist/CtClassType.java b/src/main/javassist/CtClassType.java
index bdf389cd..8d8e67d1 100644
--- a/src/main/javassist/CtClassType.java
+++ b/src/main/javassist/CtClassType.java
@@ -618,11 +618,16 @@ class CtClassType extends CtClass {
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;
@@ -721,13 +726,18 @@ class CtClassType extends CtClass {
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;
@@ -870,12 +880,18 @@ class CtClassType extends CtClass {
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;
diff --git a/src/main/javassist/CtMember.java b/src/main/javassist/CtMember.java
index d6bd14f1..b04e107a 100644
--- a/src/main/javassist/CtMember.java
+++ b/src/main/javassist/CtMember.java
@@ -25,6 +25,16 @@ public abstract class CtMember {
protected CtMember(CtClass clazz) { declaringClass = clazz; }
+ static CtMember append(CtMember list, CtMember previousTail, CtMember tail) {
+ tail.next = null;
+ if (list == null)
+ return tail;
+ else {
+ previousTail.next = tail;
+ return list;
+ }
+ }
+
static CtMember append(CtMember list, CtMember tail) {
tail.next = null;
if (list == null)
diff --git a/tutorial/tutorial.html b/tutorial/tutorial.html
index f591abe7..271e88de 100644
--- a/tutorial/tutorial.html
+++ b/tutorial/tutorial.html
@@ -298,7 +298,9 @@ such a method call, the <code>ClassPool</code>
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
@@ -337,13 +339,26 @@ cp.appendSystemPath(); // or append another path by appendClassPath()
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>
@@ -435,7 +450,7 @@ transformation.
<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();