CanonicalAnnotationSyntax

SUGGESTION

Simplification

View source code on GitHub

Summary

Omit redundant syntax from annotation declarations

Suppression

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

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

Samples

Replacement

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

 package pkg;
 
 import pkg.A.Foo;
 
 interface A {
   @interface Foo {
     String[] value() default {};
 
     int[] value2() default {};
   }
 
-  @pkg.A.Foo()
+  @pkg.A.Foo
   A functional1();
 
-  @A.Foo()
+  @A.Foo
   A functional2();
 
-  @Foo()
+  @Foo
   A functional3();
 
-  @pkg.A.Foo(value = "foo")
+  @pkg.A.Foo("foo")
   A verbose1();
 
-  @A.Foo(value = "a'b")
+  @A.Foo("a'b")
   A verbose2();
 
-  @Foo(value = "a" + "\nb")
+  @Foo("a" + "\nb")
   A verbose3();
 
-  @pkg.A.Foo(value = {"foo"})
+  @pkg.A.Foo("foo")
   A moreVerbose1();
 
-  @A.Foo(value = {"a'b"})
+  @A.Foo("a'b")
   A moreVerbose2();
 
-  @Foo(value = {"a" + "\nb"})
+  @Foo("a" + "\nb")
   A moreVerbose3();
 
   @pkg.A.Foo(
       value = {"foo", "bar"},
-      value2 = {2})
+      value2 = 2)
   A extended1();
 
   @A.Foo(
       value = {"a'b", "c'd"},
-      value2 = {2})
+      value2 = 2)
   A extended2();
 
   @Foo(
       value = {"a" + "\nb", "c" + "\nd"},
-      value2 = {2})
+      value2 = 2)
   A extended3();
 
-  @pkg.A.Foo({
-    "foo", "bar",
-  })
+  @pkg.A.Foo({"foo", "bar"})
   A trailingComma1();
 
-  @A.Foo({
-    "a'b", "c'd",
-  })
+  @A.Foo({"a'b", "c'd"})
   A trailingComma2();
 
-  @Foo({
-    "a" + "\nb",
-    "c" + "\nd",
-  })
+  @Foo({"a" + "\nb", "c" + "\nd"})
   A trailingComma3();
 }
 

Identification

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

package pkg;

import pkg.A.Foo;

interface A {
  @interface Foo {
    int[] value() default {};

    int[] value2() default {};
  }

  @pkg.A.Foo
  A minimal1();

  @A.Foo
  A minimal2();

  @Foo
  A minimal3();

  // BUG: Diagnostic contains:
  @pkg.A.Foo()
  A functional1();

  // BUG: Diagnostic contains:
  @A.Foo()
  A functional2();

  // BUG: Diagnostic contains:
  @Foo()
  A functional3();

  @pkg.A.Foo(1)
  A simple1();

  @A.Foo(1)
  A simple2();

  @Foo(1)
  A simple3();

  // BUG: Diagnostic contains:
  @pkg.A.Foo({1})
  A singleton1();

  // BUG: Diagnostic contains:
  @A.Foo({1})
  A singleton2();

  // BUG: Diagnostic contains:
  @Foo({1})
  A singleton3();

  // BUG: Diagnostic contains:
  @pkg.A.Foo(value = 1)
  A verbose1();

  // BUG: Diagnostic contains:
  @A.Foo(value = 1)
  A verbose2();

  // BUG: Diagnostic contains:
  @Foo(value = 1)
  A verbose3();

  @pkg.A.Foo(value2 = 2)
  A custom1();

  @A.Foo(value2 = 2)
  A custom2();

  @Foo(value2 = 2)
  A custom3();

  // BUG: Diagnostic contains:
  @pkg.A.Foo(value2 = {2})
  A customSingleton1();

  // BUG: Diagnostic contains:
  @A.Foo(value2 = {2})
  A customSingleton2();

  // BUG: Diagnostic contains:
  @Foo(value2 = {2})
  A customSingleton3();

  @pkg.A.Foo(value2 = {2, 2})
  A customPair1();

  @A.Foo(value2 = {2, 2})
  A customPair2();

  @Foo(value2 = {2, 2})
  A customPair3();

  @pkg.A.Foo(value = 1, value2 = 2)
  A extended1();

  @A.Foo(value = 1, value2 = 2)
  A extended2();

  @Foo(value = 1, value2 = 2)
  A extended3();

  // BUG: Diagnostic contains:
  @pkg.A.Foo({
    1, 1,
  })
  A trailingComma1();

  // BUG: Diagnostic contains:
  @A.Foo({
    1, 1,
  })
  A trailingComma2();

  // BUG: Diagnostic contains:
  @Foo({
    1, 1,
  })
  A trailingComma3();
}

Copyright © 2017-2024 Picnic Technologies BV