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 * @param ignoredAnnotations method annotations. 230 */ 231 public void setIgnoredAnnotations(String... ignoredAnnotations) { 232 this.ignoredAnnotations = Arrays.stream(ignoredAnnotations).collect(Collectors.toSet()); 233 } 234 235 @Override 236 public int[] getDefaultTokens() { 237 return getRequiredTokens(); 238 } 239 240 @Override 241 public int[] getAcceptableTokens() { 242 return getRequiredTokens(); 243 } 244 245 @Override 246 public int[] getRequiredTokens() { 247 // The check does not subscribe to CLASS_DEF token as now it is stateless. If the check 248 // subscribes to CLASS_DEF token it will become stateful, since we need to have additional 249 // stack to hold CLASS_DEF tokens. 250 return new int[] {TokenTypes.METHOD_DEF}; 251 } 252 253 @Override 254 public boolean isCommentNodesRequired() { 255 return true; 256 } 257 258 @Override 259 public void visitToken(DetailAST ast) { 260 if (!hasJavadocComment(ast) 261 && canBeOverridden(ast) 262 && (isNativeMethod(ast) 263 || !hasEmptyImplementation(ast)) 264 && !hasIgnoredAnnotation(ast, ignoredAnnotations)) { 265 final DetailAST classDef = getNearestClassOrEnumDefinition(ast); 266 if (canBeSubclassed(classDef)) { 267 final String className = classDef.findFirstToken(TokenTypes.IDENT).getText(); 268 final String methodName = ast.findFirstToken(TokenTypes.IDENT).getText(); 269 log(ast, MSG_KEY, className, methodName); 270 } 271 } 272 } 273 274 /** 275 * Checks whether a method has a javadoc comment. 276 * @param methodDef method definition token. 277 * @return true if a method has a javadoc comment. 278 */ 279 private static boolean hasJavadocComment(DetailAST methodDef) { 280 return hasJavadocCommentOnToken(methodDef, TokenTypes.MODIFIERS) 281 || hasJavadocCommentOnToken(methodDef, TokenTypes.TYPE); 282 } 283 284 /** 285 * Checks whether a token has a javadoc comment. 286 * 287 * @param methodDef method definition token. 288 * @param tokenType token type. 289 * @return true if a token has a javadoc comment. 290 */ 291 private static boolean hasJavadocCommentOnToken(DetailAST methodDef, int tokenType) { 292 final DetailAST token = methodDef.findFirstToken(tokenType); 293 return branchContainsJavadocComment(token); 294 } 295 296 /** 297 * Checks whether a javadoc comment exists under the token. 298 * 299 * @param token tree token. 300 * @return true if a javadoc comment exists under the token. 301 */ 302 private static boolean branchContainsJavadocComment(DetailAST token) { 303 boolean result = false; 304 DetailAST curNode = token; 305 while (curNode != null) { 306 if (curNode.getType() == TokenTypes.BLOCK_COMMENT_BEGIN 307 && JavadocUtil.isJavadocComment(curNode)) { 308 result = true; 309 break; 310 } 311 312 DetailAST toVisit = curNode.getFirstChild(); 313 while (toVisit == null) { 314 if (curNode == token) { 315 break; 316 } 317 318 toVisit = curNode.getNextSibling(); 319 curNode = curNode.getParent(); 320 } 321 curNode = toVisit; 322 } 323 324 return result; 325 } 326 327 /** 328 * Checks whether a methods is native. 329 * @param ast method definition token. 330 * @return true if a methods is native. 331 */ 332 private static boolean isNativeMethod(DetailAST ast) { 333 final DetailAST mods = ast.findFirstToken(TokenTypes.MODIFIERS); 334 return mods.findFirstToken(TokenTypes.LITERAL_NATIVE) != null; 335 } 336 337 /** 338 * Checks whether a method has only comments in the body (has an empty implementation). 339 * Method is OK if its implementation is empty. 340 * @param ast method definition token. 341 * @return true if a method has only comments in the body. 342 */ 343 private static boolean hasEmptyImplementation(DetailAST ast) { 344 boolean hasEmptyBody = true; 345 final DetailAST methodImplOpenBrace = ast.findFirstToken(TokenTypes.SLIST); 346 final DetailAST methodImplCloseBrace = methodImplOpenBrace.getLastChild(); 347 final Predicate<DetailAST> predicate = currentNode -> { 348 return currentNode != methodImplCloseBrace 349 && !TokenUtil.isCommentType(currentNode.getType()); 350 }; 351 final Optional<DetailAST> methodBody = 352 TokenUtil.findFirstTokenByPredicate(methodImplOpenBrace, predicate); 353 if (methodBody.isPresent()) { 354 hasEmptyBody = false; 355 } 356 return hasEmptyBody; 357 } 358 359 /** 360 * Checks whether a method can be overridden. 361 * Method can be overridden if it is not private, abstract, final or static. 362 * Note that the check has nothing to do for interfaces. 363 * @param methodDef method definition token. 364 * @return true if a method can be overridden in a subclass. 365 */ 366 private static boolean canBeOverridden(DetailAST methodDef) { 367 final DetailAST modifiers = methodDef.findFirstToken(TokenTypes.MODIFIERS); 368 return ScopeUtil.getSurroundingScope(methodDef).isIn(Scope.PROTECTED) 369 && !ScopeUtil.isInInterfaceOrAnnotationBlock(methodDef) 370 && modifiers.findFirstToken(TokenTypes.LITERAL_PRIVATE) == null 371 && modifiers.findFirstToken(TokenTypes.ABSTRACT) == null 372 && modifiers.findFirstToken(TokenTypes.FINAL) == null 373 && modifiers.findFirstToken(TokenTypes.LITERAL_STATIC) == null; 374 } 375 376 /** 377 * Checks whether a method has any of ignored annotations. 378 * @param methodDef method definition token. 379 * @param annotations a set of ignored annotations. 380 * @return true if a method has any of ignored annotations. 381 */ 382 private static boolean hasIgnoredAnnotation(DetailAST methodDef, Set<String> annotations) { 383 final DetailAST modifiers = methodDef.findFirstToken(TokenTypes.MODIFIERS); 384 final Optional<DetailAST> annotation = TokenUtil.findFirstTokenByPredicate(modifiers, 385 currentToken -> { 386 return currentToken.getType() == TokenTypes.ANNOTATION 387 && annotations.contains(getAnnotationName(currentToken)); 388 }); 389 return annotation.isPresent(); 390 } 391 392 /** 393 * Gets the name of the annotation. 394 * @param annotation to get name of. 395 * @return the name of the annotation. 396 */ 397 private static String getAnnotationName(DetailAST annotation) { 398 final DetailAST dotAst = annotation.findFirstToken(TokenTypes.DOT); 399 final String name; 400 if (dotAst == null) { 401 name = annotation.findFirstToken(TokenTypes.IDENT).getText(); 402 } 403 else { 404 name = dotAst.findFirstToken(TokenTypes.IDENT).getText(); 405 } 406 return name; 407 } 408 409 /** 410 * Returns CLASS_DEF or ENUM_DEF token which is the nearest to the given ast node. 411 * Searches the tree towards the root until it finds a CLASS_DEF or ENUM_DEF node. 412 * @param ast the start node for searching. 413 * @return the CLASS_DEF or ENUM_DEF token. 414 */ 415 private static DetailAST getNearestClassOrEnumDefinition(DetailAST ast) { 416 DetailAST searchAST = ast; 417 while (searchAST.getType() != TokenTypes.CLASS_DEF 418 && searchAST.getType() != TokenTypes.ENUM_DEF) { 419 searchAST = searchAST.getParent(); 420 } 421 return searchAST; 422 } 423 424 /** 425 * Checks if the given class (given CLASS_DEF node) can be subclassed. 426 * @param classDef class definition token. 427 * @return true if the containing class can be subclassed. 428 */ 429 private static boolean canBeSubclassed(DetailAST classDef) { 430 final DetailAST modifiers = classDef.findFirstToken(TokenTypes.MODIFIERS); 431 return classDef.getType() != TokenTypes.ENUM_DEF 432 && modifiers.findFirstToken(TokenTypes.FINAL) == null 433 && hasDefaultOrExplicitNonPrivateCtor(classDef); 434 } 435 436 /** 437 * Checks whether a class has default or explicit non-private constructor. 438 * @param classDef class ast token. 439 * @return true if a class has default or explicit non-private constructor. 440 */ 441 private static boolean hasDefaultOrExplicitNonPrivateCtor(DetailAST classDef) { 442 // check if subclassing is prevented by having only private ctors 443 final DetailAST objBlock = classDef.findFirstToken(TokenTypes.OBJBLOCK); 444 445 boolean hasDefaultConstructor = true; 446 boolean hasExplicitNonPrivateCtor = false; 447 448 DetailAST candidate = objBlock.getFirstChild(); 449 450 while (candidate != null) { 451 if (candidate.getType() == TokenTypes.CTOR_DEF) { 452 hasDefaultConstructor = false; 453 454 final DetailAST ctorMods = 455 candidate.findFirstToken(TokenTypes.MODIFIERS); 456 if (ctorMods.findFirstToken(TokenTypes.LITERAL_PRIVATE) == null) { 457 hasExplicitNonPrivateCtor = true; 458 break; 459 } 460 } 461 candidate = candidate.getNextSibling(); 462 } 463 464 return hasDefaultConstructor || hasExplicitNonPrivateCtor; 465 } 466 467}