Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/runtime/LoaderConstraints/duplicateParentLE/PreemptingChildClassLoader.java
41153 views
1
/*
2
* Copyright (c) 2018, 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.util.*;
25
import java.io.*;
26
27
public class PreemptingChildClassLoader extends ParentClassLoader {
28
29
private static ClassLoader p = new ParentClassLoader();
30
private final Set<String> names = new HashSet<>();
31
boolean checkLoaded = true;
32
33
public PreemptingChildClassLoader(String... names) {
34
for (String n : names) this.names.add(n);
35
}
36
37
public PreemptingChildClassLoader(String name, String[] names) {
38
super(name, p);
39
for (String n : names) this.names.add(n);
40
}
41
42
public PreemptingChildClassLoader(String name, String[] names, boolean cL) {
43
super(name, p);
44
for (String n : names) this.names.add(n);
45
checkLoaded = cL;
46
}
47
48
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
49
if (!names.contains(name)) return super.loadClass(name, resolve);
50
Class<?> result = checkLoaded ? findLoadedClass(name) : null;
51
if (result == null) {
52
String filename = name.replace('.', '/') + ".class";
53
try (InputStream data = getResourceAsStream(filename)) {
54
if (data == null) throw new ClassNotFoundException();
55
try (ByteArrayOutputStream buffer = new ByteArrayOutputStream()) {
56
int b;
57
do {
58
b = data.read();
59
if (b >= 0) buffer.write(b);
60
} while (b >= 0);
61
byte[] bytes = buffer.toByteArray();
62
result = defineClass(name, bytes, 0, bytes.length);
63
}
64
} catch (IOException e) {
65
throw new ClassNotFoundException("Error reading class file", e);
66
}
67
}
68
if (resolve) resolveClass(result);
69
return result;
70
}
71
72
}
73
74