8238599: Refactor and simplify implAddOpensToAllUnnamed
Reviewed-by: alanb
This commit is contained in:
parent
9f21d809ab
commit
9abc1a4441
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -909,12 +909,12 @@ public final class Module implements AnnotatedElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates a module to open all packages returned by the given iterator to
|
* Updates a module to open all packages in the given sets to all unnamed
|
||||||
* all unnamed modules.
|
* modules.
|
||||||
*
|
*
|
||||||
* @apiNote Used during startup to open packages for illegal access.
|
* @apiNote Used during startup to open packages for illegal access.
|
||||||
*/
|
*/
|
||||||
void implAddOpensToAllUnnamed(Iterator<String> iterator) {
|
void implAddOpensToAllUnnamed(Set<String> concealedPkgs, Set<String> exportedPkgs) {
|
||||||
if (jdk.internal.misc.VM.isModuleSystemInited()) {
|
if (jdk.internal.misc.VM.isModuleSystemInited()) {
|
||||||
throw new IllegalStateException("Module system already initialized");
|
throw new IllegalStateException("Module system already initialized");
|
||||||
}
|
}
|
||||||
@ -923,12 +923,17 @@ public final class Module implements AnnotatedElement {
|
|||||||
// the packages to all unnamed modules.
|
// the packages to all unnamed modules.
|
||||||
Map<String, Set<Module>> openPackages = this.openPackages;
|
Map<String, Set<Module>> openPackages = this.openPackages;
|
||||||
if (openPackages == null) {
|
if (openPackages == null) {
|
||||||
openPackages = new HashMap<>();
|
openPackages = new HashMap<>((4 * (concealedPkgs.size() + exportedPkgs.size()) / 3) + 1);
|
||||||
} else {
|
} else {
|
||||||
openPackages = new HashMap<>(openPackages);
|
openPackages = new HashMap<>(openPackages);
|
||||||
}
|
}
|
||||||
while (iterator.hasNext()) {
|
implAddOpensToAllUnnamed(concealedPkgs, openPackages);
|
||||||
String pn = iterator.next();
|
implAddOpensToAllUnnamed(exportedPkgs, openPackages);
|
||||||
|
this.openPackages = openPackages;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void implAddOpensToAllUnnamed(Set<String> pkgs, Map<String, Set<Module>> openPackages) {
|
||||||
|
for (String pn : pkgs) {
|
||||||
Set<Module> prev = openPackages.putIfAbsent(pn, ALL_UNNAMED_MODULE_SET);
|
Set<Module> prev = openPackages.putIfAbsent(pn, ALL_UNNAMED_MODULE_SET);
|
||||||
if (prev != null) {
|
if (prev != null) {
|
||||||
prev.add(ALL_UNNAMED_MODULE);
|
prev.add(ALL_UNNAMED_MODULE);
|
||||||
@ -937,10 +942,8 @@ public final class Module implements AnnotatedElement {
|
|||||||
// update VM to export the package
|
// update VM to export the package
|
||||||
addExportsToAllUnnamed0(this, pn);
|
addExportsToAllUnnamed0(this, pn);
|
||||||
}
|
}
|
||||||
this.openPackages = openPackages;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// -- services --
|
// -- services --
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -56,6 +56,7 @@ import java.util.Objects;
|
|||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.PropertyPermission;
|
import java.util.PropertyPermission;
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
@ -2234,8 +2235,8 @@ public final class System {
|
|||||||
public void addOpensToAllUnnamed(Module m, String pn) {
|
public void addOpensToAllUnnamed(Module m, String pn) {
|
||||||
m.implAddOpensToAllUnnamed(pn);
|
m.implAddOpensToAllUnnamed(pn);
|
||||||
}
|
}
|
||||||
public void addOpensToAllUnnamed(Module m, Iterator<String> packages) {
|
public void addOpensToAllUnnamed(Module m, Set<String> concealedPackages, Set<String> exportedPackages) {
|
||||||
m.implAddOpensToAllUnnamed(packages);
|
m.implAddOpensToAllUnnamed(concealedPackages, exportedPackages);
|
||||||
}
|
}
|
||||||
public void addUses(Module m, Class<?> service) {
|
public void addUses(Module m, Class<?> service) {
|
||||||
m.implAddUses(service);
|
m.implAddUses(service);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -37,6 +37,7 @@ import java.security.ProtectionDomain;
|
|||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@ -221,9 +222,9 @@ public interface JavaLangAccess {
|
|||||||
void addOpensToAllUnnamed(Module m, String pkg);
|
void addOpensToAllUnnamed(Module m, String pkg);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates module m to open all packages returned by the given iterator.
|
* Updates module m to open all packages in the given sets.
|
||||||
*/
|
*/
|
||||||
void addOpensToAllUnnamed(Module m, Iterator<String> packages);
|
void addOpensToAllUnnamed(Module m, Set<String> concealedPkgs, Set<String> exportedPkgs);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates module m to use a service.
|
* Updates module m to use a service.
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -801,6 +801,7 @@ public final class ModuleBootstrap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// open specific packages in the system modules
|
// open specific packages in the system modules
|
||||||
|
Set<String> emptySet = Set.of();
|
||||||
for (Module m : bootLayer.modules()) {
|
for (Module m : bootLayer.modules()) {
|
||||||
ModuleDescriptor descriptor = m.getDescriptor();
|
ModuleDescriptor descriptor = m.getDescriptor();
|
||||||
String name = m.getName();
|
String name = m.getName();
|
||||||
@ -816,8 +817,8 @@ public final class ModuleBootstrap {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
Set<String> concealedPackages = concealedPackagesToOpen.getOrDefault(name, Set.of());
|
Set<String> concealedPackages = concealedPackagesToOpen.getOrDefault(name, emptySet);
|
||||||
Set<String> exportedPackages = exportedPackagesToOpen.getOrDefault(name, Set.of());
|
Set<String> exportedPackages = exportedPackagesToOpen.getOrDefault(name, emptySet);
|
||||||
|
|
||||||
// refresh the set of concealed and exported packages if needed
|
// refresh the set of concealed and exported packages if needed
|
||||||
if (extraExportsOrOpens) {
|
if (extraExportsOrOpens) {
|
||||||
@ -850,8 +851,7 @@ public final class ModuleBootstrap {
|
|||||||
|
|
||||||
// open the packages to unnamed modules
|
// open the packages to unnamed modules
|
||||||
JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
|
JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
|
||||||
jla.addOpensToAllUnnamed(m, concat(concealedPackages.iterator(),
|
jla.addOpensToAllUnnamed(m, concealedPackages, exportedPackages);
|
||||||
exportedPackages.iterator()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.complete();
|
builder.complete();
|
||||||
@ -995,25 +995,6 @@ public final class ModuleBootstrap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns an iterator that yields all elements of the first iterator
|
|
||||||
* followed by all the elements of the second iterator.
|
|
||||||
*/
|
|
||||||
static <T> Iterator<T> concat(Iterator<T> iterator1, Iterator<T> iterator2) {
|
|
||||||
return new Iterator<T>() {
|
|
||||||
@Override
|
|
||||||
public boolean hasNext() {
|
|
||||||
return iterator1.hasNext() || iterator2.hasNext();
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public T next() {
|
|
||||||
if (iterator1.hasNext()) return iterator1.next();
|
|
||||||
if (iterator2.hasNext()) return iterator2.next();
|
|
||||||
throw new NoSuchElementException();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wraps a (potentially not thread safe) ModuleFinder created during startup
|
* Wraps a (potentially not thread safe) ModuleFinder created during startup
|
||||||
* for use after startup.
|
* for use after startup.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user