1 /* 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 import java.io.BufferedInputStream; 26 import java.io.IOException; 27 import java.io.InputStream; 28 import java.util.Map; 29 import java.util.Set; 30 import java.util.Vector; 31 import jdk.internal.org.objectweb.asm.*; 32 // Compile with -XDignore.symbol.file=true 33 34 public class BogoLoader extends ClassLoader { 35 36 static interface VisitorMaker { 37 ClassVisitor make(ClassVisitor visitor); 38 } 39 40 41 /** 42 * Use this property to verify that the desired classloading is happening. 43 */ 44 private final boolean verbose = Boolean.getBoolean("bogoloader.verbose"); 45 /** 46 * Use this property to disable replacement for testing purposes. 47 */ 48 private final boolean noReplace = Boolean.getBoolean("bogoloader.noreplace"); 49 50 /** 51 * Set of class names that should be loaded with this loader. 52 * Others are loaded with the system class loader, except for those 53 * that are transformed. 54 */ 55 private Set<String> nonSystem; 56 57 /** 58 * Map from class names to a bytecode transformer factory. 59 */ 60 private Map<String, VisitorMaker> replaced; 61 62 /** 63 * Keep track (not terribly efficiently) of which classes have already 64 * been loaded by this class loader. 65 */ 66 private final Vector<String> history = new Vector<String>(); 67 68 private boolean useSystemLoader(String name) { 69 return ! nonSystem.contains(name) && ! replaced.containsKey(name); 70 } 71 72 public BogoLoader(Set<String> non_system, Map<String, VisitorMaker> replaced) { 73 super(Thread.currentThread().getContextClassLoader()); 74 this.nonSystem = non_system; 75 this.replaced = replaced; 76 } 77 78 private byte[] readResource(String className) throws IOException { 79 return readResource(className, "class"); 80 } 81 82 private byte[] readResource(String className, String suffix) throws IOException { 83 // Note to the unwary -- "/" works on Windows, leave it alone. 84 String fileName = className.replace('.', '/') + "." + suffix; 85 InputStream origStream = getResourceAsStream(fileName); 86 if (origStream == null) { 87 throw new IOException("Resource not found : " + fileName); 88 } 89 BufferedInputStream stream = new java.io.BufferedInputStream(origStream); 90 byte[] data = new byte[stream.available()]; 91 int how_many = stream.read(data); 92 // Really ought to deal with the corner cases of stream.available() 93 return data; 94 } 95 96 protected byte[] getClass(String name) throws ClassNotFoundException, 97 IOException { 98 return readResource(name, "class"); 99 } 100 101 /** 102 * Loads the named class from the system class loader unless 103 * the name appears in either replaced or nonSystem. 104 * nonSystem classes are loaded into this classloader, 105 * and replaced classes get their content from the specified array 106 * of bytes (and are also loaded into this classloader). 107 */ 108 protected Class<?> loadClass(String name, boolean resolve) 109 throws ClassNotFoundException { 110 Class<?> clazz; 111 112 if (history.contains(name)) { 113 Class<?> c = this.findLoadedClass(name); 114 return c; 115 } 116 if (useSystemLoader(name)) { 117 clazz = findSystemClass(name); 118 if (verbose) System.err.println("Loading system class " + name); 119 } else { 120 history.add(name); 121 try { 122 if (verbose) { 123 System.err.println("Loading classloader class " + name); 124 } 125 byte[] classData = getClass(name);; 126 boolean expanded = false; 127 if (!noReplace && replaced.containsKey(name)) { 128 if (verbose) { 129 System.err.println("Replacing class " + name); 130 } 131 ClassReader cr = new ClassReader(classData); 132 ClassWriter cw = new ClassWriter(0); 133 VisitorMaker vm = replaced.get(name); 134 cr.accept(vm.make(cw), 0); 135 classData = cw.toByteArray(); 136 } 137 clazz = defineClass(name, classData, 0, classData.length); 138 } catch (java.io.EOFException ioe) { 139 throw new ClassNotFoundException( 140 "IO Exception in reading class : " + name + " ", ioe); 141 } catch (ClassFormatError ioe) { 142 throw new ClassNotFoundException( 143 "ClassFormatError in reading class file: ", ioe); 144 } catch (IOException ioe) { 145 throw new ClassNotFoundException( 146 "IO Exception in reading class file: ", ioe); 147 } 148 } 149 if (clazz == null) { 150 throw new ClassNotFoundException(name); 151 } 152 if (resolve) { 153 resolveClass(clazz); 154 } 155 return clazz; 156 } 157 }