Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/runtime/8003720/VictimClassLoader.java
32284 views
1
/*
2
* Copyright (c) 2012, 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 sun.misc.IOUtils;
26
27
public class VictimClassLoader extends ClassLoader {
28
public static long counter = 0;
29
30
private int which = (int) ++counter;
31
32
protected VictimClassLoader() {
33
super(VictimClassLoader.class.getClassLoader());
34
}
35
36
protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
37
Class c;
38
if (!name.endsWith("Victim")) {
39
c = super.loadClass(name, resolve);
40
return c;
41
}
42
43
c = findLoadedClass(name);
44
if (c != null) {
45
return c;
46
}
47
48
byte[] buf = readClassFile(name);
49
c = defineClass(name, buf, 0, buf.length);
50
resolveClass(c);
51
52
if (c.getClassLoader() != this) {
53
throw new AssertionError();
54
}
55
56
Test8003720.println("loaded " + c + "#" + System.identityHashCode(c) + " in " + c.getClassLoader());
57
return c;
58
}
59
60
static byte[] readClassFile(String name) {
61
try {
62
String rname = name.substring(name.lastIndexOf('.') + 1) + ".class";
63
java.net.URL url = VictimClassLoader.class.getResource(rname);
64
Test8003720.println("found " + rname + " = " + url);
65
66
java.net.URLConnection connection = url.openConnection();
67
int contentLength = connection.getContentLength();
68
byte[] buf = readFully(connection.getInputStream(), contentLength);
69
70
return Asmator.fixup(buf);
71
} catch (java.io.IOException ex) {
72
throw new Error(ex);
73
}
74
}
75
76
static byte[] readFully(java.io.InputStream in, int len) throws java.io.IOException {
77
byte[] b = IOUtils.readAllBytes(in);
78
if (len != -1 && b.length != len)
79
throw new java.io.IOException("Expected:" + len + ", actual:" + b.length);
80
return b;
81
}
82
83
public void finalize() {
84
Test8003720.println("Goodbye from " + this);
85
}
86
87
public String toString() {
88
return "VictimClassLoader#" + which;
89
}
90
}
91
92