Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/GetModulesInfo/JvmtiGetAllModulesTest.java
41153 views
1
/*
2
* Copyright (c) 2016, 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
/**
25
* @test
26
* @summary Verifies the JVMTI GetAllModules API
27
* @requires vm.jvmti
28
* @library /test/lib
29
* @run main/othervm/native -agentlib:JvmtiGetAllModulesTest JvmtiGetAllModulesTest
30
*
31
*/
32
import java.lang.module.ModuleReference;
33
import java.lang.module.ModuleFinder;
34
import java.lang.module.ModuleReader;
35
import java.lang.module.ModuleDescriptor;
36
import java.lang.module.Configuration;
37
import java.util.Arrays;
38
import java.util.Set;
39
import java.util.Map;
40
import java.util.function.Supplier;
41
import java.util.Objects;
42
import java.util.Optional;
43
import java.net.URI;
44
import java.util.HashSet;
45
import java.util.HashMap;
46
import java.util.stream.Collectors;
47
import jdk.test.lib.Asserts;
48
49
public class JvmtiGetAllModulesTest {
50
51
static class MyModuleReference extends ModuleReference {
52
public MyModuleReference(ModuleDescriptor descriptor, URI uri) {
53
super(descriptor, uri);
54
}
55
56
// Trivial implementation to make the class non-abstract
57
public ModuleReader open() {
58
return null;
59
}
60
}
61
62
private static native Module[] getModulesNative();
63
64
private static Set<Module> getModulesJVMTI() {
65
66
Set<Module> modules = Arrays.stream(getModulesNative()).collect(Collectors.toSet());
67
68
// JVMTI reports unnamed modules, Java API does not
69
// remove the unnamed modules here, so the resulting report can be expected
70
// to be equal to what Java reports
71
modules.removeIf(mod -> !mod.isNamed());
72
73
// jdk.proxy1 and jdk.proxy2 modules are dynamically initialized by Graal code in case Graal VM is used.
74
// We need to filter them out because they are not part of boot modules. See more details in JDK-8195156.
75
modules.removeIf(mod -> mod.getName().startsWith("jdk.proxy"));
76
77
return modules;
78
}
79
80
public static void main(String[] args) throws Exception {
81
82
final String MY_MODULE_NAME = "myModule";
83
84
// Verify that JVMTI reports exactly the same info as Java regarding the named modules
85
Asserts.assertEquals(ModuleLayer.boot().modules(), getModulesJVMTI());
86
87
// Load a new named module
88
ModuleDescriptor descriptor = ModuleDescriptor.newModule(MY_MODULE_NAME).build();
89
ModuleFinder finder = finderOf(descriptor);
90
ClassLoader loader = new ClassLoader() {};
91
Configuration parent = ModuleLayer.boot().configuration();
92
Configuration cf = parent.resolve(finder, ModuleFinder.of(), Set.of(MY_MODULE_NAME));
93
ModuleLayer my = ModuleLayer.boot().defineModules(cf, m -> loader);
94
95
// Verify that the loaded module is indeed reported by JVMTI
96
Set<Module> jvmtiModules = getModulesJVMTI();
97
for (Module mod : my.modules()) {
98
if (!jvmtiModules.contains(mod)) {
99
throw new RuntimeException("JVMTI did not report the loaded named module: " + mod.getName());
100
}
101
}
102
103
}
104
105
/**
106
* Returns a ModuleFinder that finds modules with the given module
107
* descriptors.
108
*/
109
static ModuleFinder finderOf(ModuleDescriptor... descriptors) {
110
111
// Create a ModuleReference for each module
112
Map<String, ModuleReference> namesToReference = new HashMap<>();
113
114
for (ModuleDescriptor descriptor : descriptors) {
115
String name = descriptor.name();
116
117
URI uri = URI.create("module:/" + name);
118
119
ModuleReference mref = new MyModuleReference(descriptor, uri);
120
121
namesToReference.put(name, mref);
122
}
123
124
return new ModuleFinder() {
125
@Override
126
public Optional<ModuleReference> find(String name) {
127
Objects.requireNonNull(name);
128
return Optional.ofNullable(namesToReference.get(name));
129
}
130
131
@Override
132
public Set<ModuleReference> findAll() {
133
return new HashSet<>(namesToReference.values());
134
}
135
};
136
}
137
138
}
139
140