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