Class MissingJavadocMethodCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public class MissingJavadocMethodCheck
    extends AbstractCheck

    Checks for missing Javadoc comments for a method or constructor. The scope to verify is specified using the Scope class and defaults to Scope.PUBLIC. To verify another scope, set property scope to a different scope.

    Javadoc is not required on a method that is tagged with the @Override annotation. However, under Java 5 it is not possible to mark a method required for an interface (this was corrected under Java 6). Hence, Checkstyle supports using the convention of using a single {@inheritDoc} tag instead of all the other tags.

    For getters and setters for the property allowMissingPropertyJavadoc, the methods must match exactly the structures below.

     public void setNumber(final int number)
     {
         mNumber = number;
     }
    
     public int getNumber()
     {
         return mNumber;
     }
    
     public boolean isSomething()
     {
         return false;
     }
     
    • Property minLineCount - Control the minimal amount of lines in method to allow no documentation. Type is int. Default value is -1.
    • Property allowedAnnotations - Configure annotations that allow missed documentation. Type is java.lang.String[]. Default value is Override.
    • Property scope - Specify the visibility scope where Javadoc comments are checked. Type is com.puppycrawl.tools.checkstyle.api.Scope. Default value is public.
    • Property excludeScope - Specify the visibility scope where Javadoc comments are not checked. Type is com.puppycrawl.tools.checkstyle.api.Scope. Default value is null.
    • Property allowMissingPropertyJavadoc - Control whether to allow missing Javadoc on accessor methods for properties (setters and getters). Type is boolean. Default value is false.
    • Property ignoreMethodNamesRegex - ignore method whose names are matching specified regex. Type is java.util.regex.Pattern. Default value is null.
    • Property tokens - tokens to check Type is java.lang.String[]. Validation type is tokenSet. Default value is: METHOD_DEF, CTOR_DEF, ANNOTATION_FIELD_DEF, COMPACT_CTOR_DEF.

    To configure the default check:

     <module name="MissingJavadocMethod"/>
     

    Example:

     public class Test {
       public Test() {} // violation, missing javadoc for constructor
       public void test() {} // violation, missing javadoc for method
       /**
        * Some description here.
        */
       public void test2() {} // OK
    
       @Override
       public String toString() { // OK
         return "Some string";
       }
    
       private void test1() {} // OK
       protected void test2() {} // OK
       void test3() {} // OK
     }
     

    To configure the check for private scope:

     <module name="MissingJavadocMethod">
       <property name="scope" value="private"/>
     </module>
     

    Example:

     public class Test {
       private void test1() {} // violation, the private method is missing javadoc
     }
     

    To configure the check for methods which are in private, but not in protected scope:

     <module name="MissingJavadocMethod">
       <property name="scope" value="private"/>
       <property name="excludeScope" value="protected"/>
     </module>
     

    Example:

     public class Test {
       private void test1() {} // violation, the private method is missing javadoc
       /**
        * Some description here
        */
       private void test1() {} // OK
       protected void test2() {} // OK
     }
     

    To configure the check for ignoring methods named foo(),foo1(),foo2(), etc.:

     <module name="MissingJavadocMethod">
       <property name="ignoreMethodNamesRegex" value="^foo.*$"/>
     </module>
     

    Example:

     public class Test {
       public void test1() {} // violation, method is missing javadoc
       public void foo() {} // OK
       public void foobar() {} // OK
     }
     

    To configure the check for ignoring missing javadoc for accessor methods:

     <module name="MissingJavadocMethod">
       <property name="allowMissingPropertyJavadoc" value="true"/>
     </module>
     

    Example:

     public class Test {
       private String text;
    
       public void test() {} // violation, method is missing javadoc
       public String getText() { return text; } // OK
       public void setText(String text) { this.text = text; } // OK
     }
     

    To configure the check with annotations that allow missed documentation:

     <module name="MissingJavadocMethod">
       <property name="allowedAnnotations" value="Override,Deprecated"/>
     </module>
     

    Example:

     public class Test {
       public void test() {} // violation, method is missing javadoc
       @Override
       public void test1() {} // OK
       @Deprecated
       public void test2() {} // OK
       @SuppressWarnings
       public void test3() {} // violation, method is missing javadoc
       /**
        * Some description here.
        */
       @SuppressWarnings
       public void test4() {} // OK
     }
     

    Parent is com.puppycrawl.tools.checkstyle.TreeWalker

    Violation Message Keys:

    • javadoc.missing
    Since:
    8.21
    • Field Detail

      • scope

        private Scope scope
        Specify the visibility scope where Javadoc comments are checked.
      • excludeScope

        private Scope excludeScope
        Specify the visibility scope where Javadoc comments are not checked.
      • minLineCount

        private int minLineCount
        Control the minimal amount of lines in method to allow no documentation.
      • allowMissingPropertyJavadoc

        private boolean allowMissingPropertyJavadoc
        Control whether to allow missing Javadoc on accessor methods for properties (setters and getters).
      • ignoreMethodNamesRegex

        private java.util.regex.Pattern ignoreMethodNamesRegex
        Ignore method whose names are matching specified regex.
      • allowedAnnotations

        private java.util.Set<java.lang.String> allowedAnnotations
        Configure annotations that allow missed documentation.
    • Method Detail

      • setAllowedAnnotations

        public void setAllowedAnnotations​(java.lang.String... userAnnotations)
        Setter to configure annotations that allow missed documentation.
        Parameters:
        userAnnotations - user's value.
      • setIgnoreMethodNamesRegex

        public void setIgnoreMethodNamesRegex​(java.util.regex.Pattern pattern)
        Setter to ignore method whose names are matching specified regex.
        Parameters:
        pattern - a pattern.
      • setMinLineCount

        public void setMinLineCount​(int value)
        Setter to control the minimal amount of lines in method to allow no documentation.
        Parameters:
        value - user's value.
      • setAllowMissingPropertyJavadoc

        public void setAllowMissingPropertyJavadoc​(boolean flag)
        Setter to control whether to allow missing Javadoc on accessor methods for properties (setters and getters).
        Parameters:
        flag - a Boolean value
      • setScope

        public void setScope​(Scope scope)
        Setter to specify the visibility scope where Javadoc comments are checked.
        Parameters:
        scope - a scope.
      • setExcludeScope

        public void setExcludeScope​(Scope excludeScope)
        Setter to specify the visibility scope where Javadoc comments are not checked.
        Parameters:
        excludeScope - a scope.
      • 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
      • getMethodsNumberOfLine

        private static int getMethodsNumberOfLine​(DetailAST methodDef)
        Some javadoc.
        Parameters:
        methodDef - Some javadoc.
        Returns:
        Some javadoc.
      • isMissingJavadocAllowed

        private boolean isMissingJavadocAllowed​(DetailAST ast)
        Checks if a missing Javadoc is allowed by the check's configuration.
        Parameters:
        ast - the tree node for the method or constructor.
        Returns:
        True if this method or constructor doesn't need Javadoc.
      • isContentsAllowMissingJavadoc

        private boolean isContentsAllowMissingJavadoc​(DetailAST ast)
        Checks if the Javadoc can be missing if the method or constructor is below the minimum line count or has a special annotation.
        Parameters:
        ast - the tree node for the method or constructor.
        Returns:
        True if this method or constructor doesn't need Javadoc.
      • matchesSkipRegex

        private boolean matchesSkipRegex​(DetailAST methodDef)
        Checks if the given method name matches the regex. In that case we skip enforcement of javadoc for this method
        Parameters:
        methodDef - METHOD_DEF
        Returns:
        true if given method name matches the regex.
      • shouldCheck

        private boolean shouldCheck​(DetailAST ast,
                                    Scope nodeScope)
        Whether we should check this node.
        Parameters:
        ast - a given node.
        nodeScope - the scope of the node.
        Returns:
        whether we should check a given node.