Coverage Report - com.puppycrawl.tools.checkstyle.checks.indentation.ClassDefHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassDefHandler
100%
23/23
85%
12/14
2
 
 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 class definitions.
 26  
  *
 27  
  * @author jrichard
 28  
  */
 29  
 public class ClassDefHandler extends BlockParentHandler
 30  
 {
 31  
     /**
 32  
      * Construct an instance of this handler with the given indentation check,
 33  
      * abstract syntax tree, and parent handler.
 34  
      *
 35  
      * @param aIndentCheck   the indentation check
 36  
      * @param aAst           the abstract syntax tree
 37  
      * @param aParent        the parent handler
 38  
      */
 39  
     public ClassDefHandler(IndentationCheck aIndentCheck,
 40  
                            DetailAST aAst,
 41  
                            ExpressionHandler aParent)
 42  
     {
 43  82
         super(aIndentCheck,
 44  
               (aAst.getType() == TokenTypes.CLASS_DEF)
 45  
               ? "class def" : ((aAst.getType() == TokenTypes.ENUM_DEF)
 46  
                                ? "enum def" : "interface def"),
 47  
               aAst, aParent);
 48  82
     }
 49  
 
 50  
     @Override
 51  
     protected DetailAST getLCurly()
 52  
     {
 53  410
         return getMainAst().findFirstToken(TokenTypes.OBJBLOCK)
 54  
             .findFirstToken(TokenTypes.LCURLY);
 55  
     }
 56  
 
 57  
     @Override
 58  
     protected DetailAST getRCurly()
 59  
     {
 60  328
         return getMainAst().findFirstToken(TokenTypes.OBJBLOCK)
 61  
             .findFirstToken(TokenTypes.RCURLY);
 62  
     }
 63  
 
 64  
     @Override
 65  
     protected DetailAST getToplevelAST()
 66  
     {
 67  82
         return null;
 68  
         // note: ident checked by hand in check indentation;
 69  
     }
 70  
 
 71  
     @Override
 72  
     protected DetailAST getListChild()
 73  
     {
 74  82
         return getMainAst().findFirstToken(TokenTypes.OBJBLOCK);
 75  
     }
 76  
 
 77  
     @Override
 78  
     public void checkIndentation()
 79  
     {
 80  
         // TODO: still need to better deal with the modifiers and "class"
 81  82
         checkModifiers();
 82  
 
 83  82
         final LineSet lines = new LineSet();
 84  
 
 85  
         // checks that line with class name starts at correct indentation,
 86  
         //  and following lines (in implements and extends clauses) are
 87  
         //  indented at least one level
 88  82
         final DetailAST ident = getMainAst().findFirstToken(TokenTypes.IDENT);
 89  82
         final int lineStart = getLineStart(ident);
 90  82
         if (!getLevel().accept(lineStart)) {
 91  9
             logError(ident, "ident", lineStart);
 92  
         }
 93  
 
 94  82
         lines.addLineAndCol(ident.getLineNo(), lineStart);
 95  
 
 96  82
         final DetailAST impl = getMainAst().findFirstToken(
 97  
             TokenTypes.IMPLEMENTS_CLAUSE);
 98  82
         if ((impl != null) && (impl.getFirstChild() != null)) {
 99  18
             findSubtreeLines(lines, impl, false);
 100  
         }
 101  
 
 102  82
         final DetailAST ext =
 103  
             getMainAst().findFirstToken(TokenTypes.EXTENDS_CLAUSE);
 104  82
         if ((ext != null) && (ext.getFirstChild() != null)) {
 105  20
             findSubtreeLines(lines, ext, false);
 106  
         }
 107  
 
 108  82
         checkLinesIndent(ident.getLineNo(), lines.lastLine(), getLevel());
 109  
 
 110  82
         super.checkIndentation();
 111  82
     }
 112  
 
 113  
     @Override
 114  
     protected int[] getCheckedChildren()
 115  
     {
 116  76
         return new int[] {
 117  
             TokenTypes.EXPR,
 118  
             TokenTypes.OBJBLOCK,
 119  
             TokenTypes.LITERAL_BREAK,
 120  
             TokenTypes.LITERAL_RETURN,
 121  
             TokenTypes.LITERAL_THROW,
 122  
             TokenTypes.LITERAL_CONTINUE,
 123  
         };
 124  
     }
 125  
 
 126  
 }