1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/runtime/jni/ReturnJNIWeak/ReturnJNIWeak.java Fri May 10 18:50:40 2019 +0000 1.3 @@ -0,0 +1,122 @@ 1.4 +/* 1.5 + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +public final class ReturnJNIWeak { 1.28 + static { 1.29 + System.loadLibrary("ReturnJNIWeak"); 1.30 + } 1.31 + 1.32 + private static final class TestObject { 1.33 + public final int value; 1.34 + 1.35 + public TestObject(int value) { 1.36 + this.value = value; 1.37 + } 1.38 + } 1.39 + 1.40 + private static volatile TestObject testObject = null; 1.41 + 1.42 + private static native void registerObject(Object o); 1.43 + private static native void unregisterObject(); 1.44 + private static native Object getObject(); 1.45 + 1.46 + // Create the test object and record it both strongly and weakly. 1.47 + private static void remember(int value) { 1.48 + TestObject o = new TestObject(value); 1.49 + registerObject(o); 1.50 + testObject = o; 1.51 + } 1.52 + 1.53 + // Remove both strong and weak references to the current test object. 1.54 + private static void forget() { 1.55 + unregisterObject(); 1.56 + testObject = null; 1.57 + } 1.58 + 1.59 + // Verify the weakly recorded object 1.60 + private static void checkValue(int value) throws Exception { 1.61 + Object o = getObject(); 1.62 + if (o == null) { 1.63 + throw new RuntimeException("Weak reference unexpectedly null"); 1.64 + } 1.65 + TestObject t = (TestObject)o; 1.66 + if (t.value != value) { 1.67 + throw new RuntimeException("Incorrect value"); 1.68 + } 1.69 + } 1.70 + 1.71 + // Verify we can create a weak reference and get it back. 1.72 + private static void testSanity() throws Exception { 1.73 + System.out.println("running testSanity"); 1.74 + int value = 5; 1.75 + try { 1.76 + remember(value); 1.77 + checkValue(value); 1.78 + } finally { 1.79 + forget(); 1.80 + } 1.81 + } 1.82 + 1.83 + // Verify weak ref value survives across collection if strong ref exists. 1.84 + private static void testSurvival() throws Exception { 1.85 + System.out.println("running testSurvival"); 1.86 + int value = 10; 1.87 + try { 1.88 + remember(value); 1.89 + checkValue(value); 1.90 + System.gc(); 1.91 + // Verify weak ref still has expected value. 1.92 + checkValue(value); 1.93 + } finally { 1.94 + forget(); 1.95 + } 1.96 + } 1.97 + 1.98 + // Verify weak ref cleared if no strong ref exists. 1.99 + private static void testClear() throws Exception { 1.100 + System.out.println("running testClear"); 1.101 + int value = 15; 1.102 + try { 1.103 + remember(value); 1.104 + checkValue(value); 1.105 + // Verify still good. 1.106 + checkValue(value); 1.107 + // Drop reference. 1.108 + testObject = null; 1.109 + System.gc(); 1.110 + // Verify weak ref cleared as expected. 1.111 + Object recorded = getObject(); 1.112 + if (recorded != null) { 1.113 + throw new RuntimeException("expected clear"); 1.114 + } 1.115 + } finally { 1.116 + forget(); 1.117 + } 1.118 + } 1.119 + 1.120 + public static void main(String[] args) throws Exception { 1.121 + testSanity(); 1.122 + testSurvival(); 1.123 + testClear(); 1.124 + } 1.125 +}