001 /*
002 * Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.lang.instrument;
027
028 import java.security.ProtectionDomain;
029
030 /*
031 * Copyright 2003 Wily Technology, Inc.
032 */
033
034 /**
035 * An agent provides an implementation of this interface in order
036 * to transform class files.
037 * The transformation occurs before the class is defined by the JVM.
038 * <P>
039 * Note the term <i>class file</i> is used as defined in the chapter
040 * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#80959">The class File Format</a>
041 * of <i>The Java Virtual Machine Specification</i>, to mean a sequence
042 * of bytes in class file format, whether or not they reside in a file.
043 *
044 * @see java.lang.instrument.Instrumentation
045 * @see java.lang.instrument.Instrumentation#addTransformer
046 * @see java.lang.instrument.Instrumentation#removeTransformer
047 * @since 1.5
048 */
049
050 public interface ClassFileTransformer {
051 /**
052 * The implementation of this method may transform the supplied class file and
053 * return a new replacement class file.
054 *
055 * <P>
056 * There are two kinds of transformers, determined by the <code>canRetransform</code>
057 * parameter of
058 * {@link java.lang.instrument.Instrumentation#addTransformer(ClassFileTransformer,boolean)}:
059 * <ul>
060 * <li><i>retransformation capable</i> transformers that were added with
061 * <code>canRetransform</code> as true
062 * </li>
063 * <li><i>retransformation incapable</i> transformers that were added with
064 * <code>canRetransform</code> as false or where added with
065 * {@link java.lang.instrument.Instrumentation#addTransformer(ClassFileTransformer)}
066 * </li>
067 * </ul>
068 *
069 * <P>
070 * Once a transformer has been registered with
071 * {@link java.lang.instrument.Instrumentation#addTransformer(ClassFileTransformer,boolean)
072 * addTransformer},
073 * the transformer will be called for every new class definition and every class redefinition.
074 * Retransformation capable transformers will also be called on every class retransformation.
075 * The request for a new class definition is made with
076 * {@link java.lang.ClassLoader#defineClass ClassLoader.defineClass}
077 * or its native equivalents.
078 * The request for a class redefinition is made with
079 * {@link java.lang.instrument.Instrumentation#redefineClasses Instrumentation.redefineClasses}
080 * or its native equivalents.
081 * The request for a class retransformation is made with
082 * {@link java.lang.instrument.Instrumentation#retransformClasses Instrumentation.retransformClasses}
083 * or its native equivalents.
084 * The transformer is called during the processing of the request, before the class file bytes
085 * have been verified or applied.
086 * When there are multiple transformers, transformations are composed by chaining the
087 * <code>transform</code> calls.
088 * That is, the byte array returned by one call to <code>transform</code> becomes the input
089 * (via the <code>classfileBuffer</code> parameter) to the next call.
090 *
091 * <P>
092 * Transformations are applied in the following order:
093 * <ul>
094 * <li>Retransformation incapable transformers
095 * </li>
096 * <li>Retransformation incapable native transformers
097 * </li>
098 * <li>Retransformation capable transformers
099 * </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[] transform(ClassLoader loader, String className,
182 Class<?> classBeingRedefined,
183 ProtectionDomain protectionDomain, byte[] classfileBuffer)
184 throws IllegalClassFormatException;
185 }
|