Skip to content

refactor: modernize code to Java 17 syntax - #1440

Open
akenra wants to merge 1 commit into
spring-cloud:mainfrom
akenra:refactor/modernize-java-syntax
Open

refactor: modernize code to Java 17 syntax#1440
akenra wants to merge 1 commit into
spring-cloud:mainfrom
akenra:refactor/modernize-java-syntax

Conversation

@akenra

@akenra akenra commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Hi @olegz!

I wanted to keep the maintenance momentum going, so this PR modernizes the codebase to Java 17 conventions and adds a tool to make sure it stays that way.

The first part is the modernization itself, across all modules:

  • Replaced instanceof + cast pairs with pattern matching (Java 16+)
  • Converted simple private static data carriers to records
  • Replaced anonymous classes with lambdas and method references
  • Simplified single-statement lambda blocks
  • Used diamond operator where the type is inferable
  • Replaced raw types with parameterized ones where possible

The second part adds a report-only PMD 7 profile:

New -Ppmd profile runs pmd:check at verify with failOnViolation=false, so it prints hints but never breaks the build.
Custom ruleset covering the same modernization themes, calibrated against the codebase to avoid false positives.
The tool already paid for itself - it found 43 instanceof + cast spots the manual pass had missed.
The profile reports zero violations on the current codebase after the refactoring.

One modernization theme is intentionally missing from the ruleset, though: anonymous classes to lambdas. The syntactic heuristic (single method, no fields) would also flag anonymous subclasses of abstract classes like new FunctionAroundWrapper() { ... }, which are not convertible - only implementations of functional interfaces are. Telling those apart requires checking whether the supertype is a functional interface, and PMD's XPath rules have no way to ask that about a resolved type. Shipping the rule as-is meant firing on every legitimate abstract class subclass, so I dropped it rather than adding noise. If PMD ever gains a functional-interface check, this rule would be a nice addition.

I also did a (AI-assisted :D) backward compatibility review of the public API before opening this PR. The changes are all limited to method internals: the records were created only from private and package-private classes, no public signatures, fields, constants or visibility were touched, and the pattern matching conversions are semantically equivalent to the casts they replace - the one spot where a variable gets reassigned mid-block was deliberately left unconverted. So client code should be unaffected, but I'd appreciate a second pair of eyes on that - let me know if anything looks off.

Hope this may be useful!

- Use pattern matching for instanceof (Java 16+)
- Convert simple private classes to records
- Replace anonymous classes with lambdas and method references
- Simplify lambda bodies and use method references
- Use diamond operator where type is inferable
- Replace raw types with parameterized ones

Add a report-only PMD 7 profile (-Ppmd) with modern Java syntax rules
to help contributors keep the codebase modern. PMD 7.26 is used via the
pmd.version override, as the PMD bundled with maven-pmd-plugin 3.28.0
lacks the overload resolution fix for method references.

Signed-off-by: akenra <37288280+akenra@users.noreply.github.com>
@akenra
akenra force-pushed the refactor/modernize-java-syntax branch from d7f7799 to 79b05ed Compare August 7, 2026 23:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant