Expand star imports in ChangePackage and related recipes when they would create ambiguity#7202
Merged
steve-aom-elliott merged 7 commits intomainfrom Apr 10, 2026
Merged
Conversation
Closed
3 tasks
a6ee247 to
c7942f8
Compare
e50f48f to
48f89f5
Compare
sambsnyd
reviewed
Apr 7, 2026
7566269 to
7bc9f18
Compare
sambsnyd
reviewed
Apr 9, 2026
Member
sambsnyd
left a comment
There was a problem hiding this comment.
The JavaSourceSet updating functionality within AddDependency , ChangeDependnecy, etc., are all very similar. Perhaps that functionality could be moved into JavaSourceSet itself to simplify and keep standard the recipes which update it
When ChangeType moves a type into a package covered by a star import, and another star import provides a type with the same simple name, add an explicit import to disambiguate. This mirrors the ambiguity handling already present in ChangePackage.
…ntegration test Move the withSourceTypesOnClasspath test helper from private methods in ChangePackageTest and ChangeTypeTest to a public static method in Assertions.java so it can be reused across test classes. Add an end-to-end integration test in ChangeDependencyGroupIdAndArtifactIdTest that composes ChangeDependencyGroupIdAndArtifactId with ChangePackage to verify that import renaming works correctly when the JavaSourceSet is updated by the dependency change recipe.
…rce set updates - Attach Markup.warn to build files when version resolution for JavaSourceSet updates fails, so users know the classpath won't be updated - Remove counter-productive G.CompilationUnit/K.CompilationUnit exclusion in Gradle AddDependency and ChangeDependency — Groovy/Kotlin source files can have JavaSourceSet markers; Gradle build files won't have one and are already filtered by the maybeSourceSet.isPresent() check - Cache computed JavaSourceSet per source set in ChangeDependency (Gradle) and ChangeDependencyGroupIdAndArtifactId (Maven) to avoid redundant per-file recomputation when all files in a source set share identical markers
… ChangeDependency Capture Maven repositories from GradleProject in the scanner and use them when downloading JARs for JavaSourceSet updates, falling back to Maven Central only if no remote repository is available. This matches the approach already used in Maven ChangeDependencyGroupIdAndArtifactId and both AddDependency recipes.
a688b74 to
a5abf62
Compare
Contributor
Author
sambsnyd
approved these changes
Apr 10, 2026
timtebeek
added a commit
to openrewrite/rewrite-hibernate
that referenced
this pull request
Apr 11, 2026
openrewrite/rewrite#7202 added JavaSourceSet marker updates to ChangeDependencyGroupIdAndArtifactId, which now modifies Java files' markers without changing text content. This causes the test framework to flag "empty diff" errors for Java source specs without explicit `after` text. Provide explicit `after` text (identical to `before`) for the Java source file in the affected test to accept the marker-only change. See openrewrite/rewrite#7349 for the upstream issue.
timtebeek
added a commit
to openrewrite/rewrite-migrate-java
that referenced
this pull request
Apr 11, 2026
openrewrite/rewrite#7202 added JavaSourceSet marker updates to ChangeDependencyGroupIdAndArtifactId, which now modifies Java files' markers without changing text content. This causes the test framework to flag "empty diff" errors for Java source specs without explicit `after` text. Provide explicit `after` text (identical to `before`) for the Java source file in the affected test to accept the marker-only change. See openrewrite/rewrite#7349 for the upstream issue.
timtebeek
added a commit
that referenced
this pull request
Apr 11, 2026
…ty diff After #7202, `ChangeDependencyGroupIdAndArtifactId` updates `JavaSourceSet` markers on Java files when dependency coordinates change. These marker-only changes produce Results with identical before/after text, which `RewriteTest` previously treated as errors ("An empty diff was generated"). Instead of failing, silently accept Results where the printed text is unchanged. This allows recipes to update markers (e.g. classpath metadata) without requiring downstream tests to add explicit `after` text for every Java source spec. The `afterRecipe` callback still fires with the updated tree, so tests that need to verify marker changes (like `updatesJavaSourceSetMarkerOnJavaFiles`) continue to work. Fixes #7349
5 tasks
steve-aom-elliott
added a commit
that referenced
this pull request
Apr 13, 2026
Dependency-modifying recipes now update JavaSourceSet markers (added in #7202), which can produce Results with identical before/after text. This caused downstream test failures in rewrite-dropwizard, rewrite-hibernate, rewrite-migrate-java, and rewrite-spring. Three fixes: 1. JavaSourceSet.addTypesForGav returns `this` when the gavKey already exists with the same types, preventing unnecessary allocations for LSTs with empty gavToTypes maps. 2. The cached overload of JavaSourceSet.updateOnSourceFile now checks whether the source file's current marker is already the cached instance before replacing it, preventing O(n) phantom diffs across files in the same source set. 3. RewriteTest accepts marker-only changes (identical printed text) instead of failing with "An empty diff was generated", since these represent legitimate internal state updates. Closes #7349
steve-aom-elliott
added a commit
that referenced
this pull request
Apr 13, 2026
Dependency-modifying recipes now update JavaSourceSet markers (added in #7202), which can produce Results with identical before/after text. This caused downstream test failures in rewrite-dropwizard, rewrite-hibernate, rewrite-migrate-java, and rewrite-spring. Three fixes: 1. JavaSourceSet.addTypesForGav returns `this` when the gavKey already exists with the same types, preventing unnecessary allocations for LSTs with empty gavToTypes maps. 2. The cached overload of JavaSourceSet.updateOnSourceFile now checks whether the source file's current marker is already the cached instance before replacing it, preventing O(n) phantom diffs across files in the same source set. 3. RewriteTest accepts marker-only changes (identical printed text) instead of failing with "An empty diff was generated", since these represent legitimate internal state updates. Closes #7349
This was referenced Apr 14, 2026
This was referenced Apr 22, 2026
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.
Summary
ChangePackagerenames a star import (e.g.,import javax.validation.constraints.*→import jakarta.validation.constraints.*), it now detects if the new package contains types whose simple names clash with types from other star imports (e.g.,org.hibernate.validator.constraints.*also hasNotBlank)JavaSourceSetclasspath to enumerate types in each package, then walks AST identifiers to determine which types are actually referencedChangeTypenow also detects when its target type is covered by a star import that would become ambiguous with another star import, and inserts an explicit import to disambiguateChangeDependencyGroupIdAndArtifactId(Maven) andAddDependency(Maven and Gradle) now update theJavaSourceSetmarker when changing or adding dependencies, so downstream recipes likeChangePackageandChangeTypehave accurate classpath data for ambiguity detectionJavaSourceSetUpdater.addDependencynow tries the project's actual Maven repositories instead of hardcoding Maven Central, fixing failures in air-gapped environments or projects using private repositorieswithSourceTypesOnClasspathtest helper extracted toAssertions.javafor reuse across test classesTest plan
changePackageExpandsStarImportWhenItWouldCreateAmbiguity— uses realjavax.validation,jakarta.validation, andorg.hibernate.validatortypes; verifies star import is expanded to explicit imports when another star import contains a type with the same simple namechangePackagePreservesStarImportWhenNoAmbiguity— verifies star import is preserved when no ambiguity existschangeTypeAddsExplicitImportWhenStarImportsWouldBeAmbiguous— verifies ChangeType inserts an explicit import when two star imports would both provide the target type's simple namecomposedWithChangePackageUpdatesImports— end-to-end test composingChangeDependencyGroupIdAndArtifactId+ChangePackageto verify import renaming works when the JavaSourceSet is updated by the dependency changeChangePackageTest,ChangeTypeTest, andChangeDependencyGroupIdAndArtifactIdTestsuites pass