Class AnnotationUseStyleCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck
-
- All Implemented Interfaces:
Configurable
,Contextualizable
public final class AnnotationUseStyleCheck extends AbstractCheck
Checks the style of elements in annotations.
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 theelementStyle
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 theElementStyleOption.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 theClosingParensOption.NEVER
type. To not enforce a closing parenthesis preference aClosingParensOption.IGNORE
type is provided. Set this through theclosingParens
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 theTrailingArrayCommaOption.NEVER
type. To not enforce a trailing array comma preference aTrailingArrayCommaOption.IGNORE
type is provided. Set this through thetrailingArrayComma
property.By default the
ElementStyleOption
is set toCOMPACT_NO_ARRAY
, theTrailingArrayCommaOption
is set toNEVER
, and theClosingParensOption
is set toNEVER
.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.
See Java Language specification, §9.7.
-
Property
elementStyle
- Define the annotation element styles. Type iscom.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck$ElementStyleOption
. Default value iscompact_no_array
. -
Property
closingParens
- Define the policy for ending parenthesis. Type iscom.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck$ClosingParensOption
. Default value isnever
. -
Property
trailingArrayComma
- Define the policy for trailing comma in arrays. Type iscom.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck$TrailingArrayCommaOption
. Default value isnever
.
To configure the check:
<module name="AnnotationUseStyle"/>
To configure the check to enforce an
expanded
style, with a trailing array comma set tonever
and always including the closing parenthesis.<module name="AnnotationUseStyle"> <property name="elementStyle" value="expanded"/> <property name="trailingArrayComma" value="never"/> <property name="closingParens" value="always"/> </module>
Parent is
com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
-
annotation.incorrect.style
-
annotation.parens.missing
-
annotation.parens.present
-
annotation.trailing.comma.missing
-
annotation.trailing.comma.present
- Since:
- 5.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
AnnotationUseStyleCheck.ClosingParensOption
Defines the two styles for defining elements in an annotation.static class
AnnotationUseStyleCheck.ElementStyleOption
Defines the styles for defining elements in an annotation.static class
AnnotationUseStyleCheck.TrailingArrayCommaOption
Defines the two styles for defining elements in an annotation.-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
MSG_KEY_ANNOTATION_INCORRECT_STYLE
A key is pointing to the warning message text in "messages.properties" file.static java.lang.String
MSG_KEY_ANNOTATION_PARENS_MISSING
A key is pointing to the warning message text in "messages.properties" file.static java.lang.String
MSG_KEY_ANNOTATION_PARENS_PRESENT
A key is pointing to the warning message text in "messages.properties" file.static java.lang.String
MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING
A key is pointing to the warning message text in "messages.properties" file.static java.lang.String
MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT
A key is pointing to the warning message text in "messages.properties" file.
-
Constructor Summary
Constructors Constructor Description AnnotationUseStyleCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int[]
getAcceptableTokens()
The configurable token set.int[]
getDefaultTokens()
Returns the default token a check is interested in.int[]
getRequiredTokens()
The tokens that this check must be registered for.void
setClosingParens(java.lang.String parens)
Setter to define the policy for ending parenthesis.void
setElementStyle(java.lang.String style)
Setter to define the annotation element styles.void
setTrailingArrayComma(java.lang.String comma)
Setter to define the policy for trailing comma in arrays.void
visitToken(DetailAST ast)
Called to process a token.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, 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
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_KEY_ANNOTATION_INCORRECT_STYLE
public static final java.lang.String MSG_KEY_ANNOTATION_INCORRECT_STYLE
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_KEY_ANNOTATION_PARENS_MISSING
public static final java.lang.String MSG_KEY_ANNOTATION_PARENS_MISSING
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_KEY_ANNOTATION_PARENS_PRESENT
public static final java.lang.String MSG_KEY_ANNOTATION_PARENS_PRESENT
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING
public static final java.lang.String MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT
public static final java.lang.String MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
-
Method Detail
-
setElementStyle
public void setElementStyle(java.lang.String style)
Setter to define the annotation element styles.- Parameters:
style
- string representation
-
setTrailingArrayComma
public void setTrailingArrayComma(java.lang.String comma)
Setter to define the policy for trailing comma in arrays.- Parameters:
comma
- string representation
-
setClosingParens
public void setClosingParens(java.lang.String parens)
Setter to define the policy for ending parenthesis.- Parameters:
parens
- string representation
-
getDefaultTokens
public int[] getDefaultTokens()
Description copied from class:AbstractCheck
Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokens
in classAbstractCheck
- Returns:
- the default tokens
- See Also:
TokenTypes
-
getRequiredTokens
public int[] getRequiredTokens()
Description copied from class:AbstractCheck
The tokens that this check must be registered for.- Specified by:
getRequiredTokens
in classAbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
getAcceptableTokens
public int[] getAcceptableTokens()
Description copied from class:AbstractCheck
The 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:
getAcceptableTokens
in classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in classAbstractCheck
- Parameters:
ast
- the token to process
-
-