I’m still trying to figure out how to use XStream with Java modules. As soon as I start using the JPMS’s module features for a project, the XStream data types become unavailable to the project. What magic spell am I missing?
I think I understand the --add-opens trick that allows XStream to use reflection for serializing data in a running JVM, but how do I get XStream dependent code to compile in the first place?
For very simple test code
package demo.xstream;
import com.thoughtworks.xstream.XStream;
public class Test {
private XStream xstream;
public Test(XStream xstream) {
this.xstream = xstream;
}
}
This compiles fine (./gradlew build) with the necessary Gradle instructions.
plugins {
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'com.thoughtworks.xstream:xstream:1.4.20'
}If I attempt to package the demo as a module, and add a module definition file (module-info.java), the Test class no longer compiles.
module demo.xstream {
requires xstream;
}There are multiple compilation errors:
Test.java: the imported package com.thoughtworks.xstream.XStream is not accessible (but is visible in the IDE).
Module-info.java: the named component xstream cannot be resolved to a module.
If I eliminate the requires xstream; but retain the module-info.java, the imported package remain inaccessible.
Perhaps I just don’t understand the JPMS rules, but I thought the xstream-1.4.20.java Jar would lead to an implicit module named xstream. Perhaps that has tightened with Java 17.
The target application is expected to have dozens of modules, many with their own persisted data type. It is very desirable to have the extension modules provide customized serializers for their data types. Efficient implementation of customized serializers requires that the modules have access to XStream data types.
In some of the extension modules, it is desirable to open a package of serialized data types to XStream, with a line in the module definition. Hand adding a variable set of extension modules to an -add-opens JVM directive does not seem scalable or extensible.
The GitHub repository leeca/XStreamExample has the sample code in place.
The short sequence of commands should replicate the challenges in a small repository.
$ git clone g...@github.com:leeca/XStreamExample.git
$ cd XStreamExample/AutomaticModule/
$ ./gradlew build # This succeeds
$ cd ../ExplicitModule/
$ ./gradlew build # This fails