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
This check controls the style with the usage of annotations.Annotations have three element styles starting with the least verbose.
To not enforce an element style aIGNOREtype is provided. The desired style can be set through theelementStyleproperty.Using the EXPANDED style is more verbose. The expanded version is sometimes referred to as "named parameters" in other languages.
Using the 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 COMPACT_NO_ARRAY style is less verbose. It is similar to the COMPACT style but single value arrays are flagged. With annotations a single value array does not need to be placed in an array initializer. 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.
The ending parenthesis are optional when using annotations with no elements. To always require ending parenthesis use the
ALWAYStype. To never have ending parenthesis use theNEVERtype. To not enforce a closing parenthesis preference aIGNOREtype is provided. Set this through theclosingParensproperty.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
ALWAYStype. To never have a trailing comma use theNEVERtype. To not enforce a trailing array comma preference aIGNOREtype is provided. Set this through thetrailingArrayCommaproperty.By default the ElementStyle is set to EXPANDED, the TrailingArrayComma is set to NEVER, and the ClosingParens is set to ALWAYS.
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.
An example shown below is set to enforce an EXPANDED style, with a trailing array comma set to NEVER 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>
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAnnotationUseStyleCheck.ClosingParensDefines the two styles for defining elements in an annotation.static classAnnotationUseStyleCheck.ElementStyleDefines the styles for defining elements in an annotation.static classAnnotationUseStyleCheck.TrailingArrayCommaDefines the two styles for defining elements in an annotation.
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringANNOTATION_ELEMENT_SINGLE_NAMEThe element name used to receive special linguistic support for annotation use.private AnnotationUseStyleCheck.ClosingParensclosingParensClosing parens option.private AnnotationUseStyleCheck.ElementStyleelementStyleElementStyle option.static java.lang.StringMSG_KEY_ANNOTATION_INCORRECT_STYLEA key is pointing to the warning message text in "messages.properties" file.static java.lang.StringMSG_KEY_ANNOTATION_PARENS_MISSINGA key is pointing to the warning message text in "messages.properties" file.static java.lang.StringMSG_KEY_ANNOTATION_PARENS_PRESENTA key is pointing to the warning message text in "messages.properties" file.static java.lang.StringMSG_KEY_ANNOTATION_TRAILING_COMMA_MISSINGA key is pointing to the warning message text in "messages.properties" file.static java.lang.StringMSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENTA key is pointing to the warning message text in "messages.properties" file.private AnnotationUseStyleCheck.TrailingArrayCommatrailingArrayCommaTrailing array comma option.
-
Constructor Summary
Constructors Constructor Description AnnotationUseStyleCheck()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidcheckCheckClosingParens(DetailAST ast)Checks 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 theAnnotationElementStyleis correct.private voidcheckTrailingComma(DetailAST annotation)Checks to see if the trailing comma is present if required or prohibited.int[]getAcceptableTokens()The configurable token set.int[]getDefaultTokens()Returns the default token a check is interested in.private static <T extends java.lang.Enum<T>>
TgetOption(java.lang.Class<T> enumClass, java.lang.String value)Retrieves anEnumtype from a @{link String String}.int[]getRequiredTokens()The tokens that this check must be registered for.private voidlogCommaViolation(DetailAST ast)Logs a trailing array comma violation if one exists.voidsetClosingParens(java.lang.String parens)Sets the ClosingParens from a string.voidsetElementStyle(java.lang.String style)Sets the ElementStyle from a string.voidsetTrailingArrayComma(java.lang.String comma)Sets the TrailingArrayComma from a string.voidvisitToken(DetailAST ast)Called to process a token.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, finishLocalSetup, 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
-
ANNOTATION_ELEMENT_SINGLE_NAME
private static final java.lang.String ANNOTATION_ELEMENT_SINGLE_NAME
The element name used to receive special linguistic support for annotation use.- See Also:
- Constant Field Values
-
elementStyle
private AnnotationUseStyleCheck.ElementStyle elementStyle
ElementStyle option.- See Also:
setElementStyle(String)
-
trailingArrayComma
private AnnotationUseStyleCheck.TrailingArrayComma trailingArrayComma
Trailing array comma option.- See Also:
setTrailingArrayComma(String)
-
closingParens
private AnnotationUseStyleCheck.ClosingParens closingParens
Closing parens option.- See Also:
setClosingParens(String)
-
-
Method Detail
-
setElementStyle
public void setElementStyle(java.lang.String style)
Sets the ElementStyle from a string.- Parameters:
style- string representation- Throws:
org.apache.commons.beanutils.ConversionException- if cannot convert string.
-
setTrailingArrayComma
public void setTrailingArrayComma(java.lang.String comma)
Sets the TrailingArrayComma from a string.- Parameters:
comma- string representation- Throws:
org.apache.commons.beanutils.ConversionException- if cannot convert string.
-
setClosingParens
public void setClosingParens(java.lang.String parens)
Sets the ClosingParens from a string.- Parameters:
parens- string representation- Throws:
org.apache.commons.beanutils.ConversionException- if cannot convert string.
-
getOption
private static <T extends java.lang.Enum<T>> T getOption(java.lang.Class<T> enumClass, java.lang.String value)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
-
getDefaultTokens
public int[] 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:
TokenTypes
-
getRequiredTokens
public int[] 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:
TokenTypes
-
getAcceptableTokens
public int[] 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:
TokenTypes
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
checkStyleType
private void checkStyleType(DetailAST annotation)
Checks to see if theAnnotationElementStyleis correct.- Parameters:
annotation- the annotation token
-
checkExpandedStyle
private void checkExpandedStyle(DetailAST annotation)
Checks for expanded style type violations.- Parameters:
annotation- the annotation token
-
checkCompactStyle
private void checkCompactStyle(DetailAST annotation)
Checks for compact style type violations.- Parameters:
annotation- the annotation token
-
checkCompactNoArrayStyle
private void checkCompactNoArrayStyle(DetailAST annotation)
Checks for compact no array style type violations.- Parameters:
annotation- the annotation token
-
checkTrailingComma
private void checkTrailingComma(DetailAST annotation)
Checks to see if the trailing comma is present if required or prohibited.- Parameters:
annotation- the annotation token
-
logCommaViolation
private void logCommaViolation(DetailAST ast)
Logs a trailing array comma violation if one exists.- Parameters:
ast- the array initANNOTATION_ARRAY_INIT.
-
checkCheckClosingParens
private void checkCheckClosingParens(DetailAST ast)
Checks to see if the closing parenthesis are present if required or prohibited.- Parameters:
ast- the annotation token
-
-