changeset 20:e2b20ae96d47

dynamic instrumentation under JDK 9
author afedorch
date Wed, 19 Jul 2017 17:34:34 -0700
parents c3aed4b2d93d
children e608a1c1471b
files src/classes/com/sun/tdk/jcov/Agent.java src/classes/com/sun/tdk/jcov/instrument/ClassMorph.java src/classes/com/sun/tdk/jcov/instrument/DeferringMethodClassAdapter.java src/classes/com/sun/tdk/jcov/util/Utils.java
diffstat 4 files changed, 20 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/src/classes/com/sun/tdk/jcov/Agent.java	Wed Jul 12 19:22:42 2017 -0700
+++ b/src/classes/com/sun/tdk/jcov/Agent.java	Wed Jul 19 17:34:34 2017 -0700
@@ -442,8 +442,6 @@
         params.enable();
         CollectDetect.enterInstrumentationCode();
 
-        updateModules();
-
         Tr transformer = new Tr("RetransformApp", flushPath);
         inst.addTransformer(transformer, true);
         if (params.isInstrumentNative()) {
@@ -458,6 +456,7 @@
         for (Class c : classes) {
             if (inst.isModifiableClass(c)
                     && classMorph.shouldTransform(c.getName().replace('.', '/'))
+                    && !c.getName().replace('.', '/').equals("jdk/internal/reflect/Reflection")
                     && !c.getName().replace('.', '/').equals("sun/reflect/Reflection")) {
                 classes[keep++] = c;
             }
@@ -513,44 +512,6 @@
 
     }
 
-    private void updateModules(){
-        try {
-            Class layer = Class.forName("java.lang.module.Layer");
-            java.lang.reflect.Method bootMethod = layer.getDeclaredMethod("boot");
-            Object layerObj = bootMethod.invoke(layer);
-
-            java.lang.reflect.Method allModulesD = layer.getDeclaredMethod("allModuleDescriptors");
-            allModulesD.setAccessible(true);
-            Set<Object> mDescriptors = (Set<Object>) allModulesD.invoke(layerObj);
-
-            Class moduleDescriptor = Class.forName("java.lang.module.ModuleDescriptor");
-            java.lang.reflect.Method nameMethod = moduleDescriptor.getDeclaredMethod("name");
-
-            for (Object md : mDescriptors){
-                String moduleName = (String) nameMethod.invoke(md);
-                updateModule(moduleName, layer, layerObj);
-            }
-        }
-        catch (Exception e){
-        }
-    }
-
-    private void updateModule(String name, Class layer, Object layerObj){
-        try{
-            java.lang.reflect.Method findModule = layer.getDeclaredMethod("findModule", String.class);
-            Object moduleOptional = findModule.invoke(layerObj, name);
-            java.lang.reflect.Method getMethod = moduleOptional.getClass().getDeclaredMethod("get");
-            Object module = getMethod.invoke(moduleOptional);
-
-            java.lang.reflect.Method getModuleMethod = Class.class.getDeclaredMethod("getModule");
-            Object jcovModule = getModuleMethod.invoke(Class.forName("com.sun.tdk.jcov.runtime.CollectDetect"));
-            java.lang.reflect.Method addReadsMethod = module.getClass().getDeclaredMethod("addReads", Class.forName("java.lang.reflect.Module"));
-            addReadsMethod.invoke(module, jcovModule);
-        }
-        catch (Exception e){
-        }
-    }
-
     private void loadFileSaverClasses() throws IOException{
         File file = new File(filename + "_load");
         new FileOutputStream(file).close();
--- a/src/classes/com/sun/tdk/jcov/instrument/ClassMorph.java	Wed Jul 12 19:22:42 2017 -0700
+++ b/src/classes/com/sun/tdk/jcov/instrument/ClassMorph.java	Wed Jul 19 17:34:34 2017 -0700
@@ -281,6 +281,11 @@
     }
 
     private String updateClassModule(String fullname){
+
+        if ("java/lang/ClassCircularityError".equals(fullname)){
+            return "module java.base";
+        }
+
         String result = null;
         try{
             if (fullname.contains("$")){
--- a/src/classes/com/sun/tdk/jcov/instrument/DeferringMethodClassAdapter.java	Wed Jul 12 19:22:42 2017 -0700
+++ b/src/classes/com/sun/tdk/jcov/instrument/DeferringMethodClassAdapter.java	Wed Jul 19 17:34:34 2017 -0700
@@ -86,7 +86,11 @@
             final String desc,
             final String signature,
             final String[] exceptions) {
-        if (!InstrumentationOptions.isSkipped(k.getFullname(), name, access) && params.isDynamicCollect() && params.isInstrumentNative() && (access & ACC_NATIVE) != 0) {
+        if (!InstrumentationOptions.isSkipped(k.getFullname(), name, access)
+                && params.isDynamicCollect()
+                && params.isInstrumentNative()
+                && (access & ACC_NATIVE) != 0
+                && !"java/lang/invoke/VarHandle".equals(k.getFullname())) {
             // Visit the native method, but change the access flags and rename it with a prefix
             int accessNative = access;
             if ((accessNative & ACC_STATIC) == 0) {
--- a/src/classes/com/sun/tdk/jcov/util/Utils.java	Wed Jul 12 19:22:42 2017 -0700
+++ b/src/classes/com/sun/tdk/jcov/util/Utils.java	Wed Jul 19 17:34:34 2017 -0700
@@ -100,6 +100,7 @@
     public final static int VER14 = 140;
     public final static int VER15 = 150;
     public final static int VER16 = 160;
+    public final static int VER9 = 190;
     private static int javaVersion = -1;
 
 
@@ -109,9 +110,14 @@
     public static int getJavaVersion() {
         if (javaVersion == -1){
             String ver = System.getProperty("java.version");
-            javaVersion = (ver.charAt(0) - '0') * 100
-                    + (ver.charAt(2) - '0') * 10
-                    + (ver.length() > 4 ? (ver.charAt(4) - '0') : 0);
+            if (ver.length() > 1) {
+                javaVersion = (ver.charAt(0) - '0') * 100
+                        + (ver.charAt(2) - '0') * 10
+                        + (ver.length() > 4 ? (ver.charAt(4) - '0') : 0);
+            }
+            else{
+                javaVersion = VER9;
+            }
         }
         return javaVersion;
     }