classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cp-patches] [generics] RFC: instrumentation for defineClasses


From: Nicolas Geoffray
Subject: Re: [cp-patches] [generics] RFC: instrumentation for defineClasses
Date: Wed, 14 Dec 2005 10:23:30 +0100
User-agent: Mozilla Thunderbird 1.0.7 (X11/20051019)

I'm committing this in since I had no objection.

2005-12-14  Nicolas Geoffray  <address@hidden>

   * java/lang/ClassLoader
   (defineClass(String,byte[],int,int,ProtectionDomain)):
   Calls VMClassLoader.defineClassWithTransformers instead
   of VMClassLoader.defineClass.
   * vm/reference/java/lang/VMClassLoader
   (defineClassWithTransformers): New method.
   (instrumenter): New Field.


Nicolas Geoffray wrote:

Hi,

I implemented the instrumentation of definition of classes in classpath. With
the previous implementation, the vm needed to call transformers, now the
call is done in java.lang.ClassLoader.defineClass. This doesn't break
the vm interface.

The vm implementor needs to fill in VMClassLoader.instrumenter the
java.lang.InstrumentationImpl object (or any other implementation, but it should
be noted that the VMClassLoader.redefineClassWithTransformers needs the
instrumentation object to have a callTransformers method)

The drawback of this implementation is that defineClass will allways test if there is an InstrumentationImpl object that is defined. If a vm doesn't want this test, it can still modify the VMClassLoader.redefineClassWithTransformers to be the
VMClassLoader.defineClass method.

Nicolas

2005-12-09  Nicolas Geoffray  <address@hidden>

    * java/lang/ClassLoader
    (defineClass(String,byte[],int,int,ProtectionDomain)):
    Calls VMClassLoader.defineClassWithTransformers instead
    of VMClassLoader.defineClass.
    * vm/reference/java/lang/VMClassLoader
    (defineClassWithTransformers): New method.
    (instrumenter): New Field.

------------------------------------------------------------------------

Index: vm/reference/java/lang/VMClassLoader.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/vm/reference/java/lang/VMClassLoader.java,v
retrieving revision 1.16.2.11
diff -u -r1.16.2.11 VMClassLoader.java
--- vm/reference/java/lang/VMClassLoader.java   2 Nov 2005 00:44:15 -0000       
1.16.2.11
+++ vm/reference/java/lang/VMClassLoader.java   9 Dec 2005 10:59:27 -0000
@@ -44,6 +44,8 @@

import java.io.File;
import java.io.IOException;
+import java.lang.InstrumentationImpl;
+import java.lang.instrument.Instrumentation;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.ProtectionDomain;
@@ -346,4 +348,41 @@
   * for this class.
   */
  static native Class findLoadedClass(ClassLoader cl, String name);
+
+  /**
+   * The Instrumentation object created by the vm when agents are defined.
+   */
+  static final Instrumentation instrumenter = null;
+
+  /**
+   * Call the transformers of the possible Instrumentation object
+   *
+   * @param loader the initiating loader
+   * @param name the name of the class
+   * @param data the data representing the classfile, in classfile format
+   * @param offset the offset into the data where the classfile starts
+   * @param len the length of the classfile data in the array
+   * @param pd the protection domain
+   * @return the new data representing the classfile
+   */
+  static final Class defineClassWithTransformers(ClassLoader loader,
+      String name, byte[] data, int offset, int len, ProtectionDomain pd)
+  {
+ + if (instrumenter != null)
+      {
+        byte[] modifiedData = new byte[len];
+        System.arraycopy(data, offset, modifiedData, 0, len);
+        modifiedData =
+          ((InstrumentationImpl)instrumenter).callTransformers(loader, name,
+            null, pd, modifiedData);
+ + return defineClass(loader, name, modifiedData, 0, modifiedData.length,
+            pd);
+      }
+    else
+      {
+        return defineClass(loader, name, data, offset, len, pd);
+      }
+  }
}
Index: java/lang/ClassLoader.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/ClassLoader.java,v
retrieving revision 1.31.2.14
diff -u -r1.31.2.14 ClassLoader.java
--- java/lang/ClassLoader.java  2 Nov 2005 00:43:33 -0000       1.31.2.14
+++ java/lang/ClassLoader.java  9 Dec 2005 10:59:32 -0000
@@ -467,7 +467,9 @@
    checkInitialized();
    if (domain == null)
      domain = StaticData.defaultProtectionDomain;
-    return VMClassLoader.defineClass(this, name, data, offset, len, domain);
+ + return VMClassLoader.defineClassWithTransformers(this, name, data, offset,
+        len, domain);
  }

  /**
------------------------------------------------------------------------

_______________________________________________
Classpath-patches mailing list
address@hidden
http://lists.gnu.org/mailman/listinfo/classpath-patches

Index: java/lang/ClassLoader.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/ClassLoader.java,v
retrieving revision 1.31.2.14
diff -u -r1.31.2.14 ClassLoader.java
--- java/lang/ClassLoader.java  2 Nov 2005 00:43:33 -0000       1.31.2.14
+++ java/lang/ClassLoader.java  14 Dec 2005 09:21:31 -0000
@@ -467,7 +467,9 @@
     checkInitialized();
     if (domain == null)
       domain = StaticData.defaultProtectionDomain;
-    return VMClassLoader.defineClass(this, name, data, offset, len, domain);
+    
+    return VMClassLoader.defineClassWithTransformers(this, name, data, offset,
+        len, domain);
   }
 
   /**
Index: vm/reference/java/lang/VMClassLoader.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/vm/reference/java/lang/VMClassLoader.java,v
retrieving revision 1.16.2.11
diff -u -r1.16.2.11 VMClassLoader.java
--- vm/reference/java/lang/VMClassLoader.java   2 Nov 2005 00:44:15 -0000       
1.16.2.11
+++ vm/reference/java/lang/VMClassLoader.java   14 Dec 2005 09:21:35 -0000
@@ -44,6 +44,8 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.lang.InstrumentationImpl;
+import java.lang.instrument.Instrumentation;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.security.ProtectionDomain;
@@ -346,4 +348,45 @@
    * for this class.
    */
   static native Class findLoadedClass(ClassLoader cl, String name);
+
+  /**
+   * The Instrumentation object created by the vm when agents are defined.
+   */
+  static final Instrumentation instrumenter = null;
+
+  /**
+   * Call the transformers of the possible Instrumentation object. This
+   * implementation assumes the instrumenter is a
+   * <code>InstrumentationImpl</code> object. VM implementors would
+   * have to redefine this method if they provide their own implementation
+   * of the <code>Instrumentation</code> interface.
+   *
+   * @param loader the initiating loader
+   * @param name the name of the class
+   * @param data the data representing the classfile, in classfile format
+   * @param offset the offset into the data where the classfile starts
+   * @param len the length of the classfile data in the array
+   * @param pd the protection domain
+   * @return the new data representing the classfile
+   */
+  static final Class defineClassWithTransformers(ClassLoader loader,
+      String name, byte[] data, int offset, int len, ProtectionDomain pd)
+  {
+    
+    if (instrumenter != null)
+      {
+        byte[] modifiedData = new byte[len];
+        System.arraycopy(data, offset, modifiedData, 0, len);
+        modifiedData =
+          ((InstrumentationImpl)instrumenter).callTransformers(loader, name,
+            null, pd, modifiedData);
+        
+        return defineClass(loader, name, modifiedData, 0, modifiedData.length,
+            pd);
+      }
+    else
+      {
+        return defineClass(loader, name, data, offset, len, pd);
+      }
+  }
 }

reply via email to

[Prev in Thread] Current Thread [Next in Thread]