1 /*
   2  * Copyright (c) 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 package runtime.valhalla.valuetypes;
  25 
  26 /* @test
  27  * @summary test JNI functions with values
  28  * @compile -XDemitQtypes -XDallowWithFieldOperator ValueWithJni.java
  29  * @run main/othervm/native -Xint -XX:+EnableValhalla runtime.valhalla.valuetypes.ValueWithJni
  30  * @run main/othervm/native -Xcomp -XX:+EnableValhalla runtime.valhalla.valuetypes.ValueWithJni
  31  */
  32 public value final class ValueWithJni {
  33 
  34     static {
  35         System.loadLibrary("ValueWithJni");
  36     }
  37 
  38     public static void main(String[] args) {
  39         testJniMonitorOps();
  40     }
  41 
  42     final int x;
  43     private ValueWithJni() { x = 0; }
  44 
  45     public native void doJniMonitorEnter();
  46     public native void doJniMonitorExit();
  47 
  48     public static ValueWithJni createValueWithJni(int x) {
  49         ValueWithJni v = ValueWithJni.default;
  50         v = __WithField(v.x, x);
  51         return v;
  52     }
  53 
  54     public static void testJniMonitorOps() {
  55         boolean sawImse = false;
  56         try {
  57             createValueWithJni(0).doJniMonitorEnter();
  58         } catch (Throwable t) {
  59             sawImse = checkImse(t);
  60         }
  61         if (!sawImse) {
  62             throw new RuntimeException("JNI MonitorEnter did not fail");
  63         }
  64         sawImse = false;
  65         try {
  66             createValueWithJni(0).doJniMonitorExit();
  67         } catch (Throwable t) {
  68             sawImse = checkImse(t);
  69         }
  70         if (!sawImse) {
  71             throw new RuntimeException("JNI MonitorExit did not fail");
  72         }
  73     }
  74 
  75     static boolean checkImse(Throwable t) {
  76         if (t instanceof IllegalMonitorStateException) {
  77             return true;
  78         }
  79         throw new RuntimeException(t);
  80     }
  81 }