Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/GetModulesInfo/JvmtiGetAllModulesTest.java
41153 views
/*1* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @summary Verifies the JVMTI GetAllModules API26* @requires vm.jvmti27* @library /test/lib28* @run main/othervm/native -agentlib:JvmtiGetAllModulesTest JvmtiGetAllModulesTest29*30*/31import java.lang.module.ModuleReference;32import java.lang.module.ModuleFinder;33import java.lang.module.ModuleReader;34import java.lang.module.ModuleDescriptor;35import java.lang.module.Configuration;36import java.util.Arrays;37import java.util.Set;38import java.util.Map;39import java.util.function.Supplier;40import java.util.Objects;41import java.util.Optional;42import java.net.URI;43import java.util.HashSet;44import java.util.HashMap;45import java.util.stream.Collectors;46import jdk.test.lib.Asserts;4748public class JvmtiGetAllModulesTest {4950static class MyModuleReference extends ModuleReference {51public MyModuleReference(ModuleDescriptor descriptor, URI uri) {52super(descriptor, uri);53}5455// Trivial implementation to make the class non-abstract56public ModuleReader open() {57return null;58}59}6061private static native Module[] getModulesNative();6263private static Set<Module> getModulesJVMTI() {6465Set<Module> modules = Arrays.stream(getModulesNative()).collect(Collectors.toSet());6667// JVMTI reports unnamed modules, Java API does not68// remove the unnamed modules here, so the resulting report can be expected69// to be equal to what Java reports70modules.removeIf(mod -> !mod.isNamed());7172// jdk.proxy1 and jdk.proxy2 modules are dynamically initialized by Graal code in case Graal VM is used.73// We need to filter them out because they are not part of boot modules. See more details in JDK-8195156.74modules.removeIf(mod -> mod.getName().startsWith("jdk.proxy"));7576return modules;77}7879public static void main(String[] args) throws Exception {8081final String MY_MODULE_NAME = "myModule";8283// Verify that JVMTI reports exactly the same info as Java regarding the named modules84Asserts.assertEquals(ModuleLayer.boot().modules(), getModulesJVMTI());8586// Load a new named module87ModuleDescriptor descriptor = ModuleDescriptor.newModule(MY_MODULE_NAME).build();88ModuleFinder finder = finderOf(descriptor);89ClassLoader loader = new ClassLoader() {};90Configuration parent = ModuleLayer.boot().configuration();91Configuration cf = parent.resolve(finder, ModuleFinder.of(), Set.of(MY_MODULE_NAME));92ModuleLayer my = ModuleLayer.boot().defineModules(cf, m -> loader);9394// Verify that the loaded module is indeed reported by JVMTI95Set<Module> jvmtiModules = getModulesJVMTI();96for (Module mod : my.modules()) {97if (!jvmtiModules.contains(mod)) {98throw new RuntimeException("JVMTI did not report the loaded named module: " + mod.getName());99}100}101102}103104/**105* Returns a ModuleFinder that finds modules with the given module106* descriptors.107*/108static ModuleFinder finderOf(ModuleDescriptor... descriptors) {109110// Create a ModuleReference for each module111Map<String, ModuleReference> namesToReference = new HashMap<>();112113for (ModuleDescriptor descriptor : descriptors) {114String name = descriptor.name();115116URI uri = URI.create("module:/" + name);117118ModuleReference mref = new MyModuleReference(descriptor, uri);119120namesToReference.put(name, mref);121}122123return new ModuleFinder() {124@Override125public Optional<ModuleReference> find(String name) {126Objects.requireNonNull(name);127return Optional.ofNullable(namesToReference.get(name));128}129130@Override131public Set<ModuleReference> findAll() {132return new HashSet<>(namesToReference.values());133}134};135}136137}138139140