Coverage Report - com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
OuterTypeNumberCheck
100%
19/19
100%
4/4
1.25
 
 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.sizes;
 21  
 
 22  
 import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
 23  
 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
 24  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 25  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 26  
 
 27  
 /**
 28  
  * Checks for the number of defined types at the "outer" level.
 29  
  * @author oliverb
 30  
  */
 31  
 @FileStatefulCheck
 32  13
 public class OuterTypeNumberCheck extends AbstractCheck {
 33  
 
 34  
     /**
 35  
      * A key is pointing to the warning message text in "messages.properties"
 36  
      * file.
 37  
      */
 38  
     public static final String MSG_KEY = "maxOuterTypes";
 39  
 
 40  
     /** The maximum allowed number of outer types. */
 41  13
     private int max = 1;
 42  
     /** Tracks the current depth in types. */
 43  
     private int currentDepth;
 44  
     /** Tracks the number of outer types found. */
 45  
     private int outerNum;
 46  
 
 47  
     @Override
 48  
     public int[] getDefaultTokens() {
 49  16
         return getRequiredTokens();
 50  
     }
 51  
 
 52  
     @Override
 53  
     public int[] getAcceptableTokens() {
 54  5
         return getRequiredTokens();
 55  
     }
 56  
 
 57  
     @Override
 58  
     public int[] getRequiredTokens() {
 59  38
         return new int[] {TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF,
 60  
             TokenTypes.ENUM_DEF, TokenTypes.ANNOTATION_DEF, };
 61  
     }
 62  
 
 63  
     @Override
 64  
     public void beginTree(DetailAST ast) {
 65  4
         currentDepth = 0;
 66  4
         outerNum = 0;
 67  4
     }
 68  
 
 69  
     @Override
 70  
     public void finishTree(DetailAST ast) {
 71  4
         if (max < outerNum) {
 72  1
             log(ast, MSG_KEY, outerNum, max);
 73  
         }
 74  4
     }
 75  
 
 76  
     @Override
 77  
     public void visitToken(DetailAST ast) {
 78  8
         if (currentDepth == 0) {
 79  7
             outerNum++;
 80  
         }
 81  8
         currentDepth++;
 82  8
     }
 83  
 
 84  
     @Override
 85  
     public void leaveToken(DetailAST ast) {
 86  8
         currentDepth--;
 87  8
     }
 88  
 
 89  
     /**
 90  
      * Sets the maximum allowed number of outer types.
 91  
      * @param max the new number.
 92  
      */
 93  
     public void setMax(int max) {
 94  3
         this.max = max;
 95  3
     }
 96  
 }