|
| 1 | +package org.alfasoftware.astra.core.refactoring.operations.sonar.s1124; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Comparator; |
| 6 | +import java.util.List; |
| 7 | +import java.util.stream.Collectors; |
| 8 | + |
| 9 | +import org.alfasoftware.astra.core.utils.ASTOperation; |
| 10 | +import org.alfasoftware.astra.core.utils.AstraUtils; |
| 11 | +import org.eclipse.jdt.core.dom.AST; |
| 12 | +import org.eclipse.jdt.core.dom.ASTNode; |
| 13 | +import org.eclipse.jdt.core.dom.BodyDeclaration; |
| 14 | +import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor; |
| 15 | +import org.eclipse.jdt.core.dom.CompilationUnit; |
| 16 | +import org.eclipse.jdt.core.dom.IExtendedModifier; |
| 17 | +import org.eclipse.jdt.core.dom.Modifier; |
| 18 | +import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor; |
| 19 | +import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; |
| 20 | +import org.eclipse.jdt.core.dom.rewrite.ListRewrite; |
| 21 | +import org.eclipse.jface.text.BadLocationException; |
| 22 | +import org.eclipse.text.edits.MalformedTreeException; |
| 23 | +import org.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
| 25 | + |
| 26 | +/** |
| 27 | + * Refactoring operation implementing SonarQube rule java:S1124 |
| 28 | + * "Modifiers should be declared in the correct order". |
| 29 | + * |
| 30 | + * <p>The Java Language Specification recommends the following canonical modifier order: |
| 31 | + * <ol> |
| 32 | + * <li>Annotations</li> |
| 33 | + * <li>{@code public}</li> |
| 34 | + * <li>{@code protected}</li> |
| 35 | + * <li>{@code private}</li> |
| 36 | + * <li>{@code abstract}</li> |
| 37 | + * <li>{@code default}</li> |
| 38 | + * <li>{@code static}</li> |
| 39 | + * <li>{@code final}</li> |
| 40 | + * <li>{@code transient}</li> |
| 41 | + * <li>{@code volatile}</li> |
| 42 | + * <li>{@code synchronized}</li> |
| 43 | + * <li>{@code native}</li> |
| 44 | + * <li>{@code strictfp}</li> |
| 45 | + * </ol> |
| 46 | + * |
| 47 | + * <p>When keyword modifiers appear out of this order on any body declaration (type, method, field, |
| 48 | + * etc.) they are reordered in-place. Annotations are left at their original positions. |
| 49 | + * |
| 50 | + * <p>Examples: |
| 51 | + * <ul> |
| 52 | + * <li>{@code static public void foo()} → {@code public static void foo()}</li> |
| 53 | + * <li>{@code final static int X = 1} → {@code static final int X = 1}</li> |
| 54 | + * <li>{@code final public static String S} → {@code public static final String S}</li> |
| 55 | + * <li>{@code synchronized static void bar()} → {@code static synchronized void bar()}</li> |
| 56 | + * </ul> |
| 57 | + * |
| 58 | + * <p>Mirrors the detection logic of SonarJava's {@code ModifiersOrderCheck}. |
| 59 | + */ |
| 60 | +public class ModifiersOrderOperation implements ASTOperation { |
| 61 | + |
| 62 | + private static final Logger log = LoggerFactory.getLogger(ModifiersOrderOperation.class); |
| 63 | + |
| 64 | + /** |
| 65 | + * Canonical modifier order per the Java Language Specification (JLS 17). |
| 66 | + * Modifiers not present in this list are placed at the end in their original relative order. |
| 67 | + */ |
| 68 | + static final List<Modifier.ModifierKeyword> MODIFIER_ORDER = List.of( |
| 69 | + Modifier.ModifierKeyword.PUBLIC_KEYWORD, |
| 70 | + Modifier.ModifierKeyword.PROTECTED_KEYWORD, |
| 71 | + Modifier.ModifierKeyword.PRIVATE_KEYWORD, |
| 72 | + Modifier.ModifierKeyword.ABSTRACT_KEYWORD, |
| 73 | + Modifier.ModifierKeyword.DEFAULT_KEYWORD, |
| 74 | + Modifier.ModifierKeyword.STATIC_KEYWORD, |
| 75 | + Modifier.ModifierKeyword.FINAL_KEYWORD, |
| 76 | + Modifier.ModifierKeyword.TRANSIENT_KEYWORD, |
| 77 | + Modifier.ModifierKeyword.VOLATILE_KEYWORD, |
| 78 | + Modifier.ModifierKeyword.SYNCHRONIZED_KEYWORD, |
| 79 | + Modifier.ModifierKeyword.NATIVE_KEYWORD, |
| 80 | + Modifier.ModifierKeyword.STRICTFP_KEYWORD |
| 81 | + ); |
| 82 | + |
| 83 | + @Override |
| 84 | + public void run(CompilationUnit compilationUnit, ASTNode node, ASTRewrite rewriter) |
| 85 | + throws IOException, MalformedTreeException, BadLocationException { |
| 86 | + |
| 87 | + if (!(node instanceof BodyDeclaration)) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + BodyDeclaration bodyDecl = (BodyDeclaration) node; |
| 92 | + |
| 93 | + @SuppressWarnings("unchecked") |
| 94 | + List<IExtendedModifier> allModifiers = bodyDecl.modifiers(); |
| 95 | + |
| 96 | + List<Modifier> keywordModifiers = allModifiers.stream() |
| 97 | + .filter(m -> m instanceof Modifier) |
| 98 | + .map(m -> (Modifier) m) |
| 99 | + .collect(Collectors.toList()); |
| 100 | + |
| 101 | + if (keywordModifiers.size() < 2) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + List<Modifier.ModifierKeyword> currentOrder = keywordModifiers.stream() |
| 106 | + .map(Modifier::getKeyword) |
| 107 | + .collect(Collectors.toList()); |
| 108 | + |
| 109 | + List<Modifier.ModifierKeyword> sortedOrder = new ArrayList<>(currentOrder); |
| 110 | + sortedOrder.sort(Comparator.comparingInt(k -> { |
| 111 | + int idx = MODIFIER_ORDER.indexOf(k); |
| 112 | + return idx == -1 ? MODIFIER_ORDER.size() : idx; |
| 113 | + })); |
| 114 | + |
| 115 | + if (currentOrder.equals(sortedOrder)) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + log.info("Reordering modifiers in [{}]: {} -> {}", |
| 120 | + AstraUtils.getNameForCompilationUnit(compilationUnit), |
| 121 | + currentOrder.stream().map(k -> k.toString()).collect(Collectors.joining(" ")), |
| 122 | + sortedOrder.stream().map(k -> k.toString()).collect(Collectors.joining(" "))); |
| 123 | + |
| 124 | + ChildListPropertyDescriptor modifiersProperty = findModifiersProperty(bodyDecl); |
| 125 | + if (modifiersProperty == null) { |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + AST ast = compilationUnit.getAST(); |
| 130 | + ListRewrite listRewrite = rewriter.getListRewrite(bodyDecl, modifiersProperty); |
| 131 | + |
| 132 | + for (int i = 0; i < keywordModifiers.size(); i++) { |
| 133 | + Modifier.ModifierKeyword target = sortedOrder.get(i); |
| 134 | + if (!currentOrder.get(i).equals(target)) { |
| 135 | + listRewrite.replace(keywordModifiers.get(i), ast.newModifier(target), null); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Finds the "modifiers" ChildListPropertyDescriptor for the given node by inspecting |
| 142 | + * its structural properties. This avoids hard-coding per-subclass property constants |
| 143 | + * (MethodDeclaration.MODIFIERS2_PROPERTY, FieldDeclaration.MODIFIERS2_PROPERTY, etc.) |
| 144 | + * which are distinct objects even though they share the same semantics. |
| 145 | + */ |
| 146 | + @SuppressWarnings("rawtypes") |
| 147 | + private static ChildListPropertyDescriptor findModifiersProperty(ASTNode node) { |
| 148 | + for (Object prop : node.structuralPropertiesForType()) { |
| 149 | + if (prop instanceof ChildListPropertyDescriptor) { |
| 150 | + ChildListPropertyDescriptor clpd = (ChildListPropertyDescriptor) prop; |
| 151 | + if ("modifiers".equals(clpd.getId())) { |
| 152 | + return clpd; |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + return null; |
| 157 | + } |
| 158 | +} |
0 commit comments