rewrite icon indicating copy to clipboard operation
rewrite copied to clipboard

Feat/java add explicit imports

Open BelmoMusta opened this issue 8 months ago • 3 comments

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

  • [x] I've added unit tests to cover both positive and negative cases
  • [x] I've read and applied the recipe conventions and best practices
  • [x] I've used the IntelliJ IDEA auto-formatter on affected files

BelmoMusta avatar Apr 19 '25 00:04 BelmoMusta

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 avatar Apr 19 '25 06:04 timtebeek

@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.

BelmoMusta avatar Apr 19 '25 23:04 BelmoMusta

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.

timtebeek avatar Apr 20 '25 18:04 timtebeek

As much as the help is appreciated, given the concerns outlined in the comments above I propose we close this PR for now: There are better ways to handle imports as opposed to directly force adding an import.

timtebeek avatar Jul 28 '25 11:07 timtebeek