Class OpenjdkMethodParameterAlignmentCheck
java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
com.puppycrawl.tools.checkstyle.api.AbstractCheck
com.puppycrawl.tools.checkstyle.checks.whitespace.OpenjdkMethodParameterAlignmentCheck
- All Implemented Interfaces:
Configurable,Contextualizable
Checks that the parameters of a wrapped method declaration follow one of the two forms
allowed by the OpenJDK style guidelines.
The OpenJDK Java Style Guidelines allow a wrapped method declaration to be formatted either by listing the parameters vertically, one per line, or by breaking the line and indenting the continuation by eight extra spaces. This check validates a wrapped declaration against those two forms:
- when every parameter that starts a new line is indented eight spaces past the declaration, the declaration uses the eight extra spaces form and any number of parameters may share a line;
- otherwise the parameters are expected to be listed vertically, that is, every parameter that starts a new line is aligned with the first parameter and each line holds exactly one parameter.
A declaration whose parameter list fits on a single line is ignored. A wrapped declaration in which no parameter starts a new line, because it is wrapped inside a parameter type rather than between two parameters, is expected to be listed vertically, so a line that holds more than one parameter is reported.
- Since:
- 14.4.0
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intThe number of extra spaces used to indent a wrapped continuation line.static final StringA key is pointing to the warning message text in "messages.properties" file.static final StringA key is pointing to the warning message text in "messages.properties" file. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a newOpenjdkMethodParameterAlignmentCheckinstance. -
Method Summary
Modifier and TypeMethodDescriptionprivate intReturns the column the given token starts in, counting the tabs that precede it on its line as the configured tab width.int[]The configurable token set.int[]Returns the default token a check is interested in.private static intgetLastLineNo(DetailAST ast) Returns the number of the last line the given parameter is declared on.private static DetailASTgetLineStart(DetailAST parameter) Returns the token the line of a parameter that starts a new line begins with.getLineStartColumns(List<DetailAST> parameters) Collects the column the line of every parameter that starts a new line begins in.getParameters(DetailAST parameters) Collects the parameters of the given parameter list.int[]The tokens that this check must be registered for.private static booleanhasMultipleParametersOnLine(List<DetailAST> parameters) Checks whether any parameter is declared on the line the parameter before it ends on.private static booleanisAlignedOn(Collection<Integer> lineStartColumns, int column) Checks whether every parameter that starts a new line begins in the given column.private static booleansharesLineWithPreviousParameter(List<DetailAST> parameters, int index) Checks whether the parameter at the given position begins on the line the parameter before it ends on.private static booleanspansMoreThanOneLine(SequencedCollection<DetailAST> parameters) Checks whether the parameters of a declaration are spread over more than one line.voidvisitToken(DetailAST ast) Called to process a token.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
-
Field Details
-
MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_WRAP
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
EIGHT_SPACES
The number of extra spaces used to indent a wrapped continuation line.- See Also:
-
-
Constructor Details
-
OpenjdkMethodParameterAlignmentCheck
public OpenjdkMethodParameterAlignmentCheck()Creates a newOpenjdkMethodParameterAlignmentCheckinstance.
-
-
Method Details
-
getDefaultTokens
Description copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokensin classAbstractCheck- Returns:
- the default tokens
- See Also:
-
getAcceptableTokens
Description copied from class:AbstractCheckThe configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Specified by:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
-
getRequiredTokens
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
getParameters
Collects the parameters of the given parameter list.- Parameters:
parameters- thePARAMETERSnode of a method declaration- Returns:
- every parameter, in declaration order
-
spansMoreThanOneLine
Checks whether the parameters of a declaration are spread over more than one line. A declaration can be wrapped without any parameter starting a new line, when it is wrapped inside a parameter type rather than between two parameters. Parameters are listed in source order, so a parameter that starts a new line always leaves the last parameter on a later line than the first one.- Parameters:
parameters- every parameter of a method declaration- Returns:
trueif the parameters do not all start on the same line
-
getLineStartColumns
Collects the column the line of every parameter that starts a new line begins in. The first parameter of a declaration is the column the other parameters are compared against, so it is never collected.- Parameters:
parameters- every parameter of a method declaration- Returns:
- the column every parameter that starts a new line is expected to be aligned in
-
hasMultipleParametersOnLine
Checks whether any parameter is declared on the line the parameter before it ends on. The comma that separates two parameters is not taken into account, as a parameter list can be wrapped before that comma instead of after it.- Parameters:
parameters- every parameter of a method declaration- Returns:
trueif a line holds more than one parameter
-
getLastLineNo
Returns the number of the last line the given parameter is declared on.- Parameters:
ast- the parameter to measure- Returns:
- the greatest line number of the tokens of the parameter
-
getLineStart
Returns the token the line of a parameter that starts a new line begins with. A parameter list can be wrapped before the comma that separates two parameters instead of after it, which leaves that comma as the first token of the line, so such a line begins at the comma rather than at the parameter. The comma that separates a parameter from the parameter before it is the sibling preceding it, so a parameter that does not start a new line is never passed here.- Parameters:
parameter- the parameter that starts a new line- Returns:
- the comma preceding the parameter when both are on one line, the first token of the parameter otherwise
-
isAlignedOn
Checks whether every parameter that starts a new line begins in the given column. A declaration whose parameters all start on one line has no parameter that starts a new line, so it is aligned on every column and is never reported.- Parameters:
lineStartColumns- the column of every parameter that starts a new linecolumn- the column the parameters are expected to begin in- Returns:
trueif every parameter that starts a new line begins in that column
-
expandedColumnNo
Returns the column the given token starts in, counting the tabs that precede it on its line as the configured tab width. Alignment is what the reader sees, so a declaration indented with tabs has to be measured the same way an editor renders it.- Parameters:
ast- the token to locate- Returns:
- the column of the token, with preceding tabs expanded
-