001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.sizes; 021 022import com.puppycrawl.tools.checkstyle.FileStatefulCheck; 023import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 024import com.puppycrawl.tools.checkstyle.api.DetailAST; 025import com.puppycrawl.tools.checkstyle.api.TokenTypes; 026 027/** 028 * <p> 029 * Checks for the number of types declared at the <i>outer</i> (or <i>root</i>) level in a file. 030 * </p> 031 * <p> 032 * Rationale: It is considered good practice to only define one outer type per file. 033 * </p> 034 * <ul> 035 * <li> 036 * Property {@code max} - Specify the maximum number of outer types allowed. 037 * Default value is {@code 1}. 038 * </li> 039 * </ul> 040 * <p> 041 * To configure the check to accept 1 outer type per file: 042 * </p> 043 * <pre> 044 * <module name="OuterTypeNumber"/> 045 * </pre> 046 * <p> 047 * To configure the check to accept 2 outer types per file: 048 * </p> 049 * <pre> 050 * <module name="OuterTypeNumber"> 051 * <property name="max" value="2"/> 052 * </module> 053 * </pre> 054 * 055 * @since 5.0 056 */ 057@FileStatefulCheck 058public class OuterTypeNumberCheck extends AbstractCheck { 059 060 /** 061 * A key is pointing to the warning message text in "messages.properties" 062 * file. 063 */ 064 public static final String MSG_KEY = "maxOuterTypes"; 065 066 /** Specify the maximum number of outer types allowed. */ 067 private int max = 1; 068 /** Tracks the current depth in types. */ 069 private int currentDepth; 070 /** Tracks the number of outer types found. */ 071 private int outerNum; 072 073 @Override 074 public int[] getDefaultTokens() { 075 return getRequiredTokens(); 076 } 077 078 @Override 079 public int[] getAcceptableTokens() { 080 return getRequiredTokens(); 081 } 082 083 @Override 084 public int[] getRequiredTokens() { 085 return new int[] {TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF, 086 TokenTypes.ENUM_DEF, TokenTypes.ANNOTATION_DEF, }; 087 } 088 089 @Override 090 public void beginTree(DetailAST ast) { 091 currentDepth = 0; 092 outerNum = 0; 093 } 094 095 @Override 096 public void finishTree(DetailAST ast) { 097 if (max < outerNum) { 098 log(ast, MSG_KEY, outerNum, max); 099 } 100 } 101 102 @Override 103 public void visitToken(DetailAST ast) { 104 if (currentDepth == 0) { 105 outerNum++; 106 } 107 currentDepth++; 108 } 109 110 @Override 111 public void leaveToken(DetailAST ast) { 112 currentDepth--; 113 } 114 115 /** 116 * Setter to specify the maximum number of outer types allowed. 117 * 118 * @param max the new number. 119 */ 120 public void setMax(int max) { 121 this.max = max; 122 } 123 124}