Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/metaspace/stressHierarchy/common/classloader/StressClassloader.java
41162 views
1
/*
2
* Copyright (c) 2013, 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
package metaspace.stressHierarchy.common.classloader;
24
25
import java.net.URLClassLoader;
26
import java.net.URL;
27
import java.util.HashSet;
28
import java.util.Set;
29
30
import metaspace.stressHierarchy.common.generateHierarchy.NodeDescriptor;
31
32
/**
33
* This classloader tries to load each class itself before forwarding to parent.
34
* Also it stores loaded classes and class names.
35
*
36
*/
37
public class StressClassloader extends URLClassLoader {
38
39
private final Set<Class<?>> loadedClasses = new HashSet<Class<?>>();
40
41
private final Set<String> loadedClassesNames = new HashSet<String>();
42
43
private String className;
44
45
private byte[] bytecode;
46
47
public StressClassloader(NodeDescriptor nodeDescriptor,
48
StressClassloader parentClassLoader) {
49
super(new URL[] {}, parentClassLoader);
50
this.className = nodeDescriptor.className;
51
this.bytecode = nodeDescriptor.bytecode;
52
}
53
54
public Set<Class<?>> getLoadedClasses() {
55
return loadedClasses;
56
}
57
58
public Set<String> getLoadedClassNames() {
59
return loadedClassesNames;
60
}
61
62
@Override
63
public Class<?> loadClass(String name) throws ClassNotFoundException {
64
if (loadedClassesNames.contains(name)) {
65
throw new RuntimeException("Classloader " + toString() + " loads class " + name + " second time! ");
66
}
67
Class<?> alreadyLoaded = findLoadedClass(name);
68
if (alreadyLoaded != null) {
69
return alreadyLoaded;
70
}
71
if (className.equals(name)) {
72
Class<?> clazz = defineClass(name, bytecode, 0, bytecode.length);
73
loadedClasses.add(clazz);
74
loadedClassesNames.add(clazz.getName());
75
return clazz;
76
} else {
77
return super.loadClass(name);
78
}
79
}
80
81
@Override
82
public String toString() {
83
return "StressClassloader@" + className;
84
}
85
86
87
88
}
89
90