Path: blob/master/test/jdk/java/lang/invoke/8022701/BogoLoader.java
41153 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*/2223import java.io.BufferedInputStream;24import java.io.IOException;25import java.io.InputStream;26import java.util.Map;27import java.util.Set;28import java.util.Vector;29import jdk.internal.org.objectweb.asm.*;3031public class BogoLoader extends ClassLoader {3233static interface VisitorMaker {34ClassVisitor make(ClassVisitor visitor);35}363738/**39* Use this property to verify that the desired classloading is happening.40*/41private final boolean verbose = Boolean.getBoolean("bogoloader.verbose");42/**43* Use this property to disable replacement for testing purposes.44*/45private final boolean noReplace = Boolean.getBoolean("bogoloader.noreplace");4647/**48* Set of class names that should be loaded with this loader.49* Others are loaded with the system class loader, except for those50* that are transformed.51*/52private Set<String> nonSystem;5354/**55* Map from class names to a bytecode transformer factory.56*/57private Map<String, VisitorMaker> replaced;5859/**60* Keep track (not terribly efficiently) of which classes have already61* been loaded by this class loader.62*/63private final Vector<String> history = new Vector<String>();6465private boolean useSystemLoader(String name) {66return ! nonSystem.contains(name) && ! replaced.containsKey(name);67}6869public BogoLoader(Set<String> non_system, Map<String, VisitorMaker> replaced) {70super(Thread.currentThread().getContextClassLoader());71this.nonSystem = non_system;72this.replaced = replaced;73}7475private byte[] readResource(String className) throws IOException {76return readResource(className, "class");77}7879private byte[] readResource(String className, String suffix) throws IOException {80// Note to the unwary -- "/" works on Windows, leave it alone.81String fileName = className.replace('.', '/') + "." + suffix;82InputStream origStream = getResourceAsStream(fileName);83if (origStream == null) {84throw new IOException("Resource not found : " + fileName);85}86BufferedInputStream stream = new java.io.BufferedInputStream(origStream);87byte[] data = new byte[stream.available()];88int how_many = stream.read(data);89// Really ought to deal with the corner cases of stream.available()90return data;91}9293protected byte[] getClass(String name) throws ClassNotFoundException,94IOException {95return readResource(name, "class");96}9798/**99* Loads the named class from the system class loader unless100* the name appears in either replaced or nonSystem.101* nonSystem classes are loaded into this classloader,102* and replaced classes get their content from the specified array103* of bytes (and are also loaded into this classloader).104*/105protected Class<?> loadClass(String name, boolean resolve)106throws ClassNotFoundException {107Class<?> clazz;108109if (history.contains(name)) {110Class<?> c = this.findLoadedClass(name);111return c;112}113if (useSystemLoader(name)) {114clazz = findSystemClass(name);115if (verbose) System.err.println("Loading system class " + name);116} else {117history.add(name);118try {119if (verbose) {120System.err.println("Loading classloader class " + name);121}122byte[] classData = getClass(name);;123boolean expanded = false;124if (!noReplace && replaced.containsKey(name)) {125if (verbose) {126System.err.println("Replacing class " + name);127}128ClassReader cr = new ClassReader(classData);129ClassWriter cw = new ClassWriter(0);130VisitorMaker vm = replaced.get(name);131cr.accept(vm.make(cw), 0);132classData = cw.toByteArray();133}134clazz = defineClass(name, classData, 0, classData.length);135} catch (java.io.EOFException ioe) {136throw new ClassNotFoundException(137"IO Exception in reading class : " + name + " ", ioe);138} catch (ClassFormatError ioe) {139throw new ClassNotFoundException(140"ClassFormatError in reading class file: ", ioe);141} catch (IOException ioe) {142throw new ClassNotFoundException(143"IO Exception in reading class file: ", ioe);144}145}146if (clazz == null) {147throw new ClassNotFoundException(name);148}149if (resolve) {150resolveClass(clazz);151}152return clazz;153}154}155156157