Path: blob/master/test/hotspot/jtreg/runtime/LoaderConstraints/duplicateParentLE/PreemptingChildClassLoader.java
41153 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*/2223import java.util.*;24import java.io.*;2526public class PreemptingChildClassLoader extends ParentClassLoader {2728private static ClassLoader p = new ParentClassLoader();29private final Set<String> names = new HashSet<>();30boolean checkLoaded = true;3132public PreemptingChildClassLoader(String... names) {33for (String n : names) this.names.add(n);34}3536public PreemptingChildClassLoader(String name, String[] names) {37super(name, p);38for (String n : names) this.names.add(n);39}4041public PreemptingChildClassLoader(String name, String[] names, boolean cL) {42super(name, p);43for (String n : names) this.names.add(n);44checkLoaded = cL;45}4647protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {48if (!names.contains(name)) return super.loadClass(name, resolve);49Class<?> result = checkLoaded ? findLoadedClass(name) : null;50if (result == null) {51String filename = name.replace('.', '/') + ".class";52try (InputStream data = getResourceAsStream(filename)) {53if (data == null) throw new ClassNotFoundException();54try (ByteArrayOutputStream buffer = new ByteArrayOutputStream()) {55int b;56do {57b = data.read();58if (b >= 0) buffer.write(b);59} while (b >= 0);60byte[] bytes = buffer.toByteArray();61result = defineClass(name, bytes, 0, bytes.length);62}63} catch (IOException e) {64throw new ClassNotFoundException("Error reading class file", e);65}66}67if (resolve) resolveClass(result);68return result;69}7071}727374