8159147: Add ClassLoader parameter to new ClassFileTransformer transform method
authorsspitsyn
Sat, 09 Jul 2016 21:41:14 -0700
changeset 39739 5d058d6bd245
parent 39738 15fea8b870eb
child 39740 d49ff02bacc9
8159147: Add ClassLoader parameter to new ClassFileTransformer transform method Summary: Add ClassLoader parameter to new ClassFileTransformer transform method Reviewed-by: alanb, dholmes, dcubed, mchung
jdk/src/java.instrument/share/classes/java/lang/instrument/ClassFileTransformer.java
jdk/src/java.instrument/share/classes/java/lang/instrument/Instrumentation.java
jdk/src/java.instrument/share/classes/sun/instrument/InstrumentationImpl.java
jdk/src/java.instrument/share/classes/sun/instrument/TransformerManager.java
jdk/src/java.instrument/share/native/libinstrument/JPLISAgent.c
jdk/src/java.instrument/share/native/libinstrument/JPLISAgent.h
jdk/test/java/lang/instrument/ATransformerManagementTestCase.java
jdk/test/java/lang/instrument/RetransformAgent.java
jdk/test/java/lang/instrument/SimpleIdentityTransformer.java
--- a/jdk/src/java.instrument/share/classes/java/lang/instrument/ClassFileTransformer.java	Sat Jul 09 17:45:16 2016 -0700
+++ b/jdk/src/java.instrument/share/classes/java/lang/instrument/ClassFileTransformer.java	Sat Jul 09 21:41:14 2016 -0700
@@ -38,7 +38,7 @@
  * A transformer of class files. An agent registers an implementation of this
  * interface using the {@link Instrumentation#addTransformer addTransformer}
  * method so that the transformer's {@link
- * ClassFileTransformer#transform(Module,String,Class,ProtectionDomain,byte[])
+ * ClassFileTransformer#transform(Module,ClassLoader,String,Class,ProtectionDomain,byte[])
  * transform} method is invoked when classes are loaded,
  * {@link Instrumentation#redefineClasses redefined}, or
  * {@link Instrumentation#retransformClasses retransformed}. The implementation
@@ -170,13 +170,13 @@
     /**
      * Transforms the given class file and returns a new replacement class file.
      * This method is invoked when the {@link Module Module} bearing {@link
-     * ClassFileTransformer#transform(Module,String,Class,ProtectionDomain,byte[])
+     * ClassFileTransformer#transform(Module,ClassLoader,String,Class,ProtectionDomain,byte[])
      * transform} is not overridden.
      *
      * @implSpec The default implementation returns null.
      *
      * @param loader                the defining loader of the class to be transformed,
-     *                              may be <code>null</code> if the bootstrap loader
+     *                              may be {@code null} if the bootstrap loader
      * @param className             the name of the class in the internal form of fully
      *                              qualified class and interface names as defined in
      *                              <i>The Java Virtual Machine Specification</i>.
@@ -208,9 +208,11 @@
      *
      * @implSpec The default implementation of this method invokes the
      * {@link #transform(ClassLoader,String,Class,ProtectionDomain,byte[]) transform}
-     * method with the {@link Module#getClassLoader() ClassLoader} for the module.
+     * method.
      *
      * @param module                the module of the class to be transformed
+     * @param loader                the defining loader of the class to be transformed,
+     *                              may be {@code null} if the bootstrap loader
      * @param className             the name of the class in the internal form of fully
      *                              qualified class and interface names as defined in
      *                              <i>The Java Virtual Machine Specification</i>.
@@ -230,15 +232,13 @@
      */
     default byte[]
     transform(  Module              module,
+                ClassLoader         loader,
                 String              className,
                 Class<?>            classBeingRedefined,
                 ProtectionDomain    protectionDomain,
                 byte[]              classfileBuffer)
         throws IllegalClassFormatException {
 
-        PrivilegedAction<ClassLoader> pa = module::getClassLoader;
-        ClassLoader loader = AccessController.doPrivileged(pa);
-
         // invoke the legacy transform method
         return transform(loader,
                          className,
--- a/jdk/src/java.instrument/share/classes/java/lang/instrument/Instrumentation.java	Sat Jul 09 17:45:16 2016 -0700
+++ b/jdk/src/java.instrument/share/classes/java/lang/instrument/Instrumentation.java	Sat Jul 09 21:41:14 2016 -0700
@@ -162,7 +162,7 @@
      *    </li>
      *    <li>for each transformer that was added with <code>canRetransform</code>
      *      false, the bytes returned by
-     *      {@link ClassFileTransformer#transform(Module,String,Class,ProtectionDomain,byte[])
+     *      {@link ClassFileTransformer#transform(Module,ClassLoader,String,Class,ProtectionDomain,byte[])
      *      transform} during the last class load or redefine are
      *      reused as the output of the transformation; note that this is
      *      equivalent to reapplying the previous transformation, unaltered;
@@ -170,7 +170,7 @@
      *    </li>
      *    <li>for each transformer that was added with <code>canRetransform</code>
      *      true, the
-     *      {@link ClassFileTransformer#transform(Module,String,Class,ProtectionDomain,byte[])
+     *      {@link ClassFileTransformer#transform(Module,ClassLoader,String,Class,ProtectionDomain,byte[])
      *      transform} method is called in these transformers
      *    </li>
      *    <li>the transformed class file bytes are installed as the new
--- a/jdk/src/java.instrument/share/classes/sun/instrument/InstrumentationImpl.java	Sat Jul 09 17:45:16 2016 -0700
+++ b/jdk/src/java.instrument/share/classes/sun/instrument/InstrumentationImpl.java	Sat Jul 09 21:41:14 2016 -0700
@@ -420,8 +420,8 @@
 
     // WARNING: the native code knows the name & signature of this method
     private byte[]
-    transform(  ClassLoader         loader,
-                Module              module,
+    transform(  Module              module,
+                ClassLoader         loader,
                 String              classname,
                 Class<?>            classBeingRedefined,
                 ProtectionDomain    protectionDomain,
@@ -444,6 +444,7 @@
             return null; // no manager, no transform
         } else {
             return mgr.transform(   module,
+                                    loader,
                                     classname,
                                     classBeingRedefined,
                                     protectionDomain,
--- a/jdk/src/java.instrument/share/classes/sun/instrument/TransformerManager.java	Sat Jul 09 17:45:16 2016 -0700
+++ b/jdk/src/java.instrument/share/classes/sun/instrument/TransformerManager.java	Sat Jul 09 21:41:14 2016 -0700
@@ -169,6 +169,7 @@
 
     public byte[]
     transform(  Module              module,
+                ClassLoader         loader,
                 String              classname,
                 Class<?>            classBeingRedefined,
                 ProtectionDomain    protectionDomain,
@@ -187,6 +188,7 @@
 
             try {
                 transformedBytes = transformer.transform(   module,
+                                                            loader,
                                                             classname,
                                                             classBeingRedefined,
                                                             protectionDomain,
--- a/jdk/src/java.instrument/share/native/libinstrument/JPLISAgent.c	Sat Jul 09 17:45:16 2016 -0700
+++ b/jdk/src/java.instrument/share/native/libinstrument/JPLISAgent.c	Sat Jul 09 21:41:14 2016 -0700
@@ -771,12 +771,11 @@
 }
 
 static jobject
-getModuleObject(JNIEnv *                jnienv,
+getModuleObject(jvmtiEnv*               jvmti,
                 jobject                 loaderObject,
                 const char*             cname) {
-    jboolean errorOutstanding = JNI_FALSE;
+    jvmtiError err = JVMTI_ERROR_NONE;
     jobject moduleObject = NULL;
-    jstring package = NULL;
 
     /* find last slash in the class name */
     char* last_slash = (cname == NULL) ? NULL : strrchr(cname, '/');
@@ -789,14 +788,9 @@
     }
     pkg_name_buf[len] = '\0';
 
-    package = (*jnienv)->NewStringUTF(jnienv, pkg_name_buf);
-    jplis_assert_msg(package != NULL, "OOM error in NewStringUTF");
+    err = (*jvmti)->GetNamedModule(jvmti, loaderObject, pkg_name_buf, &moduleObject);
+    jplis_assert_msg(err == JVMTI_ERROR_NONE, "error in the JVMTI GetNamedModule");
 
-    moduleObject = JVM_GetModuleByPackageName(jnienv, loaderObject, package);
-
-    errorOutstanding = checkForAndClearThrowable(jnienv);
-    jplis_assert_msg(!errorOutstanding,
-                     "error in lookup of a module of the class being instrumented");
     free((void*)pkg_name_buf);
     return moduleObject;
 }
@@ -862,7 +856,7 @@
             jobject moduleObject = NULL;
 
             if (classBeingRedefined == NULL) {
-                moduleObject = getModuleObject(jnienv, loaderObject, name);
+                moduleObject = getModuleObject(jvmti(agent), loaderObject, name);
             } else {
                 // Redefine or retransform, InstrumentationImpl.transform() will use
                 // classBeingRedefined.getModule() to get the module.
@@ -873,8 +867,8 @@
                                                 jnienv,
                                                 agent->mInstrumentationImpl,
                                                 agent->mTransform,
+                                                moduleObject,
                                                 loaderObject,
-                                                moduleObject,
                                                 classNameStringObject,
                                                 classBeingRedefined,
                                                 protectionDomain,
--- a/jdk/src/java.instrument/share/native/libinstrument/JPLISAgent.h	Sat Jul 09 17:45:16 2016 -0700
+++ b/jdk/src/java.instrument/share/native/libinstrument/JPLISAgent.h	Sat Jul 09 21:41:14 2016 -0700
@@ -66,7 +66,7 @@
 #define JPLIS_INSTRUMENTIMPL_AGENTMAININVOKER_METHODSIGNATURE "(Ljava/lang/String;Ljava/lang/String;)V"
 #define JPLIS_INSTRUMENTIMPL_TRANSFORM_METHODNAME           "transform"
 #define JPLIS_INSTRUMENTIMPL_TRANSFORM_METHODSIGNATURE      \
-    "(Ljava/lang/ClassLoader;Ljava/lang/reflect/Module;Ljava/lang/String;Ljava/lang/Class;Ljava/security/ProtectionDomain;[BZ)[B"
+    "(Ljava/lang/reflect/Module;Ljava/lang/ClassLoader;Ljava/lang/String;Ljava/lang/Class;Ljava/security/ProtectionDomain;[BZ)[B"
 
 
 /*
--- a/jdk/test/java/lang/instrument/ATransformerManagementTestCase.java	Sat Jul 09 17:45:16 2016 -0700
+++ b/jdk/test/java/lang/instrument/ATransformerManagementTestCase.java	Sat Jul 09 21:41:14 2016 -0700
@@ -302,6 +302,7 @@
         public byte[]
         transform(
             Module module,
+            ClassLoader loader,
             String className,
             Class<?> classBeingRedefined,
             ProtectionDomain    protectionDomain,
@@ -311,6 +312,7 @@
             if (classBeingRedefined != null) checkInTransformer(MyClassFileTransformer.this);
 
             return super.transform(     module,
+                                        loader,
                                         className,
                                         classBeingRedefined,
                                         protectionDomain,
--- a/jdk/test/java/lang/instrument/RetransformAgent.java	Sat Jul 09 17:45:16 2016 -0700
+++ b/jdk/test/java/lang/instrument/RetransformAgent.java	Sat Jul 09 21:41:14 2016 -0700
@@ -69,6 +69,7 @@
         }
 
         public byte[] transform(Module module,
+                                ClassLoader loader,
                                 String className,
                                 Class<?> classBeingRedefined,
                                 ProtectionDomain    protectionDomain,
--- a/jdk/test/java/lang/instrument/SimpleIdentityTransformer.java	Sat Jul 09 17:45:16 2016 -0700
+++ b/jdk/test/java/lang/instrument/SimpleIdentityTransformer.java	Sat Jul 09 21:41:14 2016 -0700
@@ -63,6 +63,7 @@
     public byte[]
     transform(
         Module module,
+        ClassLoader loader,
         String className,
         Class<?> classBeingRedefined,
         ProtectionDomain    protectionDomain,