View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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.design;
21  
22  import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
23  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
24  import com.puppycrawl.tools.checkstyle.api.DetailAST;
25  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26  import com.puppycrawl.tools.checkstyle.utils.ScopeUtils;
27  
28  /**
29   * <p>
30   * Check nested (internal) classes/interfaces are declared at the bottom of the
31   * class after all method and field declarations.
32   * </p>
33   *
34   * @author <a href="mailto:ryly@mail.ru">Ruslan Dyachenko</a>
35   */
36  @FileStatefulCheck
37  public class InnerTypeLastCheck extends AbstractCheck {
38  
39      /**
40       * A key is pointing to the warning message text in "messages.properties"
41       * file.
42       */
43      public static final String MSG_KEY = "arrangement.members.before.inner";
44  
45      /** Meet a root class. */
46      private boolean rootClass = true;
47  
48      @Override
49      public int[] getDefaultTokens() {
50          return getRequiredTokens();
51      }
52  
53      @Override
54      public int[] getAcceptableTokens() {
55          return getRequiredTokens();
56      }
57  
58      @Override
59      public int[] getRequiredTokens() {
60          return new int[] {TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF};
61      }
62  
63      @Override
64      public void visitToken(DetailAST ast) {
65          // First root class
66          if (rootClass) {
67              rootClass = false;
68          }
69          else {
70              DetailAST nextSibling = ast.getNextSibling();
71              while (nextSibling != null) {
72                  if (!ScopeUtils.isInCodeBlock(ast)
73                      && (nextSibling.getType() == TokenTypes.VARIABLE_DEF
74                          || nextSibling.getType() == TokenTypes.METHOD_DEF)) {
75                      log(nextSibling.getLineNo(), nextSibling.getColumnNo(),
76                          MSG_KEY);
77                  }
78                  nextSibling = nextSibling.getNextSibling();
79              }
80          }
81      }
82  
83      @Override
84      public void leaveToken(DetailAST ast) {
85          // Is this a root class
86          if (ast.getParent() == null) {
87              rootClass = true;
88          }
89      }
90  
91  }