Skip to content
Permalink
Browse files

8238599: Refactor and simplify implAddOpensToAllUnnamed

Reviewed-by: alanb
  • Loading branch information
cl4es committed Feb 7, 2020
1 parent 9f21d80 commit 9abc1a4441372b368a3acbc2a4ade45c9752737d
@@ -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 @@ private void implAddExportsOrOpens(String pn,
}

/**
* 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 @@ void implAddOpensToAllUnnamed(Iterator<String> iterator) {
// 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 @@ void implAddOpensToAllUnnamed(Iterator<String> iterator) {
// update VM to export the package
addExportsToAllUnnamed0(this, pn);
}
this.openPackages = openPackages;
}


// -- services --

/**
@@ -56,6 +56,7 @@
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 void addOpens(Module m, String pn, Module other) {
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);
@@ -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.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 @@
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.
@@ -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 @@ private static void addIllegalAccess(ModuleFinder upgradeModulePath,
}

// 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 @@ private static void addIllegalAccess(ModuleFinder upgradeModulePath,
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 @@ private static void addIllegalAccess(ModuleFinder upgradeModulePath,

// 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 @@ static String option(String prefix) {
}
}

/**
* 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.

0 comments on commit 9abc1a4

Please sign in to comment.