Path: blob/master/test/hotspot/jtreg/runtime/LoaderConstraints/ldrCnstrFldMsg/pkg/ClassLoaderForChildGrandFoo.java
41155 views
/*1* Copyright (c) 2018, 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*22*/2324package pkg;2526import java.util.*;27import java.io.*;2829// This class loader loads Foo, Child, and Grand.30public class ClassLoaderForChildGrandFoo extends ClassLoader {3132ClassLoader l2 = null;3334private final Set<String> names = new HashSet<>();3536public ClassLoaderForChildGrandFoo(String... names) {37for (String n : names) this.names.add(n);38}3940protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {41// Create class loader l2 to load Parent and Foo. And, pass this class42// loader to l2 so l2 can use it to load Grand.43if (name.contains("Parent") && l2 == null) {44l2 = new ClassLoaderForParentFoo(this, "pkg.Foo", "pkg.Grand", "pkg.Parent");45Class<?> b = l2.loadClass("pkg.Parent");46return b;47}48if (!names.contains(name)) return super.loadClass(name, resolve);49Class<?> result = findLoadedClass(name);5051if (result == null) {52String filename = name.replace('.', '/') + ".class";53try (InputStream data = getResourceAsStream(filename)) {54if (data == null) throw new ClassNotFoundException();55try (ByteArrayOutputStream buffer = new ByteArrayOutputStream()) {56int b;57do {58b = data.read();59if (b >= 0) buffer.write(b);60} while (b >= 0);61byte[] bytes = buffer.toByteArray();62result = defineClass(name, bytes, 0, bytes.length);63}64} catch (IOException e) {65throw new ClassNotFoundException("Error reading class file", e);66}67}68if (resolve) resolveClass(result);69return result;70}7172}737475