Skip to content

Add --add-opens JVM args to surefire/failsafe for Java 25#1055

Merged
timtebeek merged 6 commits intomainfrom
add-surefire-add-opens-java25
Apr 13, 2026
Merged

Add --add-opens JVM args to surefire/failsafe for Java 25#1055
timtebeek merged 6 commits intomainfrom
add-surefire-add-opens-java25

Conversation

@timtebeek
Copy link
Copy Markdown
Member

@timtebeek timtebeek commented Apr 10, 2026

Summary

  • Add new AddSurefireArgLine recipe that merges --add-opens arguments into the argLine configuration of maven-surefire-plugin and maven-failsafe-plugin
  • Wire it into UpgradePluginsForJava25 with the full set of --add-opens flags needed for reflection-heavy test frameworks
  • Handles existing configuration merging without duplicating arguments

Why this is needed

Since JEP 403: Strongly Encapsulate JDK Internals (delivered in Java 17), the --illegal-access escape hatch was permanently removed. JDK internal packages are strongly encapsulated, and the only way to allow deep reflection (setAccessible(true)) into them is through explicit --add-opens JVM arguments.

Test frameworks that rely heavily on reflection and runtime bytecode generation — Mockito, Spring Test, Hibernate, and their shared dependency ByteBuddy — need access to internal packages like java.lang, java.lang.reflect, java.util, sun.nio.ch, and jdk.internal.misc. Without --add-opens, tests fail at runtime with InaccessibleObjectException when these frameworks attempt deep reflection.

When upgrading to Java 25 specifically, there is an additional complication: ByteBuddy validates the JVM's class file version number, and Java 25 uses class file version 69. ByteBuddy versions that predate full Java 25 support throw IllegalStateException when encountering an unknown version. The flag -Dnet.bytebuddy.experimental=true disables this version guard, allowing ByteBuddy to function on Java 25 even before a fully compatible release is available.

The existing UpgradePluginsForJava25 recipe upgrades plugin and dependency versions but does not add these required JVM arguments to the Maven Surefire/Failsafe fork configuration, leaving tests broken after migration.

Specific --add-opens flags and why they're needed

Flag Used by
java.base/java.lang Mockito, Spring (proxy creation, reflection on core types)
java.base/java.util Hibernate, Spring (reflection on collections internals)
java.base/java.lang.reflect Mockito, ByteBuddy (runtime proxy/mock generation)
java.base/java.text Hibernate, Spring (date/number formatting internals)
java.base/sun.nio.ch Netty, reactor-based test infrastructure
java.base/java.io Serialization frameworks, Spring resource loading
java.base/java.nio Netty, NIO-based test infrastructure
java.base/jdk.internal.misc ByteBuddy (low-level memory access for bytecode generation)
java.base/sun.security.action Spring Security, JDK internal privilege escalation
java.base/jdk.internal.reflect Mockito, ByteBuddy (reflection factory access)
-Dnet.bytebuddy.experimental=true ByteBuddy (accept Java 25 class file version 69)

Related issues

References

Solution

New AddSurefireArgLine recipe that:

  • Finds maven-surefire-plugin and maven-failsafe-plugin declarations (including in pluginManagement)
  • Adds <configuration><argLine>...</argLine></configuration> when no configuration exists
  • Adds <argLine> to existing configuration that lacks it
  • Merges new flags into existing argLine values, skipping duplicates
  • Handles implicit groupId and property references like ${argLine}

Test plan

  • 10 new test cases covering all configuration scenarios

  • Integration test updated in UpgradeToJava25Test

  • Full build passes

  • Fixes moderneinc/customer-requests#1821

Java 25 enforces module boundaries more strictly, causing tests that use
reflection (Mockito, Spring, Hibernate) to fail without --add-opens args.
Add a new AddSurefireArgLine recipe that merges --add-opens arguments
into the argLine configuration of maven-surefire-plugin and
maven-failsafe-plugin, and wire it into UpgradePluginsForJava25.

The recipe handles plugins with no configuration, existing configuration
without argLine, and existing argLine values (merging without duplicates).

Fixes moderneinc/customer-requests#1821
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Apr 10, 2026
@timtebeek timtebeek added the recipe Recipe requested label Apr 11, 2026
Split the single unconditional AddSurefireArgLine invocation into three
conditional wrapper recipes gated by ModuleHasDependency preconditions:
- AddSurefireArgLineForMockito: Mockito/ByteBuddy flags, gated on mockito-*
- AddSurefireArgLineForSpring: Spring flags, gated on spring-*
- AddSurefireArgLineForNetty: Netty flags, gated on netty-*

This avoids adding unnecessary --add-opens arguments to projects that
don't use those frameworks.
@timtebeek timtebeek moved this from In Progress to Ready to Review in OpenRewrite Apr 11, 2026
timtebeek and others added 2 commits April 12, 2026 00:58
…t years and formatting

- Rename recipe class to better reflect that it covers both surefire and failsafe
- Update copyright year to 2026 in new files
- Fix extra spaces inside parentheses in recipe implementation
- Update all YAML, examples, and CSV references
- Regenerate recipes.csv
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.2</version>
<configuration>
<argLine>--add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMED</argLine>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that it's necessary for it to function, but might be nice to put these onto separate lines for legibility at some point.

@timtebeek timtebeek merged commit 38caf16 into main Apr 13, 2026
1 check passed
@timtebeek timtebeek deleted the add-surefire-add-opens-java25 branch April 13, 2026 13:36
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Apr 13, 2026
mergify Bot added a commit to robfrank/linklift that referenced this pull request May 3, 2026
…30.1 to 3.34.0 [skip ci]

Bumps [org.openrewrite.recipe:rewrite-migrate-java](https://github.com/openrewrite/rewrite-migrate-java) from 3.30.1 to 3.34.0.
Release notes

*Sourced from [org.openrewrite.recipe:rewrite-migrate-java's releases](https://github.com/openrewrite/rewrite-migrate-java/releases).*

> 3.34.0
> ------
>
> What's Changed
> --------------
>
> * Ensure `case` always has a space before the label in `IfElseIfConstructToSwitch` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1071](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1071)
> * Add `UsePredicateNot` to replace cast-and-negate with `Predicate.not(..)` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1072](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1072)
>
> **Full Changelog**: <openrewrite/rewrite-migrate-java@v3.33.0...v3.34.0>
>
> 3.33.1
> ------
>
> **Full Changelog**: <openrewrite/rewrite-migrate-java@v3.33.0...v3.33.1>
>
> 3.33.0
> ------
>
> What's Changed
> --------------
>
> * Add URLEqualsHashCodeRecipes to Java best practices by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1058](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1058)
> * Add --add-opens JVM args to surefire/failsafe for Java 25 by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1055](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1055)
> * Collapse UpgradeDockerImageVersion cartesian via ChangeFrom capture by [`@​jkschneider`](https://github.com/jkschneider) in [openrewrite/rewrite-migrate-java#1060](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1060)
> * Add NoJodaTime to Java best practices by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1061](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1061)
> * Support `copyOf` migration for Guava by [`@​greg-at-moderne`](https://github.com/greg-at-moderne) in [openrewrite/rewrite-migrate-java#1062](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1062)
> * Rename `_` identifier to `__` for Java ≤ 8 source files by [`@​Jenson3210`](https://github.com/Jenson3210) in [openrewrite/rewrite-migrate-java#1063](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1063)
> * Fix `ClassCastException` on parenthesized initializers in `var` recipes by [`@​knutwannheden`](https://github.com/knutwannheden) in [openrewrite/rewrite-migrate-java#1064](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1064)
> * Gate Kotlin-based Java version cap on actual Kotlin sources by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1066](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1066)
> * Fix `MigrateCollections*` template errors on complex expression contexts by [`@​knutwannheden`](https://github.com/knutwannheden) in [openrewrite/rewrite-migrate-java#1067](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1067)
>
> **Full Changelog**: <openrewrite/rewrite-migrate-java@v3.32.1...v3.33.0>
>
> v3.32.1
> -------
>
> What's Changed
> --------------
>
> * Remove parentheses from single unnamed lambda parameters by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1054](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1054)
> * Skip Kotlin and Groovy files in ReplaceUnusedVariablesWithUnderscore by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1056](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1056)
>
> **Full Changelog**: <openrewrite/rewrite-migrate-java@v3.32.0...v3.32.1>
>
> 3.32.0
> ------
>
> What's Changed
> --------------
>
> * Cap Java version at 24 for Kotlin <2.3 in UpgradeToJava25 by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1035](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1035)
> * Inline JavaTemplate fields at their usage sites by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1036](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1036)
> * Use `JavaTemplate.apply()` static method by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1037](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1037)
> * Fix ClassCastException in UseVarForGenericMethodInvocations by [`@​knutwannheden`](https://github.com/knutwannheden) in [openrewrite/rewrite-migrate-java#1039](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1039)
> * Upgrade Mockito to 5.17.x in Java 25 migration recipe by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1041](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1041)
> * Migrate Mojarra and Glassfish JSF dependencies to Jakarta Faces by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1040](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1040)
> * Upgrade jakarta.annotation-api to 3.0.x in Jakarta EE 11 recipe by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1042](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1042)
> * Move type-use annotations to array brackets during JSpecify migration by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1038](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1038)
> * Add explicit casts for visitor visit() return type by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1049](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1049)
> * Add UseVarForTypeCast and UseVarForConstructors to UseVar recipe by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1050](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1050)
> * Replace Collections.emptyXXX with Immutable Static Factory Methods by [`@​mkarg`](https://github.com/mkarg) in [openrewrite/rewrite-migrate-java#1045](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1045)
> * Fix `AddMissingMethodImplementation` generating stubs for inherited methods by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1051](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1051)
> * Add EnableLombokAnnotationProcessor to LombokBestPractices by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1052](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1052)

... (truncated)


Commits

* [`61c1777`](openrewrite/rewrite-migrate-java@61c1777) Extract documentation examples from tests
* [`95028a5`](openrewrite/rewrite-migrate-java@95028a5) Add `UsePredicateNot` to replace cast-and-negate with `Predicate.not(..)` ([#1](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1)...
* [`a58b8b2`](openrewrite/rewrite-migrate-java@a58b8b2) Ensure `case` always has a space before the label in `IfElseIfConstructToSwit...
* [`eac4344`](openrewrite/rewrite-migrate-java@eac4344) Fix `MigrateCollections*` template errors on complex expression contexts ([#1067](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1067))
* [`0ca5cc8`](openrewrite/rewrite-migrate-java@0ca5cc8) Extract documentation examples from tests
* [`968a22f`](openrewrite/rewrite-migrate-java@968a22f) Gate Kotlin-based Java version cap on actual Kotlin sources ([#1066](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1066))
* [`a471dd1`](openrewrite/rewrite-migrate-java@a471dd1) [Auto] SDKMAN! Java candidates as of 2026-04-20T1116
* [`3b7b950`](openrewrite/rewrite-migrate-java@3b7b950) Fix `ClassCastException` on parenthesized initializers in `var` recipes ([#1064](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1064))
* [`461609f`](openrewrite/rewrite-migrate-java@461609f) Rename `_` identifier to `__` for Java ≤ 8 source files ([#1063](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1063))
* [`634e7d9`](openrewrite/rewrite-migrate-java@634e7d9) Support `copyOf` migration for Guava ([#1062](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1062))
* Additional commits viewable in [compare view](openrewrite/rewrite-migrate-java@v3.30.1...v3.34.0)
  
[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility\_score?dependency-name=org.openrewrite.recipe:rewrite-migrate-java&package-manager=maven&previous-version=3.30.1&new-version=3.34.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)
Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.
[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)
---
Dependabot commands and options
  
You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot show  ignore conditions` will show all of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

recipe Recipe requested

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants