• Home
  • Raw
  • Download

Lines Matching full:code

40 <p>The class <code>Javassist.CtClass</code> is an absatract
41 representation of a class file. A <code>CtClass</code> (compile-time
52 <p>This program first obtains a <code>ClassPool</code> object, which
54 <code>ClassPool</code> object is a container of <code>CtClass</code>
56 constructing a <code>CtClass</code> object and records the
60 from a <code>ClassPool</code> object
61 a reference to a <code>CtClass</code> object representing that class.
62 <code>get()</code> in <code>ClassPool</code> is used for this purpose.
64 <code>CtClass</code> object representing a class
65 <code>test.Rectangle</code> is obtained from the
66 <code>ClassPool</code> object and it is assigned to a variable
67 <code>cc</code>.
68 The <code>ClassPool</code> object returned by <code>getDfault()</code>
71 <p>From the implementation viewpoint, <code>ClassPool</code> is a hash
72 table of <code>CtClass</code> objects, which uses the class names as
73 keys. <code>get()</code> in <code>ClassPool</code> searches this hash
74 table to find a <code>CtClass</code> object associated with the
75 specified key. If such a <code>CtClass</code> object is not found,
76 <code>get()</code> reads a class file to construct a new
77 <code>CtClass</code> object, which is recorded in the hash table and
78 then returned as the resulting value of <code>get()</code>.
80 <p>The <code>CtClass</code> object obtained from a <code>ClassPool</code>
83 a <code>CtClass</code></a> will be presented later).
85 <code>test.Rectangle</code> is changed into a class
86 <code>test.Point</code>. This change is reflected on the original
87 class file when <code>writeFile()</code> in <code>CtClass()</code> is
90 <p><code>writeFile()</code> translates the <code>CtClass</code> object
93 modified bytecode. To obtain the bytecode, call <code>toBytecode()</code>:
99 <p>You can directly load the <code>CtClass</code> as well:
105 <p><code>toClass()</code> requests the context class loader for the current
106 thread to load the class file represented by the <code>CtClass</code>. It
107 returns a <code>java.lang.Class</code> object representing the loaded class.
113 <p>To define a new class from scratch, <code>makeClass()</code>
114 must be called on a <code>ClassPool</code>.
121 <p>This program defines a class <code>Point</code>
123 Member methods of <code>Point</code> can be created with
124 factory methods declared in <code>CtNewMethod</code> and
125 appended to <code>Point</code> with <code>addMethod()</code>
126 in <code>CtClass</code>.
128 <p><code>makeClass()</code> cannot create a new interface;
129 <code>makeInterface()</code> in <code>ClassPool</code> can do.
131 <code>abstractMethod()</code> in <code>CtNewMethod</code>.
137 <p>If a <code>CtClass</code> object is converted into a class file by
138 <code>writeFile()</code>, <code>toClass()</code>, or
139 <code>toBytecode()</code>, Javassist freezes that <code>CtClass</code>
140 object. Further modifications of that <code>CtClass</code> object are
145 <p>A frozen <code>CtClass</code> can be defrost so that
156 <p>After <code>defrost()</code> is called, the <code>CtClass</code>
159 <p>If <code>ClassPool.doPruning</code> is set to <code>true</code>,
161 in a <code>CtClass</code> object
165 (<code>attribute_info</code> structures) in that object.
166 For example, <code>Code_attribute</code> structures (method bodies)
169 <code>CtClass</code> object is pruned, the bytecode of a method is not
171 The pruned <code>CtClass</code> object cannot be defrost again.
172 The default value of <code>ClassPool.doPruning</code> is <code>false</code>.
174 <p>To disallow pruning a particular <code>CtClass</code>,
175 <code>stopPruning()</code> must be called on that object in advance:
185 <p>The <code>CtClass</code> object <code>cc</code> is not pruned.
186 Thus it can be defrost after <code>writeFile()</code> is called.
191 <code>debugWriteFile()</code> is a convenient method
200 <p>The default <code>ClassPool</code> returned
201 by a static method <code>ClassPool.getDefault()</code>
204 the <code>ClassPool</code> object may not be able to find user classes</em>
207 registered to the <code>ClassPool</code>. Suppose that <code>pool</code>
208 refers to a <code>ClassPool</code> object:
216 the class of the object that <code>this</code> refers to.
217 You can use any <code>Class</code> object as an argument instead of
218 <code>this.getClass()</code>. The class path used for loading the
219 class represented by that <code>Class</code> object is registered.
223 For example, the following code adds a directory
224 <code>/usr/local/javalib</code>
243 package <code>org.javassist</code>. For example, to load a class
244 <code>org.javassist.test.Main</code>, its class file will be obtained from:
250 to a <code>ClassPool</code> object
251 and construct a <code>CtClass</code> object from that array. To do this,
252 use <code>ByteArrayClassPath</code>. For example,
262 <p>The obtained <code>CtClass</code> object represents
263 a class defined by the class file specified by <code>b</code>.
264 The <code>ClassPool</code> reads a class file from the given
265 <code>ByteArrayClassPath</code> if <code>get()</code> is called
266 and the class name given to <code>get()</code> is equal to
267 one specified by <code>name</code>.
270 can use <code>makeClass()</code> in <code>ClassPool</code>:
278 <p><code>makeClass()</code> returns the <code>CtClass</code> object
280 <code>makeClass()</code> for eagerly feeding class files to
281 the <code>ClassPool</code> object. This might improve performance
283 a <code>ClassPool</code> object reads a class file on demand,
285 <code>makeClass()</code> can be used for optimizing this search.
286 The <code>CtClass</code> constructed by <code>makeClass()</code>
287 is kept in the <code>ClassPool</code> object and the class file is never
291 class implementing <code>ClassPath</code> interface and give an
292 instance of that class to <code>insertClassPath()</code> in
293 <code>ClassPool</code>. This allows a non-standard resource to be
302 A <code>ClassPool</code> object is a container of <code>CtClass</code>
303 objects. Once a <code>CtClass</code> object is created, it is
304 recorded in a <code>ClassPool</code> for ever. This is because a
305 compiler may need to access the <code>CtClass</code> object later when
306 it compiles source code that refers to the class represented by that
307 <code>CtClass</code>.
310 For example, suppose that a new method <code>getter()</code> is added
311 to a <code>CtClass</code> object representing <code>Point</code>
312 class. Later, the program attempts to compile source code including a
313 method call to <code>getter()</code> in <code>Point</code> and use the
314 compiled code as the body of a method, which will be added to another
315 class <code>Line</code>. If the <code>CtClass</code> object representing
316 <code>Point</code> is lost, the compiler cannot compile the method call
317 to <code>getter()</code>. Note that the original class definition does
318 not include <code>getter()</code>. Therefore, to correctly compile
319 such a method call, the <code>ClassPool</code>
320 must contain all the instances of <code>CtClass</code> all the time of
328 This specification of <code>ClassPool</code> may cause huge memory
329 consumption if the number of <code>CtClass</code> objects becomes
333 can explicitly remove an unnecessary <code>CtClass</code> object from
334 the <code>ClassPool</code>. If you call <code>detach()</code> on a
335 <code>CtClass</code> object, then that <code>CtClass</code> object is
336 removed from the <code>ClassPool</code>. For example,
345 <code>CtClass</code> object after <code>detach()</code> is called.
346 However, you can call <code>get()</code> on <code>ClassPool</code>
347 to make a new instance of <code>CtClass</code> representing
348 the same class. If you call <code>get()</code>, the <code>ClassPool</code>
349 reads a class file again and newly creates a <code>CtClass</code>
350 object, which is returned by <code>get()</code>.
353 Another idea is to occasionally replace a <code>ClassPool</code> with
354 a new one and discard the old one. If an old <code>ClassPool</code>
355 is garbage collected, the <code>CtClass</code> objects included in
356 that <code>ClassPool</code> are also garbage collected.
357 To create a new instance of <code>ClassPool</code>, execute the following
358 code snippet:
365 <p>This creates a <code>ClassPool</code> object that behaves as the
366 default <code>ClassPool</code> returned by
367 <code>ClassPool.getDefault()</code> does.
368 Note that <code>ClassPool.getDefault()</code> is a singleton factory method
369 provided for convenience. It creates a <code>ClassPool</code> object in
371 <code>ClassPool</code> and reuses it.
372 A <code>ClassPool</code> object returned by <code>getDefault()</code>
373 does not have a special role. <code>getDefault()</code> is a convenience
376 <p>Note that <code>new ClassPool(true)</code> is a convenient constructor,
377 which constructs a <code>ClassPool</code> object and appends the system
379 equivalent to the following code:
390 creating multiple instances of <code>ClassPool</code> might be necessary;
391 an instance of <code>ClassPool</code> should be created
393 The program should create a <code>ClassPool</code> object by not calling
394 <code>getDefault()</code> but a constructor of <code>ClassPool</code>.
397 Multiple <code>ClassPool</code> objects can be cascaded like
398 <code>java.lang.ClassLoader</code>. For example,
407 If <code>child.get()</code> is called, the child <code>ClassPool</code>
408 first delegates to the parent <code>ClassPool</code>. If the parent
409 <code>ClassPool</code> fails to find a class file, then the child
410 <code>ClassPool</code> attempts to find a class file
411 under the <code>./classes</code> directory.
414 If <code>child.childFirstLookup</code> is true, the child
415 <code>ClassPool</code> attempts to find a class file before delegating
416 to the parent <code>ClassPool</code>. For example,
436 <p>This program first obtains the <code>CtClass</code> object for
437 class <code>Point</code>. Then it calls <code>setName()</code> to
438 give a new name <code>Pair</code> to that <code>CtClass</code> object.
440 definition represented by that <code>CtClass</code> object are changed
441 from <code>Point</code> to <code>Pair</code>. The other part of the
444 <p>Note that <code>setName()</code> in <code>CtClass</code> changes a
445 record in the <code>ClassPool</code> object. From the implementation
446 viewpoint, a <code>ClassPool</code> object is a hash table of
447 <code>CtClass</code> objects. <code>setName()</code> changes
448 the key associated to the <code>CtClass</code> object in the hash
452 <p>Therefore, if <code>get("Point")</code> is later called on the
453 <code>ClassPool</code> object again, then it never returns the
454 <code>CtClass</code> object that the variable <code>cc</code> refers to.
455 The <code>ClassPool</code> object reads
457 <code>Point.class</code> again and it constructs a new <code>CtClass</code>
458 object for class <code>Point</code>.
459 This is because the <code>CtClass</code> object associated with the name
460 <code>Point</code> does not exist any more.
472 <p><code>cc1</code> and <code>cc2</code> refer to the same instance of
473 <code>CtClass</code> that <code>cc</code> does whereas
474 <code>cc3</code> does not. Note that, after
475 <code>cc.setName("Pair")</code> is executed, the <code>CtClass</code>
476 object that <code>cc</code> and <code>cc1</code> refer to represents
477 the <code>Pair</code> class.
479 <p>The <code>ClassPool</code> object is used to maintain one-to-one
480 mapping between classes and <code>CtClass</code> objects. Javassist
481 never allows two distinct <code>CtClass</code> objects to represent
482 the same class unless two independent <code>ClassPool</code> are created.
487 <code>ClassPool</code>, which is returned by
488 <code>ClassPool.getDefault()</code>, execute the following code
489 snippet (this code was already <a href="#avoidmemory">shown above</a>):
495 <p>If you have two <code>ClassPool</code> objects, then you can
496 obtain, from each <code>ClassPool</code>, a distinct
497 <code>CtClass</code> object representing the same class file. You can
498 differently modify these <code>CtClass</code> objects to generate
503 <p>Once a <code>CtClass</code> object is converted into a class file
504 by <code>writeFile()</code> or <code>toBytecode()</code>, Javassist
505 rejects further modifications of that <code>CtClass</code> object.
506 Hence, after the <code>CtClass</code> object representing <code>Point</code>
507 class is converted into a class file, you cannot define <code>Pair</code>
508 class as a copy of <code>Point</code> since executing <code>setName()</code>
509 on <code>Point</code> is rejected.
510 The following code snippet is wrong:
519 <p>To avoid this restriction, you should call <code>getAndRename()</code>
520 in <code>ClassPool</code>. For example,
529 <p>If <code>getAndRename()</code> is called, the <code>ClassPool</code>
530 first reads <code>Point.class</code> for creating a new <code>CtClass</code>
531 object representing <code>Point</code> class. However, it renames that
532 <code>CtClass</code> object from <code>Point</code> to <code>Pair</code> before
533 it records that <code>CtClass</code> object in a hash table.
534 Thus <code>getAndRename()</code>
535 can be executed after <code>writeFile()</code> or <code>toBytecode()</code>
536 is called on the the <code>CtClass</code> object representing <code>Point</code>
547 <ul><li>1. Get a <code>CtClass</code> object by calling
548 <code>ClassPool.get()</code>,
550 <li>3. Call <code>writeFile()</code> or <code>toBytecode()</code>
551 on that <code>CtClass</code> object to obtain a modified class file.
565 <h3>3.1 The <code>toClass</code> method in <code>CtClass</code></h3>
568 <p>The <code>CtClass</code> provides a convenience method
569 <code>toClass()</code>, which requests the context class loader for
570 the current thread to load the class represented by the <code>CtClass</code>
572 otherwise, a <code>SecurityException</code> may be thrown.
574 <p>The following program shows how to use <code>toClass()</code>:
596 <p><code>Test.main()</code> inserts a call to <code>println()</code>
597 in the method body of <code>say()</code> in <code>Hello</code>. Then
598 it constructs an instance of the modified <code>Hello</code> class
599 and calls <code>say()</code> on that instance.
602 <code>Hello</code> class is never loaded before <code>toClass()</code>
604 <code>Hello</code> class before <code>toClass()</code> requests to
605 load the modified <code>Hello</code> class. Hence loading the
606 modified <code>Hello</code> class would be failed
607 (<code>LinkageError</code> is thrown). For example, if
608 <code>main()</code> in <code>Test</code> is something like this:
619 <p>then the original <code>Hello</code> class is loaded at the first
620 line of <code>main</code> and the call to <code>toClass()</code>
622 versions of the <code>Hello</code> class at the same time.
626 <code>toClass()</code> might be inappropriate. In this case, you
627 would see an unexpected <code>ClassCastException</code>. To avoid
629 to <code>toClass()</code>. For example, if <code>bean</code> is your
630 session bean object, then the following code:
636 <p>would work. You should give <code>toClass()</code> the class loader
638 the <code>bean</code> object).
640 <p><code>toClass()</code> is provided for convenience. If you need
671 and throws a <em><code>ClassCastException</code></em>.
673 <p>For example, the following code snippet throws an exception:
683 The <code>Box</code> class is loaded by two class loaders.
684 Suppose that a class loader CL loads a class including this code snippet.
685 Since this code snippet refers to <code>MyClassLoader</code>,
686 <code>Class</code>, <code>Object</code>, and <code>Box</code>,
688 Hence the type of the variable <code>b</code> is the <code>Box</code>
690 On the other hand, <code>myLoader</code> also loads the <code>Box</code>
691 class. The object <code>obj</code> is an instance of
692 the <code>Box</code> class loaded by <code>myLoader</code>.
694 <code>ClassCastException</code> since the class of <code>obj</code> is
695 a different verison of the <code>Box</code> class from one used as the
696 type of the variable <code>b</code>.
740 <p>Suppose that a class <code>Window</code> is loaded by a class loader L.
741 Both the initiator and the real loader of <code>Window</code> are L.
742 Since the definition of <code>Window</code> refers to <code>Box</code>,
743 the JVM will request L to load <code>Box</code>.
745 The initiator of <code>Box</code> is L but the real loader is PL.
746 In this case, the initiator of <code>Point</code> is not L but PL
747 since it is the same as the real loader of <code>Box</code>.
748 Thus L is never requested to load <code>Point</code>.
773 <p>Now, the definition of <code>Window</code> also refers to
774 <code>Point</code>. In this case, the class loader L must
775 also delegate to PL if it is requested to load <code>Point</code>.
781 If L does not delegate to PL when <code>Point</code>
782 is loaded, <code>widthIs()</code> would throw a ClassCastException.
783 Since the real loader of <code>Box</code> is PL,
784 <code>Point</code> referred to in <code>Box</code> is also loaded by PL.
785 Therefore, the resulting value of <code>getSize()</code>
786 is an instance of <code>Point</code> loaded by PL
787 whereas the type of the variable <code>p</code> in <code>widthIs()</code>
788 is <code>Point</code> loaded by L.
800 then the programmer of <code>Window</code> could break the encapsulation
801 of <code>Point</code> objects.
802 For example, the field <code>x</code>
803 is private in <code>Point</code> loaded by PL.
804 However, the <code>Window</code> class could
805 directly access the value of <code>x</code>
806 if L loads <code>Point</code> with the following definition:
826 <h3>3.3 Using <code>javassist.Loader</code></h3>
829 <code>javassist.Loader</code>. This class loader uses a
830 <code>javassist.ClassPool</code> object for reading a class file.
832 <p>For example, <code>javassist.Loader</code> can be used for loading
854 <p>This program modifies a class <code>test.Rectangle</code>. The
855 superclass of <code>test.Rectangle</code> is set to a
856 <code>test.Point</code> class. Then this program loads the modified
858 <code>test.Rectangle</code> class.
861 the users can add an event listener to a <code>javassist.Loader</code>.
873 <p>The method <code>start()</code> is called when this event listener
874 is added to a <code>javassist.Loader</code> object by
875 <code>addTranslator()</code> in <code>javassist.Loader</code>. The
876 method <code>onLoad()</code> is called before
877 <code>javassist.Loader</code> loads a class. <code>onLoad()</code>
894 <p>Note that <code>onLoad()</code> does not have to call
895 <code>toBytecode()</code> or <code>writeFile()</code> since
896 <code>javassist.Loader</code> calls these methods to obtain a class
899 <p>To run an application class <code>MyApp</code> with a
900 <code>MyTranslator</code> object, write a main class as following:
922 <p>The class <code>MyApp</code> and the other application classes
923 are translated by <code>MyTranslator</code>.
925 <p>Note that <em>application</em> classes like <code>MyApp</code> cannot
926 access the <em>loader</em> classes such as <code>Main2</code>,
927 <code>MyTranslator</code>, and <code>ClassPool</code> because they
929 by <code>javassist.Loader</code> whereas the loader classes such as
930 <code>Main2</code> are by the default Java class loader.
932 <p><code>javassist.Loader</code> searches for classes in a different
933 order from <code>java.lang.ClassLoader</code>.
934 <code>ClassLoader</code> first delegates the loading operations to
938 <code>javassist.Loader</code> attempts
942 <ul><li>the classes are not found by calling <code>get()</code> on
943 a <code>ClassPool</code> object, or
946 <code>delegateLoadingOf()</code>
959 <code>javassist.Loader</code>.
1005 <p>The class <code>MyApp</code> is an application program.
1007 <code>./class</code> directory, which must <em>not</em> be included
1008 in the class search path. Otherwise, <code>MyApp.class</code> would
1010 loader of <code>SampleLoader</code>.
1011 The directory name <code>./class</code> is specified by
1012 <code>insertClassPath()</code> in the constructor.
1013 You can choose a different name instead of <code>./class</code> if you want.
1016 <ul><code>% java SampleLoader</code></ul>
1018 <p>The class loader loads the class <code>MyApp</code>
1019 (<code>./class/MyApp.class</code>) and calls
1020 <code>MyApp.main()</code> with the command line parameters.
1025 <code>MyApp</code> class in a name space separated from the name space
1026 that the class <code>SampleLoader</code> belongs to because the two
1029 <code>MyApp</code> class cannot directly access the class
1030 <code>SampleLoader</code>.
1036 <p>The system classes like <code>java.lang.String</code> cannot be
1038 Therefore, <code>SampleLoader</code> or <code>javassist.Loader</code>
1043 adds a new field <code>hiddenValue</code> to <code>java.lang.String</code>:
1050 <p>This program produces a file <code>"./java/lang/String.class"</code>.
1052 <p>To run your program <code>MyApp</code>
1053 with this modified <code>String</code> class, do as follows:
1059 <p>Suppose that the definition of <code>MyApp</code> is as follows:
1067 <p>If the modified <code>String</code> class is correctly loaded,
1068 <code>MyApp</code> prints <code>hiddenValue</code>.
1071 overriding a system class in <code>rt.jar</code> should not be
1073 binary code license.</i>
1091 <code>javassist.tools.HotSwapper</code>.