8238599: Refactor and simplify implAddOpensToAllUnnamed

Reviewed-by: alanb
This commit is contained in:
Claes Redestad 2020-02-07 09:47:25 +01:00
parent 9f21d809ab
commit 9abc1a4441
4 changed files with 24 additions and 38 deletions

View File

@ -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.
*
* 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
* all unnamed modules.
* Updates a module to open all packages in the given sets to all unnamed
* modules.
*
* @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()) {
throw new IllegalStateException("Module system already initialized");
}
@ -923,12 +923,17 @@ public final class Module implements AnnotatedElement {
// the packages to all unnamed modules.
Map<String, Set<Module>> openPackages = this.openPackages;
if (openPackages == null) {
openPackages = new HashMap<>();
openPackages = new HashMap<>((4 * (concealedPkgs.size() + exportedPkgs.size()) / 3) + 1);
} else {
openPackages = new HashMap<>(openPackages);
}
while (iterator.hasNext()) {
String pn = iterator.next();
implAddOpensToAllUnnamed(concealedPkgs, openPackages);
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);
if (prev != null) {
prev.add(ALL_UNNAMED_MODULE);
@ -937,10 +942,8 @@ public final class Module implements AnnotatedElement {
// update VM to export the package
addExportsToAllUnnamed0(this, pn);
}
this.openPackages = openPackages;
}
// -- services --
/**

View File

@ -56,6 +56,7 @@ import java.util.Objects;
import java.util.Properties;
import java.util.PropertyPermission;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.function.Supplier;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
@ -2234,8 +2235,8 @@ public final class System {
public void addOpensToAllUnnamed(Module m, String pn) {
m.implAddOpensToAllUnnamed(pn);
}
public void addOpensToAllUnnamed(Module m, Iterator<String> packages) {
m.implAddOpensToAllUnnamed(packages);
public void addOpensToAllUnnamed(Module m, Set<String> concealedPackages, Set<String> exportedPackages) {
m.implAddOpensToAllUnnamed(concealedPackages, exportedPackages);
}
public void addUses(Module m, Class<?> service) {
m.implAddUses(service);

View File

@ -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.
*
* 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.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
@ -221,9 +222,9 @@ public interface JavaLangAccess {
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.

View File

@ -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.
*
* 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
Set<String> emptySet = Set.of();
for (Module m : bootLayer.modules()) {
ModuleDescriptor descriptor = m.getDescriptor();
String name = m.getName();
@ -816,8 +817,8 @@ public final class ModuleBootstrap {
continue;
}
Set<String> concealedPackages = concealedPackagesToOpen.getOrDefault(name, Set.of());
Set<String> exportedPackages = exportedPackagesToOpen.getOrDefault(name, Set.of());
Set<String> concealedPackages = concealedPackagesToOpen.getOrDefault(name, emptySet);
Set<String> exportedPackages = exportedPackagesToOpen.getOrDefault(name, emptySet);
// refresh the set of concealed and exported packages if needed
if (extraExportsOrOpens) {
@ -850,8 +851,7 @@ public final class ModuleBootstrap {
// open the packages to unnamed modules
JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
jla.addOpensToAllUnnamed(m, concat(concealedPackages.iterator(),
exportedPackages.iterator()));
jla.addOpensToAllUnnamed(m, concealedPackages, exportedPackages);
}
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
* for use after startup.