public class ConstructorWithoutParamsCheck
extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
Rationale: constructors of certain classes must always take arguments to properly instantiate objects. Exception classes are the primary example: their objects must contain enough info to find out why an exception occurred (see "Effective Java", item 63). Constructing an exception without a cause exception or an exception message leaves out such info and thus should be prohibited.
This check prohibits classes which simple names match the RegExp defined in 'classNameFormat' property.
Default configuration:
<module name="ConstructorWithoutParamsCheck">
<property name="classNameFormat" value=".*Exception$"/>
<property name="ignoredClassNameFormat" value="UnsupportedOperationException"/>
</module>
Examples:
// Assume a RegExp in classNameFormat catches example class names
// the check can prohibit default constructors of built-in classes
RuntimeException ex = new RuntimeException(); // violation expected
// the check ignores classes which names match ignoredClassNameFormat
// the default config ignores UnsupportedOperationException
UnsupportedOperationException ex2 = new UnsupportedOperationException(); // no violation expected
// the check allows constructors with empty arguments
RuntimeException ex = new RuntimeException(""); // no violation expected
// the check can prohibit default constructors of user-defined classes
public class Clazz1 {
}
Clazz1 o1 = new Clazz1(); // violation expected
// the check can prohibit user-defined parameterless constructors
public class Clazz2 {
Clazz2() {
foobar();
}
}
Clazz2 o2 = new Clazz2(); // violation expected
For more examples, see InputConstructorWithoutParamsCheck. For discussion, see the sevntu-checkstyle issue 412
.| Modifier and Type | Field and Description |
|---|---|
static String |
MSG_KEY
This key points to the warning message in the "messages.properties" file.
|
| Constructor and Description |
|---|
ConstructorWithoutParamsCheck() |
| Modifier and Type | Method and Description |
|---|---|
int[] |
getAcceptableTokens() |
int[] |
getDefaultTokens() |
int[] |
getRequiredTokens() |
void |
setClassNameFormat(String classNameFormat)
Sets the classNameFormat based on the XML configuration value.
|
void |
setIgnoredClassNameFormat(String ignoredClassNameFormat)
Sets the ignoredClassNameFormat based on the XML configuration value.
|
void |
visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast) |
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_KEY
public void setClassNameFormat(String classNameFormat)
classNameFormat - the regexp patternpublic void setIgnoredClassNameFormat(String ignoredClassNameFormat)
ignoredClassNameFormat - the regexp patternpublic int[] getDefaultTokens()
getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getRequiredTokens()
getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getAcceptableTokens()
getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheckCopyright © 2021. All rights reserved.