如何在测试运行时将测试添加到生产代码中,以便它们都在同一个Java 9模块中并且可以使用reflections相互访问?
我到目前为止已经尝试过:
module-info.java
)→效果很好,但不是我想要的。--patch-module
虚拟地添加另一个文件夹(到使用--module-path
指定的文件夹中)→它使用“普通”代码但不使用反射,它找不到--module-path
指定的类。--patch-module
指定我的测试和生产代码→它只会在我首先指定的文件夹中找到类。--add-opens mymodule/mypackge=mymodule
或...=ALL-UNNAMED
以将其打开以进行反射→看起来没有任何效果。所以我的完整测试行是:
java \
--patch-module com.stackoverflow.examplemodule=ModuleInfoTest:ModuleInfoExample \
--module-path ModuleInfoExample \
--add-opens com.stackoverflow.examplemodule/com.stackoverflow.examplepackage=com.stackoverflow.examplemodule \
--add-opens com.stackoverflow.examplemodule/com.stackoverflow.examplepackage=ALL-UNNAMED \
--module com.stackoverflow.examplemodule/com.stackoverflow.examplepackage.Main
我位于包含以下子目录和文件的目录中:
我正在使用:
openjdk version "13.0.2" 2020-01-14
OpenJDK Runtime Environment (build 13.0.2+8)
OpenJDK 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)
来自Java 13 java command,这是您尝试使用的两个选项:
--add-opens module/package=target-module(,target-module)*
--patch-module module=file(;file)*
但是:
--add-opens
对您没有帮助,因为它会将一个模块打开到其他模块。您只有一个模块。--patch-module
必须指定要修补到模块中的目录(或jarfile)。我注意到那里没有一个;
,而不是您以前使用的:
。在我看来,您一直在用:ModuleInfoExample
告诉Java从模块所在目录的同一文件中修补文件。您只需要将ModuleInfoTest/
中的文件添加到模块中。我根据您的问题创建了结构并运行它:
javac -d target/ModuleInfoExample src/ModuleInfoExample/*.java src/ModuleInfoExample/com/stackoverflow/examplepackage/*.java
javac -cp target/ModuleInfoExample -d target/ModuleInfoTest src/ModuleInfoTest/com/stackoverflow/examplepackage/*.java
java --module-path target/ModuleInfoExample --module com.stackoverflow.examplemodule/com.stackoverflow.examplepackage.Main
prints:
Hello world - I'm private
java --module-path target/ModuleInfoExample --patch-module com.stackoverflow.examplemodule=target/ModuleInfoTest --module com.stackoverflow.examplemodule/com.stackoverflow.examplepackage.AnyClass
prints:
Inside AnyClass - calling Main: Hello world - I'm private
field.get() = I'm private
field.get() = I'm not private anymore
>tree /f
..snip..
C:.
+---src
¦ +---ModuleInfoExample
¦ ¦ ¦ module-info.java
¦ ¦ ¦
¦ ¦ +---com
¦ ¦ +---stackoverflow
¦ ¦ +---examplepackage
¦ ¦ Main.java
¦ ¦
¦ +---ModuleInfoTest
¦ +---com
¦ +---stackoverflow
¦ +---examplepackage
¦ AnyClass.java
¦
+---target
+---ModuleInfoExample
¦ ¦ module-info.class
¦ ¦
¦ +---com
¦ +---stackoverflow
¦ +---examplepackage
¦ Main.class
¦
+---ModuleInfoTest
+---com
+---stackoverflow
+---examplepackage
AnyClass.class
module com.stackoverflow.examplemodule {
// exports com.stackoverflow.examplepackage; // no need to export. Nothing is using this
}
package com.stackoverflow.examplepackage;
public class Main {
private String privateString = "I'm private";
public static void main(String[] args) {
new Main().hello();
}
public void hello(){
System.out.println("Hello world - " + privateString);
}
}
package com.stackoverflow.examplepackage;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
public class AnyClass {
public static void main(String[] args) {
testhello();
System.out.println();
breakhello();
}
public static void testhello(){
System.out.print("Inside AnyClass - calling Main: ");
Main test = new Main();
test.hello();
}
public static void breakhello(){
try {
// Not necessary - same package, but..
Class<?> mainClass = Class.forName("com.stackoverflow.examplepackage.Main");
Constructor<?> constructor = mainClass.getConstructor();
Object main = constructor.newInstance();
// Getting, printing and changing the field..
Field field = mainClass.getDeclaredField("privateString");
field.setAccessible(true);
System.out.println(" field.get() = " + field.get(main));
field.set(main,"I'm not private anymore");
System.out.println(" field.get() = " + field.get(main));
} catch (Exception e) { // Sorry, all in one big bucket
System.out.println("Error: " + e);
}
}
}