Class DesignForExtensionCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public class DesignForExtensionCheck
    extends AbstractCheck
    The check finds classes that are designed for extension (subclass creation).

    Nothing wrong could be with founded classes. This check makes sense only for library projects (not an application projects) which care of ideal OOP-design to make sure that class works in all cases even misusage. Even in library projects this check most likely will find classes that are designed for extension by somebody. User needs to use suppressions extensively to got a benefit from this check, and keep in suppressions all confirmed/known classes that are deigned for inheritance intentionally to let the check catch only new classes, and bring this to team/user attention.

    ATTENTION: Only user can decide whether a class is designed for extension or not. The check just shows all classes which are possibly designed for extension. If smth inappropriate is found please use suppression.

    ATTENTION: If the method which can be overridden in a subclass has a javadoc comment (a good practise is to explain its self-use of overridable methods) the check will not rise a violation. The violation can also be skipped if the method which can be overridden in a subclass has one or more annotations that are specified in ignoredAnnotations option. Note, that by default @Override annotation is not included in the ignoredAnnotations set as in a subclass the method which has the annotation can also be overridden in its subclass.

    More specifically, the check enforces a programming style where superclasses provide empty "hooks" that can be implemented by subclasses.

    The check finds classes that have overridable methods (public or protected methods that are non-static, not-final, non-abstract) and have non-empty implementation.

    This protects superclasses against being broken by subclasses. The downside is that subclasses are limited in their flexibility, in particular, they cannot prevent execution of code in the superclass, but that also means that subclasses cannot forget to call their super method.

    The check has the following options:

    • ignoredAnnotations - annotations which allow the check to skip the method from validation. Default value is Test, Before, After, BeforeClass, AfterClass.
    • Field Detail

      • MSG_KEY

        public static final java.lang.String MSG_KEY
        A key is pointing to the warning message text in "messages.properties" file.
        See Also:
        Constant Field Values
      • ignoredAnnotations

        private java.util.Set<java.lang.String> ignoredAnnotations
        A set of annotations which allow the check to skip the method from validation.
    • Constructor Detail

      • DesignForExtensionCheck

        public DesignForExtensionCheck()
    • Method Detail

      • setIgnoredAnnotations

        public void setIgnoredAnnotations​(java.lang.String... ignoredAnnotations)
        Sets annotations which allow the check to skip the method from validation.
        Parameters:
        ignoredAnnotations - method annotations.
      • getDefaultTokens

        public int[] getDefaultTokens()
        Description copied from class: AbstractCheck
        Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.
        Specified by:
        getDefaultTokens in class AbstractCheck
        Returns:
        the default tokens
        See Also:
        TokenTypes
      • getAcceptableTokens

        public int[] getAcceptableTokens()
        Description copied from class: AbstractCheck
        The configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.
        Specified by:
        getAcceptableTokens in class AbstractCheck
        Returns:
        the token set this check is designed for.
        See Also:
        TokenTypes
      • getRequiredTokens

        public int[] getRequiredTokens()
        Description copied from class: AbstractCheck
        The tokens that this check must be registered for.
        Specified by:
        getRequiredTokens in class AbstractCheck
        Returns:
        the token set this must be registered for.
        See Also:
        TokenTypes
      • isCommentNodesRequired

        public boolean isCommentNodesRequired()
        Description copied from class: AbstractCheck
        Whether comment nodes are required or not.
        Overrides:
        isCommentNodesRequired in class AbstractCheck
        Returns:
        false as a default value.
      • hasJavadocComment

        private boolean hasJavadocComment​(DetailAST methodDef)
        Checks whether a method has a javadoc comment.
        Parameters:
        methodDef - method definition token.
        Returns:
        true if a method has a javadoc comment.
      • isNativeMethod

        private boolean isNativeMethod​(DetailAST ast)
        Checks whether a methods is native.
        Parameters:
        ast - method definition token.
        Returns:
        true if a methods is native.
      • hasEmptyImplementation

        private static boolean hasEmptyImplementation​(DetailAST ast)
        Checks whether a method has only comments in the body (has an empty implementation). Method is OK if its implementation is empty.
        Parameters:
        ast - method definition token.
        Returns:
        true if a method has only comments in the body.
      • canBeOverridden

        private boolean canBeOverridden​(DetailAST methodDef)
        Checks whether a method can be overridden. Method can be overridden if it is not private, abstract, final or static. Note that the check has nothing to do for interfaces.
        Parameters:
        methodDef - method definition token.
        Returns:
        true if a method can be overridden in a subclass.
      • hasIgnoredAnnotation

        private static boolean hasIgnoredAnnotation​(DetailAST methodDef,
                                                    java.util.Set<java.lang.String> annotations)
        Checks whether a method has any of ignored annotations.
        Parameters:
        methodDef - method definition token.
        annotations - a set of ignored annotations.
        Returns:
        true if a method has any of ignored annotations.
      • getAnnotationName

        private static java.lang.String getAnnotationName​(DetailAST annotation)
        Gets the name of the annotation.
        Parameters:
        annotation - to get name of.
        Returns:
        the name of the annotation.
      • getNearestClassOrEnumDefinition

        private static DetailAST getNearestClassOrEnumDefinition​(DetailAST ast)
        Returns CLASS_DEF or ENUM_DEF token which is the nearest to the given ast node. Searches the tree towards the root until it finds a CLASS_DEF or ENUM_DEF node.
        Parameters:
        ast - the start node for searching.
        Returns:
        the CLASS_DEF or ENUM_DEF token.
      • canBeSubclassed

        private static boolean canBeSubclassed​(DetailAST classDef)
        Checks if the given class (given CLASS_DEF node) can be subclassed.
        Parameters:
        classDef - class definition token.
        Returns:
        true if the containing class can be subclassed.
      • hasDefaultOrExplicitNonPrivateCtor

        private static boolean hasDefaultOrExplicitNonPrivateCtor​(DetailAST classDef)
        Checks whether a class has default or explicit non-private constructor.
        Parameters:
        classDef - class ast token.
        Returns:
        true if a class has default or explicit non-private constructor.