Class EmptyPublicCtorInClassCheck

  • All Implemented Interfaces:
    com.puppycrawl.tools.checkstyle.api.Configurable, com.puppycrawl.tools.checkstyle.api.Contextualizable

    public class EmptyPublicCtorInClassCheck
    extends com.puppycrawl.tools.checkstyle.api.AbstractCheck

    This Check looks for useless empty public constructors. Class constructor is considered useless by this Check if and only if class has exactly one ctor, which is public, empty(one that has no statements) and default.

    Example 1. Check will generate violation for this code:

     class Dummy {
         public Dummy() {
         }
     }
     

    Example 2. Check will not generate violation for this code:

     class Dummy {
         private Dummy() {
         }
     }
     

    class Dummy has only one ctor, which is not public.

    Example 3. Check will not generate violation for this code:

     class Dummy {
         public Dummy() {
         }
         public Dummy(int i) {
         }
     }
     

    class Dummy has multiple ctors.

    Check has two properties:

    "classAnnotationNames" - This property contains regex for canonical names of class annotations, which require class to have empty public ctor. Check will not log violations for classes marked with annotations that match this regex. Default option value is "javax\.persistence\.Entity".

    "ctorAnnotationNames" - This property contains regex for canonical names of ctor annotations, which make empty public ctor essential. Check will not log violations for ctors marked with annotations that match this regex. Default option value is "com\.google\.inject\.Inject".

    Following configuration will adjust Check to skip classes which annotated with "javax.persistence.Entity" and classes which has empty public ctor with "com\.google\.inject\.Inject".
       <module name="EmptyPublicCtorInClassCheck">
         <property name="classAnnotationNames" value="javax\.persistence\.Entity"/>
         <property name="ctorAnnotationNames" value="com\.google\.inject\.Inject"/>
       </module>
     
    Since:
    1.13.0
    Author:
    Zuy Alexey
    • Nested Class Summary

      • Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static String MSG_KEY
      Violation message key.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void beginTree​(com.puppycrawl.tools.checkstyle.api.DetailAST aRootNode)  
      int[] getAcceptableTokens()  
      int[] getDefaultTokens()  
      int[] getRequiredTokens()  
      void setClassAnnotationNames​(String regex)
      Sets regex which matches names of class annotations which require class to have public no-argument ctor.
      void setCtorAnnotationNames​(String regex)
      Sets regex which matches names of ctor annotations which make empty public ctor essential.
      void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST node)  
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck

        clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter

        finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        configure, contextualize, getConfiguration, setupChild
    • Constructor Detail

      • EmptyPublicCtorInClassCheck

        public EmptyPublicCtorInClassCheck()
    • Method Detail

      • setClassAnnotationNames

        public void setClassAnnotationNames​(String regex)
        Sets regex which matches names of class annotations which require class to have public no-argument ctor.
        Parameters:
        regex - regex to match annotation names.
      • setCtorAnnotationNames

        public void setCtorAnnotationNames​(String regex)
        Sets regex which matches names of ctor annotations which make empty public ctor essential.
        Parameters:
        regex - regex to match annotation names.
      • getDefaultTokens

        public int[] getDefaultTokens()
        Specified by:
        getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getAcceptableTokens

        public int[] getAcceptableTokens()
        Specified by:
        getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getRequiredTokens

        public int[] getRequiredTokens()
        Specified by:
        getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • beginTree

        public void beginTree​(com.puppycrawl.tools.checkstyle.api.DetailAST aRootNode)
        Overrides:
        beginTree in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • visitToken

        public void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST node)
        Overrides:
        visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheck