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 ... }

    Author:
    maxvetrenko
    • 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.
      • 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
      • isAllowedPosition

        public static boolean isAllowedPosition​(DetailAST annotation,
                                                int... allowedPositions)
        Checks whether position of annotation is allowed.
        Parameters:
        annotation - annotation token.
        allowedPositions - an array of allowed annotation positions.
        Returns:
        true if position of annotation is allowed.