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 24ac036
Show file tree
Hide file tree
Showing 2 changed files with 15 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 {

Check warning on line 32 in sofa-boot-project/sofa-boot/src/main/java/com/alipay/sofa/boot/util/ModuleUtil.java

View check run for this annotation

Codecov / codecov/patch

sofa-boot-project/sofa-boot/src/main/java/com/alipay/sofa/boot/util/ModuleUtil.java#L32

Added line #L32 was not covered by tests

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,7 +61,7 @@ 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);
Expand All @@ -76,8 +83,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 Expand Up @@ -245,4 +251,7 @@ public static Map<String, Module> getNameToModule() {
return (Map<String, Module>) getModuleLayerFieldsValue("nameToModule");
}

public static void reset() {
isExported.set(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class AutoModuleExportApplicationContextInitializerTests {

@BeforeEach
void setUp() {
ModuleUtil.reset();
contextRunner = new ApplicationContextRunner()
.withInitializer(new AutoModuleExportApplicationContextInitializer());
}
Expand Down

0 comments on commit 24ac036

Please sign in to comment.