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