001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2025 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018/////////////////////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.modifier; 021 022import java.util.ArrayList; 023import java.util.Iterator; 024import java.util.List; 025 026import com.puppycrawl.tools.checkstyle.StatelessCheck; 027import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 028import com.puppycrawl.tools.checkstyle.api.DetailAST; 029import com.puppycrawl.tools.checkstyle.api.TokenTypes; 030 031/** 032 * <div> 033 * Checks that the order of modifiers conforms to the suggestions in the 034 * <a href="https://docs.oracle.com/javase/specs/jls/se16/preview/specs/sealed-classes-jls.html"> 035 * Java Language specification, § 8.1.1, 8.3.1, 8.4.3</a> and 036 * <a href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-9.html">9.4</a>. 037 * The correct order is: 038 * </div> 039 * 040 * <ol> 041 * <li> {@code public} </li> 042 * <li> {@code protected} </li> 043 * <li> {@code private} </li> 044 * <li> {@code abstract} </li> 045 * <li> {@code default} </li> 046 * <li> {@code static} </li> 047 * <li> {@code sealed} </li> 048 * <li> {@code non-sealed} </li> 049 * <li> {@code final} </li> 050 * <li> {@code transient} </li> 051 * <li> {@code volatile} </li> 052 * <li> {@code synchronized} </li> 053 * <li> {@code native} </li> 054 * <li> {@code strictfp} </li> 055 * </ol> 056 * 057 * <p> 058 * In additional, modifiers are checked to ensure all annotations 059 * are declared before all other modifiers. 060 * </p> 061 * 062 * <p> 063 * Rationale: Code is easier to read if everybody follows 064 * a standard. 065 * </p> 066 * 067 * <p> 068 * ATTENTION: We skip 069 * <a href="https://www.oracle.com/technical-resources/articles/java/ma14-architect-annotations.html"> 070 * type annotations</a> from validation. 071 * </p> 072 * 073 * @since 3.0 074 */ 075@StatelessCheck 076public class ModifierOrderCheck 077 extends AbstractCheck { 078 079 /** 080 * A key is pointing to the warning message text in "messages.properties" 081 * file. 082 */ 083 public static final String MSG_ANNOTATION_ORDER = "annotation.order"; 084 085 /** 086 * A key is pointing to the warning message text in "messages.properties" 087 * file. 088 */ 089 public static final String MSG_MODIFIER_ORDER = "mod.order"; 090 091 /** 092 * The order of modifiers as suggested in sections 8.1.1, 093 * 8.3.1 and 8.4.3 of the JLS. 094 */ 095 private static final String[] JLS_ORDER = { 096 "public", "protected", "private", "abstract", "default", "static", 097 "sealed", "non-sealed", "final", "transient", "volatile", 098 "synchronized", "native", "strictfp", 099 }; 100 101 @Override 102 public int[] getDefaultTokens() { 103 return getRequiredTokens(); 104 } 105 106 @Override 107 public int[] getAcceptableTokens() { 108 return getRequiredTokens(); 109 } 110 111 @Override 112 public int[] getRequiredTokens() { 113 return new int[] {TokenTypes.MODIFIERS}; 114 } 115 116 @Override 117 public void visitToken(DetailAST ast) { 118 final List<DetailAST> mods = new ArrayList<>(); 119 DetailAST modifier = ast.getFirstChild(); 120 while (modifier != null) { 121 mods.add(modifier); 122 modifier = modifier.getNextSibling(); 123 } 124 125 if (!mods.isEmpty()) { 126 final DetailAST error = checkOrderSuggestedByJls(mods); 127 if (error != null) { 128 if (error.getType() == TokenTypes.ANNOTATION) { 129 log(error, 130 MSG_ANNOTATION_ORDER, 131 error.getFirstChild().getText() 132 + error.getFirstChild().getNextSibling() 133 .getText()); 134 } 135 else { 136 log(error, MSG_MODIFIER_ORDER, error.getText()); 137 } 138 } 139 } 140 } 141 142 /** 143 * Checks if the modifiers were added in the order suggested 144 * in the Java language specification. 145 * 146 * @param modifiers list of modifier AST tokens 147 * @return null if the order is correct, otherwise returns the offending 148 * modifier AST. 149 */ 150 private static DetailAST checkOrderSuggestedByJls(List<DetailAST> modifiers) { 151 final Iterator<DetailAST> iterator = modifiers.iterator(); 152 153 // Speed past all initial annotations 154 DetailAST modifier = skipAnnotations(iterator); 155 156 DetailAST offendingModifier = null; 157 158 // All modifiers are annotations, no problem 159 if (modifier.getType() != TokenTypes.ANNOTATION) { 160 int index = 0; 161 162 while (modifier != null 163 && offendingModifier == null) { 164 if (modifier.getType() == TokenTypes.ANNOTATION) { 165 if (!isAnnotationOnType(modifier)) { 166 // Annotation not at start of modifiers, bad 167 offendingModifier = modifier; 168 } 169 break; 170 } 171 172 while (index < JLS_ORDER.length 173 && !JLS_ORDER[index].equals(modifier.getText())) { 174 index++; 175 } 176 177 if (index == JLS_ORDER.length) { 178 // Current modifier is out of JLS order 179 offendingModifier = modifier; 180 } 181 else if (iterator.hasNext()) { 182 modifier = iterator.next(); 183 } 184 else { 185 // Reached end of modifiers without problem 186 modifier = null; 187 } 188 } 189 } 190 return offendingModifier; 191 } 192 193 /** 194 * Skip all annotations in modifier block. 195 * 196 * @param modifierIterator iterator for collection of modifiers 197 * @return modifier next to last annotation 198 */ 199 private static DetailAST skipAnnotations(Iterator<DetailAST> modifierIterator) { 200 DetailAST modifier; 201 do { 202 modifier = modifierIterator.next(); 203 } while (modifierIterator.hasNext() && modifier.getType() == TokenTypes.ANNOTATION); 204 return modifier; 205 } 206 207 /** 208 * Checks whether annotation on type takes place. 209 * 210 * @param modifier modifier token. 211 * @return true if annotation on type takes place. 212 */ 213 private static boolean isAnnotationOnType(DetailAST modifier) { 214 boolean annotationOnType = false; 215 final DetailAST modifiers = modifier.getParent(); 216 final DetailAST definition = modifiers.getParent(); 217 final int definitionType = definition.getType(); 218 if (definitionType == TokenTypes.VARIABLE_DEF 219 || definitionType == TokenTypes.PARAMETER_DEF 220 || definitionType == TokenTypes.CTOR_DEF) { 221 annotationOnType = true; 222 } 223 else if (definitionType == TokenTypes.METHOD_DEF) { 224 final DetailAST typeToken = definition.findFirstToken(TokenTypes.TYPE); 225 final int methodReturnType = typeToken.getLastChild().getType(); 226 if (methodReturnType != TokenTypes.LITERAL_VOID) { 227 annotationOnType = true; 228 } 229 } 230 return annotationOnType; 231 } 232 233}