jdk/src/java.instrument/share/classes/java/lang/instrument/ClassFileTransformer.java
changeset 39739 5d058d6bd245
parent 36511 9d0388c6b336
child 43712 5dfd0950317c
equal deleted inserted replaced
39738:15fea8b870eb 39739:5d058d6bd245
    36 
    36 
    37 /**
    37 /**
    38  * A transformer of class files. An agent registers an implementation of this
    38  * A transformer of class files. An agent registers an implementation of this
    39  * interface using the {@link Instrumentation#addTransformer addTransformer}
    39  * interface using the {@link Instrumentation#addTransformer addTransformer}
    40  * method so that the transformer's {@link
    40  * method so that the transformer's {@link
    41  * ClassFileTransformer#transform(Module,String,Class,ProtectionDomain,byte[])
    41  * ClassFileTransformer#transform(Module,ClassLoader,String,Class,ProtectionDomain,byte[])
    42  * transform} method is invoked when classes are loaded,
    42  * transform} method is invoked when classes are loaded,
    43  * {@link Instrumentation#redefineClasses redefined}, or
    43  * {@link Instrumentation#redefineClasses redefined}, or
    44  * {@link Instrumentation#retransformClasses retransformed}. The implementation
    44  * {@link Instrumentation#retransformClasses retransformed}. The implementation
    45  * should override one of the {@code transform} methods defined here.
    45  * should override one of the {@code transform} methods defined here.
    46  * Transformers are invoked before the class is defined by the Java virtual
    46  * Transformers are invoked before the class is defined by the Java virtual
   168 public interface ClassFileTransformer {
   168 public interface ClassFileTransformer {
   169 
   169 
   170     /**
   170     /**
   171      * Transforms the given class file and returns a new replacement class file.
   171      * Transforms the given class file and returns a new replacement class file.
   172      * This method is invoked when the {@link Module Module} bearing {@link
   172      * This method is invoked when the {@link Module Module} bearing {@link
   173      * ClassFileTransformer#transform(Module,String,Class,ProtectionDomain,byte[])
   173      * ClassFileTransformer#transform(Module,ClassLoader,String,Class,ProtectionDomain,byte[])
   174      * transform} is not overridden.
   174      * transform} is not overridden.
   175      *
   175      *
   176      * @implSpec The default implementation returns null.
   176      * @implSpec The default implementation returns null.
   177      *
   177      *
   178      * @param loader                the defining loader of the class to be transformed,
   178      * @param loader                the defining loader of the class to be transformed,
   179      *                              may be <code>null</code> if the bootstrap loader
   179      *                              may be {@code null} if the bootstrap loader
   180      * @param className             the name of the class in the internal form of fully
   180      * @param className             the name of the class in the internal form of fully
   181      *                              qualified class and interface names as defined in
   181      *                              qualified class and interface names as defined in
   182      *                              <i>The Java Virtual Machine Specification</i>.
   182      *                              <i>The Java Virtual Machine Specification</i>.
   183      *                              For example, <code>"java/util/List"</code>.
   183      *                              For example, <code>"java/util/List"</code>.
   184      * @param classBeingRedefined   if this is triggered by a redefine or retransform,
   184      * @param classBeingRedefined   if this is triggered by a redefine or retransform,
   206     /**
   206     /**
   207      * Transforms the given class file and returns a new replacement class file.
   207      * Transforms the given class file and returns a new replacement class file.
   208      *
   208      *
   209      * @implSpec The default implementation of this method invokes the
   209      * @implSpec The default implementation of this method invokes the
   210      * {@link #transform(ClassLoader,String,Class,ProtectionDomain,byte[]) transform}
   210      * {@link #transform(ClassLoader,String,Class,ProtectionDomain,byte[]) transform}
   211      * method with the {@link Module#getClassLoader() ClassLoader} for the module.
   211      * method.
   212      *
   212      *
   213      * @param module                the module of the class to be transformed
   213      * @param module                the module of the class to be transformed
       
   214      * @param loader                the defining loader of the class to be transformed,
       
   215      *                              may be {@code null} if the bootstrap loader
   214      * @param className             the name of the class in the internal form of fully
   216      * @param className             the name of the class in the internal form of fully
   215      *                              qualified class and interface names as defined in
   217      *                              qualified class and interface names as defined in
   216      *                              <i>The Java Virtual Machine Specification</i>.
   218      *                              <i>The Java Virtual Machine Specification</i>.
   217      *                              For example, <code>"java/util/List"</code>.
   219      *                              For example, <code>"java/util/List"</code>.
   218      * @param classBeingRedefined   if this is triggered by a redefine or retransform,
   220      * @param classBeingRedefined   if this is triggered by a redefine or retransform,
   228      *
   230      *
   229      * @since  9
   231      * @since  9
   230      */
   232      */
   231     default byte[]
   233     default byte[]
   232     transform(  Module              module,
   234     transform(  Module              module,
       
   235                 ClassLoader         loader,
   233                 String              className,
   236                 String              className,
   234                 Class<?>            classBeingRedefined,
   237                 Class<?>            classBeingRedefined,
   235                 ProtectionDomain    protectionDomain,
   238                 ProtectionDomain    protectionDomain,
   236                 byte[]              classfileBuffer)
   239                 byte[]              classfileBuffer)
   237         throws IllegalClassFormatException {
   240         throws IllegalClassFormatException {
   238 
       
   239         PrivilegedAction<ClassLoader> pa = module::getClassLoader;
       
   240         ClassLoader loader = AccessController.doPrivileged(pa);
       
   241 
   241 
   242         // invoke the legacy transform method
   242         // invoke the legacy transform method
   243         return transform(loader,
   243         return transform(loader,
   244                          className,
   244                          className,
   245                          classBeingRedefined,
   245                          classBeingRedefined,