Lines Matching full:code
42 <p>The class <code>Javassist.CtClass</code> is an abstract
43 representation of a class file. A <code>CtClass</code> (compile-time
54 <p>This program first obtains a <code>ClassPool</code> object, which
56 <code>ClassPool</code> object is a container of <code>CtClass</code>
58 constructing a <code>CtClass</code> object and records the
62 from a <code>ClassPool</code> object
63 a reference to a <code>CtClass</code> object representing that class.
64 <code>get()</code> in <code>ClassPool</code> is used for this purpose.
66 <code>CtClass</code> object representing a class
67 <code>test.Rectangle</code> is obtained from the
68 <code>ClassPool</code> object and it is assigned to a variable
69 <code>cc</code>.
70 The <code>ClassPool</code> object returned by <code>getDefault()</code>
73 <p>From the implementation viewpoint, <code>ClassPool</code> is a hash
74 table of <code>CtClass</code> objects, which uses the class names as
75 keys. <code>get()</code> in <code>ClassPool</code> searches this hash
76 table to find a <code>CtClass</code> object associated with the
77 specified key. If such a <code>CtClass</code> object is not found,
78 <code>get()</code> reads a class file to construct a new
79 <code>CtClass</code> object, which is recorded in the hash table and
80 then returned as the resulting value of <code>get()</code>.
82 <p>The <code>CtClass</code> object obtained from a <code>ClassPool</code>
85 a <code>CtClass</code></a> will be presented later).
87 <code>test.Rectangle</code> is changed into a class
88 <code>test.Point</code>. This change is reflected on the original
89 class file when <code>writeFile()</code> in <code>CtClass()</code> is
92 <p><code>writeFile()</code> translates the <code>CtClass</code> object
95 modified bytecode. To obtain the bytecode, call <code>toBytecode()</code>:
101 <p>You can directly load the <code>CtClass</code> as well:
107 <p><code>toClass()</code> requests the context class loader for the current
108 thread to load the class file represented by the <code>CtClass</code>. It
109 returns a <code>java.lang.Class</code> object representing the loaded class.
115 <p>To define a new class from scratch, <code>makeClass()</code>
116 must be called on a <code>ClassPool</code>.
123 <p>This program defines a class <code>Point</code>
125 Member methods of <code>Point</code> can be created with
126 factory methods declared in <code>CtNewMethod</code> and
127 appended to <code>Point</code> with <code>addMethod()</code>
128 in <code>CtClass</code>.
130 <p><code>makeClass()</code> cannot create a new interface;
131 <code>makeInterface()</code> in <code>ClassPool</code> can do.
133 <code>abstractMethod()</code> in <code>CtNewMethod</code>.
139 <p>If a <code>CtClass</code> object is converted into a class file by
140 <code>writeFile()</code>, <code>toClass()</code>, or
141 <code>toBytecode()</code>, Javassist freezes that <code>CtClass</code>
142 object. Further modifications of that <code>CtClass</code> object are
147 <p>A frozen <code>CtClass</code> can be defrost so that
158 <p>After <code>defrost()</code> is called, the <code>CtClass</code>
161 <p>If <code>ClassPool.doPruning</code> is set to <code>true</code>,
163 in a <code>CtClass</code> object
167 (<code>attribute_info</code> structures) in that object.
168 For example, <code>Code_attribute</code> structures (method bodies)
171 <code>CtClass</code> object is pruned, the bytecode of a method is not
173 The pruned <code>CtClass</code> object cannot be defrost again.
174 The default value of <code>ClassPool.doPruning</code> is <code>false</code>.
176 <p>To disallow pruning a particular <code>CtClass</code>,
177 <code>stopPruning()</code> must be called on that object in advance:
187 <p>The <code>CtClass</code> object <code>cc</code> is not pruned.
188 Thus it can be defrost after <code>writeFile()</code> is called.
193 <code>debugWriteFile()</code> is a convenient method
202 <p>The default <code>ClassPool</code> returned
203 by a static method <code>ClassPool.getDefault()</code>
206 the <code>ClassPool</code> object may not be able to find user classes</em>
209 registered to the <code>ClassPool</code>. Suppose that <code>pool</code>
210 refers to a <code>ClassPool</code> object:
218 the class of the object that <code>this</code> refers to.
219 You can use any <code>Class</code> object as an argument instead of
220 <code>this.getClass()</code>. The class path used for loading the
221 class represented by that <code>Class</code> object is registered.
225 For example, the following code adds a directory
226 <code>/usr/local/javalib</code>
245 package <code>org.javassist</code>. For example, to load a class
246 <code>org.javassist.test.Main</code>, its class file will be obtained from:
252 to a <code>ClassPool</code> object
253 and construct a <code>CtClass</code> object from that array. To do this,
254 use <code>ByteArrayClassPath</code>. For example,
264 <p>The obtained <code>CtClass</code> object represents
265 a class defined by the class file specified by <code>b</code>.
266 The <code>ClassPool</code> reads a class file from the given
267 <code>ByteArrayClassPath</code> if <code>get()</code> is called
268 and the class name given to <code>get()</code> is equal to
269 one specified by <code>name</code>.
272 can use <code>makeClass()</code> in <code>ClassPool</code>:
280 <p><code>makeClass()</code> returns the <code>CtClass</code> object
282 <code>makeClass()</code> for eagerly feeding class files to
283 the <code>ClassPool</code> object. This might improve performance
285 a <code>ClassPool</code> object reads a class file on demand,
287 <code>makeClass()</code> can be used for optimizing this search.
288 The <code>CtClass</code> constructed by <code>makeClass()</code>
289 is kept in the <code>ClassPool</code> object and the class file is never
293 class implementing <code>ClassPath</code> interface and give an
294 instance of that class to <code>insertClassPath()</code> in
295 <code>ClassPool</code>. This allows a non-standard resource to be
304 A <code>ClassPool</code> object is a container of <code>CtClass</code>
305 objects. Once a <code>CtClass</code> object is created, it is
306 recorded in a <code>ClassPool</code> for ever. This is because a
307 compiler may need to access the <code>CtClass</code> object later when
308 it compiles source code that refers to the class represented by that
309 <code>CtClass</code>.
312 For example, suppose that a new method <code>getter()</code> is added
313 to a <code>CtClass</code> object representing <code>Point</code>
314 class. Later, the program attempts to compile source code including a
315 method call to <code>getter()</code> in <code>Point</code> and use the
316 compiled code as the body of a method, which will be added to another
317 class <code>Line</code>. If the <code>CtClass</code> object representing
318 <code>Point</code> is lost, the compiler cannot compile the method call
319 to <code>getter()</code>. Note that the original class definition does
320 not include <code>getter()</code>. Therefore, to correctly compile
321 such a method call, the <code>ClassPool</code>
322 must contain all the instances of <code>CtClass</code> all the time of
330 This specification of <code>ClassPool</code> may cause huge memory
331 consumption if the number of <code>CtClass</code> objects becomes
335 can explicitly remove an unnecessary <code>CtClass</code> object from
336 the <code>ClassPool</code>. If you call <code>detach()</code> on a
337 <code>CtClass</code> object, then that <code>CtClass</code> object is
338 removed from the <code>ClassPool</code>. For example,
347 <code>CtClass</code> object after <code>detach()</code> is called.
348 However, you can call <code>get()</code> on <code>ClassPool</code>
349 to make a new instance of <code>CtClass</code> representing
350 the same class. If you call <code>get()</code>, the <code>ClassPool</code>
351 reads a class file again and newly creates a <code>CtClass</code>
352 object, which is returned by <code>get()</code>.
355 Another idea is to occasionally replace a <code>ClassPool</code> with
356 a new one and discard the old one. If an old <code>ClassPool</code>
357 is garbage collected, the <code>CtClass</code> objects included in
358 that <code>ClassPool</code> are also garbage collected.
359 To create a new instance of <code>ClassPool</code>, execute the following
360 code snippet:
367 <p>This creates a <code>ClassPool</code> object that behaves as the
368 default <code>ClassPool</code> returned by
369 <code>ClassPool.getDefault()</code> does.
370 Note that <code>ClassPool.getDefault()</code> is a singleton factory method
371 provided for convenience. It creates a <code>ClassPool</code> object in
373 <code>ClassPool</code> and reuses it.
374 A <code>ClassPool</code> object returned by <code>getDefault()</code>
375 does not have a special role. <code>getDefault()</code> is a convenience
378 <p>Note that <code>new ClassPool(true)</code> is a convenient constructor,
379 which constructs a <code>ClassPool</code> object and appends the system
381 equivalent to the following code:
392 creating multiple instances of <code>ClassPool</code> might be necessary;
393 an instance of <code>ClassPool</code> should be created
395 The program should create a <code>ClassPool</code> object by not calling
396 <code>getDefault()</code> but a constructor of <code>ClassPool</code>.
399 Multiple <code>ClassPool</code> objects can be cascaded like
400 <code>java.lang.ClassLoader</code>. For example,
409 If <code>child.get()</code> is called, the child <code>ClassPool</code>
410 first delegates to the parent <code>ClassPool</code>. If the parent
411 <code>ClassPool</code> fails to find a class file, then the child
412 <code>ClassPool</code> attempts to find a class file
413 under the <code>./classes</code> directory.
416 If <code>child.childFirstLookup</code> is true, the child
417 <code>ClassPool</code> attempts to find a class file before delegating
418 to the parent <code>ClassPool</code>. For example,
438 <p>This program first obtains the <code>CtClass</code> object for
439 class <code>Point</code>. Then it calls <code>setName()</code> to
440 give a new name <code>Pair</code> to that <code>CtClass</code> object.
442 definition represented by that <code>CtClass</code> object are changed
443 from <code>Point</code> to <code>Pair</code>. The other part of the
446 <p>Note that <code>setName()</code> in <code>CtClass</code> changes a
447 record in the <code>ClassPool</code> object. From the implementation
448 viewpoint, a <code>ClassPool</code> object is a hash table of
449 <code>CtClass</code> objects. <code>setName()</code> changes
450 the key associated to the <code>CtClass</code> object in the hash
454 <p>Therefore, if <code>get("Point")</code> is later called on the
455 <code>ClassPool</code> object again, then it never returns the
456 <code>CtClass</code> object that the variable <code>cc</code> refers to.
457 The <code>ClassPool</code> object reads
459 <code>Point.class</code> again and it constructs a new <code>CtClass</code>
460 object for class <code>Point</code>.
461 This is because the <code>CtClass</code> object associated with the name
462 <code>Point</code> does not exist any more.
474 <p><code>cc1</code> and <code>cc2</code> refer to the same instance of
475 <code>CtClass</code> that <code>cc</code> does whereas
476 <code>cc3</code> does not. Note that, after
477 <code>cc.setName("Pair")</code> is executed, the <code>CtClass</code>
478 object that <code>cc</code> and <code>cc1</code> refer to represents
479 the <code>Pair</code> class.
481 <p>The <code>ClassPool</code> object is used to maintain one-to-one
482 mapping between classes and <code>CtClass</code> objects. Javassist
483 never allows two distinct <code>CtClass</code> objects to represent
484 the same class unless two independent <code>ClassPool</code> are created.
489 <code>ClassPool</code>, which is returned by
490 <code>ClassPool.getDefault()</code>, execute the following code
491 snippet (this code was already <a href="#avoidmemory">shown above</a>):
497 <p>If you have two <code>ClassPool</code> objects, then you can
498 obtain, from each <code>ClassPool</code>, a distinct
499 <code>CtClass</code> object representing the same class file. You can
500 differently modify these <code>CtClass</code> objects to generate
505 <p>Once a <code>CtClass</code> object is converted into a class file
506 by <code>writeFile()</code> or <code>toBytecode()</code>, Javassist
507 rejects further modifications of that <code>CtClass</code> object.
508 Hence, after the <code>CtClass</code> object representing <code>Point</code>
509 class is converted into a class file, you cannot define <code>Pair</code>
510 class as a copy of <code>Point</code> since executing <code>setName()</code>
511 on <code>Point</code> is rejected.
512 The following code snippet is wrong:
521 <p>To avoid this restriction, you should call <code>getAndRename()</code>
522 in <code>ClassPool</code>. For example,
531 <p>If <code>getAndRename()</code> is called, the <code>ClassPool</code>
532 first reads <code>Point.class</code> for creating a new <code>CtClass</code>
533 object representing <code>Point</code> class. However, it renames that
534 <code>CtClass</code> object from <code>Point</code> to <code>Pair</code> before
535 it records that <code>CtClass</code> object in a hash table.
536 Thus <code>getAndRename()</code>
537 can be executed after <code>writeFile()</code> or <code>toBytecode()</code>
538 is called on the the <code>CtClass</code> object representing <code>Point</code>
549 <ul><li>1. Get a <code>CtClass</code> object by calling
550 <code>ClassPool.get()</code>,
552 <li>3. Call <code>writeFile()</code> or <code>toBytecode()</code>
553 on that <code>CtClass</code> object to obtain a modified class file.
567 <h3>3.1 The <code>toClass</code> method in <code>CtClass</code></h3>
570 <p>The <code>CtClass</code> provides a convenience method
571 <code>toClass()</code>, which requests the context class loader for
572 the current thread to load the class represented by the <code>CtClass</code>
574 otherwise, a <code>SecurityException</code> may be thrown.
576 <p>The following program shows how to use <code>toClass()</code>:
598 <p><code>Test.main()</code> inserts a call to <code>println()</code>
599 in the method body of <code>say()</code> in <code>Hello</code>. Then
600 it constructs an instance of the modified <code>Hello</code> class
601 and calls <code>say()</code> on that instance.
604 <code>Hello</code> class is never loaded before <code>toClass()</code>
606 <code>Hello</code> class before <code>toClass()</code> requests to
607 load the modified <code>Hello</code> class. Hence loading the
608 modified <code>Hello</code> class would be failed
609 (<code>LinkageError</code> is thrown). For example, if
610 <code>main()</code> in <code>Test</code> is something like this:
621 <p>then the original <code>Hello</code> class is loaded at the first
622 line of <code>main</code> and the call to <code>toClass()</code>
624 versions of the <code>Hello</code> class at the same time.
628 <code>toClass()</code> might be inappropriate. In this case, you
629 would see an unexpected <code>ClassCastException</code>. To avoid
631 to <code>toClass()</code>. For example, if <code>bean</code> is your
632 session bean object, then the following code:
638 <p>would work. You should give <code>toClass()</code> the class loader
640 the <code>bean</code> object).
642 <p><code>toClass()</code> is provided for convenience. If you need
673 and throws a <em><code>ClassCastException</code></em>.
675 <p>For example, the following code snippet throws an exception:
685 The <code>Box</code> class is loaded by two class loaders.
686 Suppose that a class loader CL loads a class including this code snippet.
687 Since this code snippet refers to <code>MyClassLoader</code>,
688 <code>Class</code>, <code>Object</code>, and <code>Box</code>,
690 Hence the type of the variable <code>b</code> is the <code>Box</code>
692 On the other hand, <code>myLoader</code> also loads the <code>Box</code>
693 class. The object <code>obj</code> is an instance of
694 the <code>Box</code> class loaded by <code>myLoader</code>.
696 <code>ClassCastException</code> since the class of <code>obj</code> is
697 a different verison of the <code>Box</code> class from one used as the
698 type of the variable <code>b</code>.
742 <p>Suppose that a class <code>Window</code> is loaded by a class loader L.
743 Both the initiator and the real loader of <code>Window</code> are L.
744 Since the definition of <code>Window</code> refers to <code>Box</code>,
745 the JVM will request L to load <code>Box</code>.
747 The initiator of <code>Box</code> is L but the real loader is PL.
748 In this case, the initiator of <code>Point</code> is not L but PL
749 since it is the same as the real loader of <code>Box</code>.
750 Thus L is never requested to load <code>Point</code>.
775 <p>Now, the definition of <code>Window</code> also refers to
776 <code>Point</code>. In this case, the class loader L must
777 also delegate to PL if it is requested to load <code>Point</code>.
783 If L does not delegate to PL when <code>Point</code>
784 is loaded, <code>widthIs()</code> would throw a ClassCastException.
785 Since the real loader of <code>Box</code> is PL,
786 <code>Point</code> referred to in <code>Box</code> is also loaded by PL.
787 Therefore, the resulting value of <code>getSize()</code>
788 is an instance of <code>Point</code> loaded by PL
789 whereas the type of the variable <code>p</code> in <code>widthIs()</code>
790 is <code>Point</code> loaded by L.
802 then the programmer of <code>Window</code> could break the encapsulation
803 of <code>Point</code> objects.
804 For example, the field <code>x</code>
805 is private in <code>Point</code> loaded by PL.
806 However, the <code>Window</code> class could
807 directly access the value of <code>x</code>
808 if L loads <code>Point</code> with the following definition:
828 <h3>3.3 Using <code>javassist.Loader</code></h3>
831 <code>javassist.Loader</code>. This class loader uses a
832 <code>javassist.ClassPool</code> object for reading a class file.
834 <p>For example, <code>javassist.Loader</code> can be used for loading
856 <p>This program modifies a class <code>test.Rectangle</code>. The
857 superclass of <code>test.Rectangle</code> is set to a
858 <code>test.Point</code> class. Then this program loads the modified
860 <code>test.Rectangle</code> class.
863 the users can add an event listener to a <code>javassist.Loader</code>.
875 <p>The method <code>start()</code> is called when this event listener
876 is added to a <code>javassist.Loader</code> object by
877 <code>addTranslator()</code> in <code>javassist.Loader</code>. The
878 method <code>onLoad()</code> is called before
879 <code>javassist.Loader</code> loads a class. <code>onLoad()</code>
896 <p>Note that <code>onLoad()</code> does not have to call
897 <code>toBytecode()</code> or <code>writeFile()</code> since
898 <code>javassist.Loader</code> calls these methods to obtain a class
901 <p>To run an application class <code>MyApp</code> with a
902 <code>MyTranslator</code> object, write a main class as following:
924 <p>The class <code>MyApp</code> and the other application classes
925 are translated by <code>MyTranslator</code>.
927 <p>Note that <em>application</em> classes like <code>MyApp</code> cannot
928 access the <em>loader</em> classes such as <code>Main2</code>,
929 <code>MyTranslator</code>, and <code>ClassPool</code> because they
931 by <code>javassist.Loader</code> whereas the loader classes such as
932 <code>Main2</code> are by the default Java class loader.
934 <p><code>javassist.Loader</code> searches for classes in a different
935 order from <code>java.lang.ClassLoader</code>.
936 <code>ClassLoader</code> first delegates the loading operations to
940 <code>javassist.Loader</code> attempts
944 <ul><li>the classes are not found by calling <code>get()</code> on
945 a <code>ClassPool</code> object, or
948 <code>delegateLoadingOf()</code>
961 <code>javassist.Loader</code>.
1007 <p>The class <code>MyApp</code> is an application program.
1009 <code>./class</code> directory, which must <em>not</em> be included
1010 in the class search path. Otherwise, <code>MyApp.class</code> would
1012 loader of <code>SampleLoader</code>.
1013 The directory name <code>./class</code> is specified by
1014 <code>insertClassPath()</code> in the constructor.
1015 You can choose a different name instead of <code>./class</code> if you want.
1018 <ul><code>% java SampleLoader</code></ul>
1020 <p>The class loader loads the class <code>MyApp</code>
1021 (<code>./class/MyApp.class</code>) and calls
1022 <code>MyApp.main()</code> with the command line parameters.
1027 <code>MyApp</code> class in a name space separated from the name space
1028 that the class <code>SampleLoader</code> belongs to because the two
1031 <code>MyApp</code> class cannot directly access the class
1032 <code>SampleLoader</code>.
1038 <p>The system classes like <code>java.lang.String</code> cannot be
1040 Therefore, <code>SampleLoader</code> or <code>javassist.Loader</code>
1045 adds a new field <code>hiddenValue</code> to <code>java.lang.String</code>:
1054 <p>This program produces a file <code>"./java/lang/String.class"</code>.
1056 <p>To run your program <code>MyApp</code>
1057 with this modified <code>String</code> class, do as follows:
1063 <p>Suppose that the definition of <code>MyApp</code> is as follows:
1071 <p>If the modified <code>String</code> class is correctly loaded,
1072 <code>MyApp</code> prints <code>hiddenValue</code>.
1075 overriding a system class in <code>rt.jar</code> should not be
1077 binary code license.</i>
1095 <code>javassist.tools.HotSwapper</code>.