Class AnnotationUseStyleCheck
- All Implemented Interfaces:
Configurable,Contextualizable
Annotations have three element styles starting with the least verbose.
-
ElementStyleOption.COMPACT_NO_ARRAY -
ElementStyleOption.COMPACT -
ElementStyleOption.EXPANDED
To not enforce an element style a ElementStyleOption.IGNORE type is provided.
The desired style can be set through the elementStyle property.
Using the ElementStyleOption.EXPANDED style is more verbose.
The expanded version is sometimes referred to as "named parameters" in other languages.
Using the ElementStyleOption.COMPACT style is less verbose.
This style can only be used when there is an element called 'value' which is either
the sole element or all other elements have default values.
Using the ElementStyleOption.COMPACT_NO_ARRAY style is less verbose.
It is similar to the ElementStyleOption.COMPACT style but single value arrays are
flagged.
With annotations a single value array does not need to be placed in an array initializer.
The ending parenthesis are optional when using annotations with no elements.
To always require ending parenthesis use the ClosingParensOption.ALWAYS type.
To never have ending parenthesis use the ClosingParensOption.NEVER type.
To not enforce a closing parenthesis preference a ClosingParensOption.IGNORE type is
provided.
Set this through the closingParens property.
Annotations also allow you to specify arrays of elements in a standard format.
As with normal arrays, a trailing comma is optional.
To always require a trailing comma use the TrailingArrayCommaOption.ALWAYS type.
To never have a trailing comma use the TrailingArrayCommaOption.NEVER type.
To not enforce a trailing array comma preference a TrailingArrayCommaOption.IGNORE type
is provided. Set this through the trailingArrayComma property.
By default, the ElementStyleOption is set to COMPACT_NO_ARRAY,
the TrailingArrayCommaOption is set to NEVER,
and the ClosingParensOption is set to NEVER.
According to the JLS, it is legal to include a trailing comma in arrays used in annotations but Sun's Java 5 & 6 compilers will not compile with this syntax. This may in be a bug in Sun's compilers since eclipse 3.4's built-in compiler does allow this syntax as defined in the JLS. Note: this was tested with compilers included with JDK versions 1.5.0.17 and 1.6.0.11 and the compiler included with eclipse 3.4.1.
- Since:
- 5.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumDefines the two styles for defining elements in an annotation.static enumDefines the styles for defining elements in an annotation.static enumDefines the two styles for defining elements in an annotation.Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final StringThe element name used to receive special linguistic support for annotation use.Define the policy for ending parenthesis.Define the annotation element styles.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.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.static final StringA key is pointing to the warning message text in "messages.properties" file.Define the policy for trailing comma in arrays. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidChecks to see if the closing parenthesis are present if required or prohibited.private voidcheckCompactNoArrayStyle(DetailAST annotation) Checks for compact no array style type violations.private voidcheckCompactStyle(DetailAST annotation) Checks for compact style type violations.private voidcheckExpandedStyle(DetailAST annotation) Checks for expanded style type violations.private voidcheckStyleType(DetailAST annotation) Checks to see if theAnnotationElementStyleOptionis correct.private voidcheckTrailingComma(DetailAST annotation) Checks to see if the trailing comma is present if required or prohibited.int[]The configurable token set.int[]Returns the default token a check is interested in.private static <T extends Enum<T>>
TRetrieves anEnumtype from a @{link String String}.int[]The tokens that this check must be registered for.private static booleanhasArguments(DetailAST annotation) Checks that annotation has arguments.private voidLogs a trailing array comma violation if one exists.voidsetClosingParens(String parens) Setter to define the policy for ending parenthesis.voidsetElementStyle(String style) Setter to define the annotation element styles.voidsetTrailingArrayComma(String comma) Setter to define the policy for trailing comma in arrays.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_ANNOTATION_INCORRECT_STYLE
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_KEY_ANNOTATION_PARENS_MISSING
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_KEY_ANNOTATION_PARENS_PRESENT
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
ANNOTATION_ELEMENT_SINGLE_NAME
The element name used to receive special linguistic support for annotation use.- See Also:
-
elementStyle
Define the annotation element styles. -
trailingArrayComma
Define the policy for trailing comma in arrays. -
closingParens
Define the policy for ending parenthesis.
-
-
Constructor Details
-
AnnotationUseStyleCheck
public AnnotationUseStyleCheck()
-
-
Method Details
-
setElementStyle
Setter to define the annotation element styles.- Parameters:
style- string representation- Since:
- 5.0
-
setTrailingArrayComma
Setter to define the policy for trailing comma in arrays.- Parameters:
comma- string representation- Since:
- 5.0
-
setClosingParens
Setter to define the policy for ending parenthesis.- Parameters:
parens- string representation- Since:
- 5.0
-
getOption
Retrieves anEnumtype from a @{link String String}.- Type Parameters:
T- the enum type- Parameters:
enumClass- the enum classvalue- the string representing the enum- Returns:
- the enum type
- Throws:
IllegalArgumentException- when unable to parse value
-
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:
-
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:
-
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:
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
checkStyleType
Checks to see if theAnnotationElementStyleOptionis correct.- Parameters:
annotation- the annotation token
-
checkExpandedStyle
Checks for expanded style type violations.- Parameters:
annotation- the annotation token
-
hasArguments
Checks that annotation has arguments.- Parameters:
annotation- to check- Returns:
- true if annotation has arguments, false otherwise
-
checkCompactStyle
Checks for compact style type violations.- Parameters:
annotation- the annotation token
-
checkCompactNoArrayStyle
Checks for compact no array style type violations.- Parameters:
annotation- the annotation token
-
checkTrailingComma
Checks to see if the trailing comma is present if required or prohibited.- Parameters:
annotation- the annotation token
-
logCommaViolation
Logs a trailing array comma violation if one exists.- Parameters:
ast- the array initANNOTATION_ARRAY_INIT.
-
checkCheckClosingParensOption
Checks to see if the closing parenthesis are present if required or prohibited.- Parameters:
ast- the annotation token
-