- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Netbeans 12.5 和 java 16
在一个 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!!!");
}
}
Consumer 和 Test_Module_A 的模块信息都是空的
module Consumer{}
module Test_Module_A{}
这两个模块的模块信息都是空的,因为我想使用 ModuleLayer.Controller 动态添加打开/导出
该类的位置没有问题,该方法可以使用 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 main 方法运行addReads 和 addOpens 方法要么不起作用,要么我只是用错了
有什么想法吗?
最佳答案
您正在创建一个包含两个模块的新模块层,并建立其中一个模块对另一个模块的访问。但是,您实际上想要为当前运行的 main 方法建立对这些模块之一的访问。
当前运行的 main 方法不能成为它刚刚创建的新模块层的一部分。假设标准启动器设置,它属于一个 Controller
模块,由系统类加载器作为引导层的一部分加载。
因此,您必须更改代码以授予对已加载的 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);
}
关于java ModuleLayer : ModuleLayer. Controller 添加方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69932308/
我创建了一个用户可以添加测试的字段。这一切运行顺利我只希望当用户点击(添加另一个测试)然后上一个(添加另一个测试)删除并且这个显示在新字段中。 所有运行良好的唯一问题是点击(添加另一个字段)之前添加另
String[] option = {"Adlawan", "Angeles", "Arreza", "Benenoso", "Bermas", "Brebant
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我正在努力将 jQuery 滚动功能添加到 nav-tab (Bootstrap 3)。我希望用户能够选择他们想要的选项卡,并在选项卡内容中有一个可以平滑滚动到 anchor 的链接。这是我的代码,可
我正在尝试在用户登录后再添加 2 个 ui 选项卡。首先,我尝试做一个之后。 $('#slideshow').tabs('remove', '4'); $("#slideshow ul li:last
我有一个包含选择元素的表单,我想通过选择添加和删除其中一些元素。这是html代码(这里也有jsfiddle http://jsfiddle.net/txhajy2w/):
正在写这个: view.backgroundColor = UIColor.white.withAlphaComponent(0.9) 等同于: view.backgroundColor = UICo
好的,如果其中有任何信息,我想将这些列添加到一起。所以说我有 账户 1 2 3 . 有 4 个帐户空间,但只有 3 个帐户。我如何创建 java 脚本来添加它。 最佳答案 Live Example H
我想知道是否有一种有效的预制算法来确定一组数字的和/差是否可以等于不同的数字。示例: 5、8、10、2,使用 + 或 - 等于 9。5 - 8 = -3 + 10 = 7 + 2 = 9 如果有一个预
我似乎有一个卡住的 git repo。它卡在所有基本的添加、提交命令上,git push 返回所有内容为最新的。 从其他帖子我已经完成了 git gc 和 git fsck/ 我认为基本的调试步骤是
我的 Oracle SQL 查询如下- Q1- select hca.account_number, hca.attribute3, SUM(rcl.extended_amou
我正在阅读 http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingG
我正在尝试添加一个“加载更多”按钮并限制下面的结果,这样投资组合页面中就不会同时加载 1000 个内容,如下所示:http://typesetdesign.com/portfolio/ 我对 PHP
我遇到这个问题,我添加了 8 个文本框,它工作正常,但是当我添加更多文本框(如 16 个文本框)时,它不会添加最后一个文本框。有人遇到过这个问题吗?提前致谢。 Live Link: JAVASCRIP
add/remove clone first row default not delete 添加/删除克隆第一行默认不删除&并获取正确的SrNo(例如:添加3行并在看到问题后删除SrNo.2)
我编码this ,但删除按钮不起作用。我在控制台中没有任何错误.. var counter = 0; var dataList = document.getElementById('materi
我有一个类似数组的对象: [1:数组[10]、2:数组[2]、3:数组[2]、4:数组[2]、5:数组[3]、6:数组[1]] 我正在尝试删除前两个元素,执行一些操作,然后将它们再次插入到同一位置。
使用的 Delphi 版本:2007 你好, 我有一个 Tecord 数组 TInfo = Record Name : String; Price : Integer; end; var Info
我使用了基本的 gridster 代码,然后我声明了通过按钮添加和删除小部件的函数它工作正常但是当我将调整大小功能添加到上面的代码中时,它都不起作用(我的意思是调整大小,添加和删除小部件) 我的js代
title 323 323 323 title 323 323 323 title 323 323 323 JS $(document).keydown(function(e){
我是一名优秀的程序员,十分优秀!