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 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.
See Java Language specification, §9.7.
elementStyle - Define the annotation element styles.
Type is com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck$ElementStyleOption.
Default value is compact_no_array.
closingParens - Define the policy for ending parenthesis.
Type is com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck$ClosingParensOption.
Default value is never.
trailingArrayComma - Define the policy for trailing comma in arrays.
Type is com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck$TrailingArrayCommaOption.
Default value is never.
To configure the check:
<module name="AnnotationUseStyle"/>
Example:
@Deprecated // OK
@SomeArrays(pooches={DOGS.LEO}) // Violation - COMPACT_NO_ARRAY
@SuppressWarnings({""}) // Violation - COMPACT_NO_ARRAY
public class TestOne
{
}
@SomeArrays(pooches={DOGS.LEO}, um={}, test={"bleh"}) // Violation - COMPACT_NO_ARRAY
@SuppressWarnings("") // OK
@Deprecated() // Violation - cannot have closing parenthesis
class TestTwo {
}
To configure the check 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>
Example:
@Deprecated // Violation - must have closing parenthesis
@SomeArrays(pooches={DOGS.LEO}) // OK
@SuppressWarnings({""}) // Violation - EXPANDED
public class TestOne
{
}
@SomeArrays(pooches={DOGS.LEO}, um={}, test={"bleh"}) // OK
@SuppressWarnings("") // Violation - EXPANDED
@Deprecated() // OK
class TestTwo {
}
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
| Modifier and Type | Class and 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.
|
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
private static java.lang.String |
ANNOTATION_ELEMENT_SINGLE_NAME
The element name used to receive special linguistic support
for annotation use.
|
private AnnotationUseStyleCheck.ClosingParensOption |
closingParens
Define the policy for ending parenthesis.
|
private AnnotationUseStyleCheck.ElementStyleOption |
elementStyle
Define the annotation element styles.
|
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.
|
private AnnotationUseStyleCheck.TrailingArrayCommaOption |
trailingArrayComma
Define the policy for trailing comma in arrays.
|
| Constructor and Description |
|---|
AnnotationUseStyleCheck() |
| Modifier and Type | Method and Description |
|---|---|
private void |
checkCheckClosingParensOption(DetailAST ast)
Checks to see if the closing parenthesis are present if required or
prohibited.
|
private void |
checkCompactNoArrayStyle(DetailAST annotation)
Checks for compact no array style type violations.
|
private void |
checkCompactStyle(DetailAST annotation)
Checks for compact style type violations.
|
private void |
checkExpandedStyle(DetailAST annotation)
Checks for expanded style type violations.
|
private void |
checkStyleType(DetailAST annotation)
Checks to see if the
AnnotationElementStyleOption
is correct. |
private void |
checkTrailingComma(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>> |
getOption(java.lang.Class<T> enumClass,
java.lang.String value)
Retrieves an
Enum type from a @{link String String}. |
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
private static boolean |
hasArguments(DetailAST annotation)
Checks that annotation has arguments.
|
private void |
logCommaViolation(DetailAST ast)
Logs a trailing array comma violation if one exists.
|
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.
|
beginTree, clearViolations, destroy, finishTree, getFileContents, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic static final java.lang.String MSG_KEY_ANNOTATION_INCORRECT_STYLE
public static final java.lang.String MSG_KEY_ANNOTATION_PARENS_MISSING
public static final java.lang.String MSG_KEY_ANNOTATION_PARENS_PRESENT
public static final java.lang.String MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING
public static final java.lang.String MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT
private static final java.lang.String ANNOTATION_ELEMENT_SINGLE_NAME
private AnnotationUseStyleCheck.ElementStyleOption elementStyle
private AnnotationUseStyleCheck.TrailingArrayCommaOption trailingArrayComma
private AnnotationUseStyleCheck.ClosingParensOption closingParens
public AnnotationUseStyleCheck()
public void setElementStyle(java.lang.String style)
style - string representationpublic void setTrailingArrayComma(java.lang.String comma)
comma - string representationpublic void setClosingParens(java.lang.String parens)
parens - string representationprivate static <T extends java.lang.Enum<T>> T getOption(java.lang.Class<T> enumClass, java.lang.String value)
Enum type from a @{link String String}.T - the enum typeenumClass - the enum classvalue - the string representing the enumjava.lang.IllegalArgumentException - when unable to parse valuepublic int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic void visitToken(DetailAST ast)
AbstractCheckvisitToken in class AbstractCheckast - the token to processprivate void checkStyleType(DetailAST annotation)
AnnotationElementStyleOption
is correct.annotation - the annotation tokenprivate void checkExpandedStyle(DetailAST annotation)
annotation - the annotation tokenprivate static boolean hasArguments(DetailAST annotation)
annotation - to checkprivate void checkCompactStyle(DetailAST annotation)
annotation - the annotation tokenprivate void checkCompactNoArrayStyle(DetailAST annotation)
annotation - the annotation tokenprivate void checkTrailingComma(DetailAST annotation)
annotation - the annotation tokenprivate void logCommaViolation(DetailAST ast)
ast - the array init
ANNOTATION_ARRAY_INIT.private void checkCheckClosingParensOption(DetailAST ast)
ast - the annotation tokenCopyright © 2001-2022. All Rights Reserved.