Class ElementUtils
Elements. This complements Elements, providing
functionality that it does not.-
Method Summary
Modifier and TypeMethodDescriptionstatic PackageElementenclosingPackage(Element elem) Returns the innermost package element enclosing the given element.static @Nullable TypeElementenclosingTypeElement(Element elem) Returns the innermost type element that is, or encloses, the given element.static @Nullable VariableElementfindFieldInType(TypeElement type, String name) Returns the field of the class ornullif not found.static Set<VariableElement> findFieldsInType(TypeElement type, Collection<String> names) Returns the elements of the fields whose simple names are innamesand are declared intype.static Set<VariableElement> findFieldsInTypeOrSuperType(TypeMirror type, Collection<String> names) Returns non-private field elements, and side-effectsnamesto 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 StringReturns 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 StringgetEnclosingClassName(ExecutableElement executableElement) Returns the binary name of the class enclosingexecutableElement.static @BinaryName StringgetEnclosingClassName(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 ElementKindCalls 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 StringgetQualifiedName(Element elt) Returns a verbose name that identifies the element.getRecordComponents(TypeElement element) Calls getRecordComponents on the given TypeElement.static CharSequencegetSimpleDescription(ExecutableElement element) Returns a user-friendly name for the given method, which includes the name of the enclosing type.static CharSequenceDeprecated.static StringgetSimpleSignature(ExecutableElement element) Returns the canonical representation of the method declaration, which contains simple names of the types only.static StringgetSourceFilePath(TypeElement element) Returns the path to the source file containingelement, which must be from source code.static @Nullable TypeElementgetSuperClass(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 TypeMirrorReturns theTypeMirrorfor usage of Element as a value.static TypeElementgetTypeElement(ProcessingEnvironment processingEnv, Class<?> clazz) Returns the TypeElement for the given class.static booleanhasAnnotation(Element element, String annotName) Given an annotation name, return true if the element has the annotation of that name.static booleanhasReceiver(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 booleaninSameClass(Element e1, Element e2) Returns true if the two elements are in the same class.static booleanReturns true if the givenElementis part of a record that has been automatically generated by the compiler.static booleanisBindingVariable(Element element) Return true if the element is a binding variable.static booleanCheck if the given element is a compact canonical record constructor.static booleanReturns true if the element is a reference to a compile-time constant.static booleanisEffectivelyFinal(Element element) Returns true if the element is a effectively final element.static booleanReturns true if the element is declared in ByteCode.static booleanisElementFromSourceCode(@Nullable Element element) Checks whether a given element came from a source file.static booleanReturns true ifelementis "com.sun.tools.javac.comp.Resolve$SymbolNotFoundError".static booleanReturns true if the element is a final element: a final field, final method, or final class.static booleanisGetter(@Nullable ExecutableElement methodElt) Returns true if the given element is a getter method.static booleanisLocalVariable(Element elt) Return true if the element is a local variable.static booleanisMethod(ExecutableElement questioned, ExecutableElement method, ProcessingEnvironment env) Returns true if the given element is, or overrides,method.static booleanisObject(TypeElement element) Check if the element is an element for 'java.lang.Object'static booleanisRecordAccessor(ExecutableElement methodElement) Returns true if the element is a record accessor method.static booleanDetermine whether the given element is of Kind RECORD_COMPONENT, in a way that works on all versions of Java.static booleanisRecordElement(Element elt) Determine whether the given element is of Kind RECORD, in a way that works on all versions of Java.static booleanReturns true iff the given element is a resource variable.static booleanReturns true if the element is a static element: whether it is a static field, static method, or static class.static booleanisString(TypeElement element) Check if the element is an element for 'java.lang.String'static booleanisTypeDeclaration(Element elt) Return true if the element is a type declaration.static booleanisTypeElement(Element element) Is the given element kind a type, i.e., a class, enum, interface, or annotation type.static booleanmatchesElement(ExecutableElement method, String methodName, Class<?>... parameters) Check that a method Element matches a signature.static @Nullable PackageElementparentPackage(PackageElement elem, Elements elements) Returns the "parent" package element for the given package element.static @Nullable TypeElementReturns the innermost type element enclosing the given element, that is different from the element itself.static TypeElementtoplevelEnclosingTypeElement(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 { } }Innerhas 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
elemis 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
elementthat 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, final 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 theTypeMirrorfor 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
Nameof 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 ornullif 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 innamesand 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
namesand are declared intype
-
findFieldsInTypeOrSuperType
public static Set<VariableElement> findFieldsInTypeOrSuperType(TypeMirror type, Collection<String> names) Returns non-private field elements, and side-effectsnamesto remove them. For every field name innamesthat is declared intypeor a supertype, add its element to the returned set and remove it fromnames.When this routine returns, the combination of the return value and
nameshas the same cardinality, and represents the same fields, asnamesdid when the method was called.- Parameters:
type- where to look for fieldsnames- simple names of fields that might be declared intypeor a supertype. Names that are found are removed from this list.- Returns:
- the
VariableElements for non-private fields that are declared intypewhose simple names were innameswhen the method was called.
-
isError
Returns true ifelementis "com.sun.tools.javac.comp.Resolve$SymbolNotFoundError".- Parameters:
element- the element to test- Returns:
- true if
elementis "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
-
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 givenElementis 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- theElementfor 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 overridemethodmethod- a method that might be overriddenenv- the processing environment- Returns:
- true if
questionedis, 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#fromElementorAnnotatedTypeFactory#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
moverrides 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
-
isRecordElement
Determine whether the given element is of Kind RECORD, in a way that works on all versions of Java.- Parameters:
elt- the element to test- Returns:
- whether the element is of the kind RECORD
-
isRecordComponentElement
Determine whether the given element is of Kind RECORD_COMPONENT, in a way that works on all versions of Java.- Parameters:
elt- the element to test- Returns:
- whether the element is of the kind RECORD_COMPONENT
-
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)