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>
| Modifier and Type | Field and Description |
|---|---|
static String |
MSG_IN_NOT_DERIVED_CLASS
Violation message key.
|
static String |
MSG_WITHOUT_ARGS
Violation message key.
|
| Constructor and Description |
|---|
UselessSuperCtorCallCheck() |
| Modifier and Type | Method and 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) |
beginTree, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeveritypublic static final String MSG_IN_NOT_DERIVED_CLASS
public static final String MSG_WITHOUT_ARGS
public void setAllowCallToNoArgsSuperCtor(boolean aAllowCallToNoArgsSuperCtor)
aAllowCallToNoArgsSuperCtor - if true, check will allow super() calls without argumentspublic void setAllowCallToNoArgsSuperCtorIfMultiplePublicCtor(boolean aAllowCall)
aAllowCall - if true, check will allow super() calls without arguments if class
has multiple public constructorspublic int[] getDefaultTokens()
getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getAcceptableTokens()
getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getRequiredTokens()
getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST aSuperCallNode)
visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheckCopyright © 2021. All rights reserved.