Mercurial > hg > graal-jvmci-8
annotate jvmci/jdk.internal.jvmci.runtime.test/src/jdk/internal/jvmci/runtime/test/RedefineClassTest.java @ 22569:ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Wed, 16 Sep 2015 13:40:05 +0200 |
parents | 575eb1036ea9 |
children |
rev | line source |
---|---|
22419
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
1 /* |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
4 * |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
5 * This code is free software; you can redistribute it and/or modify it |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
6 * under the terms of the GNU General Public License version 2 only, as |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
7 * published by the Free Software Foundation. |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
8 * |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
13 * accompanied this code). |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
14 * |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
15 * You should have received a copy of the GNU General Public License version |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
18 * |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
21 * questions. |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
22 */ |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
23 package jdk.internal.jvmci.runtime.test; |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
24 |
22569
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22505
diff
changeset
|
25 import static org.junit.Assume.assumeTrue; |
22419
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
26 |
22569
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22505
diff
changeset
|
27 import java.io.FileOutputStream; |
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22505
diff
changeset
|
28 import java.io.IOException; |
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22505
diff
changeset
|
29 import java.io.InputStream; |
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22505
diff
changeset
|
30 import java.lang.instrument.ClassFileTransformer; |
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22505
diff
changeset
|
31 import java.lang.instrument.IllegalClassFormatException; |
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22505
diff
changeset
|
32 import java.lang.instrument.Instrumentation; |
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22505
diff
changeset
|
33 import java.lang.management.ManagementFactory; |
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22505
diff
changeset
|
34 import java.lang.reflect.Method; |
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22505
diff
changeset
|
35 import java.nio.file.Files; |
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22505
diff
changeset
|
36 import java.nio.file.Path; |
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22505
diff
changeset
|
37 import java.security.ProtectionDomain; |
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22505
diff
changeset
|
38 import java.util.Arrays; |
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22505
diff
changeset
|
39 import java.util.jar.Attributes; |
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22505
diff
changeset
|
40 import java.util.jar.JarEntry; |
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22505
diff
changeset
|
41 import java.util.jar.JarOutputStream; |
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22505
diff
changeset
|
42 import java.util.jar.Manifest; |
22419
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
43 |
22569
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22505
diff
changeset
|
44 import javax.tools.ToolProvider; |
22419
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
45 |
22569
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22505
diff
changeset
|
46 import jdk.internal.jvmci.meta.ResolvedJavaMethod; |
22419
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
47 |
22569
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22505
diff
changeset
|
48 import org.junit.Assert; |
ec96f33a101d
updated imports to be explicit and added checkstyle rule to forbid * imports
Doug Simon <doug.simon@oracle.com>
parents:
22505
diff
changeset
|
49 import org.junit.Test; |
22419
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
50 |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
51 /** |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
52 * Tests that {@link ResolvedJavaMethod}s are safe in the context of class redefinition being used |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
53 * to redefine the method to which they refer. |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
54 */ |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
55 public class RedefineClassTest extends TypeUniverse { |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
56 |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
57 static class Foo { |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
58 public static Object getName() { |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
59 return "foo"; |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
60 } |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
61 } |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
62 |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
63 @Test |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
64 public void test() throws Throwable { |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
65 |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
66 Method fooMethod = Foo.class.getDeclaredMethod("getName"); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
67 |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
68 ResolvedJavaMethod foo1 = metaAccess.lookupJavaMethod(fooMethod); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
69 ResolvedJavaMethod foo2 = metaAccess.lookupJavaMethod(fooMethod); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
70 |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
71 String foo1Code = Arrays.toString(foo1.getCode()); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
72 String foo2Code = Arrays.toString(foo2.getCode()); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
73 |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
74 Assert.assertEquals("foo", Foo.getName()); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
75 |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
76 redefineFoo(); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
77 System.gc(); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
78 |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
79 // Make sure the transformation happened |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
80 Assert.assertEquals("bar", Foo.getName()); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
81 |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
82 Assert.assertEquals(foo1Code, Arrays.toString(foo1.getCode())); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
83 Assert.assertEquals(foo2Code, Arrays.toString(foo1.getCode())); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
84 } |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
85 |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
86 /** |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
87 * Adds the class file bytes for a given class to a JAR stream. |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
88 */ |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
89 static void add(JarOutputStream jar, Class<?> c) throws IOException { |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
90 String name = c.getName(); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
91 String classAsPath = name.replace('.', '/') + ".class"; |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
92 jar.putNextEntry(new JarEntry(classAsPath)); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
93 |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
94 InputStream stream = c.getClassLoader().getResourceAsStream(classAsPath); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
95 |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
96 int nRead; |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
97 byte[] buf = new byte[1024]; |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
98 while ((nRead = stream.read(buf, 0, buf.length)) != -1) { |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
99 jar.write(buf, 0, nRead); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
100 } |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
101 |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
102 jar.closeEntry(); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
103 } |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
104 |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
105 protected void redefineFoo() throws Exception { |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
106 Manifest manifest = new Manifest(); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
107 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
108 Attributes mainAttrs = manifest.getMainAttributes(); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
109 mainAttrs.putValue("Agent-Class", FooAgent.class.getName()); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
110 mainAttrs.putValue("Can-Redefine-Classes", "true"); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
111 mainAttrs.putValue("Can-Retransform-Classes", "true"); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
112 |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
113 Path jar = Files.createTempFile("myagent", ".jar"); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
114 try { |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
115 JarOutputStream jarStream = new JarOutputStream(new FileOutputStream(jar.toFile()), manifest); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
116 add(jarStream, FooAgent.class); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
117 add(jarStream, FooTransformer.class); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
118 jarStream.close(); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
119 |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
120 loadAgent(jar); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
121 } finally { |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
122 Files.deleteIfExists(jar); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
123 } |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
124 } |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
125 |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
126 public static void loadAgent(Path agent) throws Exception { |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
127 String vmName = ManagementFactory.getRuntimeMXBean().getName(); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
128 int p = vmName.indexOf('@'); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
129 assumeTrue("VM name not in <pid>@<host> format: " + vmName, p != -1); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
130 String pid = vmName.substring(0, p); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
131 ClassLoader cl = ToolProvider.getSystemToolClassLoader(); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
132 Class<?> c = Class.forName("com.sun.tools.attach.VirtualMachine", true, cl); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
133 Method attach = c.getDeclaredMethod("attach", String.class); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
134 Method loadAgent = c.getDeclaredMethod("loadAgent", String.class, String.class); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
135 Method detach = c.getDeclaredMethod("detach"); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
136 Object vm = attach.invoke(null, pid); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
137 loadAgent.invoke(vm, agent.toString(), ""); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
138 detach.invoke(vm); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
139 } |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
140 |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
141 public static class FooAgent { |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
142 |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
143 public static void agentmain(@SuppressWarnings("unused") String args, Instrumentation inst) throws Exception { |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
144 if (inst.isRedefineClassesSupported() && inst.isRetransformClassesSupported()) { |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
145 inst.addTransformer(new FooTransformer(), true); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
146 Class<?>[] allClasses = inst.getAllLoadedClasses(); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
147 for (int i = 0; i < allClasses.length; i++) { |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
148 Class<?> c = allClasses[i]; |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
149 if (c == Foo.class) { |
22505
575eb1036ea9
fixed all existing -Xlint warnings
Doug Simon <doug.simon@oracle.com>
parents:
22454
diff
changeset
|
150 inst.retransformClasses(new Class<?>[]{c}); |
22419
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
151 } |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
152 } |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
153 } |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
154 } |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
155 } |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
156 |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
157 /** |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
158 * This transformer replaces the first instance of the constant "foo" in the class file for |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
159 * {@link Foo} with "bar". |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
160 */ |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
161 static class FooTransformer implements ClassFileTransformer { |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
162 |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
163 @Override |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
164 public byte[] transform(ClassLoader cl, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
165 if (Foo.class.equals(classBeingRedefined)) { |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
166 String cf = new String(classfileBuffer); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
167 int i = cf.indexOf("foo"); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
168 Assert.assertTrue("cannot find \"foo\" constant in " + Foo.class.getSimpleName() + "'s class file", i > 0); |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
169 classfileBuffer[i] = 'b'; |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
170 classfileBuffer[i + 1] = 'a'; |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
171 classfileBuffer[i + 2] = 'r'; |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
172 } |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
173 return classfileBuffer; |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
174 } |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
175 } |
c06f5d0a3395
added RedefineClassTest in disabled mode until JVMCI is class redefinition safe
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
176 } |