Class ConstructorWithoutParamsCheck
- 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.design.ConstructorWithoutParamsCheck
-
- All Implemented Interfaces:
com.puppycrawl.tools.checkstyle.api.Configurable
,com.puppycrawl.tools.checkstyle.api.Contextualizable
public class ConstructorWithoutParamsCheck extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
This check prohibits usage of parameterless constructors, including the default ones.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
.- Since:
- 1.20.0
- Author:
- Sergey Dudoladov
-
-
Constructor Summary
Constructors Constructor Description ConstructorWithoutParamsCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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)
-
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_KEY
public static final String MSG_KEY
This key points to the warning message in the "messages.properties" file.- See Also:
- Constant Field Values
-
-
Method Detail
-
setClassNameFormat
public void setClassNameFormat(String classNameFormat)
Sets the classNameFormat based on the XML configuration value.- Parameters:
classNameFormat
- the regexp pattern
-
setIgnoredClassNameFormat
public void setIgnoredClassNameFormat(String ignoredClassNameFormat)
Sets the ignoredClassNameFormat based on the XML configuration value.- Parameters:
ignoredClassNameFormat
- the regexp pattern
-
getDefaultTokens
public int[] getDefaultTokens()
- Specified by:
getDefaultTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getRequiredTokens
public int[] getRequiredTokens()
- Specified by:
getRequiredTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getAcceptableTokens
public int[] getAcceptableTokens()
- Specified by:
getAcceptableTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
visitToken
public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
- Overrides:
visitToken
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
-