1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
/*
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package nsk.share.classload;
import java.io.*;
import java.util.*;
import nsk.share.*;
/**
* Classloader that generates classes on the fly.
*
* This classloader can load classes with name starting with 'Class'.
* It will use nsk.share.classload.TemplateClass as template and will
* replace class name in the bytecode of template class. It can be used
* for example to detect memory leaks in class loading or to quickly
* fill PermGen.
*/
public class GeneratingClassLoader extends ClassLoader {
public static final String DEFAULT_CLASSNAME = TemplateClass.class.getName();
public static final String PREFIX = "Class";
private final String [] classPath;
private byte[] bytecode;
private int[] offsets;
private final String encoding = "UTF8";
private final String templateClassName;
private final byte[] templateClassNameBytes;
/**
* Create generating class loader that will use class file
* for given class from classpath as template.
*/
public GeneratingClassLoader(String templateClassName) {
this.templateClassName = templateClassName;
classPath = System.getProperty("java.class.path").split(File.pathSeparator);
try {
templateClassNameBytes = templateClassName.getBytes(encoding);
} catch (UnsupportedEncodingException e) {
throw new TestBug(e);
}
}
/**
* Create generating class loader that will use class file
* for nsk.share.classload.TemplateClass as template.
*/
public GeneratingClassLoader() {
this("nsk.share.classload.TemplateClass");
}
public int getNameLength() {
return templateClassName.length();
}
public String getPrefix() {
return PREFIX;
}
public String getClassName(int number) {
StringBuffer sb = new StringBuffer();
sb.append(PREFIX);
sb.append(number);
int n = templateClassName.length() - sb.length();
for (int i = 0; i < n; ++i)
sb.append("_");
return sb.toString();
}
public synchronized Class loadClass(String name) throws ClassNotFoundException {
return loadClass(name, false);
}
public synchronized Class loadClass(String name, boolean resolve)
throws ClassNotFoundException {
Class c = findLoadedClass(name);
if (c != null)
return c;
if (!name.startsWith(PREFIX))
return super.loadClass(name, resolve);
if (name.length() != templateClassName.length())
throw new ClassNotFoundException("Only can load classes with name.length() = " + getNameLength() + " got: '" + name + "' length: " + name.length());
byte[] bytecode = getPatchedByteCode(name);
c = defineClass(name, bytecode, 0, bytecode.length);
if (resolve)
resolveClass(c);
return c;
}
private byte[] getPatchedByteCode(String name) throws ClassNotFoundException {
try {
byte[] bytecode = getByteCode();
String fname = name.replace(".", File.separator);
byte[] replaceBytes = fname.getBytes(encoding);
for (int offset : offsets) {
for (int i = 0; i < replaceBytes.length; ++i)
bytecode[offset + i] = replaceBytes[i];
}
return bytecode;
} catch (UnsupportedEncodingException e) {
throw new TestBug(e);
}
}
private byte[] getByteCode() throws ClassNotFoundException {
if (bytecode == null)
readByteCode();
if (offsets == null) {
getOffsets(bytecode);
if (offsets == null)
throw new TestBug("Class name not found in template class file");
}
return (byte[]) bytecode.clone();
}
private void readByteCode() throws ClassNotFoundException {
String fname = templateClassName.replace(".", File.separator) + ".class";
File target = null;
for (int i = 0; i < classPath.length; ++i) {
target = new File(classPath[i] + File.separator + fname);
if (target.exists())
break;
}
if (target == null || !target.exists())
throw new ClassNotFoundException("File not found: " + target);
try {
bytecode = FileUtils.readFile(target);
} catch (IOException e) {
throw new ClassNotFoundException(templateClassName, e);
}
}
private void getOffsets(byte[] bytecode) {
List<Integer> offsets = new ArrayList<Integer>();
if (this.offsets == null) {
String pname = templateClassName.replace(".", "/");
try {
byte[] pnameb = pname.getBytes(encoding);
int i = 0;
while (true) {
while (i < bytecode.length) {
int j = 0;
while (j < pnameb.length && bytecode[i + j] == pnameb[j])
++j;
if (j == pnameb.length)
break;
i++;
}
if (i == bytecode.length)
break;
offsets.add(new Integer(i));
i++;
}
} catch (UnsupportedEncodingException e) {
throw new TestBug(e);
}
this.offsets = new int[offsets.size()];
for (int i = 0; i < offsets.size(); ++i)
this.offsets[i] = offsets.get(i).intValue();
}
}
}
|