1 /* 2 * Copyright (c) 2015, 2019, 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.*; 26 import java.lang.invoke.MethodHandles; 27 import java.lang.invoke.MethodHandles.Lookup; 28 import static java.lang.invoke.MethodHandles.*; 29 import java.lang.reflect.*; 30 import java.util.jar.*; 31 32 public class Util { 33 /** 34 * Invoke the lookup.defineClass() class method to define the class stored in clsFile, 35 * with the following modification: 36 * <ul> 37 * <li> All ASCII strings in the class file bytes that matches fromString will be replaced with toString. 38 * NOTE: the two strings must be the exact same length. 39 * </ul> 40 */ 41 public static Class<?> defineModifiedClass(ClassLoader loader, File clsFile, String fromString, String toString) 42 throws FileNotFoundException, IOException, NoSuchMethodException, IllegalAccessException, 43 InvocationTargetException, ClassNotFoundException 44 { 45 try (DataInputStream dis = new DataInputStream(new FileInputStream(clsFile))) { 46 byte[] buff = new byte[(int)clsFile.length()]; 47 dis.readFully(buff); 48 replace(buff, fromString, toString); 49 50 System.out.println("Loading from: " + clsFile + " (" + buff.length + " bytes)"); 51 52 53 // We directly call into Lookup.defineClass() to define the "Super" class. Also, 54 // rewrite its classfile so that it returns ___yyy___ instead of ___xxx___. Changing the 55 // classfile will guarantee that this class will NOT be loaded from the CDS archive. 56 Class<?> target = Class.forName("Hello", false, loader); 57 Lookup lookup = privateLookupIn(target, lookup()); 58 Class<?> cls = lookup.defineClass(buff); 59 System.out.println("Loaded : " + cls); 60 61 return cls; 62 } 63 } 64 65 /** 66 * @return the number of occurrences of the <code>from</code> string that 67 * have been replaced. 68 */ 69 public static int replace(byte buff[], String from, String to) { 70 if (to.length() != from.length()) { 71 throw new RuntimeException("bad strings"); 72 } 73 byte f[] = asciibytes(from); 74 byte t[] = asciibytes(to); 75 byte f0 = f[0]; 76 77 int numReplaced = 0; 78 int max = buff.length - f.length; 79 for (int i=0; i<max; ) { 80 if (buff[i] == f0 && replace(buff, f, t, i)) { 81 i += f.length; 82 numReplaced ++; 83 } else { 84 i++; 85 } 86 } 87 return numReplaced; 88 } 89 90 public static boolean replace(byte buff[], byte f[], byte t[], int i) { 91 for (int x=0; x<f.length; x++) { 92 if (buff[x+i] != f[x]) { 93 return false; 94 } 95 } 96 for (int x=0; x<f.length; x++) { 97 buff[x+i] = t[x]; 98 } 99 return true; 100 } 101 102 static byte[] asciibytes(String s) { 103 byte b[] = new byte[s.length()]; 104 for (int i=0; i<b.length; i++) { 105 b[i] = (byte)s.charAt(i); 106 } 107 return b; 108 } 109 110 public static Class defineClassFromJAR(ClassLoader loader, File jarFile, String className) 111 throws FileNotFoundException, IOException, NoSuchMethodException, IllegalAccessException, 112 InvocationTargetException, ClassNotFoundException { 113 return defineClassFromJAR(loader, jarFile, className, null, null); 114 } 115 116 /** 117 * Invoke the lookup.defineClass() class method to define the named class stored in a JAR file. 118 * 119 * If a class exists both in the classpath, as well as in the list of URLs of a URLClassLoader, 120 * by default, the URLClassLoader will not define the class, and instead will delegate to the 121 * app loader. This method is an easy way to force the class to be defined by the URLClassLoader. 122 * 123 * Optionally, you can modify the contents of the classfile buffer. See comments in 124 * defineModifiedClass. 125 */ 126 public static Class<?> defineClassFromJAR(ClassLoader loader, File jarFile, String className, 127 String fromString, String toString) 128 throws FileNotFoundException, IOException, NoSuchMethodException, IllegalAccessException, 129 InvocationTargetException, ClassNotFoundException 130 { 131 byte[] buff = getClassFileFromJar(jarFile, className); 132 133 if (fromString != null) { 134 replace(buff, fromString, toString); 135 } 136 137 //System.out.println("Loading from: " + ent + " (" + buff.length + " bytes)"); 138 139 Class<?> target = Class.forName("Hello", false, loader); 140 Lookup lookup = privateLookupIn(target, lookup()); 141 Class<?> cls = lookup.defineClass(buff); 142 143 //System.out.println("Loaded : " + cls); 144 return cls; 145 } 146 147 public static byte[] getClassFileFromJar(File jarFile, String className) throws FileNotFoundException, IOException { 148 JarFile jf = new JarFile(jarFile); 149 JarEntry ent = jf.getJarEntry(className.replace('.', '/') + ".class"); 150 151 try (DataInputStream dis = new DataInputStream(jf.getInputStream(ent))) { 152 byte[] buff = new byte[(int)ent.getSize()]; 153 dis.readFully(buff); 154 return buff; 155 } 156 } 157 158 }