public final class AnnotationUseStyleCheck extends AbstractCheck
Annotations have three element styles starting with the least verbose.
To not enforce an element style aIGNORE
type is provided. The desired style
can be set through the elementStyle
property.
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
ALWAYS
type. To never have ending parenthesis
use the NEVER
type. To not enforce a
closing parenthesis preference a 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 ALWAYS
type. To never have a trailing comma use the
NEVER
type. To not enforce a trailing
array comma preference a IGNORE
type
is provided. Set this through the trailingArrayComma
property.
By default the ElementStyle is set to COMPACT_NO_ARRAY, the TrailingArrayComma is set to NEVER, and the ClosingParens 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.
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>
Modifier and Type | Class and Description |
---|---|
static class |
AnnotationUseStyleCheck.ClosingParens
Defines the two styles for defining
elements in an annotation.
|
static class |
AnnotationUseStyleCheck.ElementStyle
Defines the styles for defining elements in an annotation.
|
static class |
AnnotationUseStyleCheck.TrailingArrayComma
Defines the two styles for defining
elements in an annotation.
|
AutomaticBean.OutputStreamOptions
Modifier and Type | Field and Description |
---|---|
static String |
MSG_KEY_ANNOTATION_INCORRECT_STYLE
A key is pointing to the warning message text in "messages.properties"
file.
|
static String |
MSG_KEY_ANNOTATION_PARENS_MISSING
A key is pointing to the warning message text in "messages.properties"
file.
|
static String |
MSG_KEY_ANNOTATION_PARENS_PRESENT
A key is pointing to the warning message text in "messages.properties"
file.
|
static String |
MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING
A key is pointing to the warning message text in "messages.properties"
file.
|
static String |
MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT
A key is pointing to the warning message text in "messages.properties"
file.
|
Constructor and Description |
---|
AnnotationUseStyleCheck() |
Modifier and Type | Method and 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(String parens)
Sets the ClosingParens from a string.
|
void |
setElementStyle(String style)
Sets the ElementStyle from a string.
|
void |
setTrailingArrayComma(String comma)
Sets the TrailingArrayComma from a string.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, clearMessages, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setClassLoader, setFileContents, setTabWidth, setTokens
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
configure, contextualize, getConfiguration, setupChild
public static final String MSG_KEY_ANNOTATION_INCORRECT_STYLE
public static final String MSG_KEY_ANNOTATION_PARENS_MISSING
public static final String MSG_KEY_ANNOTATION_PARENS_PRESENT
public static final String MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING
public static final String MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT
public AnnotationUseStyleCheck()
public void setElementStyle(String style)
style
- string representationorg.apache.commons.beanutils.ConversionException
- if cannot convert string.public void setTrailingArrayComma(String comma)
comma
- string representationorg.apache.commons.beanutils.ConversionException
- if cannot convert string.public void setClosingParens(String parens)
parens
- string representationorg.apache.commons.beanutils.ConversionException
- if cannot convert string.public int[] getDefaultTokens()
AbstractCheck
getDefaultTokens
in class AbstractCheck
TokenTypes
public int[] getRequiredTokens()
AbstractCheck
getRequiredTokens
in class AbstractCheck
TokenTypes
public int[] getAcceptableTokens()
AbstractCheck
getAcceptableTokens
in class AbstractCheck
TokenTypes
public void visitToken(DetailAST ast)
AbstractCheck
visitToken
in class AbstractCheck
ast
- the token to processCopyright © 2001–2018. All rights reserved.