JvmtiGetAllModulesTest.java revision 12290:8953c0318163
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 */
31import java.lang.reflect.Layer;
32import java.lang.reflect.Module;
33import java.lang.module.ModuleReference;
34import java.lang.module.ModuleFinder;
35import java.lang.module.ModuleReader;
36import java.lang.module.ModuleDescriptor;
37import java.lang.module.Configuration;
38import java.util.Arrays;
39import java.util.Set;
40import java.util.Map;
41import java.util.function.Supplier;
42import java.util.Objects;
43import java.util.Optional;
44import java.net.URI;
45import java.util.HashSet;
46import java.util.HashMap;
47import java.util.stream.Collectors;
48import jdk.test.lib.Asserts;
49
50public 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                = ModuleDescriptor.module(MY_MODULE_NAME).build();
76        ModuleFinder finder = finderOf(descriptor);
77        ClassLoader loader = new ClassLoader() {};
78        Configuration parent = Layer.boot().configuration();
79        Configuration cf = parent.resolveRequires(finder, ModuleFinder.of(), Set.of(MY_MODULE_NAME));
80        Layer my = Layer.boot().defineModules(cf, m -> loader);
81
82        // Verify that the loaded module is indeed reported by JVMTI
83        Set<Module> jvmtiModules = getModulesJVMTI();
84        for (Module mod : my.modules()) {
85            if (!jvmtiModules.contains(mod)) {
86                throw new RuntimeException("JVMTI did not report the loaded named module: " + mod.getName());
87            }
88        }
89
90    }
91
92    /**
93     * Returns a ModuleFinder that finds modules with the given module
94     * descriptors.
95     */
96    static ModuleFinder finderOf(ModuleDescriptor... descriptors) {
97
98        // Create a ModuleReference for each module
99        Map<String, ModuleReference> namesToReference = new HashMap<>();
100
101        for (ModuleDescriptor descriptor : descriptors) {
102            String name = descriptor.name();
103
104            URI uri = URI.create("module:/" + name);
105
106            Supplier<ModuleReader> supplier = () -> {
107                throw new UnsupportedOperationException();
108            };
109
110            ModuleReference mref = new ModuleReference(descriptor, uri, supplier);
111
112            namesToReference.put(name, mref);
113        }
114
115        return new ModuleFinder() {
116            @Override
117            public Optional<ModuleReference> find(String name) {
118                Objects.requireNonNull(name);
119                return Optional.ofNullable(namesToReference.get(name));
120            }
121
122            @Override
123            public Set<ModuleReference> findAll() {
124                return new HashSet<>(namesToReference.values());
125            }
126        };
127    }
128
129}
130