1 of 15

Automating Code Refactoring with OpenRewrite

Matt Busche

@mrbusche

2 of 15

Software Trends

  • 20% custom
  • 80% third party
  • Maintenance is highly repeatable

3 of 15

Safely modernize your source code

  • Abstract syntax tree (AST)
  • Idiomatic updates

4 of 15

Why use OpenRewrite?

  • Fully automated
  • Most experienced engineers end up doing the upgrades
  • Migrate to Java 17 runs 180 migration recipes
  • Replace deprecated APIs with new equivalent
  • Necessary changes
  • Best practices to enable developers to learn from the automation

5 of 15

Getting started

  • Spring Boot 3.x best practices
  • mvn -U org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-spring:RELEASE -Drewrite.activeRecipes=org.openrewrite.java.spring.boot3.SpringBoot3BestPractices -Drewrite.exportDatatables=true

6 of 15

Spring Boot 3.x best practices

7 of 15

Migrate to Java 21

  • Migrate to Java 17
  • Upgrade build to Java 21
  • Remove illegal semicolons
  • Replace Thread.resume(), Thread.stop(), and Thread.suspend() with throw new UnsupportedOperationException()
  • URLConstructorsToURI Refaster recipes
  • Adopt SequencedCollection
  • Prefer Locale.of(..) over new Locale(..)
  • Replace deprecated Runtime#exec() methods
  • Upgrade actions/setup-java java-version
  • Upgrade plugins to Java 21 compatible versions
  • Avoid using the deprecated empty finalize() method in java.desktop
  • Adopt javax.security.auth.Subject.current() and javax.security.auth.Subject.callAs() methods`

8 of 15

Migrate to Spring Boot 3.3

  • Migrate to Spring Boot 3.2
  • Migrate Spring Boot properties to 3.3
  • Upgrade Gradle or Maven dependency versions
    • groupId: org.springframework.boot
    • artifactId: *
    • newVersion: 3.3.x
    • overrideManagedVersion: false
  • Upgrade Maven plugin version
    • groupId: org.springframework.boot
    • artifactId: spring-boot-maven-plugin
    • newVersion: 3.3.x
  • Upgrade Gradle or Maven dependency versions
    • groupId: org.springframework
    • artifactId: *
    • newVersion: 6.1.x
  • Upgrade Maven parent project version
    • groupId: org.springframework.boot
    • artifactId: spring-boot-starter-parent
    • newVersion: 3.3.x
  • Update a Gradle plugin by id
    • pluginIdPattern: org.springframework.boot
    • newVersion: 3.3.x
  • Migrate to Micrometer 1.13
  • Update a Gradle plugin by id
    • pluginIdPattern: org.graalvm.buildtools.native
    • newVersion: 0.10.x
  • Change Gradle or Maven dependency
    • oldGroupId: com.datastax.oss
    • oldArtifactId: *
    • newGroupId: org.apache.cassandra
  • Upgrade Gradle or Maven dependency versions
    • groupId: org.springdoc
    • artifactId: *
    • newVersion: 2.6.x

9 of 15

Spring Boot 3.x best practices

10 of 15

Gradle updates

11 of 15

Migrate to Junit 5 from 4

  • Released Oct 2017; Part of “Migrate to Spring Boot 2.4”

12 of 15

Caveats

  • Enables virtual threads by default on Java 21
  • Legacy apps may have one-time struggles

13 of 15

Create recipe

  • Moderne working with hospital
  • SQL recipe to find Oracle concat, || vs Postgres CONCAT()

14 of 15

IntelliJ features

15 of 15

QUESTIONS?