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 88c7e01
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 31 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 {

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);
}
}

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();
Expand All @@ -72,12 +81,14 @@ public static void exportAllModulePackageToAll() {
addExportsToAll(module, pkgName);
}));
}
} catch (Throwable ignored) {

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

/**
* Updates this module to open a package to all unnamed modules.
*
* @param moduleName
* @param packageName
* @see java.lang.Module#implAddOpensToAllUnnamed(String)
Expand All @@ -87,6 +98,52 @@ public static boolean addOpensToAllUnnamed(String moduleName, String packageName
}

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

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

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

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

return invokeModuleMethod(implAddOpens, moduleName, packageName);
}

/**
* Updates this module to open a package to another module.
*
* @param module
* @param packageName
Expand All @@ -98,6 +155,17 @@ public static boolean addOpensToAll(Module module, String packageName) {
}

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

/**
* Updates this module to export a package unconditionally.
* @param module
* @param packageName
* @see java.lang.Module#implAddExports(String)
Expand All @@ -106,22 +174,22 @@ 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);
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);
}
}
return false;
Expand All @@ -131,8 +199,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);
}
return false;
}
Expand All @@ -144,12 +212,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);
}
return null;
}
Expand All @@ -167,13 +236,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);
}
return null;
}

/**
* 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 {
Expand All @@ -38,7 +37,6 @@ public static Unsafe unsafe() {
theUnsafeField.setAccessible(true);
unsafe = (Unsafe) theUnsafeField.get(null);
} catch (Throwable ignored) {
// 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) {
}
}
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 88c7e01

Please sign in to comment.