Skip to content

Commit e62f8b1

Browse files
authored
Order type annotations last (#594)
1 parent 1cdcfbd commit e62f8b1

2 files changed

Lines changed: 73 additions & 1 deletion

File tree

src/main/java/org/openrewrite/staticanalysis/ReorderAnnotations.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.openrewrite.internal.ListUtils;
2323
import org.openrewrite.java.JavaIsoVisitor;
2424
import org.openrewrite.java.tree.J;
25+
import org.openrewrite.java.tree.JavaType;
26+
import org.openrewrite.java.tree.TypeUtils;
2527

2628
import java.util.ArrayList;
2729
import java.util.Comparator;
@@ -41,7 +43,32 @@ public String getDescription() {
4143
return "Consistently order annotations by comparing their simple name.";
4244
}
4345

44-
private static final Comparator<J.Annotation> comparator = Comparator.comparing(J.Annotation::getSimpleName);
46+
private static final Comparator<J.Annotation> comparator = Comparator
47+
.comparing((J.Annotation a) -> {
48+
// If the annotation is a type use annotation, it should be ordered last
49+
if (a.getType() instanceof JavaType.Class) {
50+
for (JavaType.FullyQualified fq : ((JavaType.Class) a.getType()).getAnnotations()) {
51+
if (TypeUtils.isOfClassType(fq, "java.lang.annotation.Target")) {
52+
for (JavaType.Annotation.ElementValue elementValue : ((JavaType.Annotation) fq).getValues()) {
53+
Object value = elementValue.getValue();
54+
if (value instanceof List) {
55+
for (Object item : (List<?>) value) {
56+
if (item instanceof JavaType.Variable &&
57+
"TYPE_USE".equals(((JavaType.Variable) item).getName())) {
58+
return 1;
59+
}
60+
}
61+
} else if (value instanceof JavaType.Variable &&
62+
"TYPE_USE".equals(((JavaType.Variable) value).getName())) {
63+
return 1;
64+
}
65+
}
66+
}
67+
}
68+
}
69+
return 0;
70+
})
71+
.thenComparing(J.Annotation::getSimpleName);
4572

4673
@Override
4774
public JavaIsoVisitor<ExecutionContext> getVisitor() {

src/test/java/org/openrewrite/staticanalysis/ReorderAnnotationsTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,33 @@ void explicitImplementationClassInApi() {
153153
);
154154
}
155155

156+
@Test
157+
void typeAnnotationsLast() {
158+
rewriteRun(
159+
//language=java
160+
java(
161+
"""
162+
import org.jspecify.annotations.Nullable;
163+
class A {
164+
@Nullable
165+
@SuppressWarnings("all")
166+
Object typeAnnotationsShould() {
167+
}
168+
}
169+
""",
170+
"""
171+
import org.jspecify.annotations.Nullable;
172+
class A {
173+
@SuppressWarnings("all")
174+
@Nullable
175+
Object typeAnnotationsShould() {
176+
}
177+
}
178+
"""
179+
)
180+
);
181+
}
182+
156183
@Nested
157184
class NoChange {
158185
@Test
@@ -192,5 +219,23 @@ void explicitImplementationClassInApi() {
192219
)
193220
);
194221
}
222+
223+
@Test
224+
void typeAnnotationAlreadyLast() {
225+
rewriteRun(
226+
//language=java
227+
java(
228+
"""
229+
import org.jspecify.annotations.Nullable;
230+
class A {
231+
@SuppressWarnings("all")
232+
@Nullable
233+
Object typeAnnotationsShould() {
234+
}
235+
}
236+
"""
237+
)
238+
);
239+
}
195240
}
196241
}

0 commit comments

Comments
 (0)