11/*
2- * Copyright (c) 2005, 2022 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2005, 2024 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2222 */
2323
2424
25- import java .io .PrintStream ;
26- import java .util .* ;
25+ import java .io .File ;
26+ import java .nio . file . Path ;
2727import java .lang .management .*;
28+
2829import bootreporter .*;
30+ import jdk .test .lib .helpers .ClassFileInstaller ;
31+ import jdk .test .lib .process .OutputAnalyzer ;
32+ import jdk .test .lib .process .ProcessTools ;
2933
34+ /*
35+ * @test
36+ * @bug 6263319
37+ * @summary test setNativeMethodPrefix
38+ * @requires ((vm.opt.StartFlightRecording == null) | (vm.opt.StartFlightRecording == false)) & ((vm.opt.FlightRecorder == null) | (vm.opt.FlightRecorder == false))
39+ * @modules java.management
40+ * java.instrument
41+ * @library /test/lib
42+ * @build bootreporter.StringIdCallback bootreporter.StringIdCallbackReporter
43+ * asmlib.Instrumentor NativeMethodPrefixAgent
44+ * @enablePreview
45+ * @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
48+ */
3049public class NativeMethodPrefixApp implements StringIdCallback {
3150
3251 // This test is fragile like a golden file test.
3352 // It assumes that a specific non-native library method will call a specific
3453 // native method. The below may need to be updated based on library changes.
3554 static String goldenNativeMethodName = "getStartupTime" ;
3655
37- static boolean gotIt [] = {false , false , false };
56+ static boolean [] gotIt = {false , false , false };
57+
58+ public static void main (String [] args ) throws Exception {
59+ if (args .length == 1 ) {
60+ if (!"roleDriver" .equals (args [0 ])) {
61+ throw new Exception ("unexpected program argument: " + args [0 ]);
62+ }
63+ // launch the NativeMethodPrefixApp java process after creating the necessary
64+ // infrastructure
65+ System .out .println ("creating agent jar" );
66+ final Path agentJar = createAgentJar ();
67+ System .out .println ("launching app, with javaagent jar: " + agentJar );
68+ launchApp (agentJar );
69+ } else {
70+ System .err .println ("running app" );
71+ new NativeMethodPrefixApp ().run ();
72+ }
73+ }
74+
75+ private static Path createAgentJar () throws Exception {
76+ final String testClassesDir = System .getProperty ("test.classes" );
77+ final Path agentJar = Path .of ("NativeMethodPrefixAgent.jar" );
78+ final String manifest = """
79+ Manifest-Version: 1.0
80+ Premain-Class: NativeMethodPrefixAgent
81+ Can-Retransform-Classes: true
82+ Can-Set-Native-Method-Prefix: true
83+ """
84+ + "Boot-Class-Path: " + testClassesDir .replace (File .separatorChar , '/' ) + "/"
85+ + "\n " ;
86+ System .out .println ("Manifest is:\n " + manifest );
87+ // create the agent jar
88+ ClassFileInstaller .writeJar (agentJar .getFileName ().toString (),
89+ ClassFileInstaller .Manifest .fromString (manifest ),
90+ "NativeMethodPrefixAgent" ,
91+ "asmlib.Instrumentor" );
92+ return agentJar ;
93+ }
3894
39- public static void main (String args []) throws Exception {
40- (new NativeMethodPrefixApp ()).run (args , System .err );
95+ private static void launchApp (final Path agentJar ) throws Exception {
96+ final OutputAnalyzer oa = ProcessTools .executeTestJava (
97+ "--enable-preview" , // due to usage of ClassFile API PreviewFeature in the agent
98+ "-javaagent:" + agentJar .toString (),
99+ NativeMethodPrefixApp .class .getName ());
100+ oa .shouldHaveExitValue (0 );
101+ // make available stdout/stderr in the logs, even in case of successful completion
102+ oa .reportDiagnosticSummary ();
41103 }
42104
43- public void run (String args [], PrintStream out ) throws Exception {
105+ private void run () throws Exception {
44106 StringIdCallbackReporter .registerCallback (this );
45107 System .err .println ("start" );
46108
@@ -57,6 +119,7 @@ public void run(String args[], PrintStream out) throws Exception {
57119 }
58120 }
59121
122+ @ Override
60123 public void tracker (String name , int id ) {
61124 if (name .endsWith (goldenNativeMethodName )) {
62125 System .err .println ("Tracked #" + id + ": MATCHED -- " + name );
0 commit comments