001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2018 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.ArrayDeque; 023import java.util.Deque; 024import java.util.LinkedList; 025import java.util.List; 026 027import com.puppycrawl.tools.checkstyle.FileStatefulCheck; 028import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 029import com.puppycrawl.tools.checkstyle.api.DetailAST; 030import com.puppycrawl.tools.checkstyle.api.TokenTypes; 031import com.puppycrawl.tools.checkstyle.utils.ScopeUtils; 032 033/** 034 * <p> 035 * Checks that class which has only private ctors 036 * is declared as final. Doesn't check for classes nested in interfaces 037 * or annotations, as they are always {@code final} there. 038 * </p> 039 * <p> 040 * An example of how to configure the check is: 041 * </p> 042 * <pre> 043 * <module name="FinalClass"/> 044 * </pre> 045 * @author o_sukhodolsky 046 */ 047@FileStatefulCheck 048public class FinalClassCheck 049 extends AbstractCheck { 050 051 /** 052 * A key is pointing to the warning message text in "messages.properties" 053 * file. 054 */ 055 public static final String MSG_KEY = "final.class"; 056 057 /** 058 * Character separate package names in qualified name of java class. 059 */ 060 private static final String PACKAGE_SEPARATOR = "."; 061 062 /** Keeps ClassDesc objects for stack of declared classes. */ 063 private Deque<ClassDesc> classes; 064 065 /** Full qualified name of the package. */ 066 private String packageName; 067 068 @Override 069 public int[] getDefaultTokens() { 070 return getRequiredTokens(); 071 } 072 073 @Override 074 public int[] getAcceptableTokens() { 075 return getRequiredTokens(); 076 } 077 078 @Override 079 public int[] getRequiredTokens() { 080 return new int[] {TokenTypes.CLASS_DEF, TokenTypes.CTOR_DEF, TokenTypes.PACKAGE_DEF}; 081 } 082 083 @Override 084 public void beginTree(DetailAST rootAST) { 085 classes = new ArrayDeque<>(); 086 packageName = ""; 087 } 088 089 @Override 090 public void visitToken(DetailAST ast) { 091 final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS); 092 093 switch (ast.getType()) { 094 case TokenTypes.PACKAGE_DEF: 095 packageName = extractQualifiedName(ast); 096 break; 097 098 case TokenTypes.CLASS_DEF: 099 registerNestedSubclassToOuterSuperClasses(ast); 100 101 final boolean isFinal = modifiers.findFirstToken(TokenTypes.FINAL) != null; 102 final boolean isAbstract = modifiers.findFirstToken(TokenTypes.ABSTRACT) != null; 103 104 final String qualifiedClassName = getQualifiedClassName(ast); 105 classes.push(new ClassDesc(qualifiedClassName, isFinal, isAbstract)); 106 break; 107 108 case TokenTypes.CTOR_DEF: 109 if (!ScopeUtils.isInEnumBlock(ast)) { 110 final ClassDesc desc = classes.peek(); 111 if (modifiers.findFirstToken(TokenTypes.LITERAL_PRIVATE) == null) { 112 desc.registerNonPrivateCtor(); 113 } 114 else { 115 desc.registerPrivateCtor(); 116 } 117 } 118 break; 119 120 default: 121 throw new IllegalStateException(ast.toString()); 122 } 123 } 124 125 @Override 126 public void leaveToken(DetailAST ast) { 127 if (ast.getType() == TokenTypes.CLASS_DEF) { 128 final ClassDesc desc = classes.pop(); 129 if (desc.isWithPrivateCtor() 130 && !desc.isDeclaredAsAbstract() 131 && !desc.isDeclaredAsFinal() 132 && !desc.isWithNonPrivateCtor() 133 && !desc.isWithNestedSubclass() 134 && !ScopeUtils.isInInterfaceOrAnnotationBlock(ast)) { 135 final String qualifiedName = desc.getQualifiedName(); 136 final String className = getClassNameFromQualifiedName(qualifiedName); 137 log(ast.getLineNo(), MSG_KEY, className); 138 } 139 } 140 } 141 142 /** 143 * Get name of class(with qualified package if specified) in extend clause. 144 * @param classExtend extend clause to extract class name 145 * @return super class name 146 */ 147 private static String extractQualifiedName(DetailAST classExtend) { 148 final String className; 149 150 if (classExtend.findFirstToken(TokenTypes.IDENT) == null) { 151 // Name specified with packages, have to traverse DOT 152 final DetailAST firstChild = classExtend.findFirstToken(TokenTypes.DOT); 153 final List<String> qualifiedNameParts = new LinkedList<>(); 154 155 qualifiedNameParts.add(0, firstChild.findFirstToken(TokenTypes.IDENT).getText()); 156 DetailAST traverse = firstChild.findFirstToken(TokenTypes.DOT); 157 while (traverse != null) { 158 qualifiedNameParts.add(0, traverse.findFirstToken(TokenTypes.IDENT).getText()); 159 traverse = traverse.findFirstToken(TokenTypes.DOT); 160 } 161 className = String.join(PACKAGE_SEPARATOR, qualifiedNameParts); 162 } 163 else { 164 className = classExtend.findFirstToken(TokenTypes.IDENT).getText(); 165 } 166 167 return className; 168 } 169 170 /** 171 * Register to outer super classes of given classAst that 172 * given classAst is extending them. 173 * @param classAst class which outer super classes will be 174 * informed about nesting subclass 175 */ 176 private void registerNestedSubclassToOuterSuperClasses(DetailAST classAst) { 177 final String currentAstSuperClassName = getSuperClassName(classAst); 178 if (currentAstSuperClassName != null) { 179 for (ClassDesc classDesc : classes) { 180 final String classDescQualifiedName = classDesc.getQualifiedName(); 181 if (doesNameInExtendMatchSuperClassName(classDescQualifiedName, 182 currentAstSuperClassName)) { 183 classDesc.registerNestedSubclass(); 184 } 185 } 186 } 187 } 188 189 /** 190 * Get qualified class name from given class Ast. 191 * @param classAst class to get qualified class name 192 * @return qualified class name of a class 193 */ 194 private String getQualifiedClassName(DetailAST classAst) { 195 final String className = classAst.findFirstToken(TokenTypes.IDENT).getText(); 196 String outerClassQualifiedName = null; 197 if (!classes.isEmpty()) { 198 outerClassQualifiedName = classes.peek().getQualifiedName(); 199 } 200 return getQualifiedClassName(packageName, outerClassQualifiedName, className); 201 } 202 203 /** 204 * Calculate qualified class name(package + class name) laying inside given 205 * outer class. 206 * @param packageName package name, empty string on default package 207 * @param outerClassQualifiedName qualified name(package + class) of outer class, 208 * null if doesn't exist 209 * @param className class name 210 * @return qualified class name(package + class name) 211 */ 212 private static String getQualifiedClassName(String packageName, String outerClassQualifiedName, 213 String className) { 214 final String qualifiedClassName; 215 216 if (outerClassQualifiedName == null) { 217 if (packageName.isEmpty()) { 218 qualifiedClassName = className; 219 } 220 else { 221 qualifiedClassName = packageName + PACKAGE_SEPARATOR + className; 222 } 223 } 224 else { 225 qualifiedClassName = outerClassQualifiedName + PACKAGE_SEPARATOR + className; 226 } 227 return qualifiedClassName; 228 } 229 230 /** 231 * Get super class name of given class. 232 * @param classAst class 233 * @return super class name or null if super class is not specified 234 */ 235 private static String getSuperClassName(DetailAST classAst) { 236 String superClassName = null; 237 final DetailAST classExtend = classAst.findFirstToken(TokenTypes.EXTENDS_CLAUSE); 238 if (classExtend != null) { 239 superClassName = extractQualifiedName(classExtend); 240 } 241 return superClassName; 242 } 243 244 /** 245 * Checks if given super class name in extend clause match super class qualified name. 246 * @param superClassQualifiedName super class qualified name (with package) 247 * @param superClassInExtendClause name in extend clause 248 * @return true if given super class name in extend clause match super class qualified name, 249 * false otherwise 250 */ 251 private static boolean doesNameInExtendMatchSuperClassName(String superClassQualifiedName, 252 String superClassInExtendClause) { 253 String superClassNormalizedName = superClassQualifiedName; 254 if (!superClassInExtendClause.contains(PACKAGE_SEPARATOR)) { 255 superClassNormalizedName = getClassNameFromQualifiedName(superClassQualifiedName); 256 } 257 return superClassNormalizedName.equals(superClassInExtendClause); 258 } 259 260 /** 261 * Get class name from qualified name. 262 * @param qualifiedName qualified class name 263 * @return class name 264 */ 265 private static String getClassNameFromQualifiedName(String qualifiedName) { 266 return qualifiedName.substring(qualifiedName.lastIndexOf(PACKAGE_SEPARATOR) + 1); 267 } 268 269 /** Maintains information about class' ctors. */ 270 private static final class ClassDesc { 271 272 /** Qualified class name(with package). */ 273 private final String qualifiedName; 274 275 /** Is class declared as final. */ 276 private final boolean declaredAsFinal; 277 278 /** Is class declared as abstract. */ 279 private final boolean declaredAsAbstract; 280 281 /** Does class have non-private ctors. */ 282 private boolean withNonPrivateCtor; 283 284 /** Does class have private ctors. */ 285 private boolean withPrivateCtor; 286 287 /** Does class have nested subclass. */ 288 private boolean withNestedSubclass; 289 290 /** 291 * Create a new ClassDesc instance. 292 * @param qualifiedName qualified class name(with package) 293 * @param declaredAsFinal indicates if the 294 * class declared as final 295 * @param declaredAsAbstract indicates if the 296 * class declared as abstract 297 */ 298 ClassDesc(String qualifiedName, boolean declaredAsFinal, boolean declaredAsAbstract) { 299 this.qualifiedName = qualifiedName; 300 this.declaredAsFinal = declaredAsFinal; 301 this.declaredAsAbstract = declaredAsAbstract; 302 } 303 304 /** 305 * Get qualified class name. 306 * @return qualified class name 307 */ 308 private String getQualifiedName() { 309 return qualifiedName; 310 } 311 312 /** Adds private ctor. */ 313 private void registerPrivateCtor() { 314 withPrivateCtor = true; 315 } 316 317 /** Adds non-private ctor. */ 318 private void registerNonPrivateCtor() { 319 withNonPrivateCtor = true; 320 } 321 322 /** Adds nested subclass. */ 323 private void registerNestedSubclass() { 324 withNestedSubclass = true; 325 } 326 327 /** 328 * Does class have private ctors. 329 * @return true if class has private ctors 330 */ 331 private boolean isWithPrivateCtor() { 332 return withPrivateCtor; 333 } 334 335 /** 336 * Does class have non-private ctors. 337 * @return true if class has non-private ctors 338 */ 339 private boolean isWithNonPrivateCtor() { 340 return withNonPrivateCtor; 341 } 342 343 /** 344 * Does class have nested subclass. 345 * @return true if class has nested subclass 346 */ 347 private boolean isWithNestedSubclass() { 348 return withNestedSubclass; 349 } 350 351 /** 352 * Is class declared as final. 353 * @return true if class is declared as final 354 */ 355 private boolean isDeclaredAsFinal() { 356 return declaredAsFinal; 357 } 358 359 /** 360 * Is class declared as abstract. 361 * @return true if class is declared as final 362 */ 363 private boolean isDeclaredAsAbstract() { 364 return declaredAsAbstract; 365 } 366 367 } 368 369}