Class EmptyPublicCtorInClassCheck
- 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.coding.EmptyPublicCtorInClassCheck
-
- All Implemented Interfaces:
com.puppycrawl.tools.checkstyle.api.Configurable
,com.puppycrawl.tools.checkstyle.api.Contextualizable
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>
- Since:
- 1.13.0
- Author:
- Zuy Alexey
-
-
Constructor Summary
Constructors Constructor Description EmptyPublicCtorInClassCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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)
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
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
Violation message key.- See Also:
- Constant Field Values
-
-
Method Detail
-
setClassAnnotationNames
public void setClassAnnotationNames(String regex)
Sets regex which matches names of class annotations which require class to have public no-argument ctor.- Parameters:
regex
- regex to match annotation names.
-
setCtorAnnotationNames
public void setCtorAnnotationNames(String regex)
Sets regex which matches names of ctor annotations which make empty public ctor essential.- Parameters:
regex
- regex to match annotation names.
-
getDefaultTokens
public int[] getDefaultTokens()
- Specified by:
getDefaultTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getAcceptableTokens
public int[] getAcceptableTokens()
- Specified by:
getAcceptableTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getRequiredTokens
public int[] getRequiredTokens()
- Specified by:
getRequiredTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
beginTree
public void beginTree(com.puppycrawl.tools.checkstyle.api.DetailAST aRootNode)
- Overrides:
beginTree
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
visitToken
public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST node)
- Overrides:
visitToken
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
-