Class UselessSuperCtorCallCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.github.sevntu.checkstyle.checks.coding.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(); } }
Case 2. no-argument "super()" is called without parameters from class ctor if class is derived, for example:
class Derived extends Base { Derived() { super(); } }
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
-
-
Field Summary
Fields Modifier and Type Field Description static String
MSG_IN_NOT_DERIVED_CLASS
Violation message key.static String
MSG_WITHOUT_ARGS
Violation message key.
-
Constructor Summary
Constructors Constructor Description UselessSuperCtorCallCheck()
-
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
-
-
-
-
Field Detail
-
MSG_IN_NOT_DERIVED_CLASS
public static final String MSG_IN_NOT_DERIVED_CLASS
Violation message key.- See Also:
- Constant Field Values
-
MSG_WITHOUT_ARGS
public static final String MSG_WITHOUT_ARGS
Violation message key.- See Also:
- Constant Field Values
-
-
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 classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getAcceptableTokens
public int[] getAcceptableTokens()
- Specified by:
getAcceptableTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getRequiredTokens
public int[] getRequiredTokens()
- Specified by:
getRequiredTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
visitToken
public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST aSuperCallNode)
- Overrides:
visitToken
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
-