Skip to content

Java recipe to reorder annotations #543

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
greg-at-moderne opened this issue Apr 16, 2025 · 8 comments
Open

Java recipe to reorder annotations #543

greg-at-moderne opened this issue Apr 16, 2025 · 8 comments
Labels
enhancement New feature or request good first issue Good for newcomers recipe

Comments

@greg-at-moderne
Copy link
Contributor

What problem are you trying to solve?

I'd like to be able to tackle inconsistent order of frequently used annotations in the code.

e.g.

    @Issue("https://github.com/openrewrite/rewrite/issues/2973")
    @Test
    @ExpectedToFail
    void explicitImplementationClassInApi() {

vs.

        @Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/112")
        @ExpectedToFail("only directly returned ternaries are taken into account")
        @Test
        void doReplaceNestedOrAssignmentTernaryWithIfElse() {

in our own code, etc. There are other combinations.

Describe the solution you'd like

Maybe a recipe to instil some deterministic order (alphabetic) or maybe some smart way of passing priorities/orders - e.g. @Test should always come first, etc.

@greg-at-moderne greg-at-moderne added enhancement New feature or request recipe labels Apr 16, 2025
@greg-at-moderne greg-at-moderne added the good first issue Good for newcomers label Apr 16, 2025
@greg-at-moderne
Copy link
Contributor Author

Marking as good-first-issue as I believe this should be quite easy for beginners to implement. Whoever does it, please come up with the description of the interface of the recipe (what arguments) first, so that we can pick whatever makes the most sense.

@timtebeek timtebeek moved this to Recipes Wanted in OpenRewrite Apr 16, 2025
@mghildiy
Copy link

I can give t a try. Please assign it to me.

@mghildiy
Copy link

I guess this work would need working in modules rewrite-java and rewrite-java-test, and so I can comment our other modules in IDE.properties.

@timtebeek
Copy link
Member

@mghildiy we tend to assign issues to folks the moment they open up a draft pull request with tests; that way issues are only assigned when there's work in progress.

On our side we also have @steve-aom-elliott looking to get started with recipe development, and figured this could be a good one to start with.

I'd implement this as a recipe with a static nested JavaIsoVisitor class that takes an optional Comparator<J.Annotation> argument to define the order in which annotations should appear. The Recipe class can then by default pass in a comparator to the visitor that only looks at the annotation name. That should be good enough to get started, and folks that want something else can implement their own recipe reusing that same visitor.

@mghildiy
Copy link

mghildiy commented May 3, 2025

@mghildiy we tend to assign issues to folks the moment they open up a draft pull request with tests; that way issues are only assigned when there's work in progress.

On our side we also have @steve-aom-elliott looking to get started with recipe development, and figured this could be a good one to start with.

I'd implement this as a recipe with a static nested JavaIsoVisitor class that takes an optional Comparator<J.Annotation> argument to define the order in which annotations should appear. The Recipe class can then by default pass in a comparator to the visitor that only looks at the annotation name. That should be good enough to get started, and folks that want something else can implement their own recipe reusing that same visitor.

Hi Tim, I am in process of configuring my system for recipe writing as explained here. So yes, I am working on this task. But if there is another person who can immediately start working on it, I am fine with that. I can pick up another beginner task then.

@timtebeek
Copy link
Member

Appreciate the offer to help @mghildiy ; perhaps best to avoid conflicts then if you start with any of the other good first issues.

@steve-aom-elliott steve-aom-elliott self-assigned this May 6, 2025
@steve-aom-elliott steve-aom-elliott moved this from Recipes Wanted to In Progress in OpenRewrite May 6, 2025
@timtebeek timtebeek transferred this issue from openrewrite/rewrite May 6, 2025
@Stephan202
Copy link

In case you plan to lexicographically order all annotations: this Error Prone code can serve as inspiration. Note that it sorts declaration annotations before type annotations; see also PicnicSupermarket/error-prone-support#182.

@keerthivasan91
Copy link

@test
@issue
@ExpectedToFail
@OtherAnnotations
void testMethod() { ... }

@steve-aom-elliott steve-aom-elliott removed their assignment May 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers recipe
Projects
Status: No status
Development

No branches or pull requests

6 participants