Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/TestReferenceRefersTo.java
41149 views
1
/*
2
* Copyright (c) 2020, 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
/*
25
* @test
26
* @bug 8256377
27
* @summary Based on test/jdk/java/lang/ref/ReferenceRefersTo.java.
28
*/
29
30
import java.lang.ref.Reference;
31
import java.lang.ref.ReferenceQueue;
32
import java.lang.ref.PhantomReference;
33
import java.lang.ref.SoftReference;
34
import java.lang.ref.WeakReference;
35
36
public class TestReferenceRefersTo {
37
private static final void fail(String msg) throws Exception {
38
throw new RuntimeException(msg);
39
}
40
41
// Test java.lang.ref.Reference::refersTo0 intrinsic.
42
private static final void test(Reference ref,
43
Object expectedValue,
44
Object unexpectedValue,
45
String kind) throws Exception {
46
if ((expectedValue != null) && ref.refersTo(null)) {
47
fail(kind + " refers to null");
48
}
49
if (!ref.refersTo(expectedValue)) {
50
fail(kind + " doesn't refer to expected value");
51
}
52
if (ref.refersTo(unexpectedValue)) {
53
fail(kind + " refers to unexpected value");
54
}
55
}
56
57
// Test java.lang.ref.PhantomReference::refersTo0 intrinsic.
58
private static final void test_phantom(PhantomReference ref,
59
Object expectedValue,
60
Object unexpectedValue) throws Exception {
61
String kind = "phantom";
62
if ((expectedValue != null) && ref.refersTo(null)) {
63
fail(kind + " refers to null");
64
}
65
if (!ref.refersTo(expectedValue)) {
66
fail(kind + " doesn't refer to expected value");
67
}
68
if (ref.refersTo(unexpectedValue)) {
69
fail(kind + " refers to unexpected value");
70
}
71
72
}
73
74
private static final void test_weak(WeakReference ref,
75
Object expectedValue,
76
Object unexpectedValue) throws Exception {
77
test(ref, expectedValue, unexpectedValue, "weak");
78
}
79
80
private static final void test_soft(SoftReference ref,
81
Object expectedValue,
82
Object unexpectedValue) throws Exception {
83
test(ref, expectedValue, unexpectedValue, "soft");
84
}
85
86
public static void main(String[] args) throws Exception {
87
var queue = new ReferenceQueue<Object>();
88
89
var obj0 = new Object();
90
var obj1 = new Object();
91
var obj2 = new Object();
92
var obj3 = new Object();
93
94
var pref = new PhantomReference(obj0, queue);
95
var wref = new WeakReference(obj1);
96
var sref = new SoftReference(obj2);
97
98
System.out.println("Warmup");
99
for (int i = 0; i < 10000; i++) {
100
test_phantom(pref, obj0, obj3);
101
test_weak(wref, obj1, obj3);
102
test_soft(sref, obj2, obj3);
103
}
104
105
System.out.println("Testing starts");
106
test_phantom(pref, obj0, obj3);
107
test_weak(wref, obj1, obj3);
108
test_soft(sref, obj2, obj3);
109
110
System.out.println("Cleaning references");
111
pref.clear();
112
wref.clear();
113
sref.clear();
114
115
System.out.println("Testing after cleaning");
116
test_phantom(pref, null, obj3);
117
test_weak(wref, null, obj3);
118
test_soft(sref, null, obj3);
119
}
120
}
121
122