Skip to main content Link Search Menu Expand Document (external link)

LexicographicalAnnotationListing

SUGGESTION

Style

View source code on GitHub

Summary

Sort annotations lexicographically where possible

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("LexicographicalAnnotationListing") to the enclosing element.

Disable this pattern completely by adding -Xep:LexicographicalAnnotationListing:OFF as compiler argument. Learn more.

Samples

Replacement

Shows the difference in example code before and after the bug pattern is applied.

 import java.lang.annotation.ElementType;
 import java.lang.annotation.Repeatable;
 import java.lang.annotation.Target;
 
 interface A {
   @Repeatable(Foos.class)
   @interface Foo {
     String[] value() default {};
 
     int[] ints() default {};
 
     Bar[] anns() default {};
   }
 
   @Target(ElementType.METHOD)
   @interface Bar {
     String[] value() default {};
   }
 
   @interface Baz {
     String[] str() default {};
   }
 
   @interface Foos {
     Foo[] value();
   }
 
   @Target(ElementType.TYPE_USE)
   @interface FooTypeUse {
     String[] value() default {};
   }
 
   @Target(ElementType.TYPE_USE)
   @interface BarTypeUse {
     String[] value() default {};
   }
 
   @Bar
   A singleAnnotation();
 
   @Bar
   @Foo
   A sortedAnnotations();
 
-  @Foo
   @Bar
+  @Foo
   A unsortedAnnotations();
 
-  @Foo()
-  @Baz()
   @Bar
+  @Baz()
+  @Foo()
   A unsortedAnnotationsWithSomeParens();
 
   @Bar
   @Baz(str = {"a", "b"})
   @Foo()
   A unsortedAnnotationsOneContainingAttributes();
 
+  @Bar({"b"})
   @Baz(str = {"a", "b"})
   @Foo(anns = {@Bar("b"), @Bar("a")})
-  @Bar({"b"})
   A unsortedAnnotationsWithAttributes();
 
   @Foo(anns = {@Bar("b"), @Bar("a")})
   @Foo(ints = {1, 2})
   @Foo({"b"})
   A sortedRepeatableAnnotation();
 
-  @Foo(anns = {@Bar("b"), @Bar("a")})
   @Bar
+  @Foo(anns = {@Bar("b"), @Bar("a")})
   @Foo(ints = {1, 2})
   A unsortedRepeatableAnnotation();
 
-  @Baz
   @Bar
-  default @FooTypeUse @BarTypeUse A unsortedWithTypeUseAnnotations() {
+  @Baz
+  default @BarTypeUse @FooTypeUse A unsortedWithTypeUseAnnotations() {
     return null;
   }
 }

Identification

Shows code lines which will (not) be flagged by this bug pattern.
A //BUG: Diagnostic contains: comment is placed above any violating line.

import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Target;

interface A {
  @Repeatable(Foos.class)
  @interface Foo {
    String[] value() default {};

    int[] ints() default {};

    Bar[] anns() default {};
  }

  @Target(ElementType.METHOD)
  @interface Bar {
    String[] value() default {};
  }

  @interface Baz {
    String[] str() default {};
  }

  @interface Foos {
    Foo[] value();
  }

  @Target(ElementType.TYPE_USE)
  @interface FooTypeUse {
    String[] value() default {};
  }

  @Target(ElementType.TYPE_USE)
  @interface BarTypeUse {
    String[] value() default {};
  }

  // BUG: Diagnostic contains:
  @Foo
  @Bar
  A unsortedSimpleCase();
  // BUG: Diagnostic contains:
  @Foo()
  @Bar()
  A unsortedWithParens();

  @Foo()
  A onlyOneAnnotation();

  @Bar
  @Foo()
  A sortedAnnotationsOneWithParens();

  // BUG: Diagnostic contains:
  @Foo
  @Baz
  @Bar
  A threeUnsortedAnnotationsSameInitialLetter();
  // BUG: Diagnostic contains:
  @Bar
  @Foo()
  @Baz
  A firstOrderedWithTwoUnsortedAnnotations();

  @Bar
  @Baz
  @Foo()
  A threeSortedAnnotations();

  // BUG: Diagnostic contains:
  @Foo({"b"})
  @Bar({"a"})
  A unsortedWithStringAttributes();
  // BUG: Diagnostic contains:
  @Baz(str = {"a", "b"})
  @Foo(ints = {1, 0})
  @Bar
  A unsortedWithAttributes();
  // BUG: Diagnostic contains:
  @Bar
  @Foo(anns = {@Bar("b"), @Bar("a")})
  @Baz
  A unsortedWithNestedBar();

  @Bar
  @Baz
  @Foo(anns = {@Bar("b"), @Bar("a")})
  A sortedWithNestedBar();

  @Foo(anns = {@Bar("b"), @Bar("a")})
  @Foo(ints = {1, 2})
  @Foo({"b"})
  A sortedRepeatableAnnotation();
  // BUG: Diagnostic contains:
  @Foo(anns = {@Bar("b"), @Bar("a")})
  @Bar
  @Foo(ints = {1, 2})
  A unsortedRepeatableAnnotation();

  // BUG: Diagnostic contains:
  default @FooTypeUse @BarTypeUse A unsortedTypeAnnotations() {
    return null;
  }

  // BUG: Diagnostic contains:
  @Baz
  @Bar
  default @FooTypeUse @BarTypeUse A unsortedTypeUseAndOtherAnnotations() {
    return null;
  }
}

Copyright © 2017-2023 Picnic Technologies BV