From: chiba Date: Fri, 4 Nov 2005 06:00:46 +0000 (+0000) Subject: updated CtMember.append() for better performance. X-Git-Tag: rel_3_17_1_ga~410 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=eb12cc5d6bcbc25ea70f678321693971a0bd921c;p=javassist.git updated CtMember.append() for better performance. git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@216 30ef5769-5b8d-40dd-aea6-55b5d6557bb3 --- 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 ClassPool must contain all the instances of CtClass all the time of program execution. +

Avoid out of memory

+

This specification of ClassPool may cause huge memory @@ -337,13 +339,26 @@ cp.appendSystemPath(); // or append another path by appendClassPath() default ClassPool returned by ClassPool.getDefault() does. Note that ClassPool.getDefault() is a singleton factory method -provided for convenience. Therefore, the default ClassPool -is never garbage-collected. +provided for convenience. It creates a ClassPool object in +the same way shown above although it keeps a single instance of +ClassPool and reuses it. +A ClassPool object returned by getDefault() +does not have a special role. getDefault() is a convenience +method. +

Cascaded ClassPools.

-ClassPool objects can be cascaded like +If a program is running on a web application server, +creating multiple instances of ClassPool might be necessary; +an instance of ClassPool should be created +for each class loader (i.e. container). +The program should create a ClassPool object by not calling +getDefault() but a constructor of ClassPool. + +

+Multiple ClassPool objects can be cascaded like java.lang.ClassLoader. For example,