How do I add my tests to my production code at test-runtime so that both are in the same Java 9 module and can access each other using reflections?
I have tried so far:
module-info.java
) → it worked perfectly, but is not what I'm looking for.--patch-module
to virtually add another folder (to the one specified using --module-path
) → it worked with "normal" code but not with reflections, it doesn't find the classes specified by --module-path
.
--patch-module
→ it only finds classes in the folder I specify first.--add-opens mymodule/mypackge=mymodule
or ...=ALL-UNNAMED
to open it for reflection → doesn't look to have any effect.So my full test line is:
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
I'm in a directory containing the following subdirectories and files:
I'm using:
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)
As I learned from the approved answer, my problem was not really "access other classes", as I phrased it. But more like find them (by scanning classpath/modulepath). However this part is already answered in this other StackOverflow question.
From Oracle's Java 13 java command these are the two options you tried to use:
--add-opens module/package=target-module(,target-module)*
--patch-module module=file(;file)*
but:
--add-opens
doesn't help you because it opens up one module to other modules. You only have one module.--patch-module
must specify the directories (or jarfiles) you want to patch into the module. I noticed that there's a ;
not a :
like you used. It seems to me that you've been telling java to patch files from same the directory where your module is at with :ModuleInfoExample
.You only need to add the files from ModuleInfoTest/
into your module.
I created the structure from your Question and ran it:
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 --module com.stackoverflow.examplemodule/com.stackoverflow.examplepackage.AnyClass
Error: Could not find or load main class com.stackoverflow.examplepackage.AnyClass in module com.stackoverflow.examplemodule
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);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With