Skip to content

Commit 1cdcfbd

Browse files
steve-aom-elliotttimtebeekgithub-actions[bot]Laurens-W
authored
Reordering Annotations (#544)
* Reordering annotations (aphabetically only for now) on method declarations. * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Suggested formatting changes * Do not make order configurable; show test failures * Keep prefix per annotation position * Update examples.yml * Sort annotations as a first show or results * Reorder class annotations as well * Also reorder field annotations * Only reorder if there's more than one annotation --------- Co-authored-by: Tim te Beek <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Laurens Westerlaken <[email protected]>
1 parent 0f250d2 commit 1cdcfbd

File tree

66 files changed

+470
-153
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+470
-153
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public Duration getEstimatedEffortPerOccurrence() {
5050
return Duration.ofMinutes(1);
5151
}
5252

53-
@Override
5453
@NonNull
54+
@Override
5555
public TreeVisitor<?, ExecutionContext> getVisitor() {
5656
return Preconditions.check(
5757
Preconditions.and(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public Duration getEstimatedEffortPerOccurrence() {
5151
public TreeVisitor<?, ExecutionContext> getVisitor() {
5252
return new JavaVisitor<ExecutionContext>() {
5353

54-
@SuppressWarnings("ConstantConditions")
5554
@Override
55+
@SuppressWarnings("ConstantConditions")
5656
public J visitUnary(J.Unary unary, ExecutionContext ctx) {
5757
if (unary.getOperator() == J.Unary.Type.Not && unary.getExpression() instanceof J.Parentheses) {
5858
J.Parentheses<?> expr = (J.Parentheses<?>) unary.getExpression();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -715,8 +715,8 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, J j) {
715715
return cu;
716716
}
717717

718-
@SuppressWarnings("unchecked")
719718
@Override
719+
@SuppressWarnings("unchecked")
720720
public <T extends J> J.ControlParentheses<T> visitControlParentheses(J.ControlParentheses<T> controlParens, J j) {
721721
if (isEqual.get()) {
722722
if (!(j instanceof J.ControlParentheses)) {
@@ -1426,8 +1426,8 @@ public J.ParameterizedType visitParameterizedType(J.ParameterizedType type, J j)
14261426
return type;
14271427
}
14281428

1429-
@SuppressWarnings("unchecked")
14301429
@Override
1430+
@SuppressWarnings("unchecked")
14311431
public <T extends J> J.Parentheses<T> visitParentheses(J.Parentheses<T> parens, J j) {
14321432
if (isEqual.get()) {
14331433
if (!(j instanceof J.Parentheses)) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public HideUtilityClassConstructorVisitor(HideUtilityClassConstructorStyle style
6464
this.utilityClassMatcher = new UtilityClassMatcher(style.getIgnoreIfAnnotatedBy());
6565
}
6666

67-
@SuppressWarnings("ConstantConditions")
6867
@Override
68+
@SuppressWarnings("ConstantConditions")
6969
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, P p) {
7070
J.ClassDeclaration c = super.visitClassDeclaration(classDecl, p);
7171
if (!EXCLUDE_CLASS_TYPES.contains(c.getKind()) && !c.hasModifier(J.Modifier.Type.Abstract) && utilityClassMatcher.isRefactorableUtilityClass(getCursor())) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,8 @@ public J visitTypeCast(J.TypeCast typeCast, Integer executionContext) {
451451
return typeCast;
452452
}
453453

454-
@SuppressWarnings("NullableProblems")
455454
@Override
455+
@SuppressWarnings("NullableProblems")
456456
public @Nullable J visitVariableDeclarations(J.VariableDeclarations multiVariable, Integer integer) {
457457
multiVariable = (J.VariableDeclarations) super.visitVariableDeclarations(multiVariable, integer);
458458
return replacements.processVariableDeclarations(multiVariable);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ public Duration getEstimatedEffortPerOccurrence() {
5353
return Duration.ofMinutes(1);
5454
}
5555

56-
@SuppressWarnings("ConstantConditions")
5756
@Override
57+
@SuppressWarnings("ConstantConditions")
5858
public TreeVisitor<?, ExecutionContext> getVisitor() {
5959
return new JavaIsoVisitor<ExecutionContext>() {
6060

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public RemoveUnusedLocalVariables(
6868
this.withSideEffects = withSideEffects;
6969
}
7070

71-
@InlineMe(replacement = "new RemoveUnusedLocalVariables(ignoreVariablesNamed, null, withSideEffects)")
7271
@Deprecated
72+
@InlineMe(replacement = "new RemoveUnusedLocalVariables(ignoreVariablesNamed, null, withSideEffects)")
7373
public RemoveUnusedLocalVariables(
7474
String @Nullable [] ignoreVariablesNamed,
7575
@Nullable Boolean withSideEffects) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
9696
return super.visitClassDeclaration(classDecl, ctx);
9797
}
9898

99-
@SuppressWarnings("all")
10099
@Override
100+
@SuppressWarnings("all")
101101
public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations.NamedVariable variable, ExecutionContext ctx) {
102102
Cursor parentScope = getCursorToParentScope(getCursor());
103103

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
* <p>
4+
* Licensed under the Moderne Source Available License (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://docs.moderne.io/licensing/moderne-source-available-license
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.staticanalysis;
17+
18+
import lombok.EqualsAndHashCode;
19+
import lombok.Value;
20+
import org.openrewrite.ExecutionContext;
21+
import org.openrewrite.Recipe;
22+
import org.openrewrite.internal.ListUtils;
23+
import org.openrewrite.java.JavaIsoVisitor;
24+
import org.openrewrite.java.tree.J;
25+
26+
import java.util.ArrayList;
27+
import java.util.Comparator;
28+
import java.util.List;
29+
30+
@Value
31+
@EqualsAndHashCode(callSuper = false)
32+
public class ReorderAnnotations extends Recipe {
33+
34+
@Override
35+
public String getDisplayName() {
36+
return "Reorder annotations alphabetically";
37+
}
38+
39+
@Override
40+
public String getDescription() {
41+
return "Consistently order annotations by comparing their simple name.";
42+
}
43+
44+
private static final Comparator<J.Annotation> comparator = Comparator.comparing(J.Annotation::getSimpleName);
45+
46+
@Override
47+
public JavaIsoVisitor<ExecutionContext> getVisitor() {
48+
return new JavaIsoVisitor<ExecutionContext>() {
49+
@Override
50+
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
51+
J.ClassDeclaration d = super.visitClassDeclaration(classDecl, ctx);
52+
if (1 < d.getLeadingAnnotations().size()) {
53+
List<J.Annotation> sortedAnnotations = new ArrayList<>(d.getLeadingAnnotations());
54+
sortedAnnotations.sort(comparator);
55+
if (!sortedAnnotations.equals(d.getLeadingAnnotations())) {
56+
return d.withLeadingAnnotations(ListUtils.map(sortedAnnotations,
57+
(i, a) -> a.withPrefix(d.getLeadingAnnotations().get(i).getPrefix())));
58+
}
59+
}
60+
return d;
61+
}
62+
63+
@Override
64+
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
65+
J.VariableDeclarations d = super.visitVariableDeclarations(multiVariable, ctx);
66+
if (1 < d.getLeadingAnnotations().size()) {
67+
List<J.Annotation> sortedAnnotations = new ArrayList<>(d.getLeadingAnnotations());
68+
sortedAnnotations.sort(comparator);
69+
if (!sortedAnnotations.equals(d.getLeadingAnnotations())) {
70+
return d.withLeadingAnnotations(ListUtils.map(sortedAnnotations,
71+
(i, a) -> a.withPrefix(d.getLeadingAnnotations().get(i).getPrefix())));
72+
}
73+
}
74+
return d;
75+
}
76+
77+
@Override
78+
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
79+
J.MethodDeclaration d = super.visitMethodDeclaration(method, ctx);
80+
if (1 < d.getLeadingAnnotations().size()) {
81+
List<J.Annotation> sortedAnnotations = new ArrayList<>(d.getLeadingAnnotations());
82+
sortedAnnotations.sort(comparator);
83+
if (!sortedAnnotations.equals(d.getLeadingAnnotations())) {
84+
return d.withLeadingAnnotations(ListUtils.map(sortedAnnotations,
85+
(i, a) -> a.withPrefix(d.getLeadingAnnotations().get(i).getPrefix())));
86+
}
87+
}
88+
return d;
89+
}
90+
};
91+
}
92+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
8686
return super.visit(tree, ctx);
8787
}
8888

89-
@SuppressWarnings("UnusedAssignment")
9089
@Override
90+
@SuppressWarnings("UnusedAssignment")
9191
public J visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
9292
if (classDecl.getType() == null) {
9393
return classDecl;

0 commit comments

Comments
 (0)