Coverage Report - com.puppycrawl.tools.checkstyle.checks.design.DesignForExtensionCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
DesignForExtensionCheck
100%
79/79
100%
62/62
0
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2017 the original author or authors.
 4  
 //
 5  
 // This library is free software; you can redistribute it and/or
 6  
 // modify it under the terms of the GNU Lesser General Public
 7  
 // License as published by the Free Software Foundation; either
 8  
 // version 2.1 of the License, or (at your option) any later version.
 9  
 //
 10  
 // This library is distributed in the hope that it will be useful,
 11  
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
 // Lesser General Public License for more details.
 14  
 //
 15  
 // You should have received a copy of the GNU Lesser General Public
 16  
 // License along with this library; if not, write to the Free Software
 17  
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 18  
 ////////////////////////////////////////////////////////////////////////////////
 19  
 
 20  
 package com.puppycrawl.tools.checkstyle.checks.design;
 21  
 
 22  
 import java.util.Arrays;
 23  
 import java.util.Optional;
 24  
 import java.util.Set;
 25  
 import java.util.function.Predicate;
 26  
 import java.util.stream.Collectors;
 27  
 
 28  
 import com.puppycrawl.tools.checkstyle.StatelessCheck;
 29  
 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
 30  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 31  
 import com.puppycrawl.tools.checkstyle.api.Scope;
 32  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 33  
 import com.puppycrawl.tools.checkstyle.utils.ScopeUtils;
 34  
 import com.puppycrawl.tools.checkstyle.utils.TokenUtils;
 35  
 
 36  
 /**
 37  
  * The check finds classes that are designed for extension (subclass creation).
 38  
  *
 39  
  * <p>
 40  
  * Nothing wrong could be with founded classes.
 41  
  * This check makes sense only for library projects (not application projects)
 42  
  * which care of ideal OOP-design to make sure that class works in all cases even misusage.
 43  
  * Even in library projects this check most likely will find classes that are designed for extension
 44  
  * by somebody. User needs to use suppressions extensively to got a benefit from this check,
 45  
  * and keep in suppressions all confirmed/known classes that are deigned for inheritance
 46  
  * intentionally to let the check catch only new classes, and bring this to team/user attention.
 47  
  * </p>
 48  
  *
 49  
  * <p>
 50  
  * ATTENTION: Only user can decide whether a class is designed for extension or not.
 51  
  * The check just shows all classes which are possibly designed for extension.
 52  
  * If smth inappropriate is found please use suppression.
 53  
  * </p>
 54  
  *
 55  
  * <p>
 56  
  * ATTENTION: If the method which can be overridden in a subclass has a javadoc comment
 57  
  * (a good practice is to explain its self-use of overridable methods) the check will not
 58  
  * rise a violation. The violation can also be skipped if the method which can be overridden
 59  
  * in a subclass has one or more annotations that are specified in ignoredAnnotations
 60  
  * option. Note, that by default @Override annotation is not included in the
 61  
  * ignoredAnnotations set as in a subclass the method which has the annotation can also be
 62  
  * overridden in its subclass.
 63  
  * </p>
 64  
  *
 65  
  * <p>
 66  
  * More specifically, the check enforces a programming style where superclasses provide empty
 67  
  * "hooks" that can be implemented by subclasses.
 68  
  * </p>
 69  
  *
 70  
  * <p>
 71  
  * The check finds classes that have overridable methods (public or protected methods
 72  
  * that are non-static, not-final, non-abstract) and have non-empty implementation.
 73  
  * </p>
 74  
  *
 75  
  * <p>
 76  
  * This protects superclasses against being broken by subclasses. The downside is that subclasses
 77  
  * are limited in their flexibility, in particular, they cannot prevent execution of code in the
 78  
  * superclass, but that also means that subclasses cannot forget to call their super method.
 79  
  * </p>
 80  
  *
 81  
  * <p>
 82  
  * The check has the following options:
 83  
  * </p>
 84  
  * <ul>
 85  
  * <li>
 86  
  * ignoredAnnotations - annotations which allow the check to skip the method from validation.
 87  
  * Default value is <b>Test, Before, After, BeforeClass, AfterClass</b>.
 88  
  * </li>
 89  
  * </ul>
 90  
  *
 91  
  * @author lkuehne
 92  
  * @author Andrei Selkin
 93  
  */
 94  
 @StatelessCheck
 95  15
 public class DesignForExtensionCheck extends AbstractCheck {
 96  
 
 97  
     /**
 98  
      * A key is pointing to the warning message text in "messages.properties"
 99  
      * file.
 100  
      */
 101  
     public static final String MSG_KEY = "design.forExtension";
 102  
 
 103  
     /**
 104  
      * A set of annotations which allow the check to skip the method from validation.
 105  
      */
 106  30
     private Set<String> ignoredAnnotations = Arrays.stream(new String[] {"Test", "Before", "After",
 107  15
         "BeforeClass", "AfterClass", }).collect(Collectors.toSet());
 108  
 
 109  
     /**
 110  
      * Sets annotations which allow the check to skip the method from validation.
 111  
      * @param ignoredAnnotations method annotations.
 112  
      */
 113  
     public void setIgnoredAnnotations(String... ignoredAnnotations) {
 114  4
         this.ignoredAnnotations = Arrays.stream(ignoredAnnotations).collect(Collectors.toSet());
 115  4
     }
 116  
 
 117  
     @Override
 118  
     public int[] getDefaultTokens() {
 119  20
         return getRequiredTokens();
 120  
     }
 121  
 
 122  
     @Override
 123  
     public int[] getAcceptableTokens() {
 124  5
         return getRequiredTokens();
 125  
     }
 126  
 
 127  
     @Override
 128  
     public int[] getRequiredTokens() {
 129  
         // The check does not subscribe to CLASS_DEF token as now it is stateless. If the check
 130  
         // subscribes to CLASS_DEF token it will become stateful, since we need to have additional
 131  
         // stack to hold CLASS_DEF tokens.
 132  46
         return new int[] {TokenTypes.METHOD_DEF};
 133  
     }
 134  
 
 135  
     @Override
 136  
     public boolean isCommentNodesRequired() {
 137  16
         return true;
 138  
     }
 139  
 
 140  
     @Override
 141  
     public void visitToken(DetailAST ast) {
 142  110
         if (!hasJavadocComment(ast)
 143  68
                 && canBeOverridden(ast)
 144  51
                 && (isNativeMethod(ast)
 145  49
                     || !hasEmptyImplementation(ast))
 146  40
                 && !hasIgnoredAnnotation(ast, ignoredAnnotations)) {
 147  
 
 148  24
             final DetailAST classDef = getNearestClassOrEnumDefinition(ast);
 149  24
             if (canBeSubclassed(classDef)) {
 150  19
                 final String className = classDef.findFirstToken(TokenTypes.IDENT).getText();
 151  19
                 final String methodName = ast.findFirstToken(TokenTypes.IDENT).getText();
 152  19
                 log(ast.getLineNo(), ast.getColumnNo(), MSG_KEY, className, methodName);
 153  
             }
 154  
         }
 155  110
     }
 156  
 
 157  
     /**
 158  
      * Checks whether a method has a javadoc comment.
 159  
      * @param methodDef method definition token.
 160  
      * @return true if a method has a javadoc comment.
 161  
      */
 162  
     private static boolean hasJavadocComment(DetailAST methodDef) {
 163  110
         final DetailAST modifiers = methodDef.findFirstToken(TokenTypes.MODIFIERS);
 164  110
         return modifiers.branchContains(TokenTypes.BLOCK_COMMENT_BEGIN);
 165  
     }
 166  
 
 167  
     /**
 168  
      * Checks whether a methods is native.
 169  
      * @param ast method definition token.
 170  
      * @return true if a methods is native.
 171  
      */
 172  
     private static boolean isNativeMethod(DetailAST ast) {
 173  51
         final DetailAST mods = ast.findFirstToken(TokenTypes.MODIFIERS);
 174  51
         return mods.findFirstToken(TokenTypes.LITERAL_NATIVE) != null;
 175  
     }
 176  
 
 177  
     /**
 178  
      * Checks whether a method has only comments in the body (has an empty implementation).
 179  
      * Method is OK if its implementation is empty.
 180  
      * @param ast method definition token.
 181  
      * @return true if a method has only comments in the body.
 182  
      */
 183  
     private static boolean hasEmptyImplementation(DetailAST ast) {
 184  49
         boolean hasEmptyBody = true;
 185  49
         final DetailAST methodImplOpenBrace = ast.findFirstToken(TokenTypes.SLIST);
 186  49
         final DetailAST methodImplCloseBrace = methodImplOpenBrace.getLastChild();
 187  49
         final Predicate<DetailAST> predicate = currentNode -> {
 188  128
             return currentNode != methodImplCloseBrace
 189  53
                 && !TokenUtils.isCommentType(currentNode.getType());
 190  
         };
 191  49
         final Optional<DetailAST> methodBody =
 192  49
             TokenUtils.findFirstTokenByPredicate(methodImplOpenBrace, predicate);
 193  49
         if (methodBody.isPresent()) {
 194  38
             hasEmptyBody = false;
 195  
         }
 196  49
         return hasEmptyBody;
 197  
     }
 198  
 
 199  
     /**
 200  
      * Checks whether a method can be overridden.
 201  
      * Method can be overridden if it is not private, abstract, final or static.
 202  
      * Note that the check has nothing to do for interfaces.
 203  
      * @param methodDef method definition token.
 204  
      * @return true if a method can be overridden in a subclass.
 205  
      */
 206  
     private static boolean canBeOverridden(DetailAST methodDef) {
 207  68
         final DetailAST modifiers = methodDef.findFirstToken(TokenTypes.MODIFIERS);
 208  136
         return ScopeUtils.getSurroundingScope(methodDef).isIn(Scope.PROTECTED)
 209  66
             && !ScopeUtils.isInInterfaceOrAnnotationBlock(methodDef)
 210  65
             && modifiers.findFirstToken(TokenTypes.LITERAL_PRIVATE) == null
 211  62
             && modifiers.findFirstToken(TokenTypes.ABSTRACT) == null
 212  60
             && modifiers.findFirstToken(TokenTypes.FINAL) == null
 213  54
             && modifiers.findFirstToken(TokenTypes.LITERAL_STATIC) == null;
 214  
     }
 215  
 
 216  
     /**
 217  
      * Checks whether a method has any of ignored annotations.
 218  
      * @param methodDef method definition token.
 219  
      * @param annotations a set of ignored annotations.
 220  
      * @return true if a method has any of ignored annotations.
 221  
      */
 222  
     private static boolean hasIgnoredAnnotation(DetailAST methodDef, Set<String> annotations) {
 223  40
         final DetailAST modifiers = methodDef.findFirstToken(TokenTypes.MODIFIERS);
 224  40
         boolean hasIgnoredAnnotation = false;
 225  40
         if (modifiers.findFirstToken(TokenTypes.ANNOTATION) != null) {
 226  20
             final Optional<DetailAST> annotation = TokenUtils.findFirstTokenByPredicate(modifiers,
 227  
                 currentToken -> {
 228  62
                     return currentToken.getType() == TokenTypes.ANNOTATION
 229  22
                         && annotations.contains(getAnnotationName(currentToken));
 230  
                 });
 231  20
             if (annotation.isPresent()) {
 232  16
                 hasIgnoredAnnotation = true;
 233  
             }
 234  
         }
 235  40
         return hasIgnoredAnnotation;
 236  
     }
 237  
 
 238  
     /**
 239  
      * Gets the name of the annotation.
 240  
      * @param annotation to get name of.
 241  
      * @return the name of the annotation.
 242  
      */
 243  
     private static String getAnnotationName(DetailAST annotation) {
 244  22
         final DetailAST dotAst = annotation.findFirstToken(TokenTypes.DOT);
 245  
         final String name;
 246  22
         if (dotAst == null) {
 247  20
             name = annotation.findFirstToken(TokenTypes.IDENT).getText();
 248  
         }
 249  
         else {
 250  2
             name = dotAst.findFirstToken(TokenTypes.IDENT).getText();
 251  
         }
 252  22
         return name;
 253  
     }
 254  
 
 255  
     /**
 256  
      * Returns CLASS_DEF or ENUM_DEF token which is the nearest to the given ast node.
 257  
      * Searches the tree towards the root until it finds a CLASS_DEF or ENUM_DEF node.
 258  
      * @param ast the start node for searching.
 259  
      * @return the CLASS_DEF or ENUM_DEF token.
 260  
      */
 261  
     private static DetailAST getNearestClassOrEnumDefinition(DetailAST ast) {
 262  24
         DetailAST searchAST = ast;
 263  72
         while (searchAST.getType() != TokenTypes.CLASS_DEF
 264  49
                && searchAST.getType() != TokenTypes.ENUM_DEF) {
 265  48
             searchAST = searchAST.getParent();
 266  
         }
 267  24
         return searchAST;
 268  
     }
 269  
 
 270  
     /**
 271  
      * Checks if the given class (given CLASS_DEF node) can be subclassed.
 272  
      * @param classDef class definition token.
 273  
      * @return true if the containing class can be subclassed.
 274  
      */
 275  
     private static boolean canBeSubclassed(DetailAST classDef) {
 276  24
         final DetailAST modifiers = classDef.findFirstToken(TokenTypes.MODIFIERS);
 277  48
         return classDef.getType() != TokenTypes.ENUM_DEF
 278  23
             && modifiers.findFirstToken(TokenTypes.FINAL) == null
 279  20
             && hasDefaultOrExplicitNonPrivateCtor(classDef);
 280  
     }
 281  
 
 282  
     /**
 283  
      * Checks whether a class has default or explicit non-private constructor.
 284  
      * @param classDef class ast token.
 285  
      * @return true if a class has default or explicit non-private constructor.
 286  
      */
 287  
     private static boolean hasDefaultOrExplicitNonPrivateCtor(DetailAST classDef) {
 288  
         // check if subclassing is prevented by having only private ctors
 289  20
         final DetailAST objBlock = classDef.findFirstToken(TokenTypes.OBJBLOCK);
 290  
 
 291  20
         boolean hasDefaultConstructor = true;
 292  20
         boolean hasExplicitNonPrivateCtor = false;
 293  
 
 294  20
         DetailAST candidate = objBlock.getFirstChild();
 295  
 
 296  722
         while (candidate != null) {
 297  703
             if (candidate.getType() == TokenTypes.CTOR_DEF) {
 298  2
                 hasDefaultConstructor = false;
 299  
 
 300  2
                 final DetailAST ctorMods =
 301  2
                         candidate.findFirstToken(TokenTypes.MODIFIERS);
 302  2
                 if (ctorMods.findFirstToken(TokenTypes.LITERAL_PRIVATE) == null) {
 303  1
                     hasExplicitNonPrivateCtor = true;
 304  1
                     break;
 305  
                 }
 306  
             }
 307  702
             candidate = candidate.getNextSibling();
 308  
         }
 309  
 
 310  20
         return hasDefaultConstructor || hasExplicitNonPrivateCtor;
 311  
     }
 312  
 }