Class AnnotationLocationCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public class AnnotationLocationCheck
    extends AbstractCheck
    Check location of annotation on language elements. By default, Check enforce to locate annotations immediately after documentation block and before target element, annotation should be located on separate line from target element.

    Attention: Annotations among modifiers are ignored (looks like false-negative) as there might be a problem with annotations for return types.

    public @Nullable Long getStartTimeOrNull() { ... }
    .

    Such annotations are better to keep close to type. Due to limitations, Checkstyle can not examine the target of an annotation.

    Example:

     @Override
     @Nullable
     public String getNameIfPresent() { ... }
     

    The check has the following options:

    • allowSamelineMultipleAnnotations - to allow annotation to be located on the same line as the target element. Default value is false.
    • allowSamelineSingleParameterlessAnnotation - to allow single parameterless annotation to be located on the same line as the target element. Default value is false.
    • allowSamelineParameterizedAnnotation - to allow parameterized annotation to be located on the same line as the target element. Default value is false.

    Example to allow single parameterless annotation on the same line:

     @Override public int hashCode() { ... }
     

    Use the following configuration:

     <module name="AnnotationLocation">
        <property name="allowSamelineMultipleAnnotations" value="false"/>
        <property name="allowSamelineSingleParameterlessAnnotation"
        value="true"/>
        <property name="allowSamelineParameterizedAnnotation" value="false"
        />
     </module>
     

    Example to allow multiple parameterized annotations on the same line:

     @SuppressWarnings("deprecation") @Mock DataLoader loader;
     

    Use the following configuration:

     <module name="AnnotationLocation">
        <property name="allowSamelineMultipleAnnotations" value="true"/>
        <property name="allowSamelineSingleParameterlessAnnotation"
        value="true"/>
        <property name="allowSamelineParameterizedAnnotation" value="true"
        />
     </module>
     

    Example to allow multiple parameterless annotations on the same line:

     @Partial @Mock DataLoader loader;
     

    Use the following configuration:

     <module name="AnnotationLocation">
        <property name="allowSamelineMultipleAnnotations" value="true"/>
        <property name="allowSamelineSingleParameterlessAnnotation"
        value="true"/>
        <property name="allowSamelineParameterizedAnnotation" value="false"
        />
     </module>
     

    The following example demonstrates how the check validates annotation of method parameters, catch parameters, foreach, for-loop variable definitions.

    Configuration:

     <module name="AnnotationLocation">
        <property name="allowSamelineMultipleAnnotations" value="false"/>
        <property name="allowSamelineSingleParameterlessAnnotation"
        value="false"/>
        <property name="allowSamelineParameterizedAnnotation" value="false"
        />
        <property name="tokens" value="VARIABLE_DEF, PARAMETER_DEF"/>
     </module>
     

    Code example ... public void test(&#64;MyAnnotation String s) { // OK ... for (&#64;MyAnnotation char c : s.toCharArray()) { ... } // OK ... try { ... } catch (&#64;MyAnnotation Exception ex) { ... } // OK ... for (&#64;MyAnnotation int i = 0; i &lt; 10; i++) { ... } // OK ... MathOperation c = (&#64;MyAnnotation int a, &#64;MyAnnotation int b) -&gt; a + b; // OK ... }

    • Field Detail

      • MSG_KEY_ANNOTATION_LOCATION_ALONE

        public static final String MSG_KEY_ANNOTATION_LOCATION_ALONE
        A key is pointing to the warning message text in "messages.properties" file.
        See Also:
        Constant Field Values
      • MSG_KEY_ANNOTATION_LOCATION

        public static final String MSG_KEY_ANNOTATION_LOCATION
        A key is pointing to the warning message text in "messages.properties" file.
        See Also:
        Constant Field Values
    • Constructor Detail

      • AnnotationLocationCheck

        public AnnotationLocationCheck()
    • Method Detail

      • setAllowSamelineSingleParameterlessAnnotation

        public final void setAllowSamelineSingleParameterlessAnnotation​(boolean allow)
        Sets if allow same line single parameterless annotation.
        Parameters:
        allow - User's value of allowSamelineSingleParameterlessAnnotation.
      • setAllowSamelineParameterizedAnnotation

        public final void setAllowSamelineParameterizedAnnotation​(boolean allow)
        Sets if allow parameterized annotation to be located on the same line as target element.
        Parameters:
        allow - User's value of allowSamelineParameterizedAnnotation.
      • setAllowSamelineMultipleAnnotations

        public final void setAllowSamelineMultipleAnnotations​(boolean allow)
        Sets if allow annotation to be located on the same line as target element.
        Parameters:
        allow - User's value of allowSamelineMultipleAnnotations.
      • 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 class AbstractCheck
        Returns:
        the default tokens
        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 class AbstractCheck
        Returns:
        the token set this check is designed for.
        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 class AbstractCheck
        Returns:
        the token set this must be registered for.
        See Also:
        TokenTypes