From: chiba Date: Fri, 20 Feb 2004 03:45:41 +0000 (+0000) Subject: update javadoc comments. X-Git-Tag: rel_3_17_1_ga~541 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0cbf7a9b805dfa809ac9795743199a4765641680;p=javassist.git update javadoc comments. git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@70 30ef5769-5b8d-40dd-aea6-55b5d6557bb3 --- diff --git a/src/main/javassist/ClassPool.java b/src/main/javassist/ClassPool.java index 4e00b335..863750a2 100644 --- a/src/main/javassist/ClassPool.java +++ b/src/main/javassist/ClassPool.java @@ -70,6 +70,17 @@ import java.util.Hashtable; * *

The implementation of this class is thread-safe. * + *

Memory consumption memo: + * + *

ClassPool objects hold all the CtClasses + * that have been created so that the consistency among modified classes + * can be guaranteed. Thus if a large number of CtClasses + * are processed, the ClassPool will consume a huge amount + * of memory. To avoid this, multiple ClassPool objects + * must be used. Note that getDefault() is a singleton + * factory. + * + * * @see javassist.CtClass * @see javassist.ClassPath * @see javassist.Translator @@ -159,7 +170,8 @@ public class ClassPool { /** * Returns the default class pool. - * The returned object is always identical. + * The returned object is always identical since this method is + * a singleton factory. * *

The default class pool searches the system search path, * which usually includes the platform library, extension @@ -167,6 +179,13 @@ public class ClassPool { * -classpath option or the CLASSPATH * environment variable. * + *

When this method is called for the first time, the default + * class pool is created with the following code snippet: + * + *

+ * * @param t null or the translator linked to the class pool. */ public static synchronized ClassPool getDefault(Translator t) { diff --git a/tutorial/tutorial.html b/tutorial/tutorial.html index c68eb814..65267f04 100644 --- a/tutorial/tutorial.html +++ b/tutorial/tutorial.html @@ -336,29 +336,153 @@ by a single class loader. You should avoid loading part of the application program with the default class loader and the rest of the program with a user-defined class loader. -

In Java, multiple class loaders can coexist in Java and they form a -tree structure. Each class loader except the bootstrap loader has a -parent class loader, which normally loaded the class of that child -class loader. Since the request to load a class can be delegated along this -hierarchy of class loaders, a class may be loaded by a class loader that -you do not request the class loading. +


-Furthermore, if a class loader CL requested to load a class C delegates -to the parenet class loader, then the class loader CL is never requested -to load any classes included in the definition of the class C. -Instead, the parent class loader of CL is requested to load them. +

4.1 Class loading in Java

-

Different class loaders can load different class files with the +

In Java, multiple class loaders can coexist and +each class loader creates its own name space. +Different class loaders can load different class files with the same class name. The loaded two classes are regarded as different -ones. If the same class file is loaded by two distinct class loaders, +ones. This feature enables us to run multiple application programs +on a single JVM. + +

If the same class file is loaded by two distinct class loaders, the JVM makes two distinct classes with the same name and definition. +The two classes are regarded as different ones. Since the two classes are not identical, an instance of one class is not assignable to a variable of the other class. The cast operation between the two classes fails and throws a ClassCastException. +

Multiple class loaders form a tree structure. +Each class loader except the bootstrap loader has a +parent class loader, which has normally loaded the class of that child +class loader. Since the request to load a class can be delegated along this +hierarchy of class loaders, a class may be loaded by a class loader that +you do not request the class loading. +Therefore, the class loader that has been requested to load a class C +may be different from the loader that actually loads the class C. +For distinction, we call the former loader the initiator of C +and we call the latter loader the real loader of C. + +

+Furthermore, if a class loader CL requested to load a class C +(the initiator of C) delegates +to the parent class loader PL, then the class loader CL is never requested +to load any classes referred to in the definition of the class C. +CL is not the initiator of those classes. +Instead, the parent class loader PL becomes their initiators +and it is requested to load them. +The classes that the definition of a class C referes to are loaded by +the real loader of C. + +

To understand this behavior, let's consider the following example. + +

+ +

Suppose that a class Window is loaded by a class loader L. +Both the initiator and the real loader of Window are L. +Since the definition of Window refers to Box, +the JVM will request L to load Box. +Here, suppose that L delegates this task to the parent class loader PL. +The initiator of Box is L but the real loader is PL. +In this case, the initiator of Point is not L but PL +since it is the same as the real loader of Box. +Thus L is never requested to load Point. + +

Next, let's consider a slightly modified example. + +

+ +

Now, the definition of Window also refers to +Point. In this case, the class loader L must +also delegate to PL if it is requested to load Point. +You must avoid having two class loaders doubly load the same +class. One of the two loaders must delegate to +the other. + +

+If L does not delegate to PL when Point +is loaded, widthIs() would throw a ClassCastException. +Since the real loader of Box is PL, +Point referred to in Box is also loaded by PL. +Therefore, the resulting value of getSize() +is an instance of Point loaded by PL +whereas the type of the variable p in widthIs() +is Point loaded by L. +The JVM regards them as distinct types and thus it throws an exception +because of type mismatch. + +

This behavior is somewhat inconvenient but necessary. +If the following statement: + +

+ +

did not throw an exception, +then the programmer of Window could break the encapsulation +of Point objects. +For example, the field x +is private in Point loaded by PL. +However, the Window class could +directly access the value of x +if L loads Point with the following definition: + +

+ +

+For more details of class loaders in Java, the following paper would +be helpful: + +

+


-

4.1 Using javassist.Loader

+

4.2 Using javassist.Loader

Javassist provides a class loader javassist.Loader. This class loader uses a @@ -429,78 +553,6 @@ are loaded by different loaders. The application classes are loaded by javassist.Loader whereas the loader classes such as Main are by the default Java class loader. -

In Java, for security reasons, the same class file may be loaded -by two distinct class loaders so that two different -classes would be created. For example, - -

- -

Suppose that a class Box is loaded by a class loader -L1 while a class Window is loaded by a class -loader L2. Then, the obejcts returned by -getBase() and getSize() are not instances of -the same class Point. -getBase() returns an instance of the class Point -loaded by L1 whereas getSize() returns an -instance of Point loaded by L2. The two versions -of the class Point are distinct. They belong to different -name spaces. For more details, see the following paper: - -

- -

To avoid this problem, the two class loaders L1 and -L2 must delegate the loading operation of the class -Point to another class loader, L3, which is -a parent class loader of L1 and L2. -delegateLoadingOf() in javassist.Loader -is a method for specifying what classes should be loaded by the -parent loader. - -

If L1 is the parent class loader of L2, -that is, if L1 loads the class of L2, -then L2 can delegate the loading operation of -Point to L1 for avoiding the problem above. -However, this technique does not work in the case below: - -

- -

Since all the classes included in a class definition loaded by -a class loader L1 are also loaded by L1, -the class of the field win in Point is -now the class Window loaded by L1. -Thus size.win = this in getSize() throws -a ClassCastException because of type mismatch; the type of -size.win is the class Point loaded by -L1 whereas the type of this is the class -Point loaded by L2. -

javassist.Loader searches for classes in a different order from java.lang.ClassLoader. ClassLoader first delegates the loading operations to @@ -522,15 +574,17 @@ to be loaded by the parent class loader.

This search order allows loading modified classes by Javassist into the JVM. However, it delegates to the parent class loader if it fails to find modified classes for some reason. Once a class is loaded by -the parent class loader, the other classes used by that class will be -also loaded without modification by the parent class loader. If your -program fails to load a modified class, you should make sure whether -all the classes using that class have been loaded by +the parent class loader, the other classes referred to in that class will be +also loaded without modification by the parent class loader. +Recall that all the classes referred to in a class C are loaded by the +real loader of C. +If your program fails to load a modified class, you should +make sure whether all the classes using that class have been loaded by javassist.Loader.


-

4.2 Writing a class loader

+

4.3 Writing a class loader

A simple class loader using Javassist is as follows: @@ -602,7 +656,7 @@ Hence, the


-

4.3 The toClass method in CtClass

+

4.4 The toClass method in CtClass

The CtClass provides a convenience method toClass, which loads the class by an internal class @@ -640,10 +694,9 @@ loaded by loadClass(). If you encounter an unexpected of the class by calling getClassLoader() in java.lang.Class. -


-

4.4 Modifying a system class

+

4.5 Modifying a system class

The system classes like java.lang.String cannot be loaded by a class loader other than the system class loader.