Path: blob/master/test/jdk/java/lang/constant/DynamicConstantDescTest.java
41149 views
/*1* Copyright (c) 2021, 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*/2223import java.lang.constant.ClassDesc;24import java.lang.constant.ConstantDesc;25import java.lang.constant.DirectMethodHandleDesc;26import java.lang.constant.DynamicConstantDesc;27import java.lang.constant.MethodHandleDesc;28import java.lang.constant.MethodTypeDesc;29import java.lang.invoke.MethodHandles;30import java.util.ArrayList;31import java.util.List;32import java.util.concurrent.Callable;33import java.util.concurrent.CountDownLatch;34import java.util.concurrent.ExecutorService;35import java.util.concurrent.Executors;36import java.util.concurrent.Future;3738/**39* @test40* @bug 826310841* @summary Verify that concurrent classloading of java.lang.constant.DynamicConstantDesc and42* java.lang.constant.ConstantDescs doesn't lead to a deadlock43* @run main/othervm DynamicConstantDescTest44* @run main/othervm DynamicConstantDescTest45* @run main/othervm DynamicConstantDescTest46* @run main/othervm DynamicConstantDescTest47* @run main/othervm DynamicConstantDescTest48*/49// Implementation note: This test cannot use testng, since by the time this test gets a chance50// to trigger a concurrent classloading of the classes it's interested in, the testng infrastructure51// would already have loaded those classes in a single main thread.52public class DynamicConstantDescTest {5354/**55* Loads {@code java.lang.constant.DynamicConstantDesc} and {@code java.lang.constant.ConstantDescs}56* and invokes {@code java.lang.constant.DynamicConstantDesc#ofCanonical()} concurrently in a thread57* of their own and expects the classloading of both those classes58* to succeed.59*/60public static void main(final String[] args) throws Exception {61final CountDownLatch taskTriggerLatch = new CountDownLatch(4);62final List<Callable<?>> tasks = new ArrayList<>();63// a bunch of tasks - some doing just Class.forName and some64// invoking DynamicConstantDesc.ofCanonical65tasks.add(new Task("java.lang.constant.DynamicConstantDesc", taskTriggerLatch));66tasks.add(new InvokeOfCanonical(taskTriggerLatch));67tasks.add(new Task("java.lang.constant.ConstantDescs", taskTriggerLatch));68tasks.add(new InvokeOfCanonical(taskTriggerLatch));69final ExecutorService executor = Executors.newFixedThreadPool(tasks.size());70try {71final Future<?>[] results = new Future[tasks.size()];72// submit73int i = 0;74for (final Callable<?> task : tasks) {75results[i++] = executor.submit(task);76}77// wait for completion78for (i = 0; i < tasks.size(); i++) {79results[i].get();80}81} finally {82executor.shutdownNow();83}84}8586private static class Task implements Callable<Class<?>> {87private final String className;88private final CountDownLatch latch;8990private Task(final String className, final CountDownLatch latch) {91this.className = className;92this.latch = latch;93}9495@Override96public Class<?> call() {97System.out.println(Thread.currentThread().getName() + " loading " + this.className);98try {99// let the other tasks know we are ready to trigger our work100latch.countDown();101// wait for the other task to let us know they are ready to trigger their work too102latch.await();103return Class.forName(this.className);104} catch (Exception e) {105throw new RuntimeException(e);106}107}108}109110enum MyEnum {A, B}111112private static class InvokeOfCanonical implements Callable<Object> {113private final CountDownLatch latch;114115private InvokeOfCanonical(final CountDownLatch latch) {116this.latch = latch;117}118119@Override120public Object call() {121System.out.println(Thread.currentThread().getName()122+ " calling DynamicConstantDesc.ofCanonical()");123try {124// let the other tasks know we are ready to trigger our work125latch.countDown();126// wait for the other task to let us know they are ready to trigger their work too127latch.await();128ConstantDesc desc = DynamicConstantDesc.ofCanonical(boostrapMethodForEnumConstant(),129"A", ClassDesc.of("DynamicConstantDescTest").nested("MyEnum"),130new ConstantDesc[0]);131if (desc == null) {132throw new Exception("DynamicConstantDesc.ofCanonical unexpectedly returned null");133}134if (!MyEnum.A.equals(desc.resolveConstantDesc(MethodHandles.lookup()))) {135throw new Exception("DynamicConstantDesc.ofCanonical returned unexpected result " + desc);136}137return desc;138} catch (Exception e) {139throw new RuntimeException(e);140}141}142143private static DirectMethodHandleDesc boostrapMethodForEnumConstant() {144ClassDesc[] args = {ClassDesc.of("java.lang.invoke.MethodHandles").nested("Lookup"),145ClassDesc.of("java.lang.String"),146ClassDesc.of("java.lang.Class")};147return MethodHandleDesc.ofMethod(java.lang.constant.DirectMethodHandleDesc.Kind.STATIC,148ClassDesc.of("java.lang.invoke.ConstantBootstraps"),149"enumConstant", MethodTypeDesc.of(ClassDesc.of("java.lang.Enum"), new ClassDesc[0])150.insertParameterTypes(0, args));151}152153}154155}156157