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