85 @Override
86 protected void registerInvocationPlugins(InvocationPlugins invocationPlugins) {
87 BytecodeProvider replacementBytecodeProvider = getSystemClassLoaderBytecodeProvider();
88 Registration r = new Registration(invocationPlugins, Original.class, replacementBytecodeProvider);
89 r.registerMethodSubstitution(Intrinsic.class, "getValue");
90 super.registerInvocationPlugins(invocationPlugins);
91 }
92
93 public static String callOriginalGetValue() {
94 // This call will be intrinsified when compiled by Graal
95 return Original.getValue();
96 }
97
98 public static String callIntrinsicGetValue() {
99 // This call will *not* be intrinsified when compiled by Graal
100 return Intrinsic.getValue();
101 }
102
103 @Test
104 public void test() throws Throwable {
105 try {
106 Class.forName("java.lang.instrument.Instrumentation");
107 } catch (ClassNotFoundException ex) {
108 // skip this test if java.instrument JDK9 module is missing
109 return;
110 }
111 String recursionPropName = getClass().getName() + ".recursion";
112 if (Java8OrEarlier || Boolean.getBoolean(recursionPropName)) {
113 testHelper();
114 } else {
115 List<String> vmArgs = withoutDebuggerArguments(getVMCommandLine());
116 vmArgs.add("-D" + recursionPropName + "=true");
117 vmArgs.add("-Djdk.attach.allowAttachSelf=true");
118 Subprocess proc = java(vmArgs, "com.oracle.mxtool.junit.MxJUnitWrapper", getClass().getName());
119 if (proc.exitCode != 0) {
120 Assert.fail(String.format("non-zero exit code %d for command:%n%s", proc.exitCode, proc));
121 }
122 }
123 }
124
181 Manifest manifest = new Manifest();
182 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
183 Attributes mainAttrs = manifest.getMainAttributes();
184 mainAttrs.putValue("Agent-Class", RedefinerAgent.class.getName());
185 mainAttrs.putValue("Can-Redefine-Classes", "true");
186 mainAttrs.putValue("Can-Retransform-Classes", "true");
187
188 Path jar = Files.createTempFile("myagent", ".jar");
189 try {
190 JarOutputStream jarStream = new JarOutputStream(new FileOutputStream(jar.toFile()), manifest);
191 add(jarStream, RedefinerAgent.class);
192 add(jarStream, Redefiner.class);
193 jarStream.close();
194
195 return loadAgent(jar);
196 } finally {
197 Files.deleteIfExists(jar);
198 }
199 }
200
201 @SuppressWarnings("deprecation")
202 public static boolean loadAgent(Path agent) throws Exception {
203 String vmName = ManagementFactory.getRuntimeMXBean().getName();
204 int p = vmName.indexOf('@');
205 assumeTrue("VM name not in <pid>@<host> format: " + vmName, p != -1);
206 String pid = vmName.substring(0, p);
207 Class<?> c;
208 if (Java8OrEarlier) {
209 ClassLoader cl = ToolProvider.getSystemToolClassLoader();
210 c = Class.forName("com.sun.tools.attach.VirtualMachine", true, cl);
211 } else {
212 try {
213 // I don't know what changed to make this necessary...
214 c = Class.forName("com.sun.tools.attach.VirtualMachine", true, RedefineIntrinsicTest.class.getClassLoader());
215 } catch (ClassNotFoundException ex) {
216 try {
217 Class.forName("javax.naming.Reference");
218 } catch (ClassNotFoundException coreNamingMissing) {
219 // if core JDK classes aren't found, we are probably running in a
220 // JDK9 java.base environment and then missing class is OK
221 return false;
|
85 @Override
86 protected void registerInvocationPlugins(InvocationPlugins invocationPlugins) {
87 BytecodeProvider replacementBytecodeProvider = getSystemClassLoaderBytecodeProvider();
88 Registration r = new Registration(invocationPlugins, Original.class, replacementBytecodeProvider);
89 r.registerMethodSubstitution(Intrinsic.class, "getValue");
90 super.registerInvocationPlugins(invocationPlugins);
91 }
92
93 public static String callOriginalGetValue() {
94 // This call will be intrinsified when compiled by Graal
95 return Original.getValue();
96 }
97
98 public static String callIntrinsicGetValue() {
99 // This call will *not* be intrinsified when compiled by Graal
100 return Intrinsic.getValue();
101 }
102
103 @Test
104 public void test() throws Throwable {
105 assumeManagementLibraryIsLoadable();
106 try {
107 Class.forName("java.lang.instrument.Instrumentation");
108 } catch (ClassNotFoundException ex) {
109 // skip this test if java.instrument JDK9 module is missing
110 return;
111 }
112 String recursionPropName = getClass().getName() + ".recursion";
113 if (Java8OrEarlier || Boolean.getBoolean(recursionPropName)) {
114 testHelper();
115 } else {
116 List<String> vmArgs = withoutDebuggerArguments(getVMCommandLine());
117 vmArgs.add("-D" + recursionPropName + "=true");
118 vmArgs.add("-Djdk.attach.allowAttachSelf=true");
119 Subprocess proc = java(vmArgs, "com.oracle.mxtool.junit.MxJUnitWrapper", getClass().getName());
120 if (proc.exitCode != 0) {
121 Assert.fail(String.format("non-zero exit code %d for command:%n%s", proc.exitCode, proc));
122 }
123 }
124 }
125
182 Manifest manifest = new Manifest();
183 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
184 Attributes mainAttrs = manifest.getMainAttributes();
185 mainAttrs.putValue("Agent-Class", RedefinerAgent.class.getName());
186 mainAttrs.putValue("Can-Redefine-Classes", "true");
187 mainAttrs.putValue("Can-Retransform-Classes", "true");
188
189 Path jar = Files.createTempFile("myagent", ".jar");
190 try {
191 JarOutputStream jarStream = new JarOutputStream(new FileOutputStream(jar.toFile()), manifest);
192 add(jarStream, RedefinerAgent.class);
193 add(jarStream, Redefiner.class);
194 jarStream.close();
195
196 return loadAgent(jar);
197 } finally {
198 Files.deleteIfExists(jar);
199 }
200 }
201
202 @SuppressWarnings({"deprecation", "unused"})
203 public static boolean loadAgent(Path agent) throws Exception {
204 String vmName = ManagementFactory.getRuntimeMXBean().getName();
205 int p = vmName.indexOf('@');
206 assumeTrue("VM name not in <pid>@<host> format: " + vmName, p != -1);
207 String pid = vmName.substring(0, p);
208 Class<?> c;
209 if (Java8OrEarlier) {
210 ClassLoader cl = ToolProvider.getSystemToolClassLoader();
211 c = Class.forName("com.sun.tools.attach.VirtualMachine", true, cl);
212 } else {
213 try {
214 // I don't know what changed to make this necessary...
215 c = Class.forName("com.sun.tools.attach.VirtualMachine", true, RedefineIntrinsicTest.class.getClassLoader());
216 } catch (ClassNotFoundException ex) {
217 try {
218 Class.forName("javax.naming.Reference");
219 } catch (ClassNotFoundException coreNamingMissing) {
220 // if core JDK classes aren't found, we are probably running in a
221 // JDK9 java.base environment and then missing class is OK
222 return false;
|