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