Add shortenFullyQualifiedTypes step (fixes #2945) - #3005
Open
maxandersen wants to merge 1 commit into
Open
Conversation
Adds a new Java formatter step that replaces fully qualified type names
with simple names and adds the required imports. For example:
java.util.List<String> items = new java.util.ArrayList<>();
becomes:
import java.util.List;
import java.util.ArrayList;
...
List<String> items = new ArrayList<>();
Uses JavaParser for AST-based type identification — no false positives
from strings, comments, or non-type contexts. Position-based text
replacement preserves original formatting.
Safety: skips when simple names conflict (two FQNs → same short name),
when an existing import claims the name for a different type, and when
the file can't be parsed. java.lang and same-package types are shortened
without adding imports.
Tested with Java 14–25 syntax: instanceof pattern matching, switch
patterns, records, sealed classes, text blocks, var with generics,
and lambda casts (16 tests).
Wired into both Gradle (shortenFullyQualifiedTypes()) and Maven
(<shortenFullyQualifiedTypes/>), designed to run before importOrder()
and removeUnusedImports().
New files:
- lib/.../java/ShortenFullyQualifiedTypesStep.java (step class)
- lib/.../glue/javaparser/ShortenQualifiedTypesFormatterFunc.java (glue)
- plugin-maven/.../java/ShortenFullyQualifiedTypes.java (Maven factory)
- testlib/.../java/ShortenFullyQualifiedTypesStepTest.java (16 tests)
Modified:
- plugin-gradle/.../JavaExtension.java (+shortenFullyQualifiedTypes())
- plugin-maven/.../java/Java.java (+addShortenFullyQualifiedTypes())
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
headsup - I used llm to generate this; first time contributing to this area but did a quick read and seemed to match similar operations and been using it in my own workflows so it at least works for my usecases :)
Summary
Adds a new Java formatter step that replaces fully qualified type names with simple names and adds the required imports. Designed to clean up LLM-generated code and other sources that use inline FQNs unnecessarily.
Before
After
Implementation
BLEEDING_EDGElanguage level to support Java 14–25 syntaxSafety
Skips rewriting when:
java.lang.*types (shortened without import)Usage
Gradle:
spotless { java { shortenFullyQualifiedTypes() importOrder() removeUnusedImports() } }Maven:
Tests
16 tests covering: basic shortening, conflict detection, existing import conflicts, java.lang handling, same-package handling, idempotency, and Java 14–25 syntax (instanceof patterns, switch patterns, records, sealed classes, text blocks, var+generics, lambda casts).
Fixes #2945