Class UselessSuperCtorCallCheck

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

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

    Checks for useless "super()" calls in ctors.

    "super()" call could be considered by Check as "useless" in two cases:

    Case 1. no-argument "super()" is called from class ctor if class is not derived, for example:

     
     class Dummy {
         Dummy() {
                 super();
         }
     }
     
     
    "super()" call is useless because class "Dummy" is not derived.

    Case 2. no-argument "super()" is called without parameters from class ctor if class is derived, for example:

     
     class Derived extends Base {
         Derived() {
                 super();
         }
     }
     
     
    Java compiler automatically inserts a call to the no-args constructor of the superclass, so there is no need to call super ctor explicitly. Check has options "allowCallToNoArgsSuperCtor" and "allowCallToNoArgsSuperCtorIfMultiplePublicCtor" to adjust check behavior for such cases( see Check`s options description for details).

    Check has following options:

    "allowCallToNoArgsSuperCtor" - if this option set to true, Check will not generate violations when "super()" called inside derived class. This option defaults to "false". If for example this option set to "true", then Check will not generate violation for cases like following:

     
     class Base {
         public Base() {
         }
     }
    
     class Derived extends Base {
         public Derived() {
             super();
         }
     }
     
     

    "allowCallToNoArgsSuperCtorIfMultiplePublicCtor" - if this option set to "true", then Check will not generate violation when "super()" called inside class ctor when class has multiple public ctors(however, setting this option to "true" will not prevent Check from logging violation if class does not extend anything). This option defaults to "false". This option may be useful for cases in which class`s ctors just forward its arguments to super ctors, thus removing "super()" in this case will make default ctors look not like others. For example:

     
     class Base {
         public Base() {
         }
    
         public Base(int i) {
         }
     }
    
     class Derived extends Base {
         public Derived() {
             super(); // this "super()" will not be considered useless if option is set to true,
                      // because "Derived" has multiple public ctors.
         }
    
         public Derived(int i) {
             super(i); // this "super()" will not be considered useless if option is set to true,
                       // because "Derived" has multiple public ctors.
         }
     }
    
     class NotDerived {
         public NotDerived() {
             super(); // this "super()" will be considered useless regardless of option value,
                      // because "NotDerived" does not extend anything.
         }
         public NotDerived(int i) {
             super(); // this "super()" will be considered useless regardless of option value,
                      // because "NotDerived" does not extend anything.
         }
     }
     
     

    Checkstyle configuration example with options "allowCallToNoArgsSuperCtor" and "allowCallToNoArgsSuperCtorIfMultiplePublicCtor" set to true.

       <module name="UselessSuperCtorCallCheck">
         <property name="allowCallToNoArgsSuperCtor" value="true"/>
         <property name="allowCallToNoArgsSuperCtorIfMultiplePublicCtor" value="true"/>
       </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
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int[] getAcceptableTokens()  
      int[] getDefaultTokens()  
      int[] getRequiredTokens()  
      void setAllowCallToNoArgsSuperCtor​(boolean aAllowCallToNoArgsSuperCtor)
      Sets flag to allowCallToNoArgsSuperCtor.
      void setAllowCallToNoArgsSuperCtorIfMultiplePublicCtor​(boolean aAllowCall)
      Sets flag to allowCallToNoArgsSuperCtorIfMultiplePublicCtor.
      void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST aSuperCallNode)  
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck

        beginTree, 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

      • UselessSuperCtorCallCheck

        public UselessSuperCtorCallCheck()
    • Method Detail

      • setAllowCallToNoArgsSuperCtor

        public void setAllowCallToNoArgsSuperCtor​(boolean aAllowCallToNoArgsSuperCtor)
        Sets flag to allowCallToNoArgsSuperCtor.
        Parameters:
        aAllowCallToNoArgsSuperCtor - if true, check will allow super() calls without arguments
      • setAllowCallToNoArgsSuperCtorIfMultiplePublicCtor

        public void setAllowCallToNoArgsSuperCtorIfMultiplePublicCtor​(boolean aAllowCall)
        Sets flag to allowCallToNoArgsSuperCtorIfMultiplePublicCtor.
        Parameters:
        aAllowCall - if true, check will allow super() calls without arguments if class has multiple public constructors
      • 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
      • visitToken

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