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