CanonicalClassNameUsage

WARNING

FragileCode

View source code on GitHub

Summary

This code should likely use the type’s canonical name

Suppression

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

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

Samples

Replacement

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

 import static com.google.errorprone.matchers.Matchers.instanceMethod;
 
 import com.google.errorprone.BugPattern;
 import tech.picnic.errorprone.utils.MoreTypes;
 
 class A {
   void m() {
-    instanceMethod().onDescendantOfAny(A.class.getName(), BugPattern.LinkType.class.getName());
-    MoreTypes.type(String.class.getName());
+    instanceMethod()
+        .onDescendantOfAny(
+            A.class.getCanonicalName(), BugPattern.LinkType.class.getCanonicalName());
+    MoreTypes.type(String.class.getCanonicalName());
   }
 }
 

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 static com.google.errorprone.matchers.Matchers.instanceMethod;

import com.google.errorprone.VisitorState;
import tech.picnic.errorprone.utils.MoreTypes;

class A {
  void m(VisitorState state) {
    String a = A.class.getName();
    String b = getClass().getName();
    A.class.getName().toString();
    System.out.println(A.class.getName());
    methodInUnnamedPackage(A.class.getName());
    instanceMethod().onExactClass(A.class.getCanonicalName());
    MoreTypes.type(A.class.getCanonicalName());
    MoreTypes.type(A.class.getCanonicalName() + ".SubType");
    instanceMethod().onExactClass(new Object() {}.getClass().getName());
    instanceMethod().onExactClass(methodInUnnamedPackage(A.class.getName()));
    // BUG: Diagnostic contains:
    instanceMethod().onExactClass(A.class.getName());
    // BUG: Diagnostic contains:
    MoreTypes.type(A.class.getName());
    // BUG: Diagnostic contains:
    state.binaryNameFromClassname(A.class.getName() + ".SubType");
  }

  String methodInUnnamedPackage(String str) {
    return str;
  }
}

Copyright © 2017-2024 Picnic Technologies BV