Module java.compiler
Package javax.lang.model.element

# Interface Element

All Superinterfaces:
  `AnnotatedConstruct`

All Known Subinterfaces:
  `ExecutableElement`, `ModuleElement`, `PackageElement`, `Parameterizable`, `QualifiedNameable`, `RecordComponentElement`, `TypeElement`, `TypeParameterElement`, `VariableElement`

public interface Element extends AnnotatedConstruct
Represents a program element such as a module, package, class, or method. Each element represents a compile-time language-level construct (and not, for example, a runtime construct of the virtual machine).

Elements should be compared using the `equals(Object)` method. There is no guarantee that any particular element will always be represented by the same object.

To implement operations based on the class of an `Element` object, either use a visitor or use the result of the `getKind()` method. Using `instanceof` is not necessarily a reliable idiom for determining the effective class of an object in this modeling hierarchy since an implementation may choose to have a single object implement multiple `Element` subinterfaces.

Since:
  1.6
See Also:
  - `Elements`
  - `TypeMirror`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `<R, P> R`
  `accept(ElementVisitor<R,P> v, P p)`
  Applies a visitor to this element.
  `TypeMirror`
  `asType()`
  Returns the type defined by this element.
  `boolean`
  `equals(Object obj)`
  Returns `true` if the argument represents the same element as `this`, or `false` otherwise.
  `<A extends Annotation>
  A`
  `getAnnotation(Class<A> annotationType)`
  Returns this construct's annotation of the specified type if such an annotation is present, else `null`.
  `List<? extends AnnotationMirror>`
  `getAnnotationMirrors()`
  Returns the annotations that are directly present on this construct.
  `<A extends Annotation>
  A[]`
  `getAnnotationsByType(Class<A> annotationType)`
  Returns annotations that are associated with this construct.
  `List<? extends Element>`
  `getEnclosedElements()`
  Returns the elements that are, loosely speaking, directly enclosed by this element.
  `Element`
  `getEnclosingElement()`
  Returns the innermost element within which this element is, loosely speaking, enclosed.
  `ElementKind`
  `getKind()`
  Returns the `kind` of this element.
  `Set<Modifier>`
  `getModifiers()`
  Returns the modifiers of this element, excluding annotations.
  `Name`
  `getSimpleName()`
  Returns the simple (unqualified) name of this element.
  `int`
  `hashCode()`
  Obeys the general contract of `Object.hashCode`.

- ## Method Details

  - ### asType

    TypeMirror asType()
    Returns the type defined by this element.
    Returns:
      the type defined by this element
    See Also:
      - `Types`
      - `ExecutableElement.asType()`
      - `ModuleElement.asType()`
      - `PackageElement.asType()`
      - `TypeElement.asType()`
      - `TypeParameterElement.asType()`
      - `VariableElement.asType()`

  - ### getKind

    ElementKind getKind()
    Returns the `kind` of this element.
    - The kind of a package is `PACKAGE`.
    - The kind of a module is `MODULE`.
    - The kind of a type element is one of `ANNOTATION_TYPE`, `CLASS`, `ENUM`, `INTERFACE`, or `RECORD`.
    - The kind of a variable is one of `ENUM_CONSTANT`, `EXCEPTION_PARAMETER`, `FIELD`, `LOCAL_VARIABLE`, `PARAMETER`, `RESOURCE_VARIABLE`, or `BINDING_VARIABLE`.
    - The kind of an executable is one of `CONSTRUCTOR`, `INSTANCE_INIT`, `METHOD`, or `STATIC_INIT`.
    - The kind of a type parameter is `TYPE_PARAMETER`.
    - The kind of a record component is `RECORD_COMPONENT`.
    Returns:
      the `kind` of this element

  - ### getModifiers

    Set<Modifier> getModifiers()
    Returns the modifiers of this element, excluding annotations. Implicit modifiers, such as the `public` and `static` modifiers of interface members (JLS section 9.3), are included.
    Returns:
      the modifiers of this element, or an empty set if there are none

  - ### getSimpleName

    Name getSimpleName()
    Returns the simple (unqualified) name of this element. The name of a generic class or interface does not include any reference to its formal type parameters. For example, the simple name of the type element representing `java.util.Set<E>` is `"Set"`. If this element represents an unnamed package or unnamed module, an empty name is returned. If it represents a constructor, the name "`<init>`" is returned. If it represents a static initializer, the name "`<clinit>`" is returned. If it represents an anonymous class or instance initializer, an empty name is returned.
    Returns:
      the simple (unqualified) name of this element
    See Also:
      - `PackageElement.getSimpleName()`
      - `ExecutableElement.getSimpleName()`
      - `TypeElement.getSimpleName()`
      - `VariableElement.getSimpleName()`
      - `ModuleElement.getSimpleName()`
      - `RecordComponentElement.getSimpleName()`

  - ### getEnclosingElement

    Element getEnclosingElement()
    Returns the innermost element within which this element is, loosely speaking, enclosed.
    - If this element is one whose declaration is lexically enclosed immediately within the declaration of another element, that other element is returned.
    - If this is a top-level class or interface, its package is returned.
    - If this is a package, its module is returned if such a module exists. Otherwise, `null` is returned.
    - If this is a type parameter, the generic element of the type parameter is returned.
    - If this is a method or constructor parameter, the executable element which declares the parameter is returned.
    - If this is a record component, the record class which declares the record component is returned.
    - If this is a module, `null` is returned.
    Returns:
      the enclosing element, or `null` if there is none
    See Also:
      - `Elements.getPackageOf(javax.lang.model.element.Element)`

  - ### getEnclosedElements

    List<? extends Element> getEnclosedElements()
    Returns the elements that are, loosely speaking, directly enclosed by this element. A class or interface is considered to enclose the fields, methods, constructors, record components, and member classes and interfaces that it directly declares. A package encloses the top-level classes and interfaces within it, but is not considered to enclose subpackages. A module encloses packages within it. Enclosed elements may include implicitly declared mandated elements. Other kinds of elements are not currently considered to enclose any elements; however, that may change as this API or the programming language evolves.
    API Note:
      Elements of certain kinds can be isolated using methods in `ElementFilter`.
    Returns:
      the enclosed elements, or an empty list if none
    See Java Language Specification:
      8.8.9 Default Constructor
      8.9 Enum Classes
      8.10 Record Classes
    See Also:
      - `TypeElement.getEnclosedElements()`
      - `PackageElement.getEnclosedElements()`
      - `ModuleElement.getEnclosedElements()`
      - `Elements.getAllMembers(javax.lang.model.element.TypeElement)`

  - ### equals

    boolean equals(Object obj)
    Returns `true` if the argument represents the same element as `this`, or `false` otherwise.
    Overrides:
      `equals` in class `Object`
    API Note:
      The identity of an element involves implicit state not directly accessible from the element's methods, including state about the presence of unrelated types. Element objects created by different implementations of these interfaces should not be expected to be equal even if "the same" element is being modeled; this is analogous to the inequality of `Class` objects for the same class file loaded through different class loaders.
    Parameters:
      `obj` - the object to be compared with this element
    Returns:
      `true` if the argument represents the same element as `this`, or `false` otherwise
    See Also:
      - `Object.hashCode()`
      - `HashMap`

  - ### hashCode

    int hashCode()
    Obeys the general contract of `Object.hashCode`.
    Overrides:
      `hashCode` in class `Object`
    Returns:
      a hash code value for this object.
    See Also:
      - `equals(java.lang.Object)`

  - ### getAnnotationMirrors

    List<? extends AnnotationMirror> getAnnotationMirrors()
    Returns the annotations that are directly present on this construct.

    To get inherited annotations as well, use `getAllAnnotationMirrors`.

    Note that any annotations returned by this method are declaration annotations.

    Specified by:
      `getAnnotationMirrors` in interface `AnnotatedConstruct`
    Returns:
      the annotations directly present on this construct; an empty list if there are none
    Since:
      1.6

  - ### getAnnotation

    <A extends Annotation> A getAnnotation(Class<A> annotationType)
    Returns this construct's annotation of the specified type if such an annotation is present, else `null`.

    The annotation returned by this method could contain an element whose value is of type `Class`. This value cannot be returned directly: information necessary to locate and load a class (such as the class loader to use) is not available, and the class might not be loadable at all. Attempting to read a `Class` object by invoking the relevant method on the returned annotation will result in a `MirroredTypeException`, from which the corresponding `TypeMirror` may be extracted. Similarly, attempting to read a `Class[]`-valued element will result in a `MirroredTypesException`.

    > Note: This method is unlike others in this and related interfaces. It operates on runtime reflective information — representations of annotation interfaces currently loaded into the VM — rather than on the representations defined by and used throughout these interfaces. Consequently, calling methods on the returned annotation object can throw many of the exceptions that can be thrown when calling methods on an annotation object returned by core reflection. This method is intended for callers that are written to operate on a known, fixed set of annotation interfaces.

    Note that any annotation returned by this method is a declaration annotation.

    Specified by:
      `getAnnotation` in interface `AnnotatedConstruct`
    Type Parameters:
      `A` - the annotation interface
    Parameters:
      `annotationType` - the `Class` object corresponding to the annotation interface
    Returns:
      this construct's annotation of the specified type if such an annotation is present, else `null`
    Since:
      1.6
    See Also:
      - `AnnotatedConstruct.getAnnotationMirrors()`
      - `AnnotatedElement.getAnnotation(java.lang.Class<T>)`
      - `EnumConstantNotPresentException`
      - `AnnotationTypeMismatchException`
      - `IncompleteAnnotationException`
      - `MirroredTypeException`
      - `MirroredTypesException`

  - ### getAnnotationsByType

    <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType)
    Returns annotations that are associated with this construct. If there are no annotations associated with this construct, the return value is an array of length 0. The order of annotations which are directly or indirectly present on a construct C is computed as if indirectly present annotations on C are directly present on C in place of their container annotation, in the order in which they appear in the value element of the container annotation. The difference between this method and `AnnotatedConstruct.getAnnotation(Class)` is that this method detects if its argument is a repeatable annotation interface, and if so, attempts to find one or more annotations of that type by "looking through" a container annotation.

    The annotations returned by this method could contain an element whose value is of type `Class`. This value cannot be returned directly: information necessary to locate and load a class (such as the class loader to use) is not available, and the class might not be loadable at all. Attempting to read a `Class` object by invoking the relevant method on the returned annotation will result in a `MirroredTypeException`, from which the corresponding `TypeMirror` may be extracted. Similarly, attempting to read a `Class[]`-valued element will result in a `MirroredTypesException`.

    > Note: This method is unlike others in this and related interfaces. It operates on runtime reflective information — representations of annotation interfaces currently loaded into the VM — rather than on the representations defined by and used throughout these interfaces. Consequently, calling methods on the returned annotation object can throw many of the exceptions that can be thrown when calling methods on an annotation object returned by core reflection. This method is intended for callers that are written to operate on a known, fixed set of annotation interfaces.

    Note that any annotations returned by this method are declaration annotations.

    Specified by:
      `getAnnotationsByType` in interface `AnnotatedConstruct`
    Type Parameters:
      `A` - the annotation interface
    Parameters:
      `annotationType` - the `Class` object corresponding to the annotation interface
    Returns:
      this construct's annotations for the specified annotation type if present on this construct, else an empty array
    Since:
      1.8
    See Also:
      - `AnnotatedConstruct.getAnnotationMirrors()`
      - `AnnotatedConstruct.getAnnotation(Class)`
      - `AnnotatedElement.getAnnotationsByType(Class)`
      - `EnumConstantNotPresentException`
      - `AnnotationTypeMismatchException`
      - `IncompleteAnnotationException`
      - `MirroredTypeException`
      - `MirroredTypesException`

  - ### accept

    <R, P> R accept(ElementVisitor<R,P> v, P p)
    Applies a visitor to this element.
    Type Parameters:
      `R` - the return type of the visitor's methods
      `P` - the type of the additional parameter to the visitor's methods
    Parameters:
      `v` - the visitor operating on this element
      `p` - additional parameter to the visitor
    Returns:
      a visitor-specified result