< prev index next >

test/hotspot/jtreg/compiler/jsr292/RedefineMethodUsedByMultipleMethodHandles.java

Print this page
rev 52334 : 8204521: compiler/jsr292/RedefineMethodUsedByMultipleMethodHandles.java fails trying to delete temp file


  98      */
  99     static void add(JarOutputStream jar, Class<?> c) throws IOException {
 100         String classAsPath = c.getName().replace('.', '/') + ".class";
 101         jar.putNextEntry(new JarEntry(classAsPath));
 102         InputStream stream = c.getClassLoader().getResourceAsStream(classAsPath);
 103 
 104         int b;
 105         while ((b = stream.read()) != -1) {
 106             jar.write(b);
 107         }
 108     }
 109 
 110     static void redefineFoo() throws Exception {
 111         Manifest manifest = new Manifest();
 112         manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
 113         Attributes mainAttrs = manifest.getMainAttributes();
 114         mainAttrs.putValue("Agent-Class", FooAgent.class.getName());
 115         mainAttrs.putValue("Can-Redefine-Classes", "true");
 116         mainAttrs.putValue("Can-Retransform-Classes", "true");
 117 
 118         Path jar = Files.createTempFile("myagent", ".jar");
 119         try {


 120             JarOutputStream jarStream = new JarOutputStream(new FileOutputStream(jar.toFile()), manifest);
 121             add(jarStream, FooAgent.class);
 122             add(jarStream, FooTransformer.class);
 123             jarStream.close();
 124             runAgent(jar);
 125         } finally {
 126             Files.deleteIfExists(jar);
 127         }
 128     }
 129 
 130     public static void runAgent(Path agent) throws Exception {
 131         String vmName = ManagementFactory.getRuntimeMXBean().getName();
 132         int p = vmName.indexOf('@');
 133         assert p != -1 : "VM name not in <pid>@<host> format: " + vmName;
 134         String pid = vmName.substring(0, p);
 135         ClassLoader cl = ClassLoader.getSystemClassLoader();
 136         Class<?> c = Class.forName("com.sun.tools.attach.VirtualMachine", true, cl);
 137         Method attach = c.getDeclaredMethod("attach", String.class);
 138         Method loadAgent = c.getDeclaredMethod("loadAgent", String.class);
 139         Method detach = c.getDeclaredMethod("detach");
 140         Object vm = attach.invoke(null, pid);
 141         loadAgent.invoke(vm, agent.toString());
 142         detach.invoke(vm);
 143     }
 144 
 145     public static class FooAgent {
 146 
 147         public static void agentmain(@SuppressWarnings("unused") String args, Instrumentation inst) throws Exception {




  98      */
  99     static void add(JarOutputStream jar, Class<?> c) throws IOException {
 100         String classAsPath = c.getName().replace('.', '/') + ".class";
 101         jar.putNextEntry(new JarEntry(classAsPath));
 102         InputStream stream = c.getClassLoader().getResourceAsStream(classAsPath);
 103 
 104         int b;
 105         while ((b = stream.read()) != -1) {
 106             jar.write(b);
 107         }
 108     }
 109 
 110     static void redefineFoo() throws Exception {
 111         Manifest manifest = new Manifest();
 112         manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
 113         Attributes mainAttrs = manifest.getMainAttributes();
 114         mainAttrs.putValue("Agent-Class", FooAgent.class.getName());
 115         mainAttrs.putValue("Can-Redefine-Classes", "true");
 116         mainAttrs.putValue("Can-Retransform-Classes", "true");
 117 
 118         // The jar file will be added to the system classloader search path.  It is not safe
 119         // to delete it while the JVM is running, so make sure to create it in the test
 120         // directory so it will be cleaned up by the test harness.
 121         Path jar = Files.createTempFile(Path.of(""), "myagent", ".jar");
 122         JarOutputStream jarStream = new JarOutputStream(new FileOutputStream(jar.toFile()), manifest);
 123         add(jarStream, FooAgent.class);
 124         add(jarStream, FooTransformer.class);
 125         jarStream.close();
 126         runAgent(jar);



 127     }
 128 
 129     public static void runAgent(Path agent) throws Exception {
 130         String vmName = ManagementFactory.getRuntimeMXBean().getName();
 131         int p = vmName.indexOf('@');
 132         assert p != -1 : "VM name not in <pid>@<host> format: " + vmName;
 133         String pid = vmName.substring(0, p);
 134         ClassLoader cl = ClassLoader.getSystemClassLoader();
 135         Class<?> c = Class.forName("com.sun.tools.attach.VirtualMachine", true, cl);
 136         Method attach = c.getDeclaredMethod("attach", String.class);
 137         Method loadAgent = c.getDeclaredMethod("loadAgent", String.class);
 138         Method detach = c.getDeclaredMethod("detach");
 139         Object vm = attach.invoke(null, pid);
 140         loadAgent.invoke(vm, agent.toString());
 141         detach.invoke(vm);
 142     }
 143 
 144     public static class FooAgent {
 145 
 146         public static void agentmain(@SuppressWarnings("unused") String args, Instrumentation inst) throws Exception {


< prev index next >