8209003: Consolidate use of empty collections in java.lang.module

Reviewed-by: alanb, mchung
This commit is contained in:
Claes Redestad 2018-08-07 23:08:52 +02:00
parent 3be8d256e9
commit db4913ba5a
14 changed files with 76 additions and 122 deletions

View file

@ -40,7 +40,6 @@ import java.net.URI;
import java.net.URL; import java.net.URL;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
@ -1123,7 +1122,7 @@ public final class Module implements AnnotatedElement {
Set<Module> reads = new HashSet<>(); Set<Module> reads = new HashSet<>();
// name -> source Module when in parent layer // name -> source Module when in parent layer
Map<String, Module> nameToSource = Collections.emptyMap(); Map<String, Module> nameToSource = Map.of();
for (ResolvedModule other : resolvedModule.reads()) { for (ResolvedModule other : resolvedModule.reads()) {
Module m2 = null; Module m2 = null;

View file

@ -173,7 +173,7 @@ public final class ModuleLayer {
Map<String, Module> map; Map<String, Module> map;
if (parents.isEmpty()) { if (parents.isEmpty()) {
map = Collections.emptyMap(); map = Map.of();
} else { } else {
map = Module.defineModules(cf, clf, this); map = Module.defineModules(cf, clf, this);
} }
@ -811,8 +811,7 @@ public final class ModuleLayer {
public Set<Module> modules() { public Set<Module> modules() {
Set<Module> modules = this.modules; Set<Module> modules = this.modules;
if (modules == null) { if (modules == null) {
this.modules = modules = this.modules = modules = Set.copyOf(nameToModule.values());
Collections.unmodifiableSet(new HashSet<>(nameToModule.values()));
} }
return modules; return modules;
} }

View file

@ -31,7 +31,6 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Deque; import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -119,10 +118,10 @@ public final class Configuration {
String targetPlatform() { return targetPlatform; } String targetPlatform() { return targetPlatform; }
private Configuration() { private Configuration() {
this.parents = Collections.emptyList(); this.parents = List.of();
this.graph = Collections.emptyMap(); this.graph = Map.of();
this.modules = Collections.emptySet(); this.modules = Set.of();
this.nameToModule = Collections.emptyMap(); this.nameToModule = Map.of();
this.targetPlatform = null; this.targetPlatform = null;
} }
@ -140,7 +139,7 @@ public final class Configuration {
i++; i++;
} }
this.parents = Collections.unmodifiableList(parents); this.parents = List.copyOf(parents);
this.graph = g; this.graph = g;
this.modules = Set.of(moduleArray); this.modules = Set.of(moduleArray);
this.nameToModule = Map.ofEntries(nameEntries); this.nameToModule = Map.ofEntries(nameEntries);
@ -554,7 +553,7 @@ public final class Configuration {
Set<ModuleDescriptor> descriptors() { Set<ModuleDescriptor> descriptors() {
if (modules.isEmpty()) { if (modules.isEmpty()) {
return Collections.emptySet(); return Set.of();
} else { } else {
return modules.stream() return modules.stream()
.map(ResolvedModule::reference) .map(ResolvedModule::reference)
@ -596,7 +595,7 @@ public final class Configuration {
} }
} }
} }
this.allConfigurations = Collections.unmodifiableList(allConfigurations); this.allConfigurations = allConfigurations; // no need to do defensive copy
} }
return allConfigurations.stream(); return allConfigurations.stream();
} }

View file

@ -102,7 +102,7 @@ public class ModuleDescriptor
* @since 9 * @since 9
* @spec JPMS * @spec JPMS
*/ */
public static enum Modifier { public enum Modifier {
/** /**
* An open module. An open module does not declare any open packages * An open module. An open module does not declare any open packages
* but the resulting module is treated as if all packages are open. * but the resulting module is treated as if all packages are open.
@ -149,7 +149,7 @@ public class ModuleDescriptor
* @since 9 * @since 9
* @spec JPMS * @spec JPMS
*/ */
public static enum Modifier { public enum Modifier {
/** /**
* The dependence causes any module which depends on the <i>current * The dependence causes any module which depends on the <i>current
@ -185,12 +185,7 @@ public class ModuleDescriptor
private Requires(Set<Modifier> ms, String mn, Version v, String vs) { private Requires(Set<Modifier> ms, String mn, Version v, String vs) {
assert v == null || vs == null; assert v == null || vs == null;
if (ms.isEmpty()) { this.mods = Set.copyOf(ms);
ms = Collections.emptySet();
} else {
ms = Collections.unmodifiableSet(EnumSet.copyOf(ms));
}
this.mods = ms;
this.name = mn; this.name = mn;
this.compiledVersion = v; this.compiledVersion = v;
this.rawCompiledVersion = vs; this.rawCompiledVersion = vs;
@ -384,7 +379,7 @@ public class ModuleDescriptor
* @since 9 * @since 9
* @spec JPMS * @spec JPMS
*/ */
public static enum Modifier { public enum Modifier {
/** /**
* The export was not explicitly or implicitly declared in the * The export was not explicitly or implicitly declared in the
@ -408,14 +403,9 @@ public class ModuleDescriptor
* Constructs an export * Constructs an export
*/ */
private Exports(Set<Modifier> ms, String source, Set<String> targets) { private Exports(Set<Modifier> ms, String source, Set<String> targets) {
if (ms.isEmpty()) { this.mods = Set.copyOf(ms);
ms = Collections.emptySet();
} else {
ms = Collections.unmodifiableSet(EnumSet.copyOf(ms));
}
this.mods = ms;
this.source = source; this.source = source;
this.targets = emptyOrUnmodifiableSet(targets); this.targets = Set.copyOf(targets);
} }
private Exports(Set<Modifier> ms, private Exports(Set<Modifier> ms,
@ -596,7 +586,7 @@ public class ModuleDescriptor
* @since 9 * @since 9
* @spec JPMS * @spec JPMS
*/ */
public static enum Modifier { public enum Modifier {
/** /**
* The open package was not explicitly or implicitly declared in * The open package was not explicitly or implicitly declared in
@ -620,14 +610,9 @@ public class ModuleDescriptor
* Constructs an Opens * Constructs an Opens
*/ */
private Opens(Set<Modifier> ms, String source, Set<String> targets) { private Opens(Set<Modifier> ms, String source, Set<String> targets) {
if (ms.isEmpty()) { this.mods = Set.copyOf(ms);
ms = Collections.emptySet();
} else {
ms = Collections.unmodifiableSet(EnumSet.copyOf(ms));
}
this.mods = ms;
this.source = source; this.source = source;
this.targets = emptyOrUnmodifiableSet(targets); this.targets = Set.copyOf(targets);
} }
private Opens(Set<Modifier> ms, private Opens(Set<Modifier> ms,
@ -800,7 +785,7 @@ public class ModuleDescriptor
private Provides(String service, List<String> providers) { private Provides(String service, List<String> providers) {
this.service = service; this.service = service;
this.providers = Collections.unmodifiableList(providers); this.providers = List.copyOf(providers);
} }
private Provides(String service, List<String> providers, boolean unused) { private Provides(String service, List<String> providers, boolean unused) {
@ -1264,18 +1249,18 @@ public class ModuleDescriptor
this.name = name; this.name = name;
this.version = version; this.version = version;
this.rawVersionString = rawVersionString; this.rawVersionString = rawVersionString;
this.modifiers = emptyOrUnmodifiableSet(modifiers); this.modifiers = Set.copyOf(modifiers);
this.open = modifiers.contains(Modifier.OPEN); this.open = modifiers.contains(Modifier.OPEN);
this.automatic = modifiers.contains(Modifier.AUTOMATIC); this.automatic = modifiers.contains(Modifier.AUTOMATIC);
assert (requires.stream().map(Requires::name).distinct().count() assert (requires.stream().map(Requires::name).distinct().count()
== requires.size()); == requires.size());
this.requires = emptyOrUnmodifiableSet(requires); this.requires = Set.copyOf(requires);
this.exports = emptyOrUnmodifiableSet(exports); this.exports = Set.copyOf(exports);
this.opens = emptyOrUnmodifiableSet(opens); this.opens = Set.copyOf(opens);
this.uses = emptyOrUnmodifiableSet(uses); this.uses = Set.copyOf(uses);
this.provides = emptyOrUnmodifiableSet(provides); this.provides = Set.copyOf(provides);
this.packages = emptyOrUnmodifiableSet(packages); this.packages = Set.copyOf(packages);
this.mainClass = mainClass; this.mainClass = mainClass;
} }
@ -1734,16 +1719,14 @@ public class ModuleDescriptor
String pn, String pn,
Set<String> targets) Set<String> targets)
{ {
Exports e = new Exports(ms, pn, targets); targets = new HashSet<>(targets);
// check targets
targets = e.targets();
if (targets.isEmpty()) if (targets.isEmpty())
throw new IllegalArgumentException("Empty target set"); throw new IllegalArgumentException("Empty target set");
if (strict) { if (strict) {
requirePackageName(e.source()); requirePackageName(pn);
targets.forEach(Checks::requireModuleName); targets.forEach(Checks::requireModuleName);
} }
Exports e = new Exports(ms, pn, targets);
return exports(e); return exports(e);
} }
@ -1769,7 +1752,7 @@ public class ModuleDescriptor
if (strict) { if (strict) {
requirePackageName(pn); requirePackageName(pn);
} }
Exports e = new Exports(ms, pn, Collections.emptySet()); Exports e = new Exports(ms, pn, Set.of());
return exports(e); return exports(e);
} }
@ -1794,7 +1777,7 @@ public class ModuleDescriptor
* or this builder is for an automatic module * or this builder is for an automatic module
*/ */
public Builder exports(String pn, Set<String> targets) { public Builder exports(String pn, Set<String> targets) {
return exports(Collections.emptySet(), pn, targets); return exports(Set.of(), pn, targets);
} }
/** /**
@ -1813,7 +1796,7 @@ public class ModuleDescriptor
* or this builder is for an automatic module * or this builder is for an automatic module
*/ */
public Builder exports(String pn) { public Builder exports(String pn) {
return exports(Collections.emptySet(), pn); return exports(Set.of(), pn);
} }
/** /**
@ -1870,16 +1853,14 @@ public class ModuleDescriptor
String pn, String pn,
Set<String> targets) Set<String> targets)
{ {
Opens opens = new Opens(ms, pn, targets); targets = new HashSet<>(targets);
// check targets
targets = opens.targets();
if (targets.isEmpty()) if (targets.isEmpty())
throw new IllegalArgumentException("Empty target set"); throw new IllegalArgumentException("Empty target set");
if (strict) { if (strict) {
requirePackageName(opens.source()); requirePackageName(pn);
targets.forEach(Checks::requireModuleName); targets.forEach(Checks::requireModuleName);
} }
Opens opens = new Opens(ms, pn, targets);
return opens(opens); return opens(opens);
} }
@ -1905,7 +1886,7 @@ public class ModuleDescriptor
if (strict) { if (strict) {
requirePackageName(pn); requirePackageName(pn);
} }
Opens e = new Opens(ms, pn, Collections.emptySet()); Opens e = new Opens(ms, pn, Set.of());
return opens(e); return opens(e);
} }
@ -1929,7 +1910,7 @@ public class ModuleDescriptor
* builder for an open module or automatic module * builder for an open module or automatic module
*/ */
public Builder opens(String pn, Set<String> targets) { public Builder opens(String pn, Set<String> targets) {
return opens(Collections.emptySet(), pn, targets); return opens(Set.of(), pn, targets);
} }
/** /**
@ -1948,7 +1929,7 @@ public class ModuleDescriptor
* builder for an open module or automatic module * builder for an open module or automatic module
*/ */
public Builder opens(String pn) { public Builder opens(String pn) {
return opens(Collections.emptySet(), pn); return opens(Set.of(), pn);
} }
/** /**
@ -2021,15 +2002,12 @@ public class ModuleDescriptor
* declared * declared
*/ */
public Builder provides(String service, List<String> providers) { public Builder provides(String service, List<String> providers) {
Provides p = new Provides(service, providers); providers = new ArrayList<>(providers);
if (providers.isEmpty())
// check providers after the set has been copied.
List<String> providerNames = p.providers();
if (providerNames.isEmpty())
throw new IllegalArgumentException("Empty providers set"); throw new IllegalArgumentException("Empty providers set");
if (strict) { if (strict) {
requireServiceTypeName(p.service()); requireServiceTypeName(service);
providerNames.forEach(Checks::requireServiceProviderName); providers.forEach(Checks::requireServiceProviderName);
} else { } else {
// Disallow service/providers in unnamed package // Disallow service/providers in unnamed package
String pn = packageName(service); String pn = packageName(service);
@ -2037,7 +2015,7 @@ public class ModuleDescriptor
throw new IllegalArgumentException(service throw new IllegalArgumentException(service
+ ": unnamed package"); + ": unnamed package");
} }
for (String name : providerNames) { for (String name : providers) {
pn = packageName(name); pn = packageName(name);
if (pn.isEmpty()) { if (pn.isEmpty()) {
throw new IllegalArgumentException(name throw new IllegalArgumentException(name
@ -2045,6 +2023,7 @@ public class ModuleDescriptor
} }
} }
} }
Provides p = new Provides(service, providers);
return provides(p); return provides(p);
} }
@ -2574,27 +2553,6 @@ public class ModuleDescriptor
return ModuleInfo.read(bb, null).descriptor(); return ModuleInfo.read(bb, null).descriptor();
} }
private static <K,V> Map<K,V> emptyOrUnmodifiableMap(Map<K,V> map) {
if (map.isEmpty()) {
return Collections.emptyMap();
} else if (map.size() == 1) {
Map.Entry<K, V> entry = map.entrySet().iterator().next();
return Collections.singletonMap(entry.getKey(), entry.getValue());
} else {
return Collections.unmodifiableMap(map);
}
}
private static <T> Set<T> emptyOrUnmodifiableSet(Set<T> set) {
if (set.isEmpty()) {
return Collections.emptySet();
} else if (set.size() == 1) {
return Collections.singleton(set.iterator().next());
} else {
return Collections.unmodifiableSet(set);
}
}
private static String packageName(String cn) { private static String packageName(String cn) {
int index = cn.lastIndexOf('.'); int index = cn.lastIndexOf('.');
return (index == -1) ? "" : cn.substring(0, index); return (index == -1) ? "" : cn.substring(0, index);
@ -2674,7 +2632,7 @@ public class ModuleDescriptor
@Override @Override
public Exports newExports(Set<Exports.Modifier> ms, String source) { public Exports newExports(Set<Exports.Modifier> ms, String source) {
return new Exports(ms, source, Collections.emptySet(), true); return new Exports(ms, source, Set.of(), true);
} }
@Override @Override
@ -2693,7 +2651,7 @@ public class ModuleDescriptor
@Override @Override
public Opens newOpens(Set<Opens.Modifier> ms, String source) { public Opens newOpens(Set<Opens.Modifier> ms, String source) {
return new Opens(ms, source, Collections.emptySet(), true); return new Opens(ms, source, Set.of(), true);
} }
@Override @Override

View file

@ -306,7 +306,7 @@ public interface ModuleFinder {
@Override @Override
public Set<ModuleReference> findAll() { public Set<ModuleReference> findAll() {
return Collections.emptySet(); return Set.of();
} }
}; };
} }

View file

@ -30,7 +30,6 @@ import java.lang.module.ModuleDescriptor.Opens;
import java.lang.module.ModuleDescriptor.Provides; import java.lang.module.ModuleDescriptor.Provides;
import java.lang.module.ModuleDescriptor.Requires; import java.lang.module.ModuleDescriptor.Requires;
import java.lang.module.ModuleDescriptor.Version; import java.lang.module.ModuleDescriptor.Version;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -148,11 +147,11 @@ final class Builder {
Builder(String name) { Builder(String name) {
this.name = name; this.name = name;
this.requires = Collections.emptySet(); this.requires = Set.of();
this.exports = Collections.emptySet(); this.exports = Set.of();
this.opens = Collections.emptySet(); this.opens = Set.of();
this.provides = Collections.emptySet(); this.provides = Set.of();
this.uses = Collections.emptySet(); this.uses = Set.of();
} }
Builder open(boolean value) { Builder open(boolean value) {
@ -253,7 +252,7 @@ final class Builder {
if (synthetic) n++; if (synthetic) n++;
if (mandated) n++; if (mandated) n++;
if (n == 0) { if (n == 0) {
return Collections.emptySet(); return Set.of();
} else { } else {
ModuleDescriptor.Modifier[] mods = new ModuleDescriptor.Modifier[n]; ModuleDescriptor.Modifier[] mods = new ModuleDescriptor.Modifier[n];
if (open) mods[--n] = ModuleDescriptor.Modifier.OPEN; if (open) mods[--n] = ModuleDescriptor.Modifier.OPEN;

View file

@ -26,7 +26,6 @@
package jdk.internal.module; package jdk.internal.module;
import java.lang.module.ModuleDescriptor; import java.lang.module.ModuleDescriptor;
import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -72,11 +71,11 @@ class ExplodedSystemModules implements SystemModules {
@Override @Override
public Map<String, Set<String>> concealedPackagesToOpen() { public Map<String, Set<String>> concealedPackagesToOpen() {
return Collections.emptyMap(); return Map.of();
} }
@Override @Override
public Map<String, Set<String>> exportedPackagesToOpen() { public Map<String, Set<String>> exportedPackagesToOpen() {
return Collections.emptyMap(); return Map.of();
} }
} }

View file

@ -568,7 +568,7 @@ public final class ModuleBootstrap {
// the system property is removed after decoding // the system property is removed after decoding
String value = getAndRemoveProperty(prefix + index); String value = getAndRemoveProperty(prefix + index);
if (value == null) { if (value == null) {
return Collections.emptySet(); return Set.of();
} else { } else {
Set<String> modules = new HashSet<>(); Set<String> modules = new HashSet<>();
while (value != null) { while (value != null) {
@ -588,7 +588,7 @@ public final class ModuleBootstrap {
private static Set<String> limitModules() { private static Set<String> limitModules() {
String value = getAndRemoveProperty("jdk.module.limitmods"); String value = getAndRemoveProperty("jdk.module.limitmods");
if (value == null) { if (value == null) {
return Collections.emptySet(); return Set.of();
} else { } else {
Set<String> names = new HashSet<>(); Set<String> names = new HashSet<>();
for (String name : value.split(",")) { for (String name : value.split(",")) {
@ -840,7 +840,7 @@ public final class ModuleBootstrap {
// the system property is removed after decoding // the system property is removed after decoding
String value = getAndRemoveProperty(prefix + index); String value = getAndRemoveProperty(prefix + index);
if (value == null) if (value == null)
return Collections.emptyMap(); return Map.of();
Map<String, List<String>> map = new HashMap<>(); Map<String, List<String>> map = new HashMap<>();

View file

@ -40,7 +40,6 @@ import java.lang.module.ModuleDescriptor.Opens;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.BufferUnderflowException; import java.nio.BufferUnderflowException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -374,7 +373,7 @@ public final class ModuleInfo {
int requires_flags = in.readUnsignedShort(); int requires_flags = in.readUnsignedShort();
Set<Requires.Modifier> mods; Set<Requires.Modifier> mods;
if (requires_flags == 0) { if (requires_flags == 0) {
mods = Collections.emptySet(); mods = Set.of();
} else { } else {
mods = new HashSet<>(); mods = new HashSet<>();
if ((requires_flags & ACC_TRANSITIVE) != 0) if ((requires_flags & ACC_TRANSITIVE) != 0)
@ -430,7 +429,7 @@ public final class ModuleInfo {
Set<Exports.Modifier> mods; Set<Exports.Modifier> mods;
int exports_flags = in.readUnsignedShort(); int exports_flags = in.readUnsignedShort();
if (exports_flags == 0) { if (exports_flags == 0) {
mods = Collections.emptySet(); mods = Set.of();
} else { } else {
mods = new HashSet<>(); mods = new HashSet<>();
if ((exports_flags & ACC_SYNTHETIC) != 0) if ((exports_flags & ACC_SYNTHETIC) != 0)
@ -470,7 +469,7 @@ public final class ModuleInfo {
Set<Opens.Modifier> mods; Set<Opens.Modifier> mods;
int opens_flags = in.readUnsignedShort(); int opens_flags = in.readUnsignedShort();
if (opens_flags == 0) { if (opens_flags == 0) {
mods = Collections.emptySet(); mods = Set.of();
} else { } else {
mods = new HashSet<>(); mods = new HashSet<>();
if ((opens_flags & ACC_SYNTHETIC) != 0) if ((opens_flags & ACC_SYNTHETIC) != 0)

View file

@ -42,7 +42,6 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -78,7 +77,7 @@ public final class ModulePatcher {
*/ */
public ModulePatcher(Map<String, List<String>> input) { public ModulePatcher(Map<String, List<String>> input) {
if (input.isEmpty()) { if (input.isEmpty()) {
this.map = Collections.emptyMap(); this.map = Map.of();
} else { } else {
Map<String, List<Path>> map = new HashMap<>(); Map<String, List<Path>> map = new HashMap<>();
for (Map.Entry<String, List<String>> e : input.entrySet()) { for (Map.Entry<String, List<String>> e : input.entrySet()) {

View file

@ -45,7 +45,6 @@ import java.nio.file.NoSuchFileException;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -217,7 +216,7 @@ public class ModulePath implements ModuleFinder {
try { try {
attrs = Files.readAttributes(entry, BasicFileAttributes.class); attrs = Files.readAttributes(entry, BasicFileAttributes.class);
} catch (NoSuchFileException e) { } catch (NoSuchFileException e) {
return Collections.emptyMap(); return Map.of();
} catch (IOException ioe) { } catch (IOException ioe) {
throw new FindException(ioe); throw new FindException(ioe);
} }
@ -236,7 +235,7 @@ public class ModulePath implements ModuleFinder {
ModuleReference mref = readModule(entry, attrs); ModuleReference mref = readModule(entry, attrs);
if (mref != null) { if (mref != null) {
String name = mref.descriptor().name(); String name = mref.descriptor().name();
return Collections.singletonMap(name, mref); return Map.of(name, mref);
} }
// not recognized // not recognized

View file

@ -28,7 +28,6 @@ package jdk.internal.module;
import java.lang.module.ModuleDescriptor; import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleDescriptor.Provides; import java.lang.module.ModuleDescriptor.Provides;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -147,7 +146,7 @@ public final class ServicesCatalog {
* the given service type. * the given service type.
*/ */
public List<ServiceProvider> findServices(String service) { public List<ServiceProvider> findServices(String service) {
return map.getOrDefault(service, Collections.emptyList()); return map.getOrDefault(service, List.of());
} }
/** /**

View file

@ -282,8 +282,8 @@ public final class SystemModuleFinders {
SystemModuleFinder(Set<ModuleReference> mrefs, SystemModuleFinder(Set<ModuleReference> mrefs,
Map<String, ModuleReference> nameToModule) { Map<String, ModuleReference> nameToModule) {
this.mrefs = Collections.unmodifiableSet(mrefs); this.mrefs = Set.copyOf(mrefs);
this.nameToModule = Collections.unmodifiableMap(nameToModule); this.nameToModule = Map.copyOf(nameToModule);
} }
@Override @Override
@ -353,7 +353,7 @@ public final class SystemModuleFinders {
} }
} }
} }
return (nameToHash != null) ? nameToHash : Collections.emptyMap(); return (nameToHash != null) ? nameToHash : Map.of();
} }
/** /**

View file

@ -147,7 +147,12 @@ public class ModuleDescriptorTest {
public void testRequiresWithRequires() { public void testRequiresWithRequires() {
Requires r1 = requires("foo"); Requires r1 = requires("foo");
ModuleDescriptor descriptor = ModuleDescriptor.newModule("m").requires(r1).build(); ModuleDescriptor descriptor = ModuleDescriptor.newModule("m").requires(r1).build();
Requires r2 = descriptor.requires().iterator().next(); assertEquals(descriptor.requires().size(), 2);
var iterator = descriptor.requires().iterator();
Requires r2 = iterator.next();
if (r2.name().equals("java.base")) {
r2 = iterator.next();
}
assertEquals(r1, r2); assertEquals(r1, r2);
} }