Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/invoke/8022701/BogoLoader.java
47311 views
/*1* Copyright (c) 2013, 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*/2324import java.io.BufferedInputStream;25import java.io.IOException;26import java.io.InputStream;27import java.util.Map;28import java.util.Set;29import java.util.Vector;30import jdk.internal.org.objectweb.asm.*;3132public class BogoLoader extends ClassLoader {3334static interface VisitorMaker {35ClassVisitor make(ClassVisitor visitor);36}373839/**40* Use this property to verify that the desired classloading is happening.41*/42private final boolean verbose = Boolean.getBoolean("bogoloader.verbose");43/**44* Use this property to disable replacement for testing purposes.45*/46private final boolean noReplace = Boolean.getBoolean("bogoloader.noreplace");4748/**49* Set of class names that should be loaded with this loader.50* Others are loaded with the system class loader, except for those51* that are transformed.52*/53private Set<String> nonSystem;5455/**56* Map from class names to a bytecode transformer factory.57*/58private Map<String, VisitorMaker> replaced;5960/**61* Keep track (not terribly efficiently) of which classes have already62* been loaded by this class loader.63*/64private final Vector<String> history = new Vector<String>();6566private boolean useSystemLoader(String name) {67return ! nonSystem.contains(name) && ! replaced.containsKey(name);68}6970public BogoLoader(Set<String> non_system, Map<String, VisitorMaker> replaced) {71super(Thread.currentThread().getContextClassLoader());72this.nonSystem = non_system;73this.replaced = replaced;74}7576private byte[] readResource(String className) throws IOException {77return readResource(className, "class");78}7980private byte[] readResource(String className, String suffix) throws IOException {81// Note to the unwary -- "/" works on Windows, leave it alone.82String fileName = className.replace('.', '/') + "." + suffix;83InputStream origStream = getResourceAsStream(fileName);84if (origStream == null) {85throw new IOException("Resource not found : " + fileName);86}87BufferedInputStream stream = new java.io.BufferedInputStream(origStream);88byte[] data = new byte[stream.available()];89int how_many = stream.read(data);90// Really ought to deal with the corner cases of stream.available()91return data;92}9394protected byte[] getClass(String name) throws ClassNotFoundException,95IOException {96return readResource(name, "class");97}9899/**100* Loads the named class from the system class loader unless101* the name appears in either replaced or nonSystem.102* nonSystem classes are loaded into this classloader,103* and replaced classes get their content from the specified array104* of bytes (and are also loaded into this classloader).105*/106protected Class<?> loadClass(String name, boolean resolve)107throws ClassNotFoundException {108Class<?> clazz;109110if (history.contains(name)) {111Class<?> c = this.findLoadedClass(name);112return c;113}114if (useSystemLoader(name)) {115clazz = findSystemClass(name);116if (verbose) System.err.println("Loading system class " + name);117} else {118history.add(name);119try {120if (verbose) {121System.err.println("Loading classloader class " + name);122}123byte[] classData = getClass(name);;124boolean expanded = false;125if (!noReplace && replaced.containsKey(name)) {126if (verbose) {127System.err.println("Replacing class " + name);128}129ClassReader cr = new ClassReader(classData);130ClassWriter cw = new ClassWriter(0);131VisitorMaker vm = replaced.get(name);132cr.accept(vm.make(cw), 0);133classData = cw.toByteArray();134}135clazz = defineClass(name, classData, 0, classData.length);136} catch (java.io.EOFException ioe) {137throw new ClassNotFoundException(138"IO Exception in reading class : " + name + " ", ioe);139} catch (ClassFormatError ioe) {140throw new ClassNotFoundException(141"ClassFormatError in reading class file: ", ioe);142} catch (IOException ioe) {143throw new ClassNotFoundException(144"IO Exception in reading class file: ", ioe);145}146}147if (clazz == null) {148throw new ClassNotFoundException(name);149}150if (resolve) {151resolveClass(clazz);152}153return clazz;154}155}156157158