commons-lang icon indicating copy to clipboard operation
commons-lang copied to clipboard

Make dependency on java.desktop optional

Open bowbahdoe opened this issue 1 year ago • 1 comments

There is only one place in commons-lang where anything from java.desktop is used, and that is AbstractCircuitBreaker.

This changes that code to lazily initialize its internal PropertyChangeSupport. This makes it so that it is safe to initialize and use that class without java.desktop on the module-path.

Upsides:

  • Smaller jlinked images for every apache commons library
  • No binary compatibility changes

Downsides:

  • Likely a small performance hit
  • It is possible for a user to give a null instance of PropertyChangeListener without depending on the java.desktop module, in which case the behavior of the method will change from "ignores null" to "fails"
  • I do not know how to add a test for this to the junit suite. We'd have to run a subset of the tests without java.desktop present. Currently I am validating manually like so
$ mvn clean compile package
$ jlink --module-path target/commons-lang3-3.15.0-SNAPSHOT.jar --add-modules org.apache.commons.lang3,jdk.jshell --output jre
$ ./jre/bin/java --list-modules
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

$ ./jre/bin/jshell

jshell> var breaker = new EventCountCircuitBreaker(1000, 1, java.util.concurrent.TimeUnit.SECONDS);
breaker ==> org.apache.commons.lang3.concurrent.EventCountCircuitBreaker@1ce92674

jshell> breaker.addChangeListener(null);
|  Error:
|  cannot access java.beans.PropertyChangeListener
|    class file for java.beans.PropertyChangeListener not found
|  breaker.addChangeListener(null);
|  ^-----------------------------^

Before

$ du -sh jre
125M    jre

$ ./jre/bin/java --list-modules
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

After

$ du -sh jre
 88M    jre

$ ./jre/bin/java --list-modules
[email protected]
[email protected]
[email protected]

bowbahdoe avatar Mar 08 '24 14:03 bowbahdoe

-1: This looks too hacky and brittle to me as we do not have ways to prevent future changes from depending on java.desktop or other modules which would make this moot. For 4.0, we can talk about making Lang a java.base only module. TBD.

garydgregory avatar Apr 01 '24 21:04 garydgregory

Closing: For 4.0, we can remove the dependency on java.desktop, probably.

garydgregory avatar Sep 02 '25 12:09 garydgregory