Skip to content

Convert 5-arg URL constructor with URLStreamHandler to URL.of - #1241

Open
harrisleesh wants to merge 1 commit into
openrewrite:mainfrom
harrisleesh:url-constructor-stream-handler
Open

harrisleesh wants to merge 1 commit into
openrewrite:mainfrom
harrisleesh:url-constructor-stream-handler

Conversation

@harrisleesh

@harrisleesh harrisleesh commented Sep 16, 2026

Copy link
Copy Markdown

What's changed?

URLConstructorsToNewURI now also matches the deprecated 5-arg constructor new URL(String protocol, String host, int port, String file, URLStreamHandler handler) and converts it to URL.of(new URI(protocol, null, host, port, file, null, null), handler), preserving the custom stream handler. The URI construction follows the same shape already used for the 3- and 4-arg variants.

What's your motivation?

Anything in particular you'd like reviewers to focus on?

  • URL.of(URI, URLStreamHandler) requires Java 20+, while the 3/4-arg replacements only need URI.toURL(). I kept everything in the same recipe since it is listed under the Java 21 migration, but happy to gate the 5-arg case behind a version precondition if you prefer.
  • Same caveat as URLThreeArgumentConstructor does not introduce newly thrown exception #467 applies: the URI constructor throws URISyntaxException where the URL constructor threw MalformedURLException.

Any additional context

Heads-up on validation: I could not resolve org.openrewrite artifacts from the Code Genome repository locally yet, so I verified the change in a standalone project against rewrite 8.90.4 — the new test fails without the recipe change and both tests pass with it. Relying on CI for the exact HEAD build; if anything fails I'll follow up promptly.

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've run ./gradlew build locally, and committed any resulting changes to recipes.csv (blocked on Code Genome credentials — no new recipe class was added, so recipes.csv should be unaffected)
  • I've formatted the lines I changed, without reformatting code I didn't touch

The URLStreamHandler variant of the deprecated URL constructor was
left out of URLConstructorsToNewURI, so occurrences like
new URL(protocol, host, port, file, handler) were not migrated.
Convert them to URL.of(new URI(protocol, null, host, port, file,
null, null), handler), which preserves the custom handler.

Fixes: openrewrite#849
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

1 participant