2
|
1 |
/*
|
|
2 |
* Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package java.lang.instrument;
|
|
27 |
|
|
28 |
import java.security.ProtectionDomain;
|
|
29 |
|
|
30 |
/*
|
|
31 |
* Copyright 2003 Wily Technology, Inc.
|
|
32 |
*/
|
|
33 |
|
|
34 |
/**
|
|
35 |
* An agent provides an implementation of this interface in order
|
|
36 |
* to transform class files.
|
|
37 |
* The transformation occurs before the class is defined by the JVM.
|
|
38 |
* <P>
|
|
39 |
* Note the term <i>class file</i> is used as defined in the chapter
|
|
40 |
* <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#80959">The class File Format</a>
|
|
41 |
* of <i>The Java Virtual Machine Specification</i>, to mean a sequence
|
|
42 |
* of bytes in class file format, whether or not they reside in a file.
|
|
43 |
*
|
|
44 |
* @see java.lang.instrument.Instrumentation
|
|
45 |
* @see java.lang.instrument.Instrumentation#addTransformer
|
|
46 |
* @see java.lang.instrument.Instrumentation#removeTransformer
|
|
47 |
* @since 1.5
|
|
48 |
*/
|
|
49 |
|
|
50 |
public interface ClassFileTransformer {
|
|
51 |
/**
|
|
52 |
* The implementation of this method may transform the supplied class file and
|
|
53 |
* return a new replacement class file.
|
|
54 |
*
|
|
55 |
* <P>
|
|
56 |
* There are two kinds of transformers, determined by the <code>canRetransform</code>
|
|
57 |
* parameter of
|
|
58 |
* {@link java.lang.instrument.Instrumentation#addTransformer(ClassFileTransformer,boolean)}:
|
|
59 |
* <ul>
|
|
60 |
* <li><i>retransformation capable</i> transformers that were added with
|
|
61 |
* <code>canRetransform</code> as true
|
|
62 |
* </li>
|
|
63 |
* <li><i>retransformation incapable</i> transformers that were added with
|
|
64 |
* <code>canRetransform</code> as false or where added with
|
|
65 |
* {@link java.lang.instrument.Instrumentation#addTransformer(ClassFileTransformer)}
|
|
66 |
* </li>
|
|
67 |
* </ul>
|
|
68 |
*
|
|
69 |
* <P>
|
|
70 |
* Once a transformer has been registered with
|
|
71 |
* {@link java.lang.instrument.Instrumentation#addTransformer(ClassFileTransformer,boolean)
|
|
72 |
* addTransformer},
|
|
73 |
* the transformer will be called for every new class definition and every class redefinition.
|
|
74 |
* Retransformation capable transformers will also be called on every class retransformation.
|
|
75 |
* The request for a new class definition is made with
|
|
76 |
* {@link java.lang.ClassLoader#defineClass ClassLoader.defineClass}
|
|
77 |
* or its native equivalents.
|
|
78 |
* The request for a class redefinition is made with
|
|
79 |
* {@link java.lang.instrument.Instrumentation#redefineClasses Instrumentation.redefineClasses}
|
|
80 |
* or its native equivalents.
|
|
81 |
* The request for a class retransformation is made with
|
|
82 |
* {@link java.lang.instrument.Instrumentation#retransformClasses Instrumentation.retransformClasses}
|
|
83 |
* or its native equivalents.
|
|
84 |
* The transformer is called during the processing of the request, before the class file bytes
|
|
85 |
* have been verified or applied.
|
|
86 |
* When there are multiple transformers, transformations are composed by chaining the
|
|
87 |
* <code>transform</code> calls.
|
|
88 |
* That is, the byte array returned by one call to <code>transform</code> becomes the input
|
|
89 |
* (via the <code>classfileBuffer</code> parameter) to the next call.
|
|
90 |
*
|
|
91 |
* <P>
|
|
92 |
* Transformations are applied in the following order:
|
|
93 |
* <ul>
|
|
94 |
* <li>Retransformation incapable transformers
|
|
95 |
* </li>
|
|
96 |
* <li>Retransformation incapable native transformers
|
|
97 |
* </li>
|
|
98 |
* <li>Retransformation capable transformers
|
|
99 |
* </li>
|
|
100 |
* <li>Retransformation capable native transformers
|
|
101 |
* </li>
|
|
102 |
* </ul>
|
|
103 |
*
|
|
104 |
* <P>
|
|
105 |
* For retransformations, the retransformation incapable transformers are not
|
|
106 |
* called, instead the result of the previous transformation is reused.
|
|
107 |
* In all other cases, this method is called.
|
|
108 |
* Within each of these groupings, transformers are called in the order registered.
|
|
109 |
* Native transformers are provided by the <code>ClassFileLoadHook</code> event
|
|
110 |
* in the Java Virtual Machine Tool Interface).
|
|
111 |
*
|
|
112 |
* <P>
|
|
113 |
* The input (via the <code>classfileBuffer</code> parameter) to the first
|
|
114 |
* transformer is:
|
|
115 |
* <ul>
|
|
116 |
* <li>for new class definition,
|
|
117 |
* the bytes passed to <code>ClassLoader.defineClass</code>
|
|
118 |
* </li>
|
|
119 |
* <li>for class redefinition,
|
|
120 |
* <code>definitions.getDefinitionClassFile()</code> where
|
|
121 |
* <code>definitions</code> is the parameter to
|
|
122 |
* {@link java.lang.instrument.Instrumentation#redefineClasses
|
|
123 |
* Instrumentation.redefineClasses}
|
|
124 |
* </li>
|
|
125 |
* <li>for class retransformation,
|
|
126 |
* the bytes passed to the new class definition or, if redefined,
|
|
127 |
* the last redefinition, with all transformations made by retransformation
|
|
128 |
* incapable transformers reapplied automatically and unaltered;
|
|
129 |
* for details see
|
|
130 |
* {@link java.lang.instrument.Instrumentation#retransformClasses
|
|
131 |
* Instrumentation.retransformClasses}
|
|
132 |
* </li>
|
|
133 |
* </ul>
|
|
134 |
*
|
|
135 |
* <P>
|
|
136 |
* If the implementing method determines that no transformations are needed,
|
|
137 |
* it should return <code>null</code>.
|
|
138 |
* Otherwise, it should create a new <code>byte[]</code> array,
|
|
139 |
* copy the input <code>classfileBuffer</code> into it,
|
|
140 |
* along with all desired transformations, and return the new array.
|
|
141 |
* The input <code>classfileBuffer</code> must not be modified.
|
|
142 |
*
|
|
143 |
* <P>
|
|
144 |
* In the retransform and redefine cases,
|
|
145 |
* the transformer must support the redefinition semantics:
|
|
146 |
* if a class that the transformer changed during initial definition is later
|
|
147 |
* retransformed or redefined, the
|
|
148 |
* transformer must insure that the second class output class file is a legal
|
|
149 |
* redefinition of the first output class file.
|
|
150 |
*
|
|
151 |
* <P>
|
|
152 |
* If the transformer throws an exception (which it doesn't catch),
|
|
153 |
* subsequent transformers will still be called and the load, redefine
|
|
154 |
* or retransform will still be attempted.
|
|
155 |
* Thus, throwing an exception has the same effect as returning <code>null</code>.
|
|
156 |
* To prevent unexpected behavior when unchecked exceptions are generated
|
|
157 |
* in transformer code, a transformer can catch <code>Throwable</code>.
|
|
158 |
* If the transformer believes the <code>classFileBuffer</code> does not
|
|
159 |
* represent a validly formatted class file, it should throw
|
|
160 |
* an <code>IllegalClassFormatException</code>;
|
|
161 |
* while this has the same effect as returning null. it facilitates the
|
|
162 |
* logging or debugging of format corruptions.
|
|
163 |
*
|
|
164 |
* @param loader the defining loader of the class to be transformed,
|
|
165 |
* may be <code>null</code> if the bootstrap loader
|
|
166 |
* @param className the name of the class in the internal form of fully
|
|
167 |
* qualified class and interface names as defined in
|
|
168 |
* <i>The Java Virtual Machine Specification</i>.
|
|
169 |
* For example, <code>"java/util/List"</code>.
|
|
170 |
* @param classBeingRedefined if this is triggered by a redefine or retransform,
|
|
171 |
* the class being redefined or retransformed;
|
|
172 |
* if this is a class load, <code>null</code>
|
|
173 |
* @param protectionDomain the protection domain of the class being defined or redefined
|
|
174 |
* @param classfileBuffer the input byte buffer in class file format - must not be modified
|
|
175 |
*
|
|
176 |
* @throws IllegalClassFormatException if the input does not represent a well-formed class file
|
|
177 |
* @return a well-formed class file buffer (the result of the transform),
|
|
178 |
or <code>null</code> if no transform is performed.
|
|
179 |
* @see Instrumentation#redefineClasses
|
|
180 |
*/
|
|
181 |
byte[]
|
|
182 |
transform( ClassLoader loader,
|
|
183 |
String className,
|
|
184 |
Class<?> classBeingRedefined,
|
|
185 |
ProtectionDomain protectionDomain,
|
|
186 |
byte[] classfileBuffer)
|
|
187 |
throws IllegalClassFormatException;
|
|
188 |
}
|