Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/TestReferenceRefersTo.java
41149 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 825637726* @summary Based on test/jdk/java/lang/ref/ReferenceRefersTo.java.27*/2829import java.lang.ref.Reference;30import java.lang.ref.ReferenceQueue;31import java.lang.ref.PhantomReference;32import java.lang.ref.SoftReference;33import java.lang.ref.WeakReference;3435public class TestReferenceRefersTo {36private static final void fail(String msg) throws Exception {37throw new RuntimeException(msg);38}3940// Test java.lang.ref.Reference::refersTo0 intrinsic.41private static final void test(Reference ref,42Object expectedValue,43Object unexpectedValue,44String kind) throws Exception {45if ((expectedValue != null) && ref.refersTo(null)) {46fail(kind + " refers to null");47}48if (!ref.refersTo(expectedValue)) {49fail(kind + " doesn't refer to expected value");50}51if (ref.refersTo(unexpectedValue)) {52fail(kind + " refers to unexpected value");53}54}5556// Test java.lang.ref.PhantomReference::refersTo0 intrinsic.57private static final void test_phantom(PhantomReference ref,58Object expectedValue,59Object unexpectedValue) throws Exception {60String kind = "phantom";61if ((expectedValue != null) && ref.refersTo(null)) {62fail(kind + " refers to null");63}64if (!ref.refersTo(expectedValue)) {65fail(kind + " doesn't refer to expected value");66}67if (ref.refersTo(unexpectedValue)) {68fail(kind + " refers to unexpected value");69}7071}7273private static final void test_weak(WeakReference ref,74Object expectedValue,75Object unexpectedValue) throws Exception {76test(ref, expectedValue, unexpectedValue, "weak");77}7879private static final void test_soft(SoftReference ref,80Object expectedValue,81Object unexpectedValue) throws Exception {82test(ref, expectedValue, unexpectedValue, "soft");83}8485public static void main(String[] args) throws Exception {86var queue = new ReferenceQueue<Object>();8788var obj0 = new Object();89var obj1 = new Object();90var obj2 = new Object();91var obj3 = new Object();9293var pref = new PhantomReference(obj0, queue);94var wref = new WeakReference(obj1);95var sref = new SoftReference(obj2);9697System.out.println("Warmup");98for (int i = 0; i < 10000; i++) {99test_phantom(pref, obj0, obj3);100test_weak(wref, obj1, obj3);101test_soft(sref, obj2, obj3);102}103104System.out.println("Testing starts");105test_phantom(pref, obj0, obj3);106test_weak(wref, obj1, obj3);107test_soft(sref, obj2, obj3);108109System.out.println("Cleaning references");110pref.clear();111wref.clear();112sref.clear();113114System.out.println("Testing after cleaning");115test_phantom(pref, null, obj3);116test_weak(wref, null, obj3);117test_soft(sref, null, obj3);118}119}120121122