Rename underscore class references across source files - #1197
Draft
martinfrancois wants to merge 1 commit into
Draft
Rename underscore class references across source files#1197martinfrancois wants to merge 1 commit into
martinfrancois wants to merge 1 commit into
Conversation
4 tasks
…name gaps classReferencesInAnotherSourceFileFollowTheRename pins that a class rename leaves references in other source files on the old, invalid _ spelling. classRenameAvoidsASecondaryClassDeclaredInAnotherSourceFile pins that the chosen name duplicates a package private class from a differently named file. Both are marked known-failing with @ExpectedToFail.
martinfrancois
force-pushed
the
repro/rename-underscore-cross-file
branch
from
August 16, 2026 12:23
7617c66 to
316df22
Compare
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.
Suggested review order: 49 of 52 (Score: 1)
Review first: openrewrite/rewrite-testing-frameworks#1084
What's changed?
Adds 2 known-failing tests to
RenameUnderscoreIdentifierTestthat reproduce two gaps whenRenameUnderscoreIdentifierrenames a class named_:classReferencesInAnotherSourceFileFollowTheRename: references to the renamed class in another source file keep the old_spelling.classRenameAvoidsASecondaryClassDeclaredInAnotherSourceFile: the chosen name__duplicates a package private class declared in another file.No recipe code changes. The tests are marked
@ExpectedToFailso the suite stays green; removing the annotation shows the failure. The test class has 11 tests on main and 13 with these additions.Both tests build their
_class with aChangeType("UNDERSCORE", "_", false)setup recipe instead of the file'srenameToUnderscore()helper, because the helper cannot produce valid cross-file references to_. A comment in the test file explains this. BeforeRenameUnderscoreIdentifierruns, the LST truly declares a class_with bound references from the other file.What's your motivation?
Recipe:
org.openrewrite.java.migrate.lang.RenameUnderscoreIdentifier.The recipe renames
_identifiers so Java 8 code compiles on Java 9 and later. It runs insideJava8toJava11, whichUpgradeToJava17andUpgradeToJava21pull in. On currentmain(01d0fe82), it renames only the class declaration identifier and does not check for names declared in other files.Case 1: class references in another source file
Before
Two files are parsed together. After the setup recipe,
_.javadeclaresclass _, and the other file contains bound references to that class:Actual after the recipe
_.javabecomesclass __, butUser.javais unchanged:User.java:(unchanged)Javac on JDK 25 reports
error: underscore not allowed hereat the type reference and the constructor call. JDK 9 through 21 report the equivalent release-9 keyword error.Expected after the recipe
References in the other file are renamed with the declaration:
Case 2: the selected name exists in another source file
Before
_.javadeclaresclass _.Other.javaalready declares a package-private class named__:Actual after the recipe
The recipe renames
class _toclass __. The two source files then declare the same class name, and javac rejects the result witherror: duplicate class: a.__.Expected after the recipe
(unchanged)The recipe MUST leave the declaration unchanged when the selected replacement name is already declared in another source file.
Found while preparing #1194, which fixes the same-file cases of both defects.
Affected code in real projects
yinwang0/rubysonarAnalyzer.java: a Java 7 static analyzer whose utility class is literally named_(declared in_.java) and is referenced from this and many other source files through calls such as_.newSessionId()and_.msg(..); the recipe from main renames only the declaration in_.javato__, so every cross-file reference keeps the old_spelling and javac rejects it as a keyword on Java 9 and later.Anything in particular you'd like reviewers to focus on?
I think both are genuine bugs, because the recipe's whole purpose is to keep the code compiling, and in both cases the output fails to compile. If you agree this should change, I would gladly prepare the fix. If this behavior is intended, feel free to close this and I know it is settled.
Any additional context
Pre-existing tests changed: None.
My open #1194 discloses both cases as explicit limits ("the same as on main") but does not fix them: its collision check sees same-file names and other files' paths only, and it renames class references within the same file only. These tests were executed on current main; per the description of #1194 they are expected to fail identically on top of it.
This reproduction was prepared with AI assistance (Claude Code). I reviewed the tests and this description.
The added reproduction tests and the existing suite together cover changed and unchanged behavior. The known-failing tests remain disabled until implementation. The formatter run was calibrated per file; untouched lines were not reformatted.
Checklist
./gradlew buildlocally, and committed any resulting changes torecipes.csv