36511
|
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 |
import java.util.function.Supplier;
|
|
36 |
|
|
37 |
|
|
38 |
/**
|
|
39 |
* This class consists exclusively of static utility methods that are useful
|
|
40 |
* for creating tests for modules.
|
|
41 |
*/
|
|
42 |
|
|
43 |
public final class ModuleUtils {
|
|
44 |
private ModuleUtils() { }
|
|
45 |
|
|
46 |
|
|
47 |
/**
|
|
48 |
* Returns a ModuleFinder that finds modules with the given module
|
|
49 |
* descriptors.
|
|
50 |
*/
|
|
51 |
static ModuleFinder finderOf(ModuleDescriptor... descriptors) {
|
|
52 |
|
|
53 |
// Create a ModuleReference for each module
|
|
54 |
Map<String, ModuleReference> namesToReference = new HashMap<>();
|
|
55 |
|
|
56 |
for (ModuleDescriptor descriptor : descriptors) {
|
|
57 |
String name = descriptor.name();
|
|
58 |
|
|
59 |
URI uri = URI.create("module:/" + name);
|
|
60 |
|
|
61 |
Supplier<ModuleReader> supplier = () -> {
|
|
62 |
throw new UnsupportedOperationException();
|
|
63 |
};
|
|
64 |
|
|
65 |
ModuleReference mref = new ModuleReference(descriptor, uri, supplier);
|
|
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 |
}
|