001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2017 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.design; 021 022import java.util.Map; 023import java.util.SortedMap; 024import java.util.TreeMap; 025 026import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 027import com.puppycrawl.tools.checkstyle.api.DetailAST; 028import com.puppycrawl.tools.checkstyle.api.TokenTypes; 029import com.puppycrawl.tools.checkstyle.utils.CommonUtils; 030 031/** 032 * Checks that each top-level class, interface 033 * or enum resides in a source file of its own. 034 * <p> 035 * Official description of a 'top-level' term:<a 036 * href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-7.html#jls-7.6"> 037 * 7.6. Top Level Type Declarations</a>. If file doesn't contains 038 * public class, enum or interface, top-level type is the first type in file. 039 * </p> 040 * <p> 041 * An example of code with violations: 042 * </p> 043 * <pre>{@code 044 * public class Foo{ 045 * //methods 046 * } 047 * 048 * class Foo2{ 049 * //methods 050 * } 051 * }</pre> 052 * <p> 053 * An example of code without top-level public type: 054 * </p> 055 * <pre>{@code 056 * class Foo{ //top-level class 057 * //methods 058 * } 059 * 060 * class Foo2{ 061 * //methods 062 * } 063 * }</pre> 064 * <p> 065 * An example of check's configuration: 066 * </p> 067 * <pre> 068 * <module name="OneTopLevelClass"/> 069 * </pre> 070 * 071 * <p> 072 * An example of code without violations: 073 * </p> 074 * <pre>{@code 075 * public class Foo{ 076 * //methods 077 * } 078 * }</pre> 079 * 080 * <p> ATTENTION: This Check does not support customization of validated tokens, 081 * so do not use the "tokens" property. 082 * </p> 083 * 084 * @author maxvetrenko 085 */ 086public class OneTopLevelClassCheck extends AbstractCheck { 087 088 /** 089 * A key is pointing to the warning message text in "messages.properties" 090 * file. 091 */ 092 public static final String MSG_KEY = "one.top.level.class"; 093 094 /** 095 * True if a java source file contains a type 096 * with a public access level modifier. 097 */ 098 private boolean publicTypeFound; 099 100 /** Mapping between type names and line numbers of the type declarations.*/ 101 private final SortedMap<Integer, String> lineNumberTypeMap = new TreeMap<>(); 102 103 @Override 104 public int[] getDefaultTokens() { 105 return getAcceptableTokens(); 106 } 107 108 // ZERO tokens as Check do Traverse of Tree himself, he does not need to subscribed to Tokens 109 @Override 110 public int[] getAcceptableTokens() { 111 return CommonUtils.EMPTY_INT_ARRAY; 112 } 113 114 @Override 115 public int[] getRequiredTokens() { 116 return getAcceptableTokens(); 117 } 118 119 @Override 120 public void beginTree(DetailAST rootAST) { 121 publicTypeFound = false; 122 lineNumberTypeMap.clear(); 123 124 DetailAST currentNode = rootAST; 125 while (currentNode != null) { 126 if (currentNode.getType() == TokenTypes.CLASS_DEF 127 || currentNode.getType() == TokenTypes.ENUM_DEF 128 || currentNode.getType() == TokenTypes.INTERFACE_DEF) { 129 if (isPublic(currentNode)) { 130 publicTypeFound = true; 131 } 132 else { 133 final String typeName = currentNode 134 .findFirstToken(TokenTypes.IDENT).getText(); 135 lineNumberTypeMap.put(currentNode.getLineNo(), typeName); 136 } 137 } 138 currentNode = currentNode.getNextSibling(); 139 } 140 } 141 142 @Override 143 public void finishTree(DetailAST rootAST) { 144 if (!lineNumberTypeMap.isEmpty()) { 145 if (!publicTypeFound) { 146 // skip first top-level type. 147 lineNumberTypeMap.remove(lineNumberTypeMap.firstKey()); 148 } 149 150 for (Map.Entry<Integer, String> entry 151 : lineNumberTypeMap.entrySet()) { 152 log(entry.getKey(), MSG_KEY, entry.getValue()); 153 } 154 } 155 } 156 157 /** 158 * Checks if a type is public. 159 * @param typeDef type definition node. 160 * @return true if a type has a public access level modifier. 161 */ 162 private static boolean isPublic(DetailAST typeDef) { 163 final DetailAST modifiers = 164 typeDef.findFirstToken(TokenTypes.MODIFIERS); 165 return modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null; 166 } 167}