Skip to content

Commit

Permalink
Auto module export with reentrant protection
Browse files Browse the repository at this point in the history
Signed-off-by: JermaineHua <[email protected]>
  • Loading branch information
CrazyHZM committed Sep 23, 2024
1 parent 6d844ab commit e95642b
Showing 1 changed file with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,29 @@
import java.lang.reflect.Field;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* @author huazhongming
* @since 4.4.0
*/
public class ModuleUtil {

private static final Logger LOGGER = SofaBootLoggerFactory
.getLogger(ModuleUtil.class);
private static final Logger LOGGER = SofaBootLoggerFactory
.getLogger(ModuleUtil.class);

private static final MethodHandle implAddOpensToAllUnnamed;

private static final MethodHandle implAddOpens;

private static final MethodHandle implAddExportsToAllUnnamed;

private static final MethodHandle implAddExports;

private static final Map<String, Module> nameToModules;

private static final AtomicBoolean isExported = new AtomicBoolean(false);

static {
implAddOpensToAllUnnamed = createModuleMethodHandle("implAddOpensToAllUnnamed",
String.class);
Expand All @@ -54,13 +61,14 @@ public class ModuleUtil {
*/
public static void exportAllJDKModulePackageToAll() {
try {
if (nameToModules != null) {
if (isExported.compareAndSet(false,true) && nameToModules != null) {
nameToModules.forEach((name, module) -> module.getPackages().forEach(pkgName -> {
if (isJDKModulePackage(pkgName)) {
addOpensToAll(module, pkgName);
addExportsToAll(module, pkgName);
}
}));
isExported.compareAndExchange(false,true);
}
} catch (Throwable t) {
LOGGER.error("Failed to export all JDK module package to all", t);
Expand All @@ -76,8 +84,7 @@ private static boolean isJDKModulePackage(String modulePackageName) {
*/
public static void exportAllModulePackageToAll() {
try {
Map<String, Module> nameToModules = getNameToModule();
if (nameToModules != null) {
if (isExported.compareAndSet(false,true) && nameToModules != null) {
nameToModules.forEach((name, module) -> module.getPackages().forEach(pkgName -> {
addOpensToAll(module, pkgName);
addExportsToAll(module, pkgName);
Expand Down

0 comments on commit e95642b

Please sign in to comment.