1 /*
 2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 import java.io.*;
25 
26 /*
27  * @test
28  *
29  * @summary converted from VM Testbase nsk/jvmti/NativeMethodBind/nativemethbind001.
30  * VM Testbase keywords: [quick, jpda, jvmti, noras]
31  * VM Testbase readme:
32  * DESCRIPTION
33  *     This test exercises the JVMTI event NativeMethodBind.
34  *     It verifies that the event will be properly sent:
35  *         - for the native method called for the first time
36  *         - when the JNI RegisterNatives() is called.
37  *     The test works as follows. The java part invokes the native method
38  *     'nativeMethod()' twice. At the first time that method registers
39  *     another native method 'anotherNativeMethod()' for the dummy class
40  *     'TestedClass'. Registration is made through the JNI RegisterNatives()
41  *     call. Being invoked at the second time, the nativeMethod() just returns.
42  *     In accordance with the spec, it is expected that the NativeMethodBind
43  *     will be generated only one time for the nativeMethod(), and only one
44  *     time for the anotherNativeMethod().
45  * COMMENTS
46  *     The test has been fixed due to the bug 4967116.
47  *     Fixed the 4995867 bug.
48  *
49  * @library /test/lib
50  * @run main/othervm/native -agentlib:nativemethbind01 nativemethbind01
51  */
52 
53 public class nativemethbind01 {
54     static {
55         try {
56             System.loadLibrary("nativemethbind01");
57         } catch (UnsatisfiedLinkError ule) {
58             System.err.println("Could not load \"nativemethbind01\" library");
59             System.err.println("java.library.path:"
60                 + System.getProperty("java.library.path"));
61             throw ule;
62         }
63     }
64 
65     native void nativeMethod(boolean registerNative);
66 
67     native int check();
68 
69     public static void main(String[] argv) {
70         new nativemethbind01().runThis();
71     }
72 
73     private void runThis() {
74         System.out.println("\nCalling native methods ...\n");
75 
76         // dummy method used to provoke the NativeMethodBind event
77         nativeMethod(true);
78 
79         // call one more time to provoke the wrong NativeMethodBind
80         // event
81         nativeMethod(false);
82 
83         int result = check();
84         if (result != 0) {
85             throw new RuntimeException("runThis() returned " + result);
86         }
87     }
88 
89    /**
90     * Dummy class used only to register native method
91     * <code>anotherNativeMethod</code> with it
92     */
93     class TestedClass {
94         native void anotherNativeMethod();
95     }
96 }