1 /* |
|
2 * Copyright (c) 2015, 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 import java.lang.module.ModuleDescriptor; |
|
25 import java.lang.module.ModuleFinder; |
|
26 import java.lang.module.ModuleReader; |
|
27 import java.lang.module.ModuleReference; |
|
28 import java.net.URI; |
|
29 import java.util.HashMap; |
|
30 import java.util.HashSet; |
|
31 import java.util.Map; |
|
32 import java.util.Objects; |
|
33 import java.util.Optional; |
|
34 import java.util.Set; |
|
35 |
|
36 |
|
37 /** |
|
38 * This class consists exclusively of static utility methods that are useful |
|
39 * for creating tests for modules. |
|
40 */ |
|
41 |
|
42 public final class ModuleUtils { |
|
43 private ModuleUtils() { } |
|
44 |
|
45 |
|
46 /** |
|
47 * Returns a ModuleFinder that finds modules with the given module |
|
48 * descriptors. |
|
49 */ |
|
50 static ModuleFinder finderOf(ModuleDescriptor... descriptors) { |
|
51 |
|
52 // Create a ModuleReference for each module |
|
53 Map<String, ModuleReference> namesToReference = new HashMap<>(); |
|
54 |
|
55 for (ModuleDescriptor descriptor : descriptors) { |
|
56 String name = descriptor.name(); |
|
57 |
|
58 URI uri = URI.create("module:/" + name); |
|
59 |
|
60 ModuleReference mref = new ModuleReference(descriptor, uri) { |
|
61 @Override |
|
62 public ModuleReader open() { |
|
63 throw new UnsupportedOperationException(); |
|
64 } |
|
65 }; |
|
66 |
|
67 namesToReference.put(name, mref); |
|
68 } |
|
69 |
|
70 return new ModuleFinder() { |
|
71 @Override |
|
72 public Optional<ModuleReference> find(String name) { |
|
73 Objects.requireNonNull(name); |
|
74 return Optional.ofNullable(namesToReference.get(name)); |
|
75 } |
|
76 @Override |
|
77 public Set<ModuleReference> findAll() { |
|
78 return new HashSet<>(namesToReference.values()); |
|
79 } |
|
80 }; |
|
81 } |
|
82 |
|
83 } |
|