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.indentation;
21  
22  import com.puppycrawl.tools.checkstyle.api.DetailAST;
23  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
24  
25  /**
26   * Handler for try blocks.
27   *
28   * @author jrichard
29   */
30  public class TryHandler extends BlockParentHandler {
31  
32      /**
33       * Construct an instance of this handler with the given indentation check,
34       * abstract syntax tree, and parent handler.
35       *
36       * @param indentCheck   the indentation check
37       * @param ast           the abstract syntax tree
38       * @param parent        the parent handler
39       */
40      public TryHandler(IndentationCheck indentCheck,
41          DetailAST ast, AbstractExpressionHandler parent) {
42          super(indentCheck, "try", ast, parent);
43      }
44  
45      /**
46       * Method to find left parenthesis of try with resources.
47       *
48       * @return DetailAst    left parenthesis of try with resources
49       */
50      private DetailAST getTryResLparen() {
51          return getMainAst().getFirstChild().getFirstChild();
52      }
53  
54      /**
55       * Method to find right parenthesis of try with resources.
56       *
57       * @return DetailAst    right parenthesis of try with resources
58       */
59      private DetailAST getTryResRparen() {
60          return getMainAst().getFirstChild().getLastChild();
61      }
62  
63      @Override
64      public IndentLevel getSuggestedChildIndent(AbstractExpressionHandler child) {
65          final IndentLevel result;
66          if (child instanceof CatchHandler
67              || child instanceof FinallyHandler) {
68              result = getIndent();
69          }
70          else {
71              result = super.getSuggestedChildIndent(child);
72          }
73          return result;
74      }
75  
76      @Override
77      public void checkIndentation() {
78          super.checkIndentation();
79          if (getMainAst().getFirstChild().getType() == TokenTypes.RESOURCE_SPECIFICATION) {
80              checkTryResParen(getTryResLparen(), "lparen");
81              checkTryResParen(getTryResRparen(), "rparen");
82              checkTryResources(getMainAst().getFirstChild());
83          }
84      }
85  
86      /**
87       * Method to check the indentation of left paren or right paren.
88       * This method itself checks whether either of these are on start of line. This method
89       * takes care of line wrapping strict condition as well.
90       *
91       * @param parenAst      lparen or rparen ast to check
92       * @param subType       name to be used in log message
93       */
94      private void checkTryResParen(final DetailAST parenAst,
95                                      final String subType) {
96          if (isOnStartOfLine(parenAst)) {
97              final IndentLevel expectedIdent = new IndentLevel(getIndent(), 0,
98                  getIndentCheck().getLineWrappingIndentation());
99  
100             checkChildIndentation(parenAst, subType, expectedIdent);
101         }
102     }
103 
104     /**
105      * Method to check indentation of try resources children.
106      * It takes into account forceStrictCondition value when logging errors.
107      * Example of usage would include checking for try parenthesis and try resources.
108      *
109      * @param ast           AST to check.
110      * @param subType       String representing child type.
111      * @param expectedIdent Expected indent level.
112      */
113     private void checkChildIndentation(DetailAST ast, String subType, IndentLevel expectedIdent) {
114         if (getIndentCheck().isForceStrictCondition()) {
115             if (!expectedIdent.isAcceptable(expandedTabsColumnNo(ast))) {
116                 logError(ast, subType, expandedTabsColumnNo(ast), expectedIdent);
117             }
118         }
119         else {
120             if (expandedTabsColumnNo(ast) < expectedIdent.getFirstIndentLevel()) {
121                 logError(ast, subType, expandedTabsColumnNo(ast), expectedIdent);
122             }
123         }
124     }
125 
126     /**
127      * Checks indentation of resources parameters in try resources.
128      *
129      * @param resourcesSpecAst   Resource specification ast
130      */
131     private void checkTryResources(final DetailAST resourcesSpecAst) {
132         final DetailAST resourcesAst = resourcesSpecAst.findFirstToken(TokenTypes.RESOURCES);
133         final int indentation = getIndent().getFirstIndentLevel()
134             + getIndentCheck().getLineWrappingIndentation();
135         final IndentLevel expectedResourceIndent = new IndentLevel(indentation);
136 
137         final String subType = "resource";
138 
139         DetailAST resourceAst = resourcesAst.getFirstChild();
140         while (resourceAst != null) {
141             if (resourceAst.getType() == TokenTypes.RESOURCE) {
142                 final DetailAST nextSibling;
143                 if (resourceAst.getNextSibling() == null) {
144                     nextSibling = getTryResRparen();
145                 }
146                 else {
147                     nextSibling = resourceAst.getNextSibling();
148                 }
149                 if (isOnStartOfLine(resourceAst)) {
150                     checkChildIndentation(resourceAst, subType, expectedResourceIndent);
151                     checkWrappingIndentation(
152                         resourceAst,
153                         nextSibling,
154                         getIndentCheck().getLineWrappingIndentation(),
155                         expectedResourceIndent.getFirstIndentLevel(),
156                         true);
157                 }
158                 else {
159                     checkWrappingIndentation(resourceAst, nextSibling);
160                 }
161             }
162             resourceAst = resourceAst.getNextSibling();
163         }
164     }
165 
166 }