ErrorProneRuntimeClasspath

SUGGESTION

FragileCode

View source code on GitHub

Summary

Prefer Class#getCanonicalName() over an equivalent string literal if and only if the type will be on the runtime classpath

Suppression

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

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

Samples

Replacement

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

+import com.google.common.collect.ImmutableList;
 import com.google.errorprone.BugCheckerRefactoringTestHelper;
+import com.google.errorprone.BugPattern;
 import com.google.errorprone.CompilationTestHelper;
+import com.google.errorprone.util.ErrorProneToken;
 import org.junit.jupiter.api.Test;
 
 class A {
   void m(Object o) {
-    m("com.google.errorprone.BugPattern");
-    m("com.google.errorprone.util.ErrorProneToken");
-    m("com.google.common.collect.ImmutableList");
-    m("java.lang.String");
-    m("java.lang.String.toString");
+    m(BugPattern.class.getCanonicalName());
+    m(ErrorProneToken.class.getCanonicalName());
+    m(ImmutableList.class.getCanonicalName());
+    m(String.class.getCanonicalName());
+    m(String.class.getCanonicalName() + ".toString");
 
-    m(CompilationTestHelper.class.getCanonicalName());
-    m(BugCheckerRefactoringTestHelper.ExpectOutput.class.getCanonicalName());
-    m(Test.class.getCanonicalName());
-    m(org.junit.jupiter.api.Nested.class.getCanonicalName());
+    m("com.google.errorprone.CompilationTestHelper");
+    m("com.google.errorprone.BugCheckerRefactoringTestHelper.ExpectOutput");
+    m("org.junit.jupiter.api.Test");
+    m("org.junit.jupiter.api.Nested");
   }
 }
 

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 com.google.common.collect.ImmutableList;
import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.BugPattern;
import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;

class A {
  @SuppressWarnings("java.lang.String")
  void m(Object o) {
    m(null);
    m(0);
    m(getClass().getName());
    m(getClass().getCanonicalName());
    m("");
    m("foo");
    m("java.util.");

    m("org.junit.jupiter.api.Test");
    m("org.junit.jupiter.api.Test.toString");
    m("com.google.errorprone.CompilationTestHelper");
    m("com.google.errorprone.CompilationTestHelper.toString");
    m("com.google.errorprone.BugCheckerRefactoringTestHelper.ExpectOutput");
    m("com.google.errorprone.BugCheckerRefactoringTestHelper.ExpectOutput.toString");
    m("com.google.errorprone.NonExistent");
    m("com.google.common.NonExistent.toString");
    m("java.lang.NonExistent");
    m("com.google.common.collect.ImmutableEnumSet");
    // BUG: Diagnostic matches: USE_CLASS_REFERENCE
    m("com.google.errorprone.BugPattern");
    // BUG: Diagnostic matches: USE_CLASS_REFERENCE
    m("com.google.errorprone.util.ErrorProneToken");
    // BUG: Diagnostic matches: USE_CLASS_REFERENCE
    m("com.google.common.collect.ImmutableList");
    // BUG: Diagnostic matches: USE_CLASS_REFERENCE
    m("java.lang.String");
    // BUG: Diagnostic matches: USE_CLASS_REFERENCE
    m("java.lang.String.toString");

    m(BugPattern.class.getCanonicalName());
    m(ImmutableList.class.getCanonicalName());
    m(String.class.getCanonicalName());
    m(void.class.getCanonicalName());
    m(boolean.class.getCanonicalName());
    m(byte.class.getCanonicalName());
    m(char.class.getCanonicalName());
    m(short.class.getCanonicalName());
    m(int.class.getCanonicalName());
    m(long.class.getCanonicalName());
    m(float.class.getCanonicalName());
    m(double.class.getCanonicalName());
    m(java.lang.Iterable.class.getCanonicalName());
    m(CompilationTestHelper.class.toString());
    // BUG: Diagnostic matches: USE_STRING_LITERAL
    m(CompilationTestHelper.class.getCanonicalName());
    // BUG: Diagnostic matches: USE_STRING_LITERAL
    m(BugCheckerRefactoringTestHelper.ExpectOutput.class.getCanonicalName());
    // BUG: Diagnostic matches: USE_STRING_LITERAL
    m(Test.class.getCanonicalName());
    // BUG: Diagnostic matches: USE_STRING_LITERAL
    m(org.junit.jupiter.api.Nested.class.getCanonicalName());
  }
}

Copyright © 2017-2024 Picnic Technologies BV