Class ClassHeaderWrapOpenjdkCheck
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.indentation.ClassHeaderWrapOpenjdkCheck
- All Implemented Interfaces:
Configurable,Contextualizable
Checks that a wrapped class, interface, enum, or record header is wrapped according to the
OpenJDK Java Style Guide.
This check only applies when the type declaration header, from the declaration
keyword through the end of its extends/implements clauses (or its
name/type parameters/record components when neither clause is present), spans more
than one line. A header whose content is entirely on one line is not in scope, even
if its opening curly brace is pushed onto its own following line; that is a brace
placement choice for other checks (such as LeftCurly) to enforce, not a
wrapping concern this check is about.
The two rules enforced are:
- Each of the
extendsandimplementsclauses, when present, must stand out on its own new line once the header has started wrapping, or once that clause's own list of supertypes wraps across lines. Wrapping that happens only inside a single supertype's type arguments (the same way method arguments may wrap) does not require the clause keyword itself to move to a new line. - Only checked when rule 1 above found nothing to report: the header should not have been wrapped in the first place if, joined back onto one line, it would still fit within 80 columns.
- 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 maximum number of columns a class header may occupy before wrapping it is considered necessary.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 newClassHeaderWrapOpenjdkCheckinstance. -
Method Summary
Modifier and TypeMethodDescriptionprivate booleancheckClausePlacement(DetailAST clause) Checks that the givenextends/implementsclause stands out on its own new line, if required.private voidcheckNecessaryWrap(DetailAST headerStart, DetailAST lcurly) Checks whether the header, reconstructed as a single line, would still have fit within the maximum column limit, in which case wrapping it was unnecessary.private static intfindFromIndex(DetailAST headerStart, int lineNo) Finds the index at which the given line's relevant content starts: the header's own starting column on the header's first line, or the start of the line otherwise.private static DetailASTfindHeaderStart(DetailAST ast) Finds the token that marks the true start of the header on the declaration's own line: the first modifier that already shares a line with the declaration keyword (class/interface/enum/record), skipping past any annotations placed on their own preceding line, or the declaration keyword itself if there is no such modifier.private static intfindToIndex(DetailAST lcurly, int lineNo, int lineLength) Finds the index at which the given line's relevant content ends: just past the opening curly brace on the line it occupies, or the end of the line otherwise.int[]The configurable token set.int[]Returns the default token a check is interested in.private static intgetEndLineNo(DetailAST ast) Returns the line number of the last token in the given subtree.int[]The tokens that this check must be registered for.private booleanisClauseMisplaced(DetailAST clause) Checks the given clause's placement if it is present.private static booleanisPrecededByWrappedClause(DetailAST previous) Checks whether the given node is anEXTENDS_CLAUSEthat already stands out on its own new line, meaning a followingimplementsclause must match it.private static booleanisTopLevelWrapped(DetailAST clause) Checks whether the given node's direct children span more than one source line, ignoring further wrapping nested inside any of those children (e.g. inside a supertype's own type arguments).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_NOT_ON_NEW_LINE
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_KEY_UNNECESSARY_WRAP
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MAX_COLUMN_LIMIT
The maximum number of columns a class header may occupy before wrapping it is considered necessary.- See Also:
-
-
Constructor Details
-
ClassHeaderWrapOpenjdkCheck
public ClassHeaderWrapOpenjdkCheck()Creates a newClassHeaderWrapOpenjdkCheckinstance.
-
-
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
-
isClauseMisplaced
Checks the given clause's placement if it is present.- Parameters:
clause- theEXTENDS_CLAUSEorIMPLEMENTS_CLAUSEto check, ornullif the type declaration has none- Returns:
trueif a violation was logged for this clause
-
findHeaderStart
Finds the token that marks the true start of the header on the declaration's own line: the first modifier that already shares a line with the declaration keyword (class/interface/enum/record), skipping past any annotations placed on their own preceding line, or the declaration keyword itself if there is no such modifier.- Parameters:
ast- the class, interface, enum, or record declaration- Returns:
- the token that starts the header
-
checkClausePlacement
Checks that the givenextends/implementsclause stands out on its own new line, if required. It is required once a precedingextendsclause has already achieved its own new line (soimplementsmust match it), or once this clause's own list of supertypes wraps across multiple lines. Wrapping in whatever precedes the clause for some other reason, such as a wrapped type parameter or record component list, does not by itself require the clause to move.- Parameters:
clause- theEXTENDS_CLAUSEorIMPLEMENTS_CLAUSEto check- Returns:
trueif a violation was logged for this clause
-
isPrecededByWrappedClause
Checks whether the given node is anEXTENDS_CLAUSEthat already stands out on its own new line, meaning a followingimplementsclause must match it.- Parameters:
previous- the node immediately preceding the clause under check- Returns:
trueifpreviousis anEXTENDS_CLAUSEthat does not share a line with whatever precedes it
-
checkNecessaryWrap
Checks whether the header, reconstructed as a single line, would still have fit within the maximum column limit, in which case wrapping it was unnecessary.- Parameters:
headerStart- the declaration keyword (class/interface/enum/record) that starts the headerlcurly- the opening brace of the type's body
-
findFromIndex
Finds the index at which the given line's relevant content starts: the header's own starting column on the header's first line, or the start of the line otherwise.- Parameters:
headerStart- the token that starts the headerlineNo- the 1-indexed line number currently being processed- Returns:
- the index at which to start extracting content from this line
-
findToIndex
Finds the index at which the given line's relevant content ends: just past the opening curly brace on the line it occupies, or the end of the line otherwise.- Parameters:
lcurly- the opening brace of the type's bodylineNo- the 1-indexed line number currently being processedlineLength- the length of the line currently being processed- Returns:
- the index at which to stop extracting content from this line
-
isTopLevelWrapped
Checks whether the given node's direct children span more than one source line, ignoring further wrapping nested inside any of those children (e.g. inside a supertype's own type arguments).- Parameters:
clause- theEXTENDS_CLAUSEorIMPLEMENTS_CLAUSEto inspect- Returns:
trueif two direct children ofclausestart on different lines
-
getEndLineNo
Returns the line number of the last token in the given subtree.- Parameters:
ast- the root of the subtree to inspect- Returns:
- the 1-indexed line number of the deepest last child of
ast
-