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 e289863
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/**
* @author huazhongming
* @date 2024/8/7 16
* @date 2024/8/7
* @since 4.4.0
*/
public class ModuleUtil {
Expand All @@ -33,15 +33,16 @@ public class ModuleUtil {
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 @@ -63,6 +64,9 @@ 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 @@ -78,6 +82,8 @@ public static void exportAllModulePackageToAll() {
}

/**
* 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 +93,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 +150,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,16 +169,16 @@ 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 {
Expand Down Expand Up @@ -144,10 +207,11 @@ 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));
Module.class.getDeclaredMethod(methodName, parameterTypes));
} catch (Throwable e) {
// ignore
}
Expand All @@ -173,7 +237,7 @@ private static Object getModuleLayerFieldsValue(String fieldName) {
}

/**
* 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,7 @@

/**
* @author huazhongming
* @date 2024/8/7 16
* @date 2024/8/7
* @since 4.4.0
*/
public class UnsafeUtil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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
* @date 2024/8/29 14
* @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 e289863

Please sign in to comment.