001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2019 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 * <p> 033 * Checks that the order of modifiers conforms to the suggestions in the 034 * <a 035 * href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html"> 036 * Java Language specification, sections 8.1.1, 8.3.1 and 8.4.3</a>. 037 * The correct order is:</p> 038 039<ol> 040 <li><span class="code">public</span></li> 041 <li><span class="code">protected</span></li> 042 043 <li><span class="code">private</span></li> 044 <li><span class="code">abstract</span></li> 045 <li><span class="code">default</span></li> 046 <li><span class="code">static</span></li> 047 <li><span class="code">final</span></li> 048 <li><span class="code">transient</span></li> 049 <li><span class="code">volatile</span></li> 050 051 <li><span class="code">synchronized</span></li> 052 <li><span class="code">native</span></li> 053 <li><span class="code">strictfp</span></li> 054</ol> 055 * In additional, modifiers are checked to ensure all annotations 056 * are declared before all other modifiers. 057 * <p> 058 * Rationale: Code is easier to read if everybody follows 059 * a standard. 060 * </p> 061 * <p> 062 * An example of how to configure the check is: 063 * </p> 064 * <pre> 065 * <module name="ModifierOrder"/> 066 * </pre> 067 */ 068@StatelessCheck 069public class ModifierOrderCheck 070 extends AbstractCheck { 071 072 /** 073 * A key is pointing to the warning message text in "messages.properties" 074 * file. 075 */ 076 public static final String MSG_ANNOTATION_ORDER = "annotation.order"; 077 078 /** 079 * A key is pointing to the warning message text in "messages.properties" 080 * file. 081 */ 082 public static final String MSG_MODIFIER_ORDER = "mod.order"; 083 084 /** 085 * The order of modifiers as suggested in sections 8.1.1, 086 * 8.3.1 and 8.4.3 of the JLS. 087 */ 088 private static final String[] JLS_ORDER = { 089 "public", "protected", "private", "abstract", "default", "static", 090 "final", "transient", "volatile", "synchronized", "native", "strictfp", 091 }; 092 093 @Override 094 public int[] getDefaultTokens() { 095 return getRequiredTokens(); 096 } 097 098 @Override 099 public int[] getAcceptableTokens() { 100 return getRequiredTokens(); 101 } 102 103 @Override 104 public int[] getRequiredTokens() { 105 return new int[] {TokenTypes.MODIFIERS}; 106 } 107 108 @Override 109 public void visitToken(DetailAST ast) { 110 final List<DetailAST> mods = new ArrayList<>(); 111 DetailAST modifier = ast.getFirstChild(); 112 while (modifier != null) { 113 mods.add(modifier); 114 modifier = modifier.getNextSibling(); 115 } 116 117 if (!mods.isEmpty()) { 118 final DetailAST error = checkOrderSuggestedByJls(mods); 119 if (error != null) { 120 if (error.getType() == TokenTypes.ANNOTATION) { 121 log(error, 122 MSG_ANNOTATION_ORDER, 123 error.getFirstChild().getText() 124 + error.getFirstChild().getNextSibling() 125 .getText()); 126 } 127 else { 128 log(error, MSG_MODIFIER_ORDER, error.getText()); 129 } 130 } 131 } 132 } 133 134 /** 135 * Checks if the modifiers were added in the order suggested 136 * in the Java language specification. 137 * 138 * @param modifiers list of modifier AST tokens 139 * @return null if the order is correct, otherwise returns the offending 140 * modifier AST. 141 */ 142 private static DetailAST checkOrderSuggestedByJls(List<DetailAST> modifiers) { 143 final Iterator<DetailAST> iterator = modifiers.iterator(); 144 145 //Speed past all initial annotations 146 DetailAST modifier = skipAnnotations(iterator); 147 148 DetailAST offendingModifier = null; 149 150 //All modifiers are annotations, no problem 151 if (modifier.getType() != TokenTypes.ANNOTATION) { 152 int index = 0; 153 154 while (modifier != null 155 && offendingModifier == null) { 156 if (modifier.getType() == TokenTypes.ANNOTATION) { 157 if (!isAnnotationOnType(modifier)) { 158 //Annotation not at start of modifiers, bad 159 offendingModifier = modifier; 160 } 161 break; 162 } 163 164 while (index < JLS_ORDER.length 165 && !JLS_ORDER[index].equals(modifier.getText())) { 166 index++; 167 } 168 169 if (index == JLS_ORDER.length) { 170 //Current modifier is out of JLS order 171 offendingModifier = modifier; 172 } 173 else if (iterator.hasNext()) { 174 modifier = iterator.next(); 175 } 176 else { 177 //Reached end of modifiers without problem 178 modifier = null; 179 } 180 } 181 } 182 return offendingModifier; 183 } 184 185 /** 186 * Skip all annotations in modifier block. 187 * @param modifierIterator iterator for collection of modifiers 188 * @return modifier next to last annotation 189 */ 190 private static DetailAST skipAnnotations(Iterator<DetailAST> modifierIterator) { 191 DetailAST modifier; 192 do { 193 modifier = modifierIterator.next(); 194 } while (modifierIterator.hasNext() && modifier.getType() == TokenTypes.ANNOTATION); 195 return modifier; 196 } 197 198 /** 199 * Checks whether annotation on type takes place. 200 * @param modifier modifier token. 201 * @return true if annotation on type takes place. 202 */ 203 private static boolean isAnnotationOnType(DetailAST modifier) { 204 boolean annotationOnType = false; 205 final DetailAST modifiers = modifier.getParent(); 206 final DetailAST definition = modifiers.getParent(); 207 final int definitionType = definition.getType(); 208 if (definitionType == TokenTypes.VARIABLE_DEF 209 || definitionType == TokenTypes.PARAMETER_DEF 210 || definitionType == TokenTypes.CTOR_DEF) { 211 annotationOnType = true; 212 } 213 else if (definitionType == TokenTypes.METHOD_DEF) { 214 final DetailAST typeToken = definition.findFirstToken(TokenTypes.TYPE); 215 final int methodReturnType = typeToken.getLastChild().getType(); 216 if (methodReturnType != TokenTypes.LITERAL_VOID) { 217 annotationOnType = true; 218 } 219 } 220 return annotationOnType; 221 } 222 223}