Skip to content

Feat/java add explicit imports #5318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

BelmoMusta
Copy link
Contributor

@BelmoMusta BelmoMusta commented Apr 19, 2025

What's changed?

This recipe appends a list of imports to java files.

What's your motivation?

I Added a recipe to append a list of imports to a java file.
I assume that there are already existing recipes to do that, but this one preserves the already existing imports style. In other words, to changes of the recipe do not interfere with the already established imports layout.
It does not order the imports, nor that it groups them in a star (*) import style.

It inserts the imports and does not check if the compilation unit is using them or not.

It takes care of the static imports as well.

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

Anyone you would like to review specifically?

@jevanlingen FYI

Have you considered any alternatives or workarounds?

Any additional context

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 used the IntelliJ IDEA auto-formatter on affected files

@timtebeek
Copy link
Member

Hmm, we had intentionally not added a recipe for this, as adding an import by itself could lead to then having an unused import. Instead we handle imports from other recipes through a method called maybeAddImport that then ensures it's added only when referenced, and correctly grouped to match the project style.

Could you describe the use case you see for having an explicit recipe? When would you need this in isolation, or how would you combine this?

My worry is having a recipe like this might lead to confusion, and folks adopting this over self contained recipes that handle the imports for the elements they are adding.

@timtebeek timtebeek added the question Further information is requested label Apr 19, 2025
@BelmoMusta
Copy link
Contributor Author

@timtebeek, I agree with you, but when it comes to the imports style and organization I don't think there is a standard style. I also believe that this kind of recipes will provide users with a certain autonomy when it comes to handling imports.

For example, I discourage the use of the * grouping way, as not every IDE is not having the same strategy of a such a grouping.

@timtebeek
Copy link
Member

For that wildcard option we have configurable styles: https://docs.openrewrite.org/concepts-and-explanations/styles
That ought to then apply those consistently when you run let's say OrderImports. I still feel that's preferred over recipes that explicitly add individual imports, however well-intentioned.

I have yet to see a convincing case before we merge and maintain this going forward, as I'm afraid folks (and AI assistants) will learn then wrong patterns when they see a recipe like this, and start adding explicit imports through this recipe.

BelmoMusta and others added 9 commits May 3, 2025 20:50
…ort.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…ImportLayoutStyle.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…ortTest.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…ortTest.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…ort.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@BelmoMusta BelmoMusta force-pushed the feat/java-add-explicit-imports branch from e2758dc to 38de4b2 Compare May 4, 2025 00:51
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

2 participants