Coverage Report - com.puppycrawl.tools.checkstyle.checks.indentation.MethodDefHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
MethodDefHandler
100%
35/35
100%
20/20
2.714
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2014  Oliver Burn
 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  
 package com.puppycrawl.tools.checkstyle.checks.indentation;
 20  
 
 21  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 22  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 23  
 
 24  
 /**
 25  
  * Handler for method definitions.
 26  
  *
 27  
  * @author jrichard
 28  
  * @author Maikel Steneker
 29  
  */
 30  
 public class MethodDefHandler 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 aIndentCheck   the indentation check
 37  
      * @param aAst           the abstract syntax tree
 38  
      * @param aParent        the parent handler
 39  
      */
 40  
     public MethodDefHandler(IndentationCheck aIndentCheck,
 41  
         DetailAST aAst, ExpressionHandler aParent)
 42  
     {
 43  136
         super(aIndentCheck, (aAst.getType() == TokenTypes.CTOR_DEF)
 44  
             ? "ctor def" : "method def", aAst, aParent);
 45  136
     }
 46  
 
 47  
     @Override
 48  
     protected DetailAST getToplevelAST()
 49  
     {
 50  
         // we check this stuff ourselves below
 51  131
         return null;
 52  
     }
 53  
 
 54  
     /**
 55  
      * Check the indentation of the method name.
 56  
      */
 57  
     private void checkIdent()
 58  
     {
 59  136
         final DetailAST ident = getMainAst().findFirstToken(TokenTypes.IDENT);
 60  136
         final int columnNo = expandedTabsColumnNo(ident);
 61  136
         if (startsLine(ident) && !getLevel().accept(columnNo)) {
 62  1
             logError(ident, "", columnNo);
 63  
         }
 64  136
     }
 65  
 
 66  
     /**
 67  
      * Check the indentation of the throws clause.
 68  
      */
 69  
     private void checkThrows()
 70  
     {
 71  136
         final DetailAST throwsAst =
 72  
             getMainAst().findFirstToken(TokenTypes.LITERAL_THROWS);
 73  136
         if (throwsAst == null) {
 74  131
             return;
 75  
         }
 76  
 
 77  5
         final int columnNo = expandedTabsColumnNo(throwsAst);
 78  5
         final IndentLevel expectedColumnNo =
 79  
             new IndentLevel(getLevel(), getIndentCheck().getThrowsIndent());
 80  
 
 81  5
         if (startsLine(throwsAst)
 82  
             && !expectedColumnNo.accept(columnNo))
 83  
         {
 84  2
             logError(throwsAst, "throws", columnNo, expectedColumnNo);
 85  
         }
 86  5
     }
 87  
 
 88  
     /**
 89  
      * Check the indentation of the method type.
 90  
      */
 91  
     private void checkType()
 92  
     {
 93  100
         final DetailAST type = getMainAst().findFirstToken(TokenTypes.TYPE);
 94  100
         final DetailAST ident = ExpressionHandler.getFirstToken(type);
 95  100
         final int columnNo = expandedTabsColumnNo(ident);
 96  100
         if (startsLine(ident) && !getLevel().accept(columnNo)) {
 97  3
             logError(ident, "return type", columnNo);
 98  
         }
 99  100
     }
 100  
 
 101  
     /**
 102  
      * Check the indentation of the method parameters.
 103  
      */
 104  
     private void checkParameters()
 105  
     {
 106  136
         final DetailAST params =
 107  
             getMainAst().findFirstToken(TokenTypes.PARAMETERS);
 108  136
         checkExpressionSubtree(params, getLevel(), false, false);
 109  136
     }
 110  
 
 111  
     @Override
 112  
     public void checkIndentation()
 113  
     {
 114  136
         checkModifiers();
 115  136
         checkIdent();
 116  136
         checkThrows();
 117  136
         if (getMainAst().getType() != TokenTypes.CTOR_DEF) {
 118  100
             checkType();
 119  
         }
 120  136
         checkParameters();
 121  
 
 122  136
         if (getLCurly() == null) {
 123  
             // asbtract method def -- no body
 124  5
             return;
 125  
         }
 126  131
         super.checkIndentation();
 127  131
     }
 128  
 }