Skip to content

Conversation

@steve-aom-elliott
Copy link
Contributor

@steve-aom-elliott steve-aom-elliott commented May 6, 2025

Reordering annotations (alphabetically only for now on method declarations alone).

Looking to define boundaries for what should be included in the changes for annotation reordering. As per the linked issue, the starting point would be a default alphabetical ordering of annotations (the ability to pass in what Comparator<J.Annotation> to use will be upcoming). It's been suggested to start with just method declarations and move on from there after, but I'm open to thoughts and suggestions.

What's changed?

  • Added a ReorderAnnotations recipe for reordering annotations (default alphabetic) on J.MethodDeclaration
  • Added simple tests for the reordering for method declarations (only using the default sorting)

Related issue with additional fixes that should be implemented first

Anyone you would like to review specifically?

@timtebeek
@greg-at-moderne

Checklist

  • I've added unit tests to cover both positive and negative cases for method declarations
  • I've implemented handling of class declarations
  • I've implemented handling of field declarations
  • I've added unit tests to cover both positive and negative cases for class declarations
  • I've added unit tests to cover both positive and negative cases for field declarations
  • I've implemented the ability to provide what Comparator<J.Annotation> you would like to use to sort the annotations on a given LST entry point
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite May 6, 2025
@steve-aom-elliott steve-aom-elliott force-pushed the feature/consistently-ordered-annotations branch from a7188bd to c25c65b Compare May 6, 2025 20:38
github-actions[bot]

This comment was marked as outdated.

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

This comment was marked as outdated.

github-actions[bot]

This comment was marked as outdated.

@Laurens-W
Copy link
Contributor

Efforts here can be continued as openrewrite/rewrite#5392 has been completed

@timtebeek
Copy link
Member

@Laurens-W no rush on this at all ofcourse, but it's odd to see that we introduce a newline for the simplest case, and there's weird indentation and shuffling of comments for the more complex case. The first I'd at least expect to be fixed; the second is optional.

@Laurens-W
Copy link
Contributor

Laurens-W commented Jun 16, 2025

For the simple case I see this happen:

The newly added recipe here calls autoformat on the method
That ends up hitting this part of the autoformat visitor
https://github.com/openrewrite/rewrite/blob/32d30c0d9d15df09963fce9927ab242ee1ffdbe3/rewrite-java/src/main/java/org/openrewrite/java/format/AutoFormatVisitor.java#L58-L66

of which the NormalizeFormatVisitor does this upon visiting a method:
https://github.com/openrewrite/rewrite/blob/32d30c0d9d15df09963fce9927ab242ee1ffdbe3/rewrite-java/src/main/java/org/openrewrite/java/format/NormalizeFormatVisitor.java#L81-L87

Basically the NormalizeFormatVisitor grabs the prefix from an annotation that hasn't had it's wrapping adjusted yet, so it adds a newline.
We could circumvent this by only invoking the WrappingAndBraces visitor but I'm not sure we want to explicitly call that from a recipe.

@timtebeek timtebeek marked this pull request as ready for review June 16, 2025 10:06
@timtebeek
Copy link
Member

Thanks for that context; in that case let's make it a little easier here and just keep whatever prefix was there before reshuffling. Would you agree?

Copy link
Member

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks all! I think this increment is ready to be merged, applied to all our projects, and enforced going forward. I'll wait for a second approval since I've made quite some changes still.

@github-project-automation github-project-automation bot moved this from Backlog to Ready to Review in OpenRewrite Jun 16, 2025
@timtebeek timtebeek requested a review from Laurens-W June 16, 2025 10:37
Copy link
Contributor

@Laurens-W Laurens-W left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@timtebeek timtebeek merged commit 1cdcfbd into main Jun 16, 2025
2 checks passed
@timtebeek timtebeek deleted the feature/consistently-ordered-annotations branch June 16, 2025 10:58
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Jun 16, 2025
@timtebeek
Copy link
Member

There's one change here that I'm not quite happy with, and was helpfully pointed out here:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Java recipe to reorder annotations

4 participants