Class ElementUtils


  • public class ElementUtils
    extends java.lang.Object
    Utility methods for analyzing Elements. This complements Elements, providing functionality that it does not.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static javax.lang.model.element.PackageElement enclosingPackage​(javax.lang.model.element.Element elem)
      Returns the innermost package element enclosing the given element.
      static @Nullable javax.lang.model.element.TypeElement enclosingTypeElement​(javax.lang.model.element.Element elem)
      Returns the innermost type element that is, or encloses, the given element.
      static @Nullable javax.lang.model.element.VariableElement findFieldInType​(javax.lang.model.element.TypeElement type, java.lang.String name)
      Returns the field of the class or null if not found.
      static java.util.Set<javax.lang.model.element.VariableElement> findFieldsInType​(javax.lang.model.element.TypeElement type, java.util.Collection<java.lang.String> names)
      Returns the elements of the fields whose simple names are in names and are declared in type.
      static java.util.Set<javax.lang.model.element.VariableElement> findFieldsInTypeOrSuperType​(javax.lang.model.type.TypeMirror type, java.util.Collection<java.lang.String> names)
      Returns non-private field elements, and side-effects names to remove them.
      static java.util.List<javax.lang.model.element.VariableElement> getAllFieldsIn​(javax.lang.model.element.TypeElement type, javax.lang.model.util.Elements elements)
      Return all fields declared in the given type or any superclass/interface.
      static java.util.List<javax.lang.model.element.ExecutableElement> getAllMethodsIn​(javax.lang.model.element.TypeElement type, javax.lang.model.util.Elements elements)
      Return all methods declared in the given type or any superclass/interface.
      static java.util.List<javax.lang.model.element.TypeElement> getAllSupertypes​(javax.lang.model.element.TypeElement type, javax.annotation.processing.ProcessingEnvironment env)
      Get all the supertypes of a given type, including the type itself.
      static java.util.List<javax.lang.model.element.TypeElement> getAllTypeElementsIn​(javax.lang.model.element.TypeElement type)
      Return all nested/inner classes/interfaces declared in the given type.
      static @BinaryName java.lang.String getBinaryName​(javax.lang.model.element.TypeElement te)
      Returns the binary name of the given type.
      static java.util.List<javax.lang.model.element.TypeElement> getDirectSuperTypeElements​(javax.lang.model.element.TypeElement type, javax.lang.model.util.Elements elements)
      Determine all type elements for the direct supertypes of the given type element.
      static @BinaryName java.lang.String getEnclosingClassName​(javax.lang.model.element.ExecutableElement executableElement)
      Returns the binary name of the class enclosing executableElement.
      static @BinaryName java.lang.String getEnclosingClassName​(javax.lang.model.element.VariableElement variableElement)
      Returns the binary name of the class enclosing variableElement.
      static java.util.List<javax.lang.model.element.VariableElement> getEnumConstants​(javax.lang.model.element.TypeElement type)
      Returns all enum constants declared in the given enumeration.
      static javax.lang.model.element.ElementKind getKindRecordAsClass​(javax.lang.model.element.Element elt)
      Calls getKind() on the given Element, but returns CLASS if the ElementKind is RECORD.
      static java.util.Set<? extends javax.lang.model.element.ExecutableElement> getOverriddenMethods​(javax.lang.model.element.ExecutableElement m, javax.lang.model.util.Types types)
      Returns the methods that are overridden or implemented by a given method.
      static @Nullable javax.lang.model.element.Name getQualifiedClassName​(javax.lang.model.element.Element element)
      Returns the qualified name of the innermost class enclosing the provided Element.
      static java.lang.String getQualifiedName​(javax.lang.model.element.Element elt)
      Returns a verbose name that identifies the element.
      static java.util.List<? extends javax.lang.model.element.Element> getRecordComponents​(javax.lang.model.element.TypeElement element)
      Calls getRecordComponents on the given TypeElement.
      static java.lang.CharSequence getSimpleDescription​(javax.lang.model.element.ExecutableElement element)
      Returns a user-friendly name for the given method, which includes the name of the enclosing type.
      static java.lang.CharSequence getSimpleNameOrDescription​(javax.lang.model.element.ExecutableElement element)
      static java.lang.String getSimpleSignature​(javax.lang.model.element.ExecutableElement element)
      Returns the canonical representation of the method declaration, which contains simple names of the types only.
      static java.lang.String getSourceFilePath​(javax.lang.model.element.TypeElement element)
      Returns the path to the source file containing element, which must be from source code.
      static @Nullable javax.lang.model.element.TypeElement getSuperClass​(javax.lang.model.element.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 java.util.List<javax.lang.model.element.TypeElement> getSuperTypes​(javax.lang.model.element.TypeElement type, javax.lang.model.util.Elements elements)
      Determine all type elements for the supertypes of the given type element.
      static javax.lang.model.type.TypeMirror getType​(javax.lang.model.element.Element element)
      Returns the TypeMirror for usage of Element as a value.
      static javax.lang.model.element.TypeElement getTypeElement​(javax.annotation.processing.ProcessingEnvironment processingEnv, java.lang.Class<?> clazz)
      Returns the TypeElement for the given class.
      static boolean hasAnnotation​(javax.lang.model.element.Element element, java.lang.String annotName)
      Given an annotation name, return true if the element has the annotation of that name.
      static boolean hasReceiver​(javax.lang.model.element.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​(javax.lang.model.element.Element e1, javax.lang.model.element.Element e2)
      Returns true if the two elements are in the same class.
      static boolean isAutoGeneratedRecordMember​(javax.lang.model.element.Element e)
      Returns true if the given Element is part of a record that has been automatically generated by the compiler.
      static boolean isBindingVariable​(javax.lang.model.element.Element element)
      Return true if the element is a binding variable.
      static boolean isCompactCanonicalRecordConstructor​(javax.lang.model.element.Element elt)
      Check if the given element is a compact canonical record constructor.
      static boolean isCompileTimeConstant​(@Nullable javax.lang.model.element.Element elt)
      Returns true if the element is a reference to a compile-time constant.
      static boolean isEffectivelyFinal​(javax.lang.model.element.Element element)
      Returns true if the element is a effectively final element.
      static boolean isElementFromByteCode​(@Nullable javax.lang.model.element.Element elt)
      Returns true if the element is declared in ByteCode.
      static boolean isElementFromSourceCode​(@Nullable javax.lang.model.element.Element element)
      Checks whether a given element came from a source file.
      static boolean isError​(javax.lang.model.element.Element element)
      Returns true if element is "com.sun.tools.javac.comp.Resolve$SymbolNotFoundError".
      static boolean isFinal​(javax.lang.model.element.Element element)
      Returns true if the element is a final element: a final field, final method, or final class.
      static boolean isLocalVariable​(javax.lang.model.element.Element elt)
      Return true if the element is a local variable.
      static boolean isMethod​(javax.lang.model.element.ExecutableElement questioned, javax.lang.model.element.ExecutableElement method, javax.annotation.processing.ProcessingEnvironment env)
      Returns true if the given element is, or overrides, method.
      static boolean isObject​(javax.lang.model.element.TypeElement element)
      Check if the element is an element for 'java.lang.Object'
      static boolean isRecordAccessor​(javax.lang.model.element.ExecutableElement methodElement)
      Returns true if the element is a record accessor method.
      static boolean isRecordComponentElement​(javax.lang.model.element.Element elt)
      Determine whether the given element is of Kind RECORD_COMPONENT, in a way that works on all versions of Java.
      static boolean isRecordElement​(javax.lang.model.element.Element elt)
      Determine whether the given element is of Kind RECORD, in a way that works on all versions of Java.
      static boolean isResourceVariable​(@Nullable javax.lang.model.element.Element elt)
      Returns true iff the given element is a resource variable.
      static boolean isStatic​(javax.lang.model.element.Element element)
      Returns true if the element is a static element: whether it is a static field, static method, or static class.
      static boolean isString​(javax.lang.model.element.TypeElement element)
      Check if the element is an element for 'java.lang.String'
      static boolean isTypeDeclaration​(javax.lang.model.element.Element elt)
      Return true if the element is a type declaration.
      static boolean isTypeElement​(javax.lang.model.element.Element element)
      Is the given element kind a type, i.e., a class, enum, interface, or annotation type.
      static boolean matchesElement​(javax.lang.model.element.ExecutableElement method, java.lang.String methodName, java.lang.Class<?>... parameters)
      Check that a method Element matches a signature.
      static @Nullable javax.lang.model.element.PackageElement parentPackage​(javax.lang.model.element.PackageElement elem, javax.lang.model.util.Elements elements)
      Returns the "parent" package element for the given package element.
      static @Nullable javax.lang.model.element.TypeElement strictEnclosingTypeElement​(javax.lang.model.element.Element elem)
      Returns the innermost type element enclosing the given element, that is different from the element itself.
      static javax.lang.model.element.TypeElement toplevelEnclosingTypeElement​(javax.lang.model.element.Element element)
      Returns the top-level type element that contains element.
      static java.util.Set<javax.lang.model.element.ElementKind> typeElementKinds()
      Return the set of kinds that represent classes.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • enclosingTypeElement

        public static @Nullable javax.lang.model.element.TypeElement enclosingTypeElement​(javax.lang.model.element.Element elem)
        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 returns Outer.
        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

        public static @Nullable javax.lang.model.element.TypeElement strictEnclosingTypeElement​(javax.lang.model.element.Element elem)
        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

        public static javax.lang.model.element.TypeElement toplevelEnclosingTypeElement​(javax.lang.model.element.Element element)
        Returns the top-level type element that contains element.
        Parameters:
        element - the element whose enclosing tye element to find
        Returns:
        a type element containing element that isn't contained in another class
      • getEnclosingClassName

        public static @BinaryName java.lang.String getEnclosingClassName​(javax.lang.model.element.ExecutableElement executableElement)
        Returns the binary name of the class enclosing executableElement.
        Parameters:
        executableElement - the ExecutableElement
        Returns:
        the binary name of the class enclosing executableElement
      • getEnclosingClassName

        public static @BinaryName java.lang.String getEnclosingClassName​(javax.lang.model.element.VariableElement variableElement)
        Returns the binary name of the class enclosing variableElement.
        Parameters:
        variableElement - the VariableElement
        Returns:
        the binary name of the class enclosing variableElement
      • enclosingPackage

        public static javax.lang.model.element.PackageElement enclosingPackage​(javax.lang.model.element.Element elem)
        Returns the innermost package element enclosing the given element. The same effect as Elements.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

        public static @Nullable javax.lang.model.element.PackageElement parentPackage​(javax.lang.model.element.PackageElement elem,
                                                                                      javax.lang.model.util.Elements elements)
        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 from
        elements - the element
        Returns:
        the parent package element or null
      • isStatic

        public static boolean isStatic​(javax.lang.model.element.Element element)
        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

        public static boolean isFinal​(javax.lang.model.element.Element element)
        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

        public static boolean isEffectivelyFinal​(javax.lang.model.element.Element element)
        Returns true if the element is a effectively final element.
        Returns:
        true if the element is effectively final
      • getType

        public static javax.lang.model.type.TypeMirror getType​(javax.lang.model.element.Element element)
        Returns the TypeMirror 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

        public static @Nullable javax.lang.model.element.Name getQualifiedClassName​(javax.lang.model.element.Element element)
        Returns the qualified name of the innermost class enclosing the provided Element.
        Parameters:
        element - an element enclosed by a class, or a TypeElement
        Returns:
        the qualified Name of the innermost class enclosing the element
      • getQualifiedName

        public static java.lang.String getQualifiedName​(javax.lang.model.element.Element elt)
        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

        public static @BinaryName java.lang.String getBinaryName​(javax.lang.model.element.TypeElement te)
        Returns the binary name of the given type.
        Parameters:
        te - a type
        Returns:
        the binary name of the type
      • getSimpleSignature

        public static java.lang.String getSimpleSignature​(javax.lang.model.element.ExecutableElement element)
        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
        public static java.lang.CharSequence getSimpleNameOrDescription​(javax.lang.model.element.ExecutableElement element)
        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

        public static java.lang.CharSequence getSimpleDescription​(javax.lang.model.element.ExecutableElement element)
        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

        public static boolean isObject​(javax.lang.model.element.TypeElement element)
        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

        public static boolean isString​(javax.lang.model.element.TypeElement element)
        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

        public static boolean isCompileTimeConstant​(@Nullable javax.lang.model.element.Element elt)
        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

        public static boolean isElementFromSourceCode​(@Nullable javax.lang.model.element.Element element)
        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

        public static boolean isElementFromByteCode​(@Nullable javax.lang.model.element.Element elt)
        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

        public static java.lang.String getSourceFilePath​(javax.lang.model.element.TypeElement element)
        Returns the path to the source file containing element, which must be from source code.
        Parameters:
        element - the type element to look at
        Returns:
        path to the source file containing element
      • findFieldInType

        public static @Nullable javax.lang.model.element.VariableElement findFieldInType​(javax.lang.model.element.TypeElement type,
                                                                                         java.lang.String name)
        Returns the field of the class or null if 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
      • findFieldsInType

        public static java.util.Set<javax.lang.model.element.VariableElement> findFieldsInType​(javax.lang.model.element.TypeElement type,
                                                                                               java.util.Collection<java.lang.String> names)
        Returns the elements of the fields whose simple names are in names and are declared in type.

        If a field isn't declared in type, its element isn't included in the returned set. If none of the fields is declared in type, 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 names and are declared in type
      • findFieldsInTypeOrSuperType

        public static java.util.Set<javax.lang.model.element.VariableElement> findFieldsInTypeOrSuperType​(javax.lang.model.type.TypeMirror type,
                                                                                                          java.util.Collection<java.lang.String> names)
        Returns non-private field elements, and side-effects names to remove them. For every field name in names that is declared in type or a supertype, add its element to the returned set and remove it from names.

        When this routine returns, the combination of the return value and names has the same cardinality, and represents the same fields, as names did when the method was called.

        Parameters:
        type - where to look for fields
        names - simple names of fields that might be declared in type or a supertype. Names that are found are removed from this list.
        Returns:
        the VariableElements for non-private fields that are declared in type whose simple names were in names when the method was called.
      • isError

        public static boolean isError​(javax.lang.model.element.Element element)
        Returns true if element 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

        public static boolean hasReceiver​(javax.lang.model.element.Element element)
        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

        public static @Nullable javax.lang.model.element.TypeElement getSuperClass​(javax.lang.model.element.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).
        Parameters:
        typeElt - a type element
        Returns:
        the superclass of typeElt
      • getSuperTypes

        public static java.util.List<javax.lang.model.element.TypeElement> getSuperTypes​(javax.lang.model.element.TypeElement type,
                                                                                         javax.lang.model.util.Elements elements)
        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 return
        elements - the Element utilities
        Returns:
        supertypes of type
      • getDirectSuperTypeElements

        public static java.util.List<javax.lang.model.element.TypeElement> getDirectSuperTypeElements​(javax.lang.model.element.TypeElement type,
                                                                                                      javax.lang.model.util.Elements elements)
        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 return
        elements - the Element utilities
        Returns:
        direct supertypes of type
      • getAllFieldsIn

        public static java.util.List<javax.lang.model.element.VariableElement> getAllFieldsIn​(javax.lang.model.element.TypeElement type,
                                                                                              javax.lang.model.util.Elements elements)
        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 return
        elements - the Element utilities
        Returns:
        fields of type
      • getEnumConstants

        public static java.util.List<javax.lang.model.element.VariableElement> getEnumConstants​(javax.lang.model.element.TypeElement type)
        Returns all enum constants declared in the given enumeration.
        Parameters:
        type - an Enum type
        Returns:
        all enum constants declared in the given enumeration
      • getAllMethodsIn

        public static java.util.List<javax.lang.model.element.ExecutableElement> getAllMethodsIn​(javax.lang.model.element.TypeElement type,
                                                                                                 javax.lang.model.util.Elements elements)
        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 return
        elements - the Element utilities
        Returns:
        methods of type
      • getAllTypeElementsIn

        public static java.util.List<javax.lang.model.element.TypeElement> getAllTypeElementsIn​(javax.lang.model.element.TypeElement type)
        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

        public static java.util.Set<javax.lang.model.element.ElementKind> typeElementKinds()
        Return the set of kinds that represent classes.
        Returns:
        the set of kinds that represent classes
      • isTypeElement

        public static boolean isTypeElement​(javax.lang.model.element.Element element)
        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

        public static boolean isTypeDeclaration​(javax.lang.model.element.Element elt)
        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

        public static boolean isLocalVariable​(javax.lang.model.element.Element elt)
        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

        public static boolean isBindingVariable​(javax.lang.model.element.Element element)
        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

        public static boolean isRecordAccessor​(javax.lang.model.element.ExecutableElement methodElement)
        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

        public static boolean isAutoGeneratedRecordMember​(javax.lang.model.element.Element e)
        Returns true if the given Element 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 - the Element for a member of a record
        Returns:
        true if the given element is generated by the compiler
      • matchesElement

        public static boolean matchesElement​(javax.lang.model.element.ExecutableElement method,
                                             java.lang.String methodName,
                                             java.lang.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
      • isMethod

        public static boolean isMethod​(javax.lang.model.element.ExecutableElement questioned,
                                       javax.lang.model.element.ExecutableElement method,
                                       javax.annotation.processing.ProcessingEnvironment env)
        Returns true if the given element is, or overrides, method.
      • hasAnnotation

        public static boolean hasAnnotation​(javax.lang.model.element.Element element,
                                            java.lang.String annotName)
        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 or AnnotatedTypeFactory#getDeclAnnotations.

        Parameters:
        element - the element
        annotName - name of the annotation
        Returns:
        true if the element has the annotation of that name
      • getTypeElement

        public static javax.lang.model.element.TypeElement getTypeElement​(javax.annotation.processing.ProcessingEnvironment processingEnv,
                                                                          java.lang.Class<?> clazz)
        Returns the TypeElement for the given class.
        Parameters:
        processingEnv - the processing environment
        clazz - a class
        Returns:
        the TypeElement for the class
      • getAllSupertypes

        public static java.util.List<javax.lang.model.element.TypeElement> getAllSupertypes​(javax.lang.model.element.TypeElement type,
                                                                                            javax.annotation.processing.ProcessingEnvironment env)
        Get 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
      • getOverriddenMethods

        public static java.util.Set<? extends javax.lang.model.element.ExecutableElement> getOverriddenMethods​(javax.lang.model.element.ExecutableElement m,
                                                                                                               javax.lang.model.util.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 m overrides or implements
      • inSameClass

        public static boolean inSameClass​(javax.lang.model.element.Element e1,
                                          javax.lang.model.element.Element e2)
        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 element
        e2 - an element
        Returns:
        true if the two elements are in the same class
      • isRecordElement

        public static boolean isRecordElement​(javax.lang.model.element.Element elt)
        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

        public static boolean isRecordComponentElement​(javax.lang.model.element.Element elt)
        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

        public static javax.lang.model.element.ElementKind getKindRecordAsClass​(javax.lang.model.element.Element elt)
        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

        public static java.util.List<? extends javax.lang.model.element.Element> getRecordComponents​(javax.lang.model.element.TypeElement element)
        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

        public static boolean isCompactCanonicalRecordConstructor​(javax.lang.model.element.Element elt)
        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

        public static boolean isResourceVariable​(@Nullable javax.lang.model.element.Element elt)
        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