Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/invoke/accessProtectedSuper/BogoLoader.java
47490 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.*;31// Compile with -XDignore.symbol.file=true3233public class BogoLoader extends ClassLoader {3435static interface VisitorMaker {36ClassVisitor make(ClassVisitor visitor);37}383940/**41* Use this property to verify that the desired classloading is happening.42*/43private final boolean verbose = Boolean.getBoolean("bogoloader.verbose");44/**45* Use this property to disable replacement for testing purposes.46*/47private final boolean noReplace = Boolean.getBoolean("bogoloader.noreplace");4849/**50* Set of class names that should be loaded with this loader.51* Others are loaded with the system class loader, except for those52* that are transformed.53*/54private Set<String> nonSystem;5556/**57* Map from class names to a bytecode transformer factory.58*/59private Map<String, VisitorMaker> replaced;6061/**62* Keep track (not terribly efficiently) of which classes have already63* been loaded by this class loader.64*/65private final Vector<String> history = new Vector<String>();6667private boolean useSystemLoader(String name) {68return ! nonSystem.contains(name) && ! replaced.containsKey(name);69}7071public BogoLoader(Set<String> non_system, Map<String, VisitorMaker> replaced) {72super(Thread.currentThread().getContextClassLoader());73this.nonSystem = non_system;74this.replaced = replaced;75}7677private byte[] readResource(String className) throws IOException {78return readResource(className, "class");79}8081private byte[] readResource(String className, String suffix) throws IOException {82// Note to the unwary -- "/" works on Windows, leave it alone.83String fileName = className.replace('.', '/') + "." + suffix;84InputStream origStream = getResourceAsStream(fileName);85if (origStream == null) {86throw new IOException("Resource not found : " + fileName);87}88BufferedInputStream stream = new java.io.BufferedInputStream(origStream);89byte[] data = new byte[stream.available()];90int how_many = stream.read(data);91// Really ought to deal with the corner cases of stream.available()92return data;93}9495protected byte[] getClass(String name) throws ClassNotFoundException,96IOException {97return readResource(name, "class");98}99100/**101* Loads the named class from the system class loader unless102* the name appears in either replaced or nonSystem.103* nonSystem classes are loaded into this classloader,104* and replaced classes get their content from the specified array105* of bytes (and are also loaded into this classloader).106*/107protected Class<?> loadClass(String name, boolean resolve)108throws ClassNotFoundException {109Class<?> clazz;110111if (history.contains(name)) {112Class<?> c = this.findLoadedClass(name);113return c;114}115if (useSystemLoader(name)) {116clazz = findSystemClass(name);117if (verbose) System.err.println("Loading system class " + name);118} else {119history.add(name);120try {121if (verbose) {122System.err.println("Loading classloader class " + name);123}124byte[] classData = getClass(name);;125boolean expanded = false;126if (!noReplace && replaced.containsKey(name)) {127if (verbose) {128System.err.println("Replacing class " + name);129}130ClassReader cr = new ClassReader(classData);131ClassWriter cw = new ClassWriter(0);132VisitorMaker vm = replaced.get(name);133cr.accept(vm.make(cw), 0);134classData = cw.toByteArray();135}136clazz = defineClass(name, classData, 0, classData.length);137} catch (java.io.EOFException ioe) {138throw new ClassNotFoundException(139"IO Exception in reading class : " + name + " ", ioe);140} catch (ClassFormatError ioe) {141throw new ClassNotFoundException(142"ClassFormatError in reading class file: ", ioe);143} catch (IOException ioe) {144throw new ClassNotFoundException(145"IO Exception in reading class file: ", ioe);146}147}148if (clazz == null) {149throw new ClassNotFoundException(name);150}151if (resolve) {152resolveClass(clazz);153}154return clazz;155}156}157158159