Class ElementUtils
Elements. This complements Elements, providing
 functionality that it does not.- 
Method SummaryModifier 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 booleanisClassElement(Element element) Deprecated.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, method, or final class.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 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- 
enclosingTypeElementReturns 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
 
- 
strictEnclosingTypeElementReturns 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
 
- 
toplevelEnclosingTypeElementReturns 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
 
- 
getEnclosingClassNameReturns the binary name of the class enclosingexecutableElement.- Parameters:
- executableElement- the ExecutableElement
- Returns:
- the binary name of the class enclosing executableElement
 
- 
getEnclosingClassNameReturns the binary name of the class enclosingvariableElement.- Parameters:
- variableElement- the VariableElement
- Returns:
- the binary name of the class enclosing variableElement
 
- 
enclosingPackageReturns 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
 
- 
parentPackageReturns 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 from
- elements- the element
- Returns:
- the parent package element or null
 
- 
isStaticReturns 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
 
- 
isFinalReturns true if the element is a final element: a final field, method, or final class.- Returns:
- true if the element is final
 
- 
isEffectivelyFinalReturns true if the element is a effectively final element.- Returns:
- true if the element is effectively final
 
- 
getTypeReturns 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
 
- 
getQualifiedClassNameReturns the qualified name of the innermost class enclosing the providedElement.- Parameters:
- element- an element enclosed by a class, or a- TypeElement
- Returns:
- the qualified Nameof the innermost class enclosing the element
 
- 
getQualifiedNameReturns a verbose name that identifies the element.- Parameters:
- elt- the element whose name to obtain
- Returns:
- the qualified name of the given element
 
- 
getBinaryNameReturns the binary name of the given type.- Parameters:
- te- a type
- Returns:
- the binary name of the type
 
- 
getSimpleSignatureReturns 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
 
- 
getSimpleNameOrDescriptionDeprecated.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
 
- 
getSimpleDescriptionReturns 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
 
- 
isObjectCheck 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
 
- 
isStringCheck 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
 
- 
isCompileTimeConstantReturns 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
 
- 
isElementFromSourceCodeChecks 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
 
- 
isElementFromByteCodeReturns 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
 
- 
getSourceFilePathReturns 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
 
- 
findFieldInTypeReturns the field of the class ornullif not found.- Parameters:
- type- the TypeElement to search
- name- name of a field
- Returns:
- the VariableElement for the field if it was found, null otherwise
 
- 
findFieldsInTypeReturns 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 fields
- names- simple names of fields that might be declared in- type
- Returns:
- the elements of the fields whose simple names are namesand are declared intype
 
- 
findFieldsInTypeOrSuperTypepublic 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 fields
- names- simple names of fields that might be declared in- typeor 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.
 
- 
isErrorReturns 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"
 
- 
hasReceiverDoes 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
 
- 
getSuperClassReturns 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
 
- 
getSuperTypesDetermine 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 return
- elements- the Element utilities
- Returns:
- supertypes of type
 
- 
getDirectSuperTypeElementsDetermine 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 return
- elements- the Element utilities
- Returns:
- direct supertypes of type
 
- 
getAllFieldsInReturn 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 return
- elements- the Element utilities
- Returns:
- fields of type
 
- 
getEnumConstantsReturns all enum constants declared in the given enumeration.- Parameters:
- type- an Enum type
- Returns:
- all enum constants declared in the given enumeration
 
- 
getAllMethodsInReturn 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 return
- elements- the Element utilities
- Returns:
- methods of type
 
- 
getAllTypeElementsInReturn all nested/inner classes/interfaces declared in the given type.- Parameters:
- type- a type
- Returns:
- all nested/inner classes/interfaces declared in type
 
- 
typeElementKindsReturn the set of kinds that represent classes.- Returns:
- the set of kinds that represent classes
 
- 
isClassElementDeprecated.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
 
- 
isTypeElementIs 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
 
- 
isTypeDeclarationReturn true if the element is a type declaration.- Parameters:
- elt- the element to test
- Returns:
- true if the argument is a type declaration
 
- 
isLocalVariableReturn true if the element is a local variable.- Parameters:
- elt- the element to test
- Returns:
- true if the argument is a local variable
 
- 
isBindingVariableReturn 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
 
- 
isRecordAccessorReturns true if the element is a record accessor method.- Parameters:
- methodElement- a method element
- Returns:
- true if the element is a record accessor method
 
- 
isAutoGeneratedRecordMemberReturns 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- the- Elementfor a member of a record
- Returns:
- true if the given element is generated by the compiler
 
- 
matchesElementpublic 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 tested
- methodName- the goal method name
- parameters- the goal formal parameter Classes
- Returns:
- true if the method matches the methodName and parameters
 
- 
isMethodpublic static boolean isMethod(ExecutableElement questioned, ExecutableElement method, ProcessingEnvironment env) Returns true if the given element is, or overrides, method.
- 
hasAnnotationGiven 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 element
- annotName- name of the annotation
- Returns:
- true if the element has the annotation of that name
 
- 
getTypeElementReturns the TypeElement for the given class.- Parameters:
- processingEnv- the processing environment
- clazz- a class
- Returns:
- the TypeElement for the class
 
- 
getAllSupertypesGet all the supertypes of a given type, including the type itself. The result includes both superclasses and implemented interfaces.- Parameters:
- type- a type
- env- 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
 
- 
getOverriddenMethodspublic static Set<? extends ExecutableElement> getOverriddenMethods(ExecutableElement m, Types types) Returns the methods that are overridden or implemented by a given method.- Parameters:
- m- a method
- types- the type utilities
- Returns:
- the methods that moverrides or implements
 
- 
inSameClassReturns 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 element
- e2- an element
- Returns:
- true if the two elements are in the same class
 
- 
getKindRecordAsClassCalls 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
 
- 
getRecordComponentsCalls 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
 
- 
isCompactCanonicalRecordConstructorCheck 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
 
- 
isResourceVariableReturns 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
 
 
- 
getSimpleDescription(javax.lang.model.element.ExecutableElement)