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.PrintStream;
 25 
 26 /*
 27  * @test
 28  *
 29  * @summary converted from VM Testbase nsk/jvmti/FieldAccess/fieldacc002.
 30  * VM Testbase keywords: [quick, jpda, jvmti, noras]
 31  * VM Testbase readme:
 32  * DESCRIPTION
 33  *     The test exercise JVMTI event callback function FieldAccess.
 34  *     The test checks if the parameters of the function contain the
 35  *     expected values for fields accessed from JNI code.
 36  * COMMENTS
 37  *     Fixed according to 4669812 bug.
 38  *     Ported from JVMDI.
 39  *
 40  * @library /test/lib
 41  * @compile --enable-preview -source ${jdk.version} fieldacc02.java
 42  * @run main/othervm/native --enable-preview -agentlib:fieldacc02 fieldacc02
 43  */
 44 
 45 
 46 
 47 public class fieldacc02 {
 48 
 49     final static int JCK_STATUS_BASE = 95;
 50 
 51     static {
 52         try {
 53             System.loadLibrary("fieldacc02");
 54         } catch (UnsatisfiedLinkError ule) {
 55             System.err.println("Could not load fieldacc02 library");
 56             System.err.println("java.library.path:"
 57                 + System.getProperty("java.library.path"));
 58             throw ule;
 59         }
 60     }
 61 
 62     static volatile int result;
 63     native static void getReady();
 64     native static int check(Object obj);
 65 
 66     public static void main(String args[]) {
 67         testKernel();
 68         testVirtual();
 69     }
 70     public static void testVirtual() {
 71         Thread thread = Thread.startVirtualThread(() -> {
 72             fieldacc02a t = new fieldacc02a();
 73             getReady();
 74             result = check(t);
 75         });
 76         try {
 77             thread.join();
 78         } catch (InterruptedException e) {
 79             throw new RuntimeException(e);
 80         }
 81 
 82         if (result != 0) {
 83             throw new RuntimeException("check failed with result " + result);
 84         }
 85     }
 86     public static void testKernel() {
 87         fieldacc02a t = new fieldacc02a();
 88         getReady();
 89         result = check(t);
 90         if (result != 0) {
 91             throw new RuntimeException("check failed with result " + result);
 92         }
 93     }
 94 }
 95 
 96 class fieldacc02a {
 97     static boolean staticBoolean = true;
 98     static byte staticByte = 1;
 99     static short staticShort = 2;
100     static int staticInt = 3;
101     static long staticLong = 4;
102     static float staticFloat = 0.5F;
103     static double staticDouble = 0.6;
104     static char staticChar = '\u0007';
105     static Object staticObject = new Object();
106     static int staticArrInt[] = {8};
107 
108     boolean instanceBoolean = false;
109     byte instanceByte = 10;
110     short instanceShort = 20;
111     int instanceInt = 30;
112     long instanceLong = 40;
113     float instanceFloat = 0.05F;
114     double instanceDouble = 0.06;
115     char instanceChar = '\u0070';
116     Object instanceObject = new Object();
117     int instanceArrInt[] = {80};
118 }