Class InternalUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic intcompareDiagnosticPosition(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.static @Nullable ClassLoadergetClassLoaderForClass(Class<? extends Object> clazz) Obtain the class loader forclazz.static com.sun.tools.javac.util.ContextHelper function to extract the javac Context from the javac processing environment.static booleanisInitName(Name name) Returns true iffnameis the constructor name"<init>".static booleanisJavaLangEnumName(Name name) Returns true iffnameis"java.lang.Enum".static booleanisJavaLangObjectName(Name name) Returns true iffnameis"java.lang.Object".static booleanisSuperName(Name name) Returns true iffnameis"super".static booleanisThisName(Name name) Returns true iffnameis"this".static booleanisValueName(Name name) Returns true iffnameis"value"(e.g., the name of an annotation element).static booleansameName(Name name, CharSequence expected) Returns true iffnamehas the same characters asexpected.static booleanReturns true iffname1andname2have the same characters.
-
Method Details
-
getJavacContext
Helper function to extract the javac Context from the javac processing environment.- Parameters:
env- the processing environment- Returns:
- the javac Context
-
getClassLoaderForClass
Obtain the class loader forclazz. 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
Compares tree1 to tree2 by the position at which a diagnostic (e.g., an error message) for the tree should be printed. -
isInitName
Returns true iffnameis 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-internedinitname is a pointer comparison, whereasName.contentEqualsdecodes the name's UTF-8 bytes into a freshStringon every call on byte-backed name tables (javac's default before JDK 23, and what Gradle's-XDuseUnsharedTableforces on all JDK versions).- Parameters:
name- a name- Returns:
- true iff name is "<init>"
-
isThisName
Returns true iffnameis"this". SeeisInitName(javax.lang.model.element.Name)for why this is faster thanName.contentEquals.- Parameters:
name- a name- Returns:
- true iff name is "this"
-
isSuperName
Returns true iffnameis"super". SeeisInitName(javax.lang.model.element.Name)for why this is faster thanName.contentEquals.- Parameters:
name- a name- Returns:
- true iff name is "super"
-
isValueName
Returns true iffnameis"value"(e.g., the name of an annotation element). SeeisInitName(javax.lang.model.element.Name)for why this is faster thanName.contentEquals.- Parameters:
name- a name- Returns:
- true iff name is "value"
-
isJavaLangObjectName
Returns true iffnameis"java.lang.Object". SeeisInitName(javax.lang.model.element.Name)for why this is faster thanName.contentEquals.- Parameters:
name- a name, typically aTypeElement's qualified name- Returns:
- true iff name is "java.lang.Object"
-
isJavaLangEnumName
Returns true iffnameis"java.lang.Enum". SeeisInitName(javax.lang.model.element.Name)for why this is faster thanName.contentEquals.- Parameters:
name- a name, typically aTypeElement's qualified name- Returns:
- true iff name is "java.lang.Enum"
-
sameName
Returns true iffnamehas the same characters asexpected.For a javac
Name, this internsexpectedinto the name's own table (cached across calls) and compares by identity, instead ofName.contentEquals, which decodes the name's UTF-8 bytes into a freshStringon every call on byte-backed name tables (javac's default before JDK 23, and what Gradle's-XDuseUnsharedTableforces 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
expectedstrings (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 nameexpected- the expected string- Returns:
- true iff name and expected represent the same characters
-
sameName
Returns true iffname1andname2have 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 tocontentEqualsif either name is not a javac name or the tables differ.- Parameters:
name1- a namename2- a name- Returns:
- true iff name1 and name2 represent the same characters
-