openrewrite / openrewrite/rewrite
`JavaVisitor#maybeAddImport()` doesn't check for conflicts
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 3.7k
- Forks
- 570
- Avg merge
- 13h 12m
- Merged PRs (30d)
- 261
Description
Many recipes use JavaVisitor#maybeAddImport() which adds an import for a type if that type (if it then also is referenced). But it appears that the AddImport recipe doesn't check for conflicts.
Assuming we have a source with an import for either org.assertj.core.util.Objects, org.testng.collections.Objects, or com.google.common.base.Objects, then a recipe like ReplaceLambdaWithMethodReference cannot really add an import for java.util.Objects like it currently does. In this specific case not only should the maybeAddImport() call be a no-op, but also the method references to Objects::isNull and Objects::nonNull should be fully qualified.
Maybe the safest course of action would be for recipes to use fully-qualified references in any Java templates and then have these get shortened in the presence of matching imports and potential imports (registered by the recipe using maybeAddImport()).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing JavaVisitor#maybeAddImport and the AddImport recipe, then inspect ReplaceLambdaWithMethodReference where java.util.Objects is added. Reproduce the conflict with the three listed Objects imports and determine whether the expected result is a no-op plus fully qualified method references; the issue does not identify tests or files.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- devtools, tooling
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100