public class OverridableMethodInConstructorCheck
extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
This check prevents any calls to overridable methods that are take place in:
public class Example {
public static void main(String[] args) {
abstract class Base {
Base() { overrideMe(); }
abstract void overrideMe();
}
class Child extends Base {
final int x;
Child(int x) { this.x = x; }
void overrideMe() {
System.out.println(x);
}
}
new Child(42); // prints "0"
}
}
Here, when Base constructor calls overrideMe, Child has not finished initializing the final int x, and the method gets the wrong value. This will almost certainly lead to bugs and errors.
Notes:
This check doesn`t handle the situation when there
is a call to an overloaded method(s).
Here`s an example:
public class Test {
public static void main(String[] args) {
class Base {
Base() {
System.out.println("Base C-tor ");
overrideMe("Foo!"); // no warnings here, because the method
// named "overrideMe" is overloaded.
}
void overrideMe() { }
void overrideMe(String str) {
System.out.println("Base overrideMe(String str) ");
}
}
class Child extends Base {
final int x;
Child(int x) {
this.x = x;
}
void overrideMe(String str) {
System.out.println("Child`s overrideMe(): " + x);
}
}
new Child(999);
}
}
Some specific method call types that aren`t supported by check:
| Modifier and Type | Field and Description |
|---|---|
static String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
static String |
MSG_KEY_LEADS
A key is pointing to the warning message text in "messages.properties"
file.
|
| Constructor and Description |
|---|
OverridableMethodInConstructorCheck() |
| Modifier and Type | Method and Description |
|---|---|
void |
beginTree(com.puppycrawl.tools.checkstyle.api.DetailAST rootAST) |
int[] |
getAcceptableTokens() |
int[] |
getDefaultTokens() |
int[] |
getRequiredTokens() |
void |
setCheckCloneMethod(boolean value)
Enable|Disable searching of calls to overridable methods from body of any
clone() method is implemented from Cloneable interface.
|
void |
setCheckReadObjectMethod(boolean value)
Enable|Disable searching of calls to overridable methods from body of any
readObject() method is implemented from Serializable interface.
|
void |
setMatchMethodsByArgCount(boolean value)
Enable|Disable matching methods by arguments count.
|
void |
visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST detailAST) |
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_KEY
public static final String MSG_KEY_LEADS
public void setCheckCloneMethod(boolean value)
value - The state of a boolean check box that enables the searching of
calls to overridable methods from body of any clone() method
is implemented from Cloneable interface.public void setMatchMethodsByArgCount(boolean value)
value - The state of a boolean check box that enables the matching of
methods by arguments count.public void setCheckReadObjectMethod(boolean value)
value - The state of a boolean check box that enables the searching of
calls to overridable methods from body of any readObject()
method is implemented from Serializable interface.public 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 beginTree(com.puppycrawl.tools.checkstyle.api.DetailAST rootAST)
beginTree in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST detailAST)
visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheckCopyright © 2021. All rights reserved.