Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/classload/GeneratingClassLoader.java
41161 views
1
/*
2
* Copyright (c) 2007, 2021, 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
package nsk.share.classload;
25
26
import java.io.*;
27
import java.util.*;
28
import nsk.share.*;
29
30
/**
31
* Classloader that generates classes on the fly.
32
*
33
* This classloader can load classes with name starting with 'Class'.
34
* It will use nsk.share.classload.TemplateClass as template and will
35
* replace class name in the bytecode of template class. It can be used
36
* for example to detect memory leaks in class loading or to quickly
37
* fill PermGen.
38
*/
39
public class GeneratingClassLoader extends ClassLoader {
40
public static final String DEFAULT_CLASSNAME = TemplateClass.class.getName();
41
public static final String PREFIX = "Class";
42
43
private final String [] classPath;
44
private byte[] bytecode;
45
private int[] offsets;
46
private final String encoding = "UTF8";
47
private final String templateClassName;
48
private final byte[] templateClassNameBytes;
49
50
/**
51
* Create generating class loader that will use class file
52
* for given class from classpath as template.
53
*/
54
public GeneratingClassLoader(String templateClassName) {
55
this.templateClassName = templateClassName;
56
classPath = System.getProperty("java.class.path").split(File.pathSeparator);
57
try {
58
templateClassNameBytes = templateClassName.getBytes(encoding);
59
} catch (UnsupportedEncodingException e) {
60
throw new TestBug(e);
61
}
62
}
63
64
/**
65
* Create generating class loader that will use class file
66
* for nsk.share.classload.TemplateClass as template.
67
*/
68
public GeneratingClassLoader() {
69
this("nsk.share.classload.TemplateClass");
70
}
71
72
public int getNameLength() {
73
return templateClassName.length();
74
}
75
76
public String getPrefix() {
77
return PREFIX;
78
}
79
80
public String getClassName(int number) {
81
StringBuffer sb = new StringBuffer();
82
sb.append(PREFIX);
83
sb.append(number);
84
int n = templateClassName.length() - sb.length();
85
for (int i = 0; i < n; ++i)
86
sb.append("_");
87
return sb.toString();
88
}
89
90
public synchronized Class loadClass(String name) throws ClassNotFoundException {
91
return loadClass(name, false);
92
}
93
94
public synchronized Class loadClass(String name, boolean resolve)
95
throws ClassNotFoundException {
96
Class c = findLoadedClass(name);
97
if (c != null)
98
return c;
99
if (!name.startsWith(PREFIX))
100
return super.loadClass(name, resolve);
101
if (name.length() != templateClassName.length())
102
throw new ClassNotFoundException("Only can load classes with name.length() = " + getNameLength() + " got: '" + name + "' length: " + name.length());
103
byte[] bytecode = getPatchedByteCode(name);
104
c = defineClass(name, bytecode, 0, bytecode.length);
105
if (resolve)
106
resolveClass(c);
107
return c;
108
}
109
110
private byte[] getPatchedByteCode(String name) throws ClassNotFoundException {
111
try {
112
byte[] bytecode = getByteCode();
113
String fname = name.replace(".", File.separator);
114
byte[] replaceBytes = fname.getBytes(encoding);
115
for (int offset : offsets) {
116
for (int i = 0; i < replaceBytes.length; ++i)
117
bytecode[offset + i] = replaceBytes[i];
118
}
119
return bytecode;
120
} catch (UnsupportedEncodingException e) {
121
throw new TestBug(e);
122
}
123
}
124
125
private byte[] getByteCode() throws ClassNotFoundException {
126
if (bytecode == null)
127
readByteCode();
128
if (offsets == null) {
129
getOffsets(bytecode);
130
if (offsets == null)
131
throw new TestBug("Class name not found in template class file");
132
}
133
return (byte[]) bytecode.clone();
134
}
135
136
private void readByteCode() throws ClassNotFoundException {
137
String fname = templateClassName.replace(".", File.separator) + ".class";
138
File target = null;
139
for (int i = 0; i < classPath.length; ++i) {
140
target = new File(classPath[i] + File.separator + fname);
141
if (target.exists())
142
break;
143
}
144
145
if (target == null || !target.exists())
146
throw new ClassNotFoundException("File not found: " + target);
147
try {
148
bytecode = FileUtils.readFile(target);
149
} catch (IOException e) {
150
throw new ClassNotFoundException(templateClassName, e);
151
}
152
}
153
154
private void getOffsets(byte[] bytecode) {
155
List<Integer> offsets = new ArrayList<Integer>();
156
if (this.offsets == null) {
157
String pname = templateClassName.replace(".", "/");
158
try {
159
byte[] pnameb = pname.getBytes(encoding);
160
int i = 0;
161
while (true) {
162
while (i < bytecode.length) {
163
int j = 0;
164
while (j < pnameb.length && bytecode[i + j] == pnameb[j])
165
++j;
166
if (j == pnameb.length)
167
break;
168
i++;
169
}
170
if (i == bytecode.length)
171
break;
172
offsets.add(Integer.valueOf(i));
173
i++;
174
}
175
} catch (UnsupportedEncodingException e) {
176
throw new TestBug(e);
177
}
178
this.offsets = new int[offsets.size()];
179
for (int i = 0; i < offsets.size(); ++i)
180
this.offsets[i] = offsets.get(i).intValue();
181
}
182
}
183
}
184
185