001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2018 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.getLineNo(), error.getColumnNo(), 122 MSG_ANNOTATION_ORDER, 123 error.getFirstChild().getText() 124 + error.getFirstChild().getNextSibling() 125 .getText()); 126 } 127 else { 128 log(error.getLineNo(), error.getColumnNo(), 129 MSG_MODIFIER_ORDER, error.getText()); 130 } 131 } 132 } 133 } 134 135 /** 136 * Checks if the modifiers were added in the order suggested 137 * in the Java language specification. 138 * 139 * @param modifiers list of modifier AST tokens 140 * @return null if the order is correct, otherwise returns the offending 141 * modifier AST. 142 */ 143 private static DetailAST checkOrderSuggestedByJls(List<DetailAST> modifiers) { 144 final Iterator<DetailAST> iterator = modifiers.iterator(); 145 146 //Speed past all initial annotations 147 DetailAST modifier = skipAnnotations(iterator); 148 149 DetailAST offendingModifier = null; 150 151 //All modifiers are annotations, no problem 152 if (modifier.getType() != TokenTypes.ANNOTATION) { 153 int index = 0; 154 155 while (modifier != null 156 && offendingModifier == null) { 157 if (modifier.getType() == TokenTypes.ANNOTATION) { 158 if (!isAnnotationOnType(modifier)) { 159 //Annotation not at start of modifiers, bad 160 offendingModifier = modifier; 161 } 162 break; 163 } 164 165 while (index < JLS_ORDER.length 166 && !JLS_ORDER[index].equals(modifier.getText())) { 167 index++; 168 } 169 170 if (index == JLS_ORDER.length) { 171 //Current modifier is out of JLS order 172 offendingModifier = modifier; 173 } 174 else if (iterator.hasNext()) { 175 modifier = iterator.next(); 176 } 177 else { 178 //Reached end of modifiers without problem 179 modifier = null; 180 } 181 } 182 } 183 return offendingModifier; 184 } 185 186 /** 187 * Skip all annotations in modifier block. 188 * @param modifierIterator iterator for collection of modifiers 189 * @return modifier next to last annotation 190 */ 191 private static DetailAST skipAnnotations(Iterator<DetailAST> modifierIterator) { 192 DetailAST modifier; 193 do { 194 modifier = modifierIterator.next(); 195 } while (modifierIterator.hasNext() && modifier.getType() == TokenTypes.ANNOTATION); 196 return modifier; 197 } 198 199 /** 200 * Checks whether annotation on type takes place. 201 * @param modifier modifier token. 202 * @return true if annotation on type takes place. 203 */ 204 private static boolean isAnnotationOnType(DetailAST modifier) { 205 boolean annotationOnType = false; 206 final DetailAST modifiers = modifier.getParent(); 207 final DetailAST definition = modifiers.getParent(); 208 final int definitionType = definition.getType(); 209 if (definitionType == TokenTypes.VARIABLE_DEF 210 || definitionType == TokenTypes.PARAMETER_DEF 211 || definitionType == TokenTypes.CTOR_DEF) { 212 annotationOnType = true; 213 } 214 else if (definitionType == TokenTypes.METHOD_DEF) { 215 final DetailAST typeToken = definition.findFirstToken(TokenTypes.TYPE); 216 final int methodReturnType = typeToken.getLastChild().getType(); 217 if (methodReturnType != TokenTypes.LITERAL_VOID) { 218 annotationOnType = true; 219 } 220 } 221 return annotationOnType; 222 } 223 224}