From 4fda748aa5e0fcc4d0207169209062399fe2aa81 Mon Sep 17 00:00:00 2001 From: chiba Date: Mon, 30 Aug 2004 17:41:36 +0000 Subject: discarded the last changes git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@129 30ef5769-5b8d-40dd-aea6-55b5d6557bb3 --- tutorial/tutorial.html | 254 +++++++++++++++++++++++++++++++----------------- tutorial/tutorial2.html | 21 ++-- tutorial/tutorial3.html | 12 +-- 3 files changed, 180 insertions(+), 107 deletions(-) (limited to 'tutorial') diff --git a/tutorial/tutorial.html b/tutorial/tutorial.html index 064f8da8..84de35b4 100644 --- a/tutorial/tutorial.html +++ b/tutorial/tutorial.html @@ -18,19 +18,18 @@ Shigeru Chiba

Next page
- + +

Defining a new class

+ +

To define a new class from scratch, makeClass() +must be called on a ClassPool. + +

+ +

This program defines a class Point +including no members. + +

Frozen classes

+ +

If a CtClass object is converted into a class file by +writeFile(), toClass(), or +toBytecode(), Javassist freezes that CtClass +object. Further modifications of that CtClass object are +not permitted. + +

When Javassist freezes a CtClass object, it also +prunes the data structure contained in that object. To reduce memory +consumption, Javassist discards some part of data structure, for +example, the data of method bodies. Thus, after a +CtClass object is pruned, the bytecode of a method is not +accessible although method names and signatures are accessible. + +

To disallow pruning a CtClass, stopPruning() +must be called in advance: + +

+ +

If a CtClass is not pruned, it can be defrost so that +modifications of the class definition can be permitted. For example, + +

+ +

Class search path

The default ClassPool returned @@ -187,21 +239,106 @@ included in the search path.


-
-

2. Defining a new class

+ +

2. ClassPool

-

To define a new class from scratch, makeClass() -must be called on a ClassPool. +

+A ClassPool object is a container of CtClass +objects. Once a CtClass object is created, it is +recorded in a ClassPool for ever. This is because a +compiler may need to access the CtClass object later when +it compiles source code that refers to the class represented by that +CtClass. + +

+For example, suppose that a new method getter() is added +to a CtClass object representing Point +class. Later, the program attempts to compile source code including a +method call to getter() in Point and use the +compiled code as the body of a method, which will be added to another +class Line. If the CtClass object representing +Point is lost, the compiler cannot compile the method call +to getter(). Note that the original class definition does +not include getter(). Therefore, to correctly compile +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 +consumption if the number of CtClass objects becomes +amazingly large (this rarely happens since Javassist tries to reduce +memory consumption in various ways). To avoid this problem, you +can explicitly remove an unnecessary CtClass object from +the ClassPool. If you call detach() on a +CtClass object, then that CtClass object is +removed from the ClassPool. For example,

-

This program defines a class Point -including no members. +

You must not call any method on that +CtClass object after detach() is called. + +

+Another idea is to occasionally replace a ClassPool with +a new one and discard the old one. If an old ClassPool +is garbage collected, the CtClass objects included in +that ClassPool are also garbage collected. +To create a new instance of ClassPool, execute the following +code snippet: -

A new class can be also defined as a copy of an existing class. +

+ +

This creates a ClassPool object that behaves as the +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. + +

Cascaded ClassPools.

+ +

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

+ +

+If child.get() is called, the child ClassPool +first delegates to the parent ClassPool. If the parent +ClassPool fails to find a class file, then the child +ClassPool attempts to find a class file +under the ./classes directory. + +

+If child.childFirstLookup is true, the child +ClassPool attempts to find a class file before delegating +to the parent ClassPool. For example, + +

+ +

Changing a class name for defining a new class

+ +

A new class can be defined as a copy of an existing class. The program below does that: