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.design; 021 022import java.util.Arrays; 023import java.util.Optional; 024import java.util.Set; 025import java.util.function.Predicate; 026import java.util.stream.Collectors; 027 028import com.puppycrawl.tools.checkstyle.StatelessCheck; 029import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 030import com.puppycrawl.tools.checkstyle.api.DetailAST; 031import com.puppycrawl.tools.checkstyle.api.Scope; 032import com.puppycrawl.tools.checkstyle.api.TokenTypes; 033import com.puppycrawl.tools.checkstyle.utils.JavadocUtil; 034import com.puppycrawl.tools.checkstyle.utils.ScopeUtil; 035import com.puppycrawl.tools.checkstyle.utils.TokenUtil; 036 037/** 038 * <p> 039 * Checks that classes are designed for extension (subclass creation). 040 * </p> 041 * <p> 042 * Nothing wrong could be with founded classes. 043 * This check makes sense only for library projects (not application projects) 044 * which care of ideal OOP-design to make sure that class works in all cases even misusage. 045 * Even in library projects this check most likely will find classes that are designed for extension 046 * by somebody. User needs to use suppressions extensively to got a benefit from this check, 047 * and keep in suppressions all confirmed/known classes that are deigned for inheritance 048 * intentionally to let the check catch only new classes, and bring this to team/user attention. 049 * </p> 050 * 051 * <p> 052 * ATTENTION: Only user can decide whether a class is designed for extension or not. 053 * The check just shows all classes which are possibly designed for extension. 054 * If smth inappropriate is found please use suppression. 055 * </p> 056 * 057 * <p> 058 * ATTENTION: If the method which can be overridden in a subclass has a javadoc comment 059 * (a good practice is to explain its self-use of overridable methods) the check will not 060 * rise a violation. The violation can also be skipped if the method which can be overridden 061 * in a subclass has one or more annotations that are specified in ignoredAnnotations 062 * option. Note, that by default @Override annotation is not included in the 063 * ignoredAnnotations set as in a subclass the method which has the annotation can also be 064 * overridden in its subclass. 065 * </p> 066 * <p> 067 * Problem is described at "Effective Java, 2nd Edition by Joshua Bloch" book, chapter 068 * "Item 17: Design and document for inheritance or else prohibit it". 069 * </p> 070 * <p> 071 * Some quotes from book: 072 * </p> 073 * <blockquote>The class must document its self-use of overridable methods. 074 * By convention, a method that invokes overridable methods contains a description 075 * of these invocations at the end of its documentation comment. The description 076 * begins with the phrase “This implementation.” 077 * </blockquote> 078 * <blockquote> 079 * The best solution to this problem is to prohibit subclassing in classes that 080 * are not designed and documented to be safely subclassed. 081 * </blockquote> 082 * <blockquote> 083 * If a concrete class does not implement a standard interface, then you may 084 * inconvenience some programmers by prohibiting inheritance. If you feel that you 085 * must allow inheritance from such a class, one reasonable approach is to ensure 086 * that the class never invokes any of its overridable methods and to document this 087 * fact. In other words, eliminate the class’s self-use of overridable methods entirely. 088 * In doing so, you’ll create a class that is reasonably safe to subclass. Overriding a 089 * method will never affect the behavior of any other method. 090 * </blockquote> 091 * <p> 092 * The check finds classes that have overridable methods (public or protected methods 093 * that are non-static, not-final, non-abstract) and have non-empty implementation. 094 * </p> 095 * <p> 096 * Rationale: This library design style protects superclasses against being broken 097 * by subclasses. The downside is that subclasses are limited in their flexibility, 098 * in particular they cannot prevent execution of code in the superclass, but that 099 * also means that subclasses cannot corrupt the state of the superclass by forgetting 100 * to call the superclass's method. 101 * </p> 102 * <p> 103 * More specifically, it enforces a programming style where superclasses provide 104 * empty "hooks" that can be implemented by subclasses. 105 * </p> 106 * <p> 107 * Example of code that cause violation as it is designed for extension: 108 * </p> 109 * <pre> 110 * public abstract class Plant { 111 * private String roots; 112 * private String trunk; 113 * 114 * protected void validate() { 115 * if (roots == null) throw new IllegalArgumentException("No roots!"); 116 * if (trunk == null) throw new IllegalArgumentException("No trunk!"); 117 * } 118 * 119 * public abstract void grow(); 120 * } 121 * 122 * public class Tree extends Plant { 123 * private List leaves; 124 * 125 * @Overrides 126 * protected void validate() { 127 * super.validate(); 128 * if (leaves == null) throw new IllegalArgumentException("No leaves!"); 129 * } 130 * 131 * public void grow() { 132 * validate(); 133 * } 134 * } 135 * </pre> 136 * <p> 137 * Example of code without violation: 138 * </p> 139 * <pre> 140 * public abstract class Plant { 141 * private String roots; 142 * private String trunk; 143 * 144 * private void validate() { 145 * if (roots == null) throw new IllegalArgumentException("No roots!"); 146 * if (trunk == null) throw new IllegalArgumentException("No trunk!"); 147 * validateEx(); 148 * } 149 * 150 * protected void validateEx() { } 151 * 152 * public abstract void grow(); 153 * } 154 * </pre> 155 * <ul> 156 * <li> 157 * Property {@code ignoredAnnotations} - Specify annotations which allow the check to 158 * skip the method from validation. 159 * Default value is {@code After, AfterClass, Before, BeforeClass, Test}. 160 * </li> 161 * </ul> 162 * <p> 163 * To configure the check: 164 * </p> 165 * <pre> 166 * <module name="DesignForExtension"/> 167 * </pre> 168 * <p> 169 * To configure the check to allow methods which have @Override and @Test annotations 170 * to be designed for extension. 171 * </p> 172 * <pre> 173 * <module name="DesignForExtension"> 174 * <property name="ignoredAnnotations" value="Override, Test"/> 175 * </module> 176 * </pre> 177 * <pre> 178 * public class A extends B { 179 * @Override 180 * public int foo() { 181 * return 2; 182 * } 183 * 184 * public int foo2() {return 8;} // violation 185 * } 186 * 187 * public class B { 188 * /** 189 * * This implementation ... 190 * @return some int value. 191 * */ 192 * public int foo() { 193 * return 1; 194 * } 195 * 196 * public int foo3() {return 3;} // violation 197 * } 198 * 199 * public class FooTest { 200 * @Test 201 * public void testFoo() { 202 * final B b = new A(); 203 * assertEquals(2, b.foo()); 204 * } 205 * 206 * public int foo4() {return 4;} // violation 207 * } 208 * </pre> 209 * 210 * @since 3.1 211 */ 212@StatelessCheck 213public class DesignForExtensionCheck extends AbstractCheck { 214 215 /** 216 * A key is pointing to the warning message text in "messages.properties" 217 * file. 218 */ 219 public static final String MSG_KEY = "design.forExtension"; 220 221 /** 222 * Specify annotations which allow the check to skip the method from validation. 223 */ 224 private Set<String> ignoredAnnotations = Arrays.stream(new String[] {"Test", "Before", "After", 225 "BeforeClass", "AfterClass", }).collect(Collectors.toSet()); 226 227 /** 228 * Setter to specify annotations which allow the check to skip the method from validation. 229 * 230 * @param ignoredAnnotations method annotations. 231 */ 232 public void setIgnoredAnnotations(String... ignoredAnnotations) { 233 this.ignoredAnnotations = Arrays.stream(ignoredAnnotations).collect(Collectors.toSet()); 234 } 235 236 @Override 237 public int[] getDefaultTokens() { 238 return getRequiredTokens(); 239 } 240 241 @Override 242 public int[] getAcceptableTokens() { 243 return getRequiredTokens(); 244 } 245 246 @Override 247 public int[] getRequiredTokens() { 248 // The check does not subscribe to CLASS_DEF token as now it is stateless. If the check 249 // subscribes to CLASS_DEF token it will become stateful, since we need to have additional 250 // stack to hold CLASS_DEF tokens. 251 return new int[] {TokenTypes.METHOD_DEF}; 252 } 253 254 @Override 255 public boolean isCommentNodesRequired() { 256 return true; 257 } 258 259 @Override 260 public void visitToken(DetailAST ast) { 261 if (!hasJavadocComment(ast) 262 && canBeOverridden(ast) 263 && (isNativeMethod(ast) 264 || !hasEmptyImplementation(ast)) 265 && !hasIgnoredAnnotation(ast, ignoredAnnotations)) { 266 final DetailAST classDef = getNearestClassOrEnumDefinition(ast); 267 if (canBeSubclassed(classDef)) { 268 final String className = classDef.findFirstToken(TokenTypes.IDENT).getText(); 269 final String methodName = ast.findFirstToken(TokenTypes.IDENT).getText(); 270 log(ast, MSG_KEY, className, methodName); 271 } 272 } 273 } 274 275 /** 276 * Checks whether a method has a javadoc comment. 277 * 278 * @param methodDef method definition token. 279 * @return true if a method has a javadoc comment. 280 */ 281 private static boolean hasJavadocComment(DetailAST methodDef) { 282 return hasJavadocCommentOnToken(methodDef, TokenTypes.MODIFIERS) 283 || hasJavadocCommentOnToken(methodDef, TokenTypes.TYPE); 284 } 285 286 /** 287 * Checks whether a token has a javadoc comment. 288 * 289 * @param methodDef method definition token. 290 * @param tokenType token type. 291 * @return true if a token has a javadoc comment. 292 */ 293 private static boolean hasJavadocCommentOnToken(DetailAST methodDef, int tokenType) { 294 final DetailAST token = methodDef.findFirstToken(tokenType); 295 return branchContainsJavadocComment(token); 296 } 297 298 /** 299 * Checks whether a javadoc comment exists under the token. 300 * 301 * @param token tree token. 302 * @return true if a javadoc comment exists under the token. 303 */ 304 private static boolean branchContainsJavadocComment(DetailAST token) { 305 boolean result = false; 306 DetailAST curNode = token; 307 while (curNode != null) { 308 if (curNode.getType() == TokenTypes.BLOCK_COMMENT_BEGIN 309 && JavadocUtil.isJavadocComment(curNode)) { 310 result = true; 311 break; 312 } 313 314 DetailAST toVisit = curNode.getFirstChild(); 315 while (toVisit == null) { 316 if (curNode == token) { 317 break; 318 } 319 320 toVisit = curNode.getNextSibling(); 321 curNode = curNode.getParent(); 322 } 323 curNode = toVisit; 324 } 325 326 return result; 327 } 328 329 /** 330 * Checks whether a methods is native. 331 * 332 * @param ast method definition token. 333 * @return true if a methods is native. 334 */ 335 private static boolean isNativeMethod(DetailAST ast) { 336 final DetailAST mods = ast.findFirstToken(TokenTypes.MODIFIERS); 337 return mods.findFirstToken(TokenTypes.LITERAL_NATIVE) != null; 338 } 339 340 /** 341 * Checks whether a method has only comments in the body (has an empty implementation). 342 * Method is OK if its implementation is empty. 343 * 344 * @param ast method definition token. 345 * @return true if a method has only comments in the body. 346 */ 347 private static boolean hasEmptyImplementation(DetailAST ast) { 348 boolean hasEmptyBody = true; 349 final DetailAST methodImplOpenBrace = ast.findFirstToken(TokenTypes.SLIST); 350 final DetailAST methodImplCloseBrace = methodImplOpenBrace.getLastChild(); 351 final Predicate<DetailAST> predicate = currentNode -> { 352 return currentNode != methodImplCloseBrace 353 && !TokenUtil.isCommentType(currentNode.getType()); 354 }; 355 final Optional<DetailAST> methodBody = 356 TokenUtil.findFirstTokenByPredicate(methodImplOpenBrace, predicate); 357 if (methodBody.isPresent()) { 358 hasEmptyBody = false; 359 } 360 return hasEmptyBody; 361 } 362 363 /** 364 * Checks whether a method can be overridden. 365 * Method can be overridden if it is not private, abstract, final or static. 366 * Note that the check has nothing to do for interfaces. 367 * 368 * @param methodDef method definition token. 369 * @return true if a method can be overridden in a subclass. 370 */ 371 private static boolean canBeOverridden(DetailAST methodDef) { 372 final DetailAST modifiers = methodDef.findFirstToken(TokenTypes.MODIFIERS); 373 return ScopeUtil.getSurroundingScope(methodDef).isIn(Scope.PROTECTED) 374 && !ScopeUtil.isInInterfaceOrAnnotationBlock(methodDef) 375 && modifiers.findFirstToken(TokenTypes.LITERAL_PRIVATE) == null 376 && modifiers.findFirstToken(TokenTypes.ABSTRACT) == null 377 && modifiers.findFirstToken(TokenTypes.FINAL) == null 378 && modifiers.findFirstToken(TokenTypes.LITERAL_STATIC) == null; 379 } 380 381 /** 382 * Checks whether a method has any of ignored annotations. 383 * 384 * @param methodDef method definition token. 385 * @param annotations a set of ignored annotations. 386 * @return true if a method has any of ignored annotations. 387 */ 388 private static boolean hasIgnoredAnnotation(DetailAST methodDef, Set<String> annotations) { 389 final DetailAST modifiers = methodDef.findFirstToken(TokenTypes.MODIFIERS); 390 final Optional<DetailAST> annotation = TokenUtil.findFirstTokenByPredicate(modifiers, 391 currentToken -> { 392 return currentToken.getType() == TokenTypes.ANNOTATION 393 && annotations.contains(getAnnotationName(currentToken)); 394 }); 395 return annotation.isPresent(); 396 } 397 398 /** 399 * Gets the name of the annotation. 400 * 401 * @param annotation to get name of. 402 * @return the name of the annotation. 403 */ 404 private static String getAnnotationName(DetailAST annotation) { 405 final DetailAST dotAst = annotation.findFirstToken(TokenTypes.DOT); 406 final String name; 407 if (dotAst == null) { 408 name = annotation.findFirstToken(TokenTypes.IDENT).getText(); 409 } 410 else { 411 name = dotAst.findFirstToken(TokenTypes.IDENT).getText(); 412 } 413 return name; 414 } 415 416 /** 417 * Returns CLASS_DEF or ENUM_DEF token which is the nearest to the given ast node. 418 * Searches the tree towards the root until it finds a CLASS_DEF or ENUM_DEF node. 419 * 420 * @param ast the start node for searching. 421 * @return the CLASS_DEF or ENUM_DEF token. 422 */ 423 private static DetailAST getNearestClassOrEnumDefinition(DetailAST ast) { 424 DetailAST searchAST = ast; 425 while (searchAST.getType() != TokenTypes.CLASS_DEF 426 && searchAST.getType() != TokenTypes.ENUM_DEF) { 427 searchAST = searchAST.getParent(); 428 } 429 return searchAST; 430 } 431 432 /** 433 * Checks if the given class (given CLASS_DEF node) can be subclassed. 434 * 435 * @param classDef class definition token. 436 * @return true if the containing class can be subclassed. 437 */ 438 private static boolean canBeSubclassed(DetailAST classDef) { 439 final DetailAST modifiers = classDef.findFirstToken(TokenTypes.MODIFIERS); 440 return classDef.getType() != TokenTypes.ENUM_DEF 441 && modifiers.findFirstToken(TokenTypes.FINAL) == null 442 && hasDefaultOrExplicitNonPrivateCtor(classDef); 443 } 444 445 /** 446 * Checks whether a class has default or explicit non-private constructor. 447 * 448 * @param classDef class ast token. 449 * @return true if a class has default or explicit non-private constructor. 450 */ 451 private static boolean hasDefaultOrExplicitNonPrivateCtor(DetailAST classDef) { 452 // check if subclassing is prevented by having only private ctors 453 final DetailAST objBlock = classDef.findFirstToken(TokenTypes.OBJBLOCK); 454 455 boolean hasDefaultConstructor = true; 456 boolean hasExplicitNonPrivateCtor = false; 457 458 DetailAST candidate = objBlock.getFirstChild(); 459 460 while (candidate != null) { 461 if (candidate.getType() == TokenTypes.CTOR_DEF) { 462 hasDefaultConstructor = false; 463 464 final DetailAST ctorMods = 465 candidate.findFirstToken(TokenTypes.MODIFIERS); 466 if (ctorMods.findFirstToken(TokenTypes.LITERAL_PRIVATE) == null) { 467 hasExplicitNonPrivateCtor = true; 468 break; 469 } 470 } 471 candidate = candidate.getNextSibling(); 472 } 473 474 return hasDefaultConstructor || hasExplicitNonPrivateCtor; 475 } 476 477}