1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.modifier;
21  
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import com.puppycrawl.tools.checkstyle.StatelessCheck;
27  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
28  import com.puppycrawl.tools.checkstyle.api.DetailAST;
29  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30  
31  /**
32   * <p>
33   * Checks that the order of modifiers conforms to the suggestions in the
34   * <a
35   * href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html">
36   * Java Language specification, sections 8.1.1, 8.3.1 and 8.4.3</a>.
37   * The correct order is:</p>
38  
39  <ol>
40    <li><span class="code">public</span></li>
41    <li><span class="code">protected</span></li>
42  
43    <li><span class="code">private</span></li>
44    <li><span class="code">abstract</span></li>
45    <li><span class="code">default</span></li>
46    <li><span class="code">static</span></li>
47    <li><span class="code">final</span></li>
48    <li><span class="code">transient</span></li>
49    <li><span class="code">volatile</span></li>
50  
51    <li><span class="code">synchronized</span></li>
52    <li><span class="code">native</span></li>
53    <li><span class="code">strictfp</span></li>
54  </ol>
55   * In additional, modifiers are checked to ensure all annotations
56   * are declared before all other modifiers.
57   * <p>
58   * Rationale: Code is easier to read if everybody follows
59   * a standard.
60   * </p>
61   * <p>
62   * An example of how to configure the check is:
63   * </p>
64   * <pre>
65   * &lt;module name="ModifierOrder"/&gt;
66   * </pre>
67   */
68  @StatelessCheck
69  public class ModifierOrderCheck
70      extends AbstractCheck {
71  
72      /**
73       * A key is pointing to the warning message text in "messages.properties"
74       * file.
75       */
76      public static final String MSG_ANNOTATION_ORDER = "annotation.order";
77  
78      /**
79       * A key is pointing to the warning message text in "messages.properties"
80       * file.
81       */
82      public static final String MSG_MODIFIER_ORDER = "mod.order";
83  
84      /**
85       * The order of modifiers as suggested in sections 8.1.1,
86       * 8.3.1 and 8.4.3 of the JLS.
87       */
88      private static final String[] JLS_ORDER = {
89          "public", "protected", "private", "abstract", "default", "static",
90          "final", "transient", "volatile", "synchronized", "native", "strictfp",
91      };
92  
93      @Override
94      public int[] getDefaultTokens() {
95          return getRequiredTokens();
96      }
97  
98      @Override
99      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 }