Skip to content

Commit a7188bd

Browse files
Reordering annotations (aphabetically only for now) on method declarations.
1 parent 60fb46d commit a7188bd

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.openrewrite.staticanalysis;
2+
3+
import lombok.EqualsAndHashCode;
4+
import lombok.Value;
5+
import org.openrewrite.ExecutionContext;
6+
import org.openrewrite.Recipe;
7+
import org.openrewrite.java.JavaIsoVisitor;
8+
import org.openrewrite.java.tree.J;
9+
10+
import java.util.ArrayList;
11+
import java.util.Comparator;
12+
import java.util.List;
13+
14+
@Value
15+
@EqualsAndHashCode(callSuper = false)
16+
public class ReorderAnnotations extends Recipe {
17+
private static final Comparator<J.Annotation> defaultComparator = Comparator.comparing(J.Annotation::getSimpleName);
18+
// TODO: Take in optional Comparator<J.Annotation>, defaulted to alphabetical
19+
@Override
20+
public String getDisplayName() {
21+
return "Reorder annotations in a consistent order";
22+
}
23+
24+
@Override
25+
public String getDescription() {
26+
return "Reorder annotations based on a provided Comparator class.";
27+
}
28+
29+
@Value
30+
@EqualsAndHashCode(callSuper = false)
31+
public static class ReorderAnnotationVisitor extends JavaIsoVisitor<ExecutionContext> {
32+
Comparator<J.Annotation> comparator;
33+
34+
@Override
35+
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
36+
J.MethodDeclaration m = super.visitMethodDeclaration(method, ctx);
37+
List<J.Annotation> annotations = m.getLeadingAnnotations();
38+
if (annotations.isEmpty())
39+
{
40+
return m;
41+
}
42+
List<J.Annotation> sortedAnnotations = new ArrayList<>(annotations);
43+
sortedAnnotations.sort(comparator);
44+
if (sortedAnnotations.equals(annotations))
45+
{
46+
return m;
47+
}
48+
for (int i = 0; i < annotations.size(); i++) {
49+
sortedAnnotations.set(i, sortedAnnotations.get(i).withPrefix(annotations.get(i).getPrefix()));
50+
}
51+
52+
return m.withLeadingAnnotations(sortedAnnotations);
53+
}
54+
}
55+
56+
@Override
57+
public JavaIsoVisitor<ExecutionContext> getVisitor() {
58+
return new ReorderAnnotationVisitor(defaultComparator);
59+
}
60+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.openrewrite.staticanalysis;
2+
3+
import org.junit.jupiter.api.Nested;
4+
import org.junit.jupiter.api.Test;
5+
import org.openrewrite.DocumentExample;
6+
import org.openrewrite.test.RewriteTest;
7+
8+
import static org.openrewrite.java.Assertions.java;
9+
10+
public class ReorderAnnotationsTest implements RewriteTest {
11+
@Nested
12+
class ParameterlessConstructor {
13+
@Test
14+
@DocumentExample
15+
void doesNotReorderAnnotationsIfNotNeeded() {
16+
rewriteRun(
17+
spec -> spec.recipe(new ReorderAnnotations()),
18+
//language=java
19+
java(
20+
"""
21+
import org.junit.jupiter.api.Test;
22+
import org.junitpioneer.jupiter.ExpectedToFail;
23+
import org.junitpioneer.jupiter.Issue;
24+
25+
class A {
26+
@ExpectedToFail
27+
@Issue("https://github.com/openrewrite/rewrite/issues/2973")
28+
@Test
29+
void explicitImplementationClassInApi() {
30+
}
31+
}
32+
"""
33+
)
34+
);
35+
}
36+
37+
@Test
38+
@DocumentExample
39+
void reordersMethodAnnotations() {
40+
rewriteRun(
41+
spec -> spec.recipe(new ReorderAnnotations()),
42+
//language=java
43+
java(
44+
"""
45+
import org.junit.jupiter.api.Test;
46+
import org.junitpioneer.jupiter.ExpectedToFail;
47+
import org.junitpioneer.jupiter.Issue;
48+
49+
class A {
50+
@Issue("https://github.com/openrewrite/rewrite/issues/2973")
51+
@Test
52+
@ExpectedToFail
53+
void explicitImplementationClassInApi() {
54+
}
55+
}
56+
""",
57+
"""
58+
import org.junit.jupiter.api.Test;
59+
import org.junitpioneer.jupiter.ExpectedToFail;
60+
import org.junitpioneer.jupiter.Issue;
61+
62+
class A {
63+
@ExpectedToFail
64+
@Issue("https://github.com/openrewrite/rewrite/issues/2973")
65+
@Test
66+
void explicitImplementationClassInApi() {
67+
}
68+
}
69+
"""
70+
)
71+
);
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)