Class InternalUtils

java.lang.Object
org.checkerframework.javacutil.InternalUtils

public class InternalUtils extends Object
Miscellaneous static utility methods.
  • Method Details

    • getJavacContext

      public static com.sun.tools.javac.util.Context getJavacContext(ProcessingEnvironment env)
      Helper function to extract the javac Context from the javac processing environment.
      Parameters:
      env - the processing environment
      Returns:
      the javac Context
    • getClassLoaderForClass

      public static @Nullable ClassLoader getClassLoaderForClass(Class<? extends Object> clazz)
      Obtain the class loader for clazz. If that is not available, return the system class loader.
      Parameters:
      clazz - the class whose class loader to find
      Returns:
      the class loader used to clazz, or the system class loader, or null if both are unavailable
    • compareDiagnosticPosition

      public static int compareDiagnosticPosition(Tree tree1, Tree tree2)
      Compares tree1 to tree2 by the position at which a diagnostic (e.g., an error message) for the tree should be printed.
    • isInitName

      public static boolean isInitName(Name name)
      Returns true iff name is the constructor name "<init>".

      javac interns all Names in a per-compilation table, so two names from the same compilation are equal iff they are identical. Comparing against the table's pre-interned init name is a pointer comparison, whereas Name.contentEquals decodes the name's UTF-8 bytes into a fresh String on every call on byte-backed name tables (javac's default before JDK 23, and what Gradle's -XDuseUnsharedTable forces on all JDK versions).

      Parameters:
      name - a name
      Returns:
      true iff name is "<init>"
    • isThisName

      public static boolean isThisName(Name name)
      Returns true iff name is "this". See isInitName(javax.lang.model.element.Name) for why this is faster than Name.contentEquals.
      Parameters:
      name - a name
      Returns:
      true iff name is "this"
    • isSuperName

      public static boolean isSuperName(Name name)
      Returns true iff name is "super". See isInitName(javax.lang.model.element.Name) for why this is faster than Name.contentEquals.
      Parameters:
      name - a name
      Returns:
      true iff name is "super"
    • isValueName

      public static boolean isValueName(Name name)
      Returns true iff name is "value" (e.g., the name of an annotation element). See isInitName(javax.lang.model.element.Name) for why this is faster than Name.contentEquals.
      Parameters:
      name - a name
      Returns:
      true iff name is "value"
    • isJavaLangObjectName

      public static boolean isJavaLangObjectName(Name name)
      Returns true iff name is "java.lang.Object". See isInitName(javax.lang.model.element.Name) for why this is faster than Name.contentEquals.
      Parameters:
      name - a name, typically a TypeElement's qualified name
      Returns:
      true iff name is "java.lang.Object"
    • isJavaLangEnumName

      public static boolean isJavaLangEnumName(Name name)
      Returns true iff name is "java.lang.Enum". See isInitName(javax.lang.model.element.Name) for why this is faster than Name.contentEquals.
      Parameters:
      name - a name, typically a TypeElement's qualified name
      Returns:
      true iff name is "java.lang.Enum"
    • sameName

      public static boolean sameName(Name name, CharSequence expected)
      Returns true iff name has the same characters as expected.

      For a javac Name, this interns expected into the name's own table (cached across calls) and compares by identity, instead of Name.contentEquals, which decodes the name's UTF-8 bytes into a fresh String on every call on byte-backed name tables (javac's default before JDK 23, and what Gradle's -XDuseUnsharedTable forces on all JDK versions). Measured ~6x faster with zero allocation there, and neutral on string-backed tables.

      Only use this for a bounded set of expected strings (annotation element names, method names a checker matches against, identifiers that occur in the source). Each distinct string is interned into the compiler's name table and cached for the duration of the compilation, so unbounded or one-off probe strings would grow both. For a fixed literal, prefer the dedicated helpers (isInitName(javax.lang.model.element.Name), isThisName(javax.lang.model.element.Name), ...) when one exists.

      Parameters:
      name - a name
      expected - the expected string
      Returns:
      true iff name and expected represent the same characters
    • sameName

      public static boolean sameName(Name name1, Name name2)
      Returns true iff name1 and name2 have the same characters.

      javac interns all Names in a per-compilation table, so two javac names from the same table are equal iff they are identical; Name.contentEquals(Name) would instead decode both names' UTF-8 bytes on byte-backed name tables. Falls back to contentEquals if either name is not a javac name or the tables differ.

      Parameters:
      name1 - a name
      name2 - a name
      Returns:
      true iff name1 and name2 represent the same characters