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 * Type is {@code int}. 038 * Default value is {@code 1}. 039 * </li> 040 * </ul> 041 * <p> 042 * To configure the check to accept 1 outer type per file: 043 * </p> 044 * <pre> 045 * <module name="OuterTypeNumber"/> 046 * </pre> 047 * <p> 048 * To configure the check to accept 2 outer types per file: 049 * </p> 050 * <pre> 051 * <module name="OuterTypeNumber"> 052 * <property name="max" value="2"/> 053 * </module> 054 * </pre> 055 * <p> 056 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 057 * </p> 058 * <p> 059 * Violation Message Keys: 060 * </p> 061 * <ul> 062 * <li> 063 * {@code maxOuterTypes} 064 * </li> 065 * </ul> 066 * 067 * @since 5.0 068 */ 069@FileStatefulCheck 070public class OuterTypeNumberCheck extends AbstractCheck { 071 072 /** 073 * A key is pointing to the warning message text in "messages.properties" 074 * file. 075 */ 076 public static final String MSG_KEY = "maxOuterTypes"; 077 078 /** Specify the maximum number of outer types allowed. */ 079 private int max = 1; 080 /** Tracks the current depth in types. */ 081 private int currentDepth; 082 /** Tracks the number of outer types found. */ 083 private int outerNum; 084 085 @Override 086 public int[] getDefaultTokens() { 087 return getRequiredTokens(); 088 } 089 090 @Override 091 public int[] getAcceptableTokens() { 092 return getRequiredTokens(); 093 } 094 095 @Override 096 public int[] getRequiredTokens() { 097 return new int[] { 098 TokenTypes.CLASS_DEF, 099 TokenTypes.INTERFACE_DEF, 100 TokenTypes.ENUM_DEF, 101 TokenTypes.ANNOTATION_DEF, 102 TokenTypes.RECORD_DEF, 103 }; 104 } 105 106 @Override 107 public void beginTree(DetailAST ast) { 108 currentDepth = 0; 109 outerNum = 0; 110 } 111 112 @Override 113 public void finishTree(DetailAST ast) { 114 if (max < outerNum) { 115 log(ast, MSG_KEY, outerNum, max); 116 } 117 } 118 119 @Override 120 public void visitToken(DetailAST ast) { 121 if (currentDepth == 0) { 122 outerNum++; 123 } 124 currentDepth++; 125 } 126 127 @Override 128 public void leaveToken(DetailAST ast) { 129 currentDepth--; 130 } 131 132 /** 133 * Setter to specify the maximum number of outer types allowed. 134 * 135 * @param max the new number. 136 */ 137 public void setMax(int max) { 138 this.max = max; 139 } 140 141}