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 8188055
27  * @summary Basic functional test of Reference.refersTo.
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 ReferenceRefersTo {
fail(String msg)37     private static final void fail(String msg) throws Exception {
38         throw new RuntimeException(msg);
39     }
40 
test(T ref, Object expectedValue, Object unexpectedValue, String kind)41     private static final <T extends Reference> void test(T ref,
42                                                          Object expectedValue,
43                                                          Object unexpectedValue,
44                                                          String kind) throws Exception {
45         if ((expectedValue != null) && ref.refersTo(null)) {
46             fail(kind + "refers to null");
47         }
48         if (!ref.refersTo(expectedValue)) {
49             fail(kind + " doesn't refer to expected value");
50         }
51         if (ref.refersTo(unexpectedValue)) {
52             fail(kind + " refers to unexpected value");
53         }
54     }
55 
main(String[] args)56     public static void main(String[] args) throws Exception {
57         var queue = new ReferenceQueue<Object>();
58 
59         var obj0 = new Object();
60         var obj1 = new Object();
61         var obj2 = new Object();
62         var obj3 = new Object();
63 
64         var pref = new PhantomReference(obj0, queue);
65         var wref = new WeakReference(obj1);
66         var sref = new SoftReference(obj2);
67 
68         test(pref, obj0, obj3, "phantom");
69         test(wref, obj1, obj3, "weak");
70         test(sref, obj2, obj3, "soft");
71 
72         pref.clear();
73         wref.clear();
74         sref.clear();
75 
76         test(pref, null, obj3, "phantom");
77         test(wref, null, obj3, "weak");
78         test(sref, null, obj3, "soft");
79     }
80 }
81