View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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.indentation;
21  
22  import java.util.Arrays;
23  
24  import com.puppycrawl.tools.checkstyle.api.DetailAST;
25  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26  
27  /**
28   * Handler for a list of statements.
29   *
30   * @author jrichard
31   */
32  public class SlistHandler extends BlockParentHandler {
33  
34      /**
35       * Parent token types.
36       */
37      private static final int[] PARENT_TOKEN_TYPES = {
38          TokenTypes.CTOR_DEF,
39          TokenTypes.METHOD_DEF,
40          TokenTypes.STATIC_INIT,
41          TokenTypes.LITERAL_SYNCHRONIZED,
42          TokenTypes.LITERAL_IF,
43          TokenTypes.LITERAL_WHILE,
44          TokenTypes.LITERAL_DO,
45          TokenTypes.LITERAL_FOR,
46          TokenTypes.LITERAL_ELSE,
47          TokenTypes.LITERAL_TRY,
48          TokenTypes.LITERAL_CATCH,
49          TokenTypes.LITERAL_FINALLY,
50      };
51  
52      static {
53          // Array sorting for binary search
54          Arrays.sort(PARENT_TOKEN_TYPES);
55      }
56  
57      /**
58       * Construct an instance of this handler with the given indentation check,
59       * abstract syntax tree, and parent handler.
60       *
61       * @param indentCheck   the indentation check
62       * @param ast           the abstract syntax tree
63       * @param parent        the parent handler
64       */
65      public SlistHandler(IndentationCheck indentCheck,
66          DetailAST ast, AbstractExpressionHandler parent) {
67          super(indentCheck, "block", ast, parent);
68      }
69  
70      @Override
71      public IndentLevel getSuggestedChildIndent(AbstractExpressionHandler child) {
72          // this is:
73          //  switch (var) {
74          //     case 3: {
75          //        break;
76          //     }
77          //  }
78          //  ... the case SLIST is followed by a user-created SLIST and
79          //  preceded by a switch
80  
81          final IndentLevel result;
82          // if our parent is a block handler we want to be transparent
83          if (getParent() instanceof BlockParentHandler
84                  && !(getParent() instanceof SlistHandler)
85              || child instanceof SlistHandler
86                  && getParent() instanceof CaseHandler) {
87              result = getParent().getSuggestedChildIndent(child);
88          }
89          else {
90              result = super.getSuggestedChildIndent(child);
91          }
92          return result;
93      }
94  
95      @Override
96      protected DetailAST getListChild() {
97          return getMainAst();
98      }
99  
100     @Override
101     protected DetailAST getLeftCurly() {
102         return getMainAst();
103     }
104 
105     @Override
106     protected DetailAST getRightCurly() {
107         return getMainAst().findFirstToken(TokenTypes.RCURLY);
108     }
109 
110     @Override
111     protected DetailAST getTopLevelAst() {
112         return null;
113     }
114 
115     /**
116      * Determine if the expression we are handling has a block parent.
117      *
118      * @return true if it does, false otherwise
119      */
120     private boolean hasBlockParent() {
121         final int parentType = getMainAst().getParent().getType();
122         return Arrays.binarySearch(PARENT_TOKEN_TYPES, parentType) >= 0;
123     }
124 
125     @Override
126     public void checkIndentation() {
127         // only need to check this if parent is not
128         // an if, else, while, do, ctor, method
129         if (!hasBlockParent() && !isSameLineCaseGroup()) {
130             super.checkIndentation();
131         }
132     }
133 
134     /**
135      * Checks if SLIST node is placed at the same line as CASE_GROUP node.
136      * @return true, if SLIST node is places at the same line as CASE_GROUP node.
137      */
138     private boolean isSameLineCaseGroup() {
139         final DetailAST parentNode = getMainAst().getParent();
140         return parentNode.getType() == TokenTypes.CASE_GROUP
141             && getMainAst().getLineNo() == parentNode.getLineNo();
142     }
143 }