Class ElementUtils
Element
s. This complements Elements
, providing
functionality that it does not.-
Method Summary
Modifier and TypeMethodDescriptionstatic PackageElement
enclosingPackage
(Element elem) Returns the innermost package element enclosing the given element.static @Nullable TypeElement
enclosingTypeElement
(Element elem) Returns the innermost type element that is, or encloses, the given element.static @Nullable VariableElement
findFieldInType
(TypeElement type, String name) Returns the field of the class ornull
if not found.static Set<VariableElement>
findFieldsInType
(TypeElement type, Collection<String> names) Returns the elements of the fields whose simple names are innames
and are declared intype
.static Set<VariableElement>
findFieldsInTypeOrSuperType
(TypeMirror type, Collection<String> names) Returns non-private field elements, and side-effectsnames
to remove them.static List<VariableElement>
getAllFieldsIn
(TypeElement type, Elements elements) Return all fields declared in the given type or any superclass/interface.static List<ExecutableElement>
getAllMethodsIn
(TypeElement type, Elements elements) Return all methods declared in the given type or any superclass/interface.static List<TypeElement>
getAllSupertypes
(TypeElement type, ProcessingEnvironment env) Get all the supertypes of a given type, including the type itself.static List<TypeElement>
Return all nested/inner classes/interfaces declared in the given type.static @BinaryName String
Returns the binary name of the given type.static List<TypeElement>
getDirectSuperTypeElements
(TypeElement type, Elements elements) Determine all type elements for the direct supertypes of the given type element.static @BinaryName String
getEnclosingClassName
(ExecutableElement executableElement) Returns the binary name of the class enclosingexecutableElement
.static @BinaryName String
getEnclosingClassName
(VariableElement variableElement) Returns the binary name of the class enclosingvariableElement
.static List<VariableElement>
getEnumConstants
(TypeElement type) Returns all enum constants declared in the given enumeration.static ElementKind
Calls getKind() on the given Element, but returns CLASS if the ElementKind is RECORD.static Set<? extends ExecutableElement>
getOverriddenMethods
(ExecutableElement m, Types types) Returns the methods that are overridden or implemented by a given method.getQualifiedClassName
(Element element) Returns the qualified name of the innermost class enclosing the providedElement
.static String
getQualifiedName
(Element elt) Returns a verbose name that identifies the element.getRecordComponents
(TypeElement element) Calls getRecordComponents on the given TypeElement.static CharSequence
getSimpleDescription
(ExecutableElement element) Returns a user-friendly name for the given method, which includes the name of the enclosing type.static CharSequence
Deprecated.static String
getSimpleSignature
(ExecutableElement element) Returns the canonical representation of the method declaration, which contains simple names of the types only.static String
getSourceFilePath
(TypeElement element) Returns the path to the source file containingelement
, which must be from source code.static @Nullable TypeElement
getSuperClass
(TypeElement typeElt) Returns a type's superclass, or null if it does not have a superclass (it is object or an interface, or the superclass is not on the classpath).static List<TypeElement>
getSuperTypes
(TypeElement type, Elements elements) Determine all type elements for the supertypes of the given type element.static TypeMirror
Returns theTypeMirror
for usage of Element as a value.static TypeElement
getTypeElement
(ProcessingEnvironment processingEnv, Class<?> clazz) Returns the TypeElement for the given class.static boolean
hasAnnotation
(Element element, String annotName) Given an annotation name, return true if the element has the annotation of that name.static boolean
hasReceiver
(Element element) Does the given element need a receiver for accesses? For example, an access to a local variable does not require a receiver.static boolean
inSameClass
(Element e1, Element e2) Returns true if the two elements are in the same class.static boolean
Returns true if the givenElement
is part of a record that has been automatically generated by the compiler.static boolean
isBindingVariable
(Element element) Return true if the element is a binding variable.static boolean
isClassElement
(Element element) Deprecated.static boolean
Check if the given element is a compact canonical record constructor.static boolean
Returns true if the element is a reference to a compile-time constant.static boolean
isEffectivelyFinal
(Element element) Returns true if the element is a effectively final element.static boolean
Returns true if the element is declared in ByteCode.static boolean
isElementFromSourceCode
(@Nullable Element element) Checks whether a given element came from a source file.static boolean
Returns true ifelement
is "com.sun.tools.javac.comp.Resolve$SymbolNotFoundError".static boolean
Returns true if the element is a final element: a final field, method, or final class.static boolean
isGetter
(@Nullable ExecutableElement methodElt) Returns true if the given element is a getter method.static boolean
isLocalVariable
(Element elt) Return true if the element is a local variable.static boolean
isMethod
(ExecutableElement questioned, ExecutableElement method, ProcessingEnvironment env) Returns true if the given element is, or overrides,method
.static boolean
isObject
(TypeElement element) Check if the element is an element for 'java.lang.Object'static boolean
isRecordAccessor
(ExecutableElement methodElement) Returns true if the element is a record accessor method.static boolean
Returns true iff the given element is a resource variable.static boolean
Returns true if the element is a static element: whether it is a static field, static method, or static class.static boolean
isString
(TypeElement element) Check if the element is an element for 'java.lang.String'static boolean
isTypeDeclaration
(Element elt) Return true if the element is a type declaration.static boolean
isTypeElement
(Element element) Is the given element kind a type, i.e., a class, enum, interface, or annotation type.static boolean
matchesElement
(ExecutableElement method, String methodName, Class<?>... parameters) Check that a method Element matches a signature.static @Nullable PackageElement
parentPackage
(PackageElement elem, Elements elements) Returns the "parent" package element for the given package element.static @Nullable TypeElement
Returns the innermost type element enclosing the given element, that is different from the element itself.static TypeElement
toplevelEnclosingTypeElement
(Element element) Returns the top-level type element that containselement
.static Set<ElementKind>
Return the set of kinds that represent classes.
-
Method Details
-
enclosingTypeElement
Returns the innermost type element that is, or encloses, the given element.Note that in this code:
class Outer { static class Inner { } }
Inner
has no enclosing type, but this method returnsOuter
.- Parameters:
elem
- the enclosed element of a class- Returns:
- the innermost type element (possibly the argument itself), or null if
elem
is not, and is not enclosed by, a type element
-
strictEnclosingTypeElement
Returns the innermost type element enclosing the given element, that is different from the element itself. By contrast,enclosingTypeElement(javax.lang.model.element.Element)
returns its argument if the argument is a type element.- Parameters:
elem
- the enclosed element of a class- Returns:
- the innermost type element, or null if no type element encloses
elem
-
toplevelEnclosingTypeElement
Returns the top-level type element that containselement
.- Parameters:
element
- the element whose enclosing tye element to find- Returns:
- a type element containing
element
that isn't contained in another class
-
getEnclosingClassName
Returns the binary name of the class enclosingexecutableElement
.- Parameters:
executableElement
- the ExecutableElement- Returns:
- the binary name of the class enclosing
executableElement
-
getEnclosingClassName
Returns the binary name of the class enclosingvariableElement
.- Parameters:
variableElement
- the VariableElement- Returns:
- the binary name of the class enclosing
variableElement
-
enclosingPackage
Returns the innermost package element enclosing the given element. The same effect asElements.getPackageOf(Element)
. Returns the element itself if it is a package.- Parameters:
elem
- the enclosed element of a package- Returns:
- the innermost package element
-
parentPackage
Returns the "parent" package element for the given package element. For package "A.B" it gives "A". For package "A" it gives the default package. For the default package it returns null.Note that packages are not enclosed within each other, we have to manually climb the namespaces. Calling "enclosingPackage" on a package element returns the package element itself again.
- Parameters:
elem
- the package to start fromelements
- the element- Returns:
- the parent package element or
null
-
isStatic
Returns true if the element is a static element: whether it is a static field, static method, or static class.- Returns:
- true if element is static
-
isFinal
Returns true if the element is a final element: a final field, method, or final class.- Returns:
- true if the element is final
-
isEffectivelyFinal
Returns true if the element is a effectively final element.- Returns:
- true if the element is effectively final
-
getType
Returns theTypeMirror
for usage of Element as a value. It returns the return type of a method element, the class type of a constructor, or simply the type mirror of the element itself.- Parameters:
element
- the element whose type to obtain- Returns:
- the type for the element used as a value
-
getQualifiedClassName
Returns the qualified name of the innermost class enclosing the providedElement
.- Parameters:
element
- an element enclosed by a class, or aTypeElement
- Returns:
- the qualified
Name
of the innermost class enclosing the element
-
getQualifiedName
Returns a verbose name that identifies the element.- Parameters:
elt
- the element whose name to obtain- Returns:
- the qualified name of the given element
-
getBinaryName
Returns the binary name of the given type.- Parameters:
te
- a type- Returns:
- the binary name of the type
-
getSimpleSignature
Returns the canonical representation of the method declaration, which contains simple names of the types only.- Parameters:
element
- a method declaration- Returns:
- the simple name of the method, followed by the simple names of the formal parameter types
-
getSimpleNameOrDescription
Deprecated.Returns a user-friendly name for the given method. Does not return"<init>"
or"<clinit>"
as ExecutableElement.getSimpleName() does.- Parameters:
element
- a method declaration- Returns:
- a user-friendly name for the method
-
getSimpleDescription
Returns a user-friendly name for the given method, which includes the name of the enclosing type. Does not return"<init>"
or"<clinit>"
as ExecutableElement.getSimpleName() does.- Parameters:
element
- a method declaration- Returns:
- a user-friendly name for the method
-
isObject
Check if the element is an element for 'java.lang.Object'- Parameters:
element
- the type element- Returns:
- true iff the element is java.lang.Object element
-
isString
Check if the element is an element for 'java.lang.String'- Parameters:
element
- the type element- Returns:
- true iff the element is java.lang.String element
-
isCompileTimeConstant
Returns true if the element is a reference to a compile-time constant.- Parameters:
elt
- an element- Returns:
- true if the element is a reference to a compile-time constant
-
isElementFromSourceCode
Checks whether a given element came from a source file.By contrast,
isElementFromByteCode(Element)
returns true if there is a classfile for the given element, even if there is also a source file.- Parameters:
element
- the element to check, or null- Returns:
- true if a source file containing the element is being compiled
-
isElementFromByteCode
Returns true if the element is declared in ByteCode. Always return false if elt is a package.- Parameters:
elt
- some element- Returns:
- true if the element is declared in ByteCode
-
getSourceFilePath
Returns the path to the source file containingelement
, which must be from source code.- Parameters:
element
- the type element to look at- Returns:
- path to the source file containing
element
-
findFieldInType
Returns the field of the class ornull
if not found.- Parameters:
type
- the TypeElement to searchname
- name of a field- Returns:
- the VariableElement for the field if it was found, null otherwise
-
findFieldsInType
Returns the elements of the fields whose simple names are innames
and are declared intype
.If a field isn't declared in
type
, its element isn't included in the returned set. If none of the fields is declared intype
, the empty set is returned.- Parameters:
type
- where to look for fieldsnames
- simple names of fields that might be declared intype
- Returns:
- the elements of the fields whose simple names are
names
and are declared intype
-
findFieldsInTypeOrSuperType
public static Set<VariableElement> findFieldsInTypeOrSuperType(TypeMirror type, Collection<String> names) Returns non-private field elements, and side-effectsnames
to remove them. For every field name innames
that is declared intype
or a supertype, add its element to the returned set and remove it fromnames
.When this routine returns, the combination of the return value and
names
has the same cardinality, and represents the same fields, asnames
did when the method was called.- Parameters:
type
- where to look for fieldsnames
- simple names of fields that might be declared intype
or a supertype. Names that are found are removed from this list.- Returns:
- the
VariableElement
s for non-private fields that are declared intype
whose simple names were innames
when the method was called.
-
isError
Returns true ifelement
is "com.sun.tools.javac.comp.Resolve$SymbolNotFoundError".- Parameters:
element
- the element to test- Returns:
- true if
element
is "com.sun.tools.javac.comp.Resolve$SymbolNotFoundError"
-
hasReceiver
Does the given element need a receiver for accesses? For example, an access to a local variable does not require a receiver.- Parameters:
element
- the element to test- Returns:
- whether the element requires a receiver for accesses
-
getSuperClass
Returns a type's superclass, or null if it does not have a superclass (it is object or an interface, or the superclass is not on the classpath).- Parameters:
typeElt
- a type element- Returns:
- the superclass of
typeElt
-
getSuperTypes
Determine all type elements for the supertypes of the given type element. This is the transitive closure of the extends and implements clauses.TODO: can we learn from the implementation of com.sun.tools.javac.model.JavacElements.getAllMembers(TypeElement)?
- Parameters:
type
- the type whose supertypes to returnelements
- the Element utilities- Returns:
- supertypes of
type
-
getDirectSuperTypeElements
Determine all type elements for the direct supertypes of the given type element. This is the union of the extends and implements clauses.- Parameters:
type
- the type whose supertypes to returnelements
- the Element utilities- Returns:
- direct supertypes of
type
-
getAllFieldsIn
Return all fields declared in the given type or any superclass/interface.TODO: should this use javax.lang.model.util.Elements.getAllMembers(TypeElement) instead of our own getSuperTypes?
- Parameters:
type
- the type whose fields to returnelements
- the Element utilities- Returns:
- fields of
type
-
getEnumConstants
Returns all enum constants declared in the given enumeration.- Parameters:
type
- an Enum type- Returns:
- all enum constants declared in the given enumeration
-
getAllMethodsIn
Return all methods declared in the given type or any superclass/interface. Note that no constructors will be returned.TODO: should this use javax.lang.model.util.Elements.getAllMembers(TypeElement) instead of our own getSuperTypes?
- Parameters:
type
- the type whose methods to returnelements
- the Element utilities- Returns:
- methods of
type
-
getAllTypeElementsIn
Return all nested/inner classes/interfaces declared in the given type.- Parameters:
type
- a type- Returns:
- all nested/inner classes/interfaces declared in
type
-
typeElementKinds
Return the set of kinds that represent classes.- Returns:
- the set of kinds that represent classes
-
isClassElement
Deprecated.Is the given element kind a type, i.e., a class, enum, interface, or annotation type.- Parameters:
element
- the element to test- Returns:
- true, iff the given kind is a class kind
-
isTypeElement
Is the given element kind a type, i.e., a class, enum, interface, or annotation type.- Parameters:
element
- the element to test- Returns:
- true, iff the given kind is a class kind
-
isTypeDeclaration
Return true if the element is a type declaration.- Parameters:
elt
- the element to test- Returns:
- true if the argument is a type declaration
-
isLocalVariable
Return true if the element is a local variable.- Parameters:
elt
- the element to test- Returns:
- true if the argument is a local variable
-
isBindingVariable
Return true if the element is a binding variable.This implementation compiles and runs under JDK 8 and 11 as well as versions that contain
ElementKind.BINDING_VARIABLE
.- Parameters:
element
- the element to test- Returns:
- true if the element is a binding variable
-
isRecordAccessor
Returns true if the element is a record accessor method.- Parameters:
methodElement
- a method element- Returns:
- true if the element is a record accessor method
-
isAutoGeneratedRecordMember
Returns true if the givenElement
is part of a record that has been automatically generated by the compiler. This can be a field that is derived from the record's header field list, or an automatically generated canonical constructor.- Parameters:
e
- theElement
for a member of a record- Returns:
- true if the given element is generated by the compiler
-
matchesElement
public static boolean matchesElement(ExecutableElement method, String methodName, Class<?>... parameters) Check that a method Element matches a signature.Note: Matching the receiver type must be done elsewhere as the Element receiver type is only populated when annotated.
- Parameters:
method
- the method Element to be testedmethodName
- the goal method nameparameters
- the goal formal parameter Classes- Returns:
- true if the method matches the methodName and parameters
-
isMethod
public static boolean isMethod(ExecutableElement questioned, ExecutableElement method, ProcessingEnvironment env) Returns true if the given element is, or overrides,method
.- Parameters:
questioned
- an element that might overridemethod
method
- a method that might be overriddenenv
- the processing environment- Returns:
- true if
questioned
is, or overrides,method
-
hasAnnotation
Given an annotation name, return true if the element has the annotation of that name.It is more efficient to use
Element#getAnnotation(Class)
, but note that both methods ignore types from annotation files, such as stub or ajava files.To include types from annotation files, use
AnnotatedTypeFactory#fromElement
orAnnotatedTypeFactory#getDeclAnnotations
.- Parameters:
element
- the elementannotName
- name of the annotation- Returns:
- true if the element has the annotation of that name
-
getTypeElement
Returns the TypeElement for the given class.- Parameters:
processingEnv
- the processing environmentclazz
- a class- Returns:
- the TypeElement for the class
-
getAllSupertypes
Get all the supertypes of a given type, including the type itself. The result includes both superclasses and implemented interfaces.- Parameters:
type
- a typeenv
- the processing environment- Returns:
- list including the type and all its supertypes, with a guarantee that direct supertypes (i.e. those that appear in extends or implements clauses) appear before indirect supertypes
-
getOverriddenMethods
public static Set<? extends ExecutableElement> getOverriddenMethods(ExecutableElement m, Types types) Returns the methods that are overridden or implemented by a given method.- Parameters:
m
- a methodtypes
- the type utilities- Returns:
- the methods that
m
overrides or implements
-
inSameClass
Returns true if the two elements are in the same class. The two elements should be class members, such as methods or fields.- Parameters:
e1
- an elemente2
- an element- Returns:
- true if the two elements are in the same class
-
getKindRecordAsClass
Calls getKind() on the given Element, but returns CLASS if the ElementKind is RECORD. This is needed because the Checker Framework runs on JDKs before the RECORD item was added, so RECORD can't be used in case statements, and usually we want to treat them the same as classes.- Parameters:
elt
- the element to get the kind for- Returns:
- the kind of the element, but CLASS if the kind was RECORD
-
getRecordComponents
Calls getRecordComponents on the given TypeElement. Uses reflection because this method is not available before JDK 16. On earlier JDKs, which don't support records anyway, an exception is thrown.- Parameters:
element
- the type element to call getRecordComponents on- Returns:
- the return value of calling getRecordComponents, or empty list if the method is not available
-
isCompactCanonicalRecordConstructor
Check if the given element is a compact canonical record constructor.- Parameters:
elt
- the element to check- Returns:
- true if the element is a compact canonical constructor of a record
-
isResourceVariable
Returns true iff the given element is a resource variable.- Parameters:
elt
- an element; may be null, in which case this method always returns false- Returns:
- true iff the given element represents a resource variable
-
isGetter
Returns true if the given element is a getter method. A getter method is an instance method with no formal parameters, whose name starts with "get", "is", "not", or "has" followed by an upper-case letter.- Parameters:
methodElt
- a method- Returns:
- true if the given element is a getter method
-
getSimpleDescription(javax.lang.model.element.ExecutableElement)