Class ConstructorsCodeFreeCheck
- All Implemented Interfaces:
com.puppycrawl.tools.checkstyle.api.Configurable,com.puppycrawl.tools.checkstyle.api.Contextualizable
A constructor must only assign fields from constructor parameters
or from newly created objects, and may delegate to another constructor
via this(...) or super(...). Calling any method
(static or instance) from inside a constructor is forbidden,
including as the right-hand side of a field assignment
(e.g. this.bar = Foo.createBar()) or as a nested argument
to a new expression.
A constructor whose only statement is a delegating
this(...) or super(...) call is exempt: such a
constructor performs no work of its own, and the method-call
arguments to the delegate (e.g. this(System.currentTimeMillis()))
are accepted as a sanctioned factory-style entry point.
Method calls nested inside lambda bodies or anonymous class bodies are not considered constructor code, because they are not executed at construction time: only the lambda object or the anonymous class instance is created. Such subtrees are skipped.
Defensive array copy idioms — Arrays.copyOf(...) and
<expr>.clone() — are also tolerated, since there is no
method-call-free way to defensively copy an array field at
construction time (Effective Java item 50).
- Since:
- 0.24
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean.OutputStreamOptions -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint[]int[]int[]voidvisitToken(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, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Constructor Details
-
ConstructorsCodeFreeCheck
public ConstructorsCodeFreeCheck()Default constructor.
-
-
Method Details
-
getDefaultTokens
public int[] getDefaultTokens()- Specified by:
getDefaultTokensin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getAcceptableTokens
public int[] getAcceptableTokens()- Specified by:
getAcceptableTokensin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getRequiredTokens
public int[] getRequiredTokens()- Specified by:
getRequiredTokensin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
visitToken
public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast) - Overrides:
visitTokenin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-