jdk/test/java/lang/instrument/RedefineClassWithNativeMethodAgent.java
changeset 36511 9d0388c6b336
parent 5506 202f599c92aa
child 44545 83b611b88ac8
--- a/jdk/test/java/lang/instrument/RedefineClassWithNativeMethodAgent.java	Tue Mar 15 13:48:26 2016 -0700
+++ b/jdk/test/java/lang/instrument/RedefineClassWithNativeMethodAgent.java	Thu Mar 17 19:04:16 2016 +0000
@@ -21,10 +21,12 @@
  * questions.
  */
 
-import java.lang.instrument.*;
-import java.net.*;
-import java.util.*;
-import java.io.*;
+import java.io.InputStream;
+import java.lang.instrument.ClassDefinition;
+import java.lang.instrument.Instrumentation;
+import java.lang.reflect.Module;
+import java.util.Timer;
+import java.util.TimerTask;
 
 public class RedefineClassWithNativeMethodAgent {
     static Class clz;
@@ -33,29 +35,20 @@
     public static void premain(String agentArgs, final Instrumentation inst) throws Exception {
         String s = agentArgs.substring(0, agentArgs.indexOf(".class"));
         clz = Class.forName(s.replace('/', '.'));
-        ClassLoader loader =
-            RedefineClassWithNativeMethodAgent.class.getClassLoader();
-        URL classURL = loader.getResource(agentArgs);
-        if (classURL == null) {
+        InputStream in;
+        Module m = clz.getModule();
+        if (m != null) {
+            in = m.getResourceAsStream(agentArgs);
+        } else {
+            ClassLoader loader =
+                RedefineClassWithNativeMethodAgent.class.getClassLoader();
+            in = loader.getResourceAsStream(agentArgs);
+        }
+        if (in == null) {
             throw new Exception("Cannot find class: " + agentArgs);
         }
-
-        int         redefineLength;
-        InputStream redefineStream;
+        byte[] buffer = in.readAllBytes();
 
-        System.out.println("Reading test class from " + classURL);
-        if (classURL.getProtocol().equals("file")) {
-            File f = new File(classURL.getFile());
-            redefineStream = new FileInputStream(f);
-            redefineLength = (int) f.length();
-        } else {
-            URLConnection conn = classURL.openConnection();
-            redefineStream = conn.getInputStream();
-            redefineLength = conn.getContentLength();
-        }
-
-        final byte[] buffer = new byte[redefineLength];
-        new BufferedInputStream(redefineStream).read(buffer);
         new Timer(true).schedule(new TimerTask() {
             public void run() {
                 try {