java ModuleLayer : ModuleLayer.Controller 添加方法不起作用
java ModuleLayer : ModuleLayer.Controller add methods don't work
我正在使用 Netbeans 12.5 和 java16
在一个 Java 模块化项目中,我有 2 个模块
Modular-Testing(/*The Project Name*/)
|
|----Consumer(/*Module Name*/)
| |
|--classes(/*Folder Name*/)
|
|--main(/*package name*/)
|
|--CustomModuleTest.java(Main Class)
|--Test_Module_A(/*Module Name*/)
|
|--classes(/*Folder Name*/)
|
|--package_a(/*package name*/)
|
|--ClassA.java
CustomModuleTest.java
public static void main(String[] args)throws Exception
{
Path baseDir=Path.of("C:/Users/Home/Documents/NetBeansProjects/Netbeans/Modular-Testing/build/modules/");
//Create the configuration for these two modules with boot-layer configuration as it's parent
Configuration level1=Configuration.resolve
(
ModuleFinder.of(baseDir.resolve("Consumer"),baseDir.resolve("Test_Module_A"))
,List.of(ModuleLayer.boot().configuration())
,ModuleFinder.of()
,List.of("Consumer","Test_Module_A")
);
//create the module layer with only one system class loader and boot layer as parent
ModuleLayer.Controller layer1=ModuleLayer.defineModulesWithOneLoader
(
level1
,List.of(ModuleLayer.boot())
,ClassLoader.getSystemClassLoader()
);
//this is the main purpose of this test. I want to call ClassA.callMe() via reflection after I have dynamically achieved the permissions for it by first
//Making Consumer Module read(requires) Test_Module_A
//Making Test_Module_A open it's package(package_a) to Consumer
//With this consumer should have full reflective access to Test_Module_A(or that's what i had hoped but it didn't work)
//Require(Read)
//Make Module Consumer--------------->Test_Module_A
layer1.addReads
(
layer1.layer().findModule("Consumer").get()
,layer1.layer().findModule("Test_Module_A").get()
);
//Open package_a
//Make Module Test_Module_A-------------------->Consumer
layer1.addOpens
(
layer1.layer().findModule("Test_Module_A").get()
,"package_a"
,layer1.layer().findModule("Consumer").get()
);
//Do the actual Test
Class targetClass=layer1.layer().findLoader("Test_Module_A").loadClass("package_a.ClassA");
Method method=targetClass.getDeclaredMethod("callMe");
method.trySetAccessible();
method.invoke(null); //<---------Crashes Here(It's a static method so no object reference)
}
A 类什么都不做
package package_a;
public class ClassA
{
private static void callMe()
{
System.out.println("Hooray You Called Me Using ModuleLayers!!!");
}
}
消费者和 Test_Module_A 的模块信息都是空的
module Consumer{}
module Test_Module_A{}
这两个模块的模块信息都是空的,因为我想使用 ModuleLayer.Controller
动态添加 Opens/Exports
class 的位置没有问题,该方法可以使用 trySetAccessible() 进行访问,但是一旦我调用 invoke 我就会收到此错误
Exception in thread "main" java.lang.IllegalAccessException: class main.CustomModuleTest (in module Consumer) cannot access class package_a.ClassA (in module Test_Module_A) because module Test_Module_A does not export package_a to module Consumer
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:385)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:687)
at java.base/java.lang.reflect.Method.invoke(Method.java:559)
测试是 运行 来自 CustomModuleTest.java 消费者模块内的主要方法
addReads 和 addOpens 方法要么不起作用,要么我只是用错了
有什么想法吗?
您正在创建一个包含两个模块的新模块层,并建立其中一个模块对另一个模块的访问。但是,您实际上想要为当前 运行 主要方法建立对这些模块之一的访问。
当前 运行 main 方法不能成为它刚刚创建的新模块层的一部分。假设标准启动器设置,它属于 Controller
模块,由系统 class 加载程序作为引导层的一部分加载。
因此,您必须更改代码以授予对已加载的 Controller
模块的访问权限,而不是授予对新层中同名新模块的访问权限。由于新模块层的新Controller
模块没有任何作用,您可以将其从层配置中删除。
public static void main(String[] args) throws Exception {
Path baseDir = Path.of(
"C:/Users/Home/Documents/NetBeansProjects/Netbeans/Modular-Testing/build/modules/");
// Create the configuration for the new module
// with boot-layer configuration as it's parent
Configuration level1=Configuration.resolve
(
ModuleFinder.of(baseDir.resolve("Test_Module_A")),
List.of(ModuleLayer.boot().configuration()),
ModuleFinder.of(), List.of("Test_Module_A")
);
//create the module layer with only one class loader and boot layer as parent
var layer1Controller = ModuleLayer.defineModulesWithOneLoader(
level1, List.of(ModuleLayer.boot()), ClassLoader.getSystemClassLoader()
);
var testModA = layer1Controller.layer().findModule("Test_Module_A").get();
var myModule = CustomModuleTest.class.getModule();
myModule.addReads(testModA); // add read edge
layer1Controller.addOpens(testModA, "package_a", myModule); // open package_a
//Do the actual Test
Class<?> targetClass = testModA.getClassLoader().loadClass("package_a.ClassA");
Method method = targetClass.getDeclaredMethod("callMe");
method.setAccessible(true);
method.invoke(null);
}
我正在使用 Netbeans 12.5 和 java16
在一个 Java 模块化项目中,我有 2 个模块
Modular-Testing(/*The Project Name*/)
|
|----Consumer(/*Module Name*/)
| |
|--classes(/*Folder Name*/)
|
|--main(/*package name*/)
|
|--CustomModuleTest.java(Main Class)
|--Test_Module_A(/*Module Name*/)
|
|--classes(/*Folder Name*/)
|
|--package_a(/*package name*/)
|
|--ClassA.java
CustomModuleTest.java
public static void main(String[] args)throws Exception
{
Path baseDir=Path.of("C:/Users/Home/Documents/NetBeansProjects/Netbeans/Modular-Testing/build/modules/");
//Create the configuration for these two modules with boot-layer configuration as it's parent
Configuration level1=Configuration.resolve
(
ModuleFinder.of(baseDir.resolve("Consumer"),baseDir.resolve("Test_Module_A"))
,List.of(ModuleLayer.boot().configuration())
,ModuleFinder.of()
,List.of("Consumer","Test_Module_A")
);
//create the module layer with only one system class loader and boot layer as parent
ModuleLayer.Controller layer1=ModuleLayer.defineModulesWithOneLoader
(
level1
,List.of(ModuleLayer.boot())
,ClassLoader.getSystemClassLoader()
);
//this is the main purpose of this test. I want to call ClassA.callMe() via reflection after I have dynamically achieved the permissions for it by first
//Making Consumer Module read(requires) Test_Module_A
//Making Test_Module_A open it's package(package_a) to Consumer
//With this consumer should have full reflective access to Test_Module_A(or that's what i had hoped but it didn't work)
//Require(Read)
//Make Module Consumer--------------->Test_Module_A
layer1.addReads
(
layer1.layer().findModule("Consumer").get()
,layer1.layer().findModule("Test_Module_A").get()
);
//Open package_a
//Make Module Test_Module_A-------------------->Consumer
layer1.addOpens
(
layer1.layer().findModule("Test_Module_A").get()
,"package_a"
,layer1.layer().findModule("Consumer").get()
);
//Do the actual Test
Class targetClass=layer1.layer().findLoader("Test_Module_A").loadClass("package_a.ClassA");
Method method=targetClass.getDeclaredMethod("callMe");
method.trySetAccessible();
method.invoke(null); //<---------Crashes Here(It's a static method so no object reference)
}
A 类什么都不做
package package_a;
public class ClassA
{
private static void callMe()
{
System.out.println("Hooray You Called Me Using ModuleLayers!!!");
}
}
消费者和 Test_Module_A 的模块信息都是空的
module Consumer{}
module Test_Module_A{}
这两个模块的模块信息都是空的,因为我想使用 ModuleLayer.Controller
动态添加 Opens/Exportsclass 的位置没有问题,该方法可以使用 trySetAccessible() 进行访问,但是一旦我调用 invoke 我就会收到此错误
Exception in thread "main" java.lang.IllegalAccessException: class main.CustomModuleTest (in module Consumer) cannot access class package_a.ClassA (in module Test_Module_A) because module Test_Module_A does not export package_a to module Consumer
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:385)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:687)
at java.base/java.lang.reflect.Method.invoke(Method.java:559)
测试是 运行 来自 CustomModuleTest.java 消费者模块内的主要方法 addReads 和 addOpens 方法要么不起作用,要么我只是用错了
有什么想法吗?
您正在创建一个包含两个模块的新模块层,并建立其中一个模块对另一个模块的访问。但是,您实际上想要为当前 运行 主要方法建立对这些模块之一的访问。
当前 运行 main 方法不能成为它刚刚创建的新模块层的一部分。假设标准启动器设置,它属于 Controller
模块,由系统 class 加载程序作为引导层的一部分加载。
因此,您必须更改代码以授予对已加载的 Controller
模块的访问权限,而不是授予对新层中同名新模块的访问权限。由于新模块层的新Controller
模块没有任何作用,您可以将其从层配置中删除。
public static void main(String[] args) throws Exception {
Path baseDir = Path.of(
"C:/Users/Home/Documents/NetBeansProjects/Netbeans/Modular-Testing/build/modules/");
// Create the configuration for the new module
// with boot-layer configuration as it's parent
Configuration level1=Configuration.resolve
(
ModuleFinder.of(baseDir.resolve("Test_Module_A")),
List.of(ModuleLayer.boot().configuration()),
ModuleFinder.of(), List.of("Test_Module_A")
);
//create the module layer with only one class loader and boot layer as parent
var layer1Controller = ModuleLayer.defineModulesWithOneLoader(
level1, List.of(ModuleLayer.boot()), ClassLoader.getSystemClassLoader()
);
var testModA = layer1Controller.layer().findModule("Test_Module_A").get();
var myModule = CustomModuleTest.class.getModule();
myModule.addReads(testModA); // add read edge
layer1Controller.addOpens(testModA, "package_a", myModule); // open package_a
//Do the actual Test
Class<?> targetClass = testModA.getClassLoader().loadClass("package_a.ClassA");
Method method = targetClass.getDeclaredMethod("callMe");
method.setAccessible(true);
method.invoke(null);
}