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