Coverage Report - com.puppycrawl.tools.checkstyle.checks.indentation.MemberDefHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
MemberDefHandler
100%
29/29
100%
18/18
2.143
 
 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 member definitions.
 27  
  *
 28  
  * @author o_sukhodolsky
 29  
  */
 30  
 public class MemberDefHandler extends AbstractExpressionHandler {
 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 MemberDefHandler(IndentationCheck indentCheck,
 40  
         DetailAST ast, AbstractExpressionHandler parent) {
 41  325
         super(indentCheck, "member def", ast, parent);
 42  325
     }
 43  
 
 44  
     @Override
 45  
     public void checkIndentation() {
 46  325
         final DetailAST modifiersNode = getMainAst().findFirstToken(TokenTypes.MODIFIERS);
 47  325
         if (modifiersNode.getChildCount() == 0) {
 48  259
             checkType();
 49  
         }
 50  
         else {
 51  66
             checkModifiers();
 52  
         }
 53  325
         final DetailAST firstNode = getMainAst();
 54  325
         final DetailAST lastNode = getVarDefStatementSemicolon(firstNode);
 55  
 
 56  325
         if (lastNode != null && !isArrayDeclaration(firstNode)) {
 57  240
             checkWrappingIndentation(firstNode, lastNode);
 58  
         }
 59  325
     }
 60  
 
 61  
     @Override
 62  
     public IndentLevel getSuggestedChildIndent(AbstractExpressionHandler child) {
 63  13
         return getIndent();
 64  
     }
 65  
 
 66  
     @Override
 67  
     protected void checkModifiers() {
 68  66
         final DetailAST modifier = getMainAst().findFirstToken(TokenTypes.MODIFIERS);
 69  66
         if (isOnStartOfLine(modifier)
 70  62
             && !getIndent().isAcceptable(expandedTabsColumnNo(modifier))) {
 71  2
             logError(modifier, "modifier", expandedTabsColumnNo(modifier));
 72  
         }
 73  66
     }
 74  
 
 75  
     /**
 76  
      * Check the indentation of the method type.
 77  
      */
 78  
     private void checkType() {
 79  259
         final DetailAST type = getMainAst().findFirstToken(TokenTypes.TYPE);
 80  259
         final DetailAST ident = AbstractExpressionHandler.getFirstToken(type);
 81  259
         final int columnNo = expandedTabsColumnNo(ident);
 82  259
         if (isOnStartOfLine(ident) && !getIndent().isAcceptable(columnNo)) {
 83  21
             logError(ident, "type", columnNo);
 84  
         }
 85  259
     }
 86  
 
 87  
     /**
 88  
      * Checks if variable_def node is array declaration.
 89  
      * @param variableDef current variable_def.
 90  
      * @return true if variable_def node is array declaration.
 91  
      */
 92  
     private static boolean isArrayDeclaration(DetailAST variableDef) {
 93  606
         return variableDef.findFirstToken(TokenTypes.TYPE)
 94  303
             .findFirstToken(TokenTypes.ARRAY_DECLARATOR) != null;
 95  
     }
 96  
 
 97  
     /**
 98  
      * Returns semicolon for variable definition statement.
 99  
      * @param variableDef
 100  
      *          ast node of type TokenTypes.VARIABLE_DEF
 101  
      * @return ast node of type TokenTypes.SEMI
 102  
      */
 103  
     private static DetailAST getVarDefStatementSemicolon(DetailAST variableDef) {
 104  325
         DetailAST lastNode = variableDef.getLastChild();
 105  325
         if (lastNode.getType() != TokenTypes.SEMI) {
 106  199
             lastNode = variableDef.getNextSibling();
 107  
         }
 108  325
         return lastNode;
 109  
     }
 110  
 }