2222 */
2323
2424
25- import java .io .File ;
2625import java .nio .file .Path ;
27- import java .lang .management .*;
2826
2927import bootreporter .*;
3028import jdk .test .lib .helpers .ClassFileInstaller ;
3331
3432/*
3533 * @test
36- * @bug 6263319
34+ * @bug 6263319 8334167
3735 * @summary test setNativeMethodPrefix
3836 * @requires ((vm.opt.StartFlightRecording == null) | (vm.opt.StartFlightRecording == false)) & ((vm.opt.FlightRecorder == null) | (vm.opt.FlightRecorder == false))
39- * @modules java.management
40- * java.instrument
37+ * @modules java.instrument
4138 * @library /test/lib
4239 * @build bootreporter.StringIdCallback bootreporter.StringIdCallbackReporter
4340 * asmlib.Instrumentor NativeMethodPrefixAgent
4441 * @enablePreview
4542 * @comment The test uses asmlib/Instrumentor.java which relies on ClassFile API PreviewFeature.
46- * @run driver/timeout=240 NativeMethodPrefixApp roleDriver
47- * @comment The test uses a higher timeout to prevent test timeouts noted in JDK-6528548
43+ * @run main/native NativeMethodPrefixApp roleDriver
4844 */
4945public class NativeMethodPrefixApp implements StringIdCallback {
5046
51- // This test is fragile like a golden file test.
52- // It assumes that a specific non-native library method will call a specific
53- // native method. The below may need to be updated based on library changes.
54- static String goldenNativeMethodName = "getStartupTime" ;
55-
47+ // we expect this native method, which is part of this test's application,
48+ // to be instrumented and invoked
49+ static String goldenNativeMethodName = "fooBarNativeMethod" ;
5650 static boolean [] gotIt = {false , false , false };
51+ private static final String testLibraryPath = System .getProperty ("test.nativepath" );
5752
5853 public static void main (String [] args ) throws Exception {
5954 if (args .length == 1 ) {
@@ -68,21 +63,19 @@ public static void main(String[] args) throws Exception {
6863 launchApp (agentJar );
6964 } else {
7065 System .err .println ("running app" );
66+ System .loadLibrary ("NativeMethodPrefix" ); // load the native library
7167 new NativeMethodPrefixApp ().run ();
7268 }
7369 }
7470
7571 private static Path createAgentJar () throws Exception {
76- final String testClassesDir = System .getProperty ("test.classes" );
7772 final Path agentJar = Path .of ("NativeMethodPrefixAgent.jar" );
7873 final String manifest = """
7974 Manifest-Version: 1.0
8075 Premain-Class: NativeMethodPrefixAgent
8176 Can-Retransform-Classes: true
8277 Can-Set-Native-Method-Prefix: true
83- """
84- + "Boot-Class-Path: " + testClassesDir .replace (File .separatorChar , '/' ) + "/"
85- + "\n " ;
78+ """ ;
8679 System .out .println ("Manifest is:\n " + manifest );
8780 // create the agent jar
8881 ClassFileInstaller .writeJar (agentJar .getFileName ().toString (),
@@ -96,6 +89,7 @@ private static void launchApp(final Path agentJar) throws Exception {
9689 final OutputAnalyzer oa = ProcessTools .executeTestJava (
9790 "--enable-preview" , // due to usage of ClassFile API PreviewFeature in the agent
9891 "-javaagent:" + agentJar .toString (),
92+ "-Djava.library.path=" + testLibraryPath ,
9993 NativeMethodPrefixApp .class .getName ());
10094 oa .shouldHaveExitValue (0 );
10195 // make available stdout/stderr in the logs, even in case of successful completion
@@ -105,10 +99,11 @@ private static void launchApp(final Path agentJar) throws Exception {
10599 private void run () throws Exception {
106100 StringIdCallbackReporter .registerCallback (this );
107101 System .err .println ("start" );
108-
109- java .lang .reflect .Array .getLength (new short [5 ]);
110- RuntimeMXBean mxbean = ManagementFactory .getRuntimeMXBean ();
111- System .err .println (mxbean .getVmVendor ());
102+ final long val = new Dummy ().callSomeNativeMethod ();
103+ if (val != 42 ) {
104+ throw new RuntimeException ("unexpected return value " + val
105+ + " from native method, expected 42" );
106+ }
112107
113108 NativeMethodPrefixAgent .checkErrors ();
114109
@@ -128,4 +123,13 @@ public void tracker(String name, int id) {
128123 System .err .println ("Tracked #" + id + ": " + name );
129124 }
130125 }
126+
127+ private static class Dummy {
128+
129+ private long callSomeNativeMethod () {
130+ return fooBarNativeMethod ();
131+ }
132+
133+ private native long fooBarNativeMethod ();
134+ }
131135}
0 commit comments