Path: blob/master/test/hotspot/jtreg/gc/metaspace/G1AddMetaspaceDependency.java
40942 views
/*1* Copyright (c) 2013, 2019, 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*/2223package gc.metaspace;2425/*26* @test G1AddMetaspaceDependency27* @bug 801019628* @requires vm.gc.G129* @library /30* @summary Checks that we don't get locking problems when adding metaspace dependencies with the G1 update buffer monitor31* @run main/othervm -XX:+UseG1GC -XX:G1UpdateBufferSize=1 gc.metaspace.G1AddMetaspaceDependency32*/3334import java.io.InputStream;3536public class G1AddMetaspaceDependency {3738static byte[] getClassBytes(String name) {39byte[] b = null;40try (InputStream is = ClassLoader.getSystemResourceAsStream(name)) {41byte[] tmp = new byte[is.available()];42is.read(tmp);43b = tmp;44} finally {45if (b == null) {46throw new RuntimeException("Unable to load class file");47}48return b;49}50}5152static final String a_name = A.class.getName();53static final String b_name = B.class.getName();5455public static void main(String... args) throws Exception {56final byte[] a_bytes = getClassBytes(a_name.replace('.', '/') + ".class");57final byte[] b_bytes = getClassBytes(b_name.replace('.', '/') + ".class");5859for (int i = 0; i < 1000; i += 1) {60runTest(a_bytes, b_bytes);61}62}6364static class Loader extends ClassLoader {65private final String myClass;66private final byte[] myBytes;67private final String friendClass;68private final ClassLoader friendLoader;6970Loader(String myClass, byte[] myBytes,71String friendClass, ClassLoader friendLoader) {72this.myClass = myClass;73this.myBytes = myBytes;74this.friendClass = friendClass;75this.friendLoader = friendLoader;76}7778Loader(String myClass, byte[] myBytes) {79this(myClass, myBytes, null, null);80}8182@Override83public Class<?> loadClass(String name) throws ClassNotFoundException {84Class<?> c = findLoadedClass(name);85if (c != null) {86return c;87}8889if (name.equals(friendClass)) {90return friendLoader.loadClass(name);91}9293if (name.equals(myClass)) {94c = defineClass(name, myBytes, 0, myBytes.length);95resolveClass(c);96return c;97}9899return findSystemClass(name);100}101102}103104private static void runTest(final byte[] a_bytes, final byte[] b_bytes) throws Exception {105Loader a_loader = new Loader(a_name, a_bytes);106Loader b_loader = new Loader(b_name, b_bytes, a_name, a_loader);107Loader c_loader = new Loader(b_name, b_bytes, a_name, a_loader);108Loader d_loader = new Loader(b_name, b_bytes, a_name, a_loader);109Loader e_loader = new Loader(b_name, b_bytes, a_name, a_loader);110Loader f_loader = new Loader(b_name, b_bytes, a_name, a_loader);111Loader g_loader = new Loader(b_name, b_bytes, a_name, a_loader);112113b_loader.loadClass(b_name);114c_loader.loadClass(b_name);115d_loader.loadClass(b_name);116e_loader.loadClass(b_name);117f_loader.loadClass(b_name);118g_loader.loadClass(b_name);119}120public class A {121}122class B extends A {123}124}125126127