Coverage Report - com.puppycrawl.tools.checkstyle.checks.indentation.TryHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
TryHandler
100%
44/44
100%
22/22
2.375
 
 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  72
         super(indentCheck, "try", ast, parent);
 42  72
     }
 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  49
         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  98
         return getMainAst().getFirstChild().getLastChild();
 60  
     }
 61  
 
 62  
     @Override
 63  
     public IndentLevel getSuggestedChildIndent(AbstractExpressionHandler child) {
 64  
         final IndentLevel result;
 65  103
         if (child instanceof CatchHandler
 66  
             || child instanceof FinallyHandler) {
 67  56
             result = getIndent();
 68  
         }
 69  
         else {
 70  47
             result = super.getSuggestedChildIndent(child);
 71  
         }
 72  103
         return result;
 73  
     }
 74  
 
 75  
     @Override
 76  
     public void checkIndentation() {
 77  72
         super.checkIndentation();
 78  72
         if (getMainAst().getFirstChild().getType() == TokenTypes.RESOURCE_SPECIFICATION) {
 79  49
             checkTryResParen(getTryResLparen(), "lparen");
 80  49
             checkTryResParen(getTryResRparen(), "rparen");
 81  49
             checkTryResources(getMainAst().getFirstChild());
 82  
         }
 83  72
     }
 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  98
         if (isOnStartOfLine(parenAst)) {
 96  22
             final IndentLevel expectedIdent = new IndentLevel(getIndent(), 0,
 97  22
                 getIndentCheck().getLineWrappingIndentation());
 98  
 
 99  22
             checkChildIndentation(parenAst, subType, expectedIdent);
 100  
         }
 101  98
     }
 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  68
         if (getIndentCheck().isForceStrictCondition()) {
 114  33
             if (!expectedIdent.isAcceptable(expandedTabsColumnNo(ast))) {
 115  6
                 logError(ast, subType, expandedTabsColumnNo(ast), expectedIdent);
 116  
             }
 117  
         }
 118  
         else {
 119  35
             if (expandedTabsColumnNo(ast) < expectedIdent.getFirstIndentLevel()) {
 120  4
                 logError(ast, subType, expandedTabsColumnNo(ast), expectedIdent);
 121  
             }
 122  
         }
 123  68
     }
 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  49
         final DetailAST resourcesAst = resourcesSpecAst.findFirstToken(TokenTypes.RESOURCES);
 132  49
         final int indentation = getIndent().getFirstIndentLevel()
 133  49
             + getIndentCheck().getLineWrappingIndentation();
 134  49
         final IndentLevel expectedResourceIndent = new IndentLevel(indentation);
 135  
 
 136  49
         final String subType = "resource";
 137  
 
 138  49
         DetailAST resourceAst = resourcesAst.getFirstChild();
 139  138
         while (resourceAst != null) {
 140  89
             if (resourceAst.getType() == TokenTypes.RESOURCE) {
 141  
                 final DetailAST nextSibling;
 142  69
                 if (resourceAst.getNextSibling() == null) {
 143  49
                     nextSibling = getTryResRparen();
 144  
                 }
 145  
                 else {
 146  20
                     nextSibling = resourceAst.getNextSibling();
 147  
                 }
 148  69
                 if (isOnStartOfLine(resourceAst)) {
 149  46
                     checkChildIndentation(resourceAst, subType, expectedResourceIndent);
 150  92
                     checkWrappingIndentation(
 151  
                         resourceAst,
 152  
                         nextSibling,
 153  46
                         getIndentCheck().getLineWrappingIndentation(),
 154  46
                         expectedResourceIndent.getFirstIndentLevel(),
 155  
                         true);
 156  
                 }
 157  
                 else {
 158  23
                     checkWrappingIndentation(resourceAst, nextSibling);
 159  
                 }
 160  
             }
 161  89
             resourceAst = resourceAst.getNextSibling();
 162  
         }
 163  49
     }
 164  
 }