Skip to content

Commit

Permalink
Add test for export all
Browse files Browse the repository at this point in the history
Signed-off-by: JermaineHua <[email protected]>
  • Loading branch information
CrazyHZM committed Aug 29, 2024
1 parent a0376e1 commit 97cc8c5
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@

/**
* @author huazhongming
* @date 2024/8/7
* @since 4.4.0
*/
public class DynamicModuleApplicationContextInitializer
implements
ApplicationContextInitializer<ConfigurableApplicationContext> {
public class DynamicModuleExportApplicationContextInitializer
implements
ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
ModuleUtil.exportAllJDKModulePackageToAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,38 @@
*/
package com.alipay.sofa.boot.util;

import com.alipay.sofa.boot.log.SofaBootLoggerFactory;
import org.slf4j.Logger;

import java.lang.invoke.MethodHandle;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.Optional;

/**
* @author huazhongming
* @date 2024/8/7 16
* @since 4.4.0
*/
public class ModuleUtil {

Check warning on line 31 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#L31

Added line #L31 was not covered by tests

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;

static {
implAddOpensToAllUnnamed = createModuleMethodHandle("implAddOpensToAllUnnamed", String.class);
implAddOpensToAllUnnamed = createModuleMethodHandle("implAddOpensToAllUnnamed",
String.class);
implAddOpens = createModuleMethodHandle("implAddOpens", String.class);
implAddExportsToAllUnnamed = createModuleMethodHandle("implAddExportsToAllUnnamed", String.class);
implAddExportsToAllUnnamed = createModuleMethodHandle("implAddExportsToAllUnnamed",
String.class);
implAddExports = createModuleMethodHandle("implAddExports", String.class);

}

/**
*
* Export all JDK module packages to all.
*/
public static void exportAllJDKModulePackageToAll() {
try {
Expand All @@ -54,15 +60,18 @@ public static void exportAllJDKModulePackageToAll() {
}
}));
}
} catch (Throwable ignored) {

} catch (Throwable t) {
LOGGER.error("Failed to export all JDK module package to all", t);

Check warning on line 64 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#L63-L64

Added lines #L63 - L64 were not covered by tests
}
}

private static boolean isJDKModulePackage(String modulePackageName) {
return modulePackageName.startsWith("java.") || modulePackageName.startsWith("jdk.");
}

/**
* Export all module packages to all.
*/
public static void exportAllModulePackageToAll() {
try {
Map<String, Module> nameToModules = getNameToModule();

Check warning on line 77 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#L77

Added line #L77 was not covered by tests
Expand All @@ -72,56 +81,107 @@ public static void exportAllModulePackageToAll() {
addExportsToAll(module, pkgName);
}));

Check warning on line 82 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#L79-L82

Added lines #L79 - L82 were not covered by tests
}
} catch (Throwable ignored) {

} catch (Throwable t) {
LOGGER.error("Failed to export all module package to all", t);
}
}

Check warning on line 87 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#L84-L87

Added lines #L84 - L87 were not covered by tests

/**
* Updates this module to open a package to all unnamed modules.
*
* @param moduleName
* @param packageName
* @see java.lang.Module#implAddOpensToAllUnnamed(String)
*/
public static boolean addOpensToAllUnnamed(String moduleName, String packageName) {
return invokeModuleMethod(implAddOpensToAllUnnamed, moduleName, packageName);

Check warning on line 96 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#L96

Added line #L96 was not covered by tests
}

/**
* Updates this module to open a package to all unnamed modules.
*
* @param module
* @param packageName
*/
public static boolean addOpensToAllUnnamed(Module module, String packageName) {
return invokeModuleMethod(implAddOpensToAllUnnamed, module, packageName);

Check warning on line 106 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#L106

Added line #L106 was not covered by tests
}

/**
* Updates this module to export a package to all unnamed modules.
*
* @param moduleName
* @param packageName
*/
public static boolean addExportsToAllUnnamed(String moduleName, String packageName) {
return invokeModuleMethod(implAddExportsToAllUnnamed, moduleName, packageName);

Check warning on line 116 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#L116

Added line #L116 was not covered by tests
}

/**
* Updates this module to export a package to all unnamed modules.
*
* @param module
* @param packageName
*/
public static boolean addExportsToAllUnnamed(Module module, String packageName) {
return invokeModuleMethod(implAddExportsToAllUnnamed, module, packageName);

Check warning on line 126 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#L126

Added line #L126 was not covered by tests
}

/**
* Updates this module to open a package to another module.
*
* @param moduleName
* @param packageName
*/
public static boolean addOpensToAll(String moduleName, String packageName) {

return invokeModuleMethod(implAddOpens, moduleName, packageName);

Check warning on line 137 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#L137

Added line #L137 was not covered by tests
}

/**
* Updates this module to open a package to another module.
*
* @param module
* @param packageName
* @see java.lang.Module#implAddOpens(String)
*/
public static boolean addOpensToAll(Module module, String packageName) {

return invokeModuleMethod(implAddOpens, module, packageName);
}

/**
* Updates this module to export a package unconditionally.
* @param moduleName
* @param packageName
*/
public static boolean addExportsToAll(String moduleName, String packageName) {
return invokeModuleMethod(implAddExports, moduleName, packageName);

Check warning on line 157 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#L157

Added line #L157 was not covered by tests
}

/**
* Updates this module to export a package unconditionally.
* @param module
* @param packageName
* @see java.lang.Module#implAddExports(String)
*/
public static boolean addExportsToAll(Module module, String packageName) {
return invokeModuleMethod(implAddExports, module, packageName);
}


/**
* invoke ModuleLayer.bootLayer method
* invoke ModuleLayer method
*
* @param method
* @param moduleName
* @param packageName
* @return
*/
public static boolean invokeModuleMethod(MethodHandle method, String moduleName, String packageName) {
public static boolean invokeModuleMethod(MethodHandle method, String moduleName,
String packageName) {
Optional<Module> findModule = ModuleLayer.boot().findModule(moduleName);

Check warning on line 179 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#L179

Added line #L179 was not covered by tests
if (findModule.isPresent()) {
try {
return invokeModuleMethod(method, findModule.get(), packageName);
} catch (Throwable e) {
// ignore
} catch (Throwable t) {
LOGGER.error("Failed to invoke ModuleLayer method: {}", method, t);

Check warning on line 184 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#L182-L184

Added lines #L182 - L184 were not covered by tests
}
}
return false;

Check warning on line 187 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#L187

Added line #L187 was not covered by tests
Expand All @@ -131,8 +191,8 @@ public static boolean invokeModuleMethod(MethodHandle method, Module module, Str
try {
method.invoke(module, packageName);
return true;
} catch (Throwable e) {
// ignore
} catch (Throwable t) {
LOGGER.error("Failed to invoke Module method: {}", method, t);

Check warning on line 195 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#L194-L195

Added lines #L194 - L195 were not covered by tests
}
return false;

Check warning on line 197 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#L197

Added line #L197 was not covered by tests
}
Expand All @@ -144,12 +204,13 @@ public static boolean invokeModuleMethod(MethodHandle method, Module module, Str
* @param parameterTypes
* @return MethodHandle
*/
private static MethodHandle createModuleMethodHandle(String methodName, Class<?>... parameterTypes) {
private static MethodHandle createModuleMethodHandle(String methodName,
Class<?>... parameterTypes) {
try {
return UnsafeUtil.implLookup().unreflect(
Module.class.getDeclaredMethod(methodName, parameterTypes));
} catch (Throwable e) {
// ignore
Module.class.getDeclaredMethod(methodName, parameterTypes));
} catch (Throwable t) {
LOGGER.error("Failed to create Module method handle: {}", methodName, t);

Check warning on line 213 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#L212-L213

Added lines #L212 - L213 were not covered by tests
}
return null;

Check warning on line 215 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#L215

Added line #L215 was not covered by tests
}
Expand All @@ -167,13 +228,13 @@ private static Object getModuleLayerFieldsValue(String fieldName) {
Field field = moduleLayerClass.getDeclaredField(fieldName);
return UnsafeUtil.implLookup().unreflectVarHandle(field).get(moduleLayer);
} catch (Throwable t) {
// ignore
LOGGER.error("Failed to get ModuleLayer field value: {}", fieldName, t);

Check warning on line 231 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#L230-L231

Added lines #L230 - L231 were not covered by tests
}
return null;

Check warning on line 233 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#L233

Added line #L233 was not covered by tests
}

/**
* Get all Modules from System.bootLayer
* Get all modules from System.bootLayer
*
* @return modules
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

/**
* @author huazhongming
* @date 2024/8/7 16
* @since 4.4.0
*/
public class UnsafeUtil {

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

View check run for this annotation

Codecov / codecov/patch

sofa-boot-project/sofa-boot/src/main/java/com/alipay/sofa/boot/util/UnsafeUtil.java#L28

Added line #L28 was not covered by tests
Expand All @@ -38,7 +37,6 @@ public static Unsafe unsafe() {
theUnsafeField.setAccessible(true);
unsafe = (Unsafe) theUnsafeField.get(null);
} catch (Throwable ignored) {

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

View check run for this annotation

Codecov / codecov/patch

sofa-boot-project/sofa-boot/src/main/java/com/alipay/sofa/boot/util/UnsafeUtil.java#L39

Added line #L39 was not covered by tests
// ignored
}
UNSAFE = unsafe;
}
Expand All @@ -55,8 +53,7 @@ public static MethodHandles.Lookup implLookup() {
long offset = unsafe().staticFieldOffset(implLookupField);
IMPL_LOOKUP = (MethodHandles.Lookup) unsafe().getObject(
unsafe().staticFieldBase(implLookupField), offset);
} catch (Throwable e) {
// ignored
} catch (Throwable ignored) {

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

View check run for this annotation

Codecov / codecov/patch

sofa-boot-project/sofa-boot/src/main/java/com/alipay/sofa/boot/util/UnsafeUtil.java#L56

Added line #L56 was not covered by tests
}
}
return IMPL_LOOKUP;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ org.springframework.boot.SpringApplicationRunListener=\
# Initializers
org.springframework.context.ApplicationContextInitializer=\
com.alipay.sofa.boot.compatibility.CompatibilityVerifierApplicationContextInitializer,\
com.alipay.sofa.boot.Initializer.DynamicModuleApplicationContextInitializer
com.alipay.sofa.boot.Initializer.DynamicModuleExportApplicationContextInitializer
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.boot.util;

import org.junit.jupiter.api.Test;

import java.lang.reflect.InaccessibleObjectException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

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

@Test
public void testExportAllJDKModulePackageToAll() throws NoSuchMethodException {

Exception exception = assertThrows(InaccessibleObjectException.class,() -> {
Method newByteChannel0Method = Files.class.getDeclaredMethod("provider", Path.class);
newByteChannel0Method.setAccessible(true);
});

assertTrue(exception.getMessage().contains("module java.base does not \"opens java.nio.file\" to unnamed module"));

ModuleUtil.exportAllJDKModulePackageToAll();

java.lang.reflect.Method newByteChannel0Method = Files.class.getDeclaredMethod("provider", Path.class);
newByteChannel0Method.setAccessible(true);
}
}

0 comments on commit 97cc8c5

Please sign in to comment.