public class EmptyPublicCtorInClassCheck
extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
This Check looks for useless empty public constructors. Class constructor is considered useless by this Check if and only if class has exactly one ctor, which is public, empty(one that has no statements) and default.
Example 1. Check will generate violation for this code:
class Dummy {
public Dummy() {
}
}
Example 2. Check will not generate violation for this code:
class Dummy {
private Dummy() {
}
}
class Dummy has only one ctor, which is not public.
Example 3. Check will not generate violation for this code:
class Dummy {
public Dummy() {
}
public Dummy(int i) {
}
}
class Dummy has multiple ctors.
Check has two properties:
"classAnnotationNames" - This property contains regex for canonical names of class annotations, which require class to have empty public ctor. Check will not log violations for classes marked with annotations that match this regex. Default option value is "javax\.persistence\.Entity".
"ctorAnnotationNames" - This property contains regex for canonical names of ctor annotations, which make empty public ctor essential. Check will not log violations for ctors marked with annotations that match this regex. Default option value is "com\.google\.inject\.Inject".
Following configuration will adjust Check to skip classes which annotated with "javax.persistence.Entity" and classes which has empty public ctor with "com\.google\.inject\.Inject".
<module name="EmptyPublicCtorInClassCheck">
<property name="classAnnotationNames" value="javax\.persistence\.Entity"/>
<property name="ctorAnnotationNames" value="com\.google\.inject\.Inject"/>
</module>
| Modifier and Type | Field and Description |
|---|---|
static String |
MSG_KEY
Violation message key.
|
| Constructor and Description |
|---|
EmptyPublicCtorInClassCheck() |
| Modifier and Type | Method and Description |
|---|---|
void |
beginTree(com.puppycrawl.tools.checkstyle.api.DetailAST aRootNode) |
int[] |
getAcceptableTokens() |
int[] |
getDefaultTokens() |
int[] |
getRequiredTokens() |
void |
setClassAnnotationNames(String regex)
Sets regex which matches names of class annotations which require class to have public
no-argument ctor.
|
void |
setCtorAnnotationNames(String regex)
Sets regex which matches names of ctor annotations which make empty public ctor essential.
|
void |
visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST node) |
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 setClassAnnotationNames(String regex)
regex - regex to match annotation names.public void setCtorAnnotationNames(String regex)
regex - regex to match annotation names.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 aRootNode)
beginTree in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST node)
visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheckCopyright © 2021. All rights reserved.