|
1 /* |
|
2 * Copyright (c) 2017, 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 public final class ReturnJNIWeak { |
|
25 static { |
|
26 System.loadLibrary("ReturnJNIWeak"); |
|
27 } |
|
28 |
|
29 private static final class TestObject { |
|
30 public final int value; |
|
31 |
|
32 public TestObject(int value) { |
|
33 this.value = value; |
|
34 } |
|
35 } |
|
36 |
|
37 private static volatile TestObject testObject = null; |
|
38 |
|
39 private static native void registerObject(Object o); |
|
40 private static native void unregisterObject(); |
|
41 private static native Object getObject(); |
|
42 |
|
43 // Create the test object and record it both strongly and weakly. |
|
44 private static void remember(int value) { |
|
45 TestObject o = new TestObject(value); |
|
46 registerObject(o); |
|
47 testObject = o; |
|
48 } |
|
49 |
|
50 // Remove both strong and weak references to the current test object. |
|
51 private static void forget() { |
|
52 unregisterObject(); |
|
53 testObject = null; |
|
54 } |
|
55 |
|
56 // Verify the weakly recorded object |
|
57 private static void checkValue(int value) throws Exception { |
|
58 Object o = getObject(); |
|
59 if (o == null) { |
|
60 throw new RuntimeException("Weak reference unexpectedly null"); |
|
61 } |
|
62 TestObject t = (TestObject)o; |
|
63 if (t.value != value) { |
|
64 throw new RuntimeException("Incorrect value"); |
|
65 } |
|
66 } |
|
67 |
|
68 // Verify we can create a weak reference and get it back. |
|
69 private static void testSanity() throws Exception { |
|
70 System.out.println("running testSanity"); |
|
71 int value = 5; |
|
72 try { |
|
73 remember(value); |
|
74 checkValue(value); |
|
75 } finally { |
|
76 forget(); |
|
77 } |
|
78 } |
|
79 |
|
80 // Verify weak ref value survives across collection if strong ref exists. |
|
81 private static void testSurvival() throws Exception { |
|
82 System.out.println("running testSurvival"); |
|
83 int value = 10; |
|
84 try { |
|
85 remember(value); |
|
86 checkValue(value); |
|
87 System.gc(); |
|
88 // Verify weak ref still has expected value. |
|
89 checkValue(value); |
|
90 } finally { |
|
91 forget(); |
|
92 } |
|
93 } |
|
94 |
|
95 // Verify weak ref cleared if no strong ref exists. |
|
96 private static void testClear() throws Exception { |
|
97 System.out.println("running testClear"); |
|
98 int value = 15; |
|
99 try { |
|
100 remember(value); |
|
101 checkValue(value); |
|
102 // Verify still good. |
|
103 checkValue(value); |
|
104 // Drop reference. |
|
105 testObject = null; |
|
106 System.gc(); |
|
107 // Verify weak ref cleared as expected. |
|
108 Object recorded = getObject(); |
|
109 if (recorded != null) { |
|
110 throw new RuntimeException("expected clear"); |
|
111 } |
|
112 } finally { |
|
113 forget(); |
|
114 } |
|
115 } |
|
116 |
|
117 public static void main(String[] args) throws Exception { |
|
118 testSanity(); |
|
119 testSurvival(); |
|
120 testClear(); |
|
121 } |
|
122 } |