ElementVisitor (Java SE 23 & JDK 23)
Description: declaration: module: java.compiler, package: javax.lang.model.element, interface: ElementVisitor
Keywords: javax.lang.model.element.ElementVisitor interface

# Interface ElementVisitor<R,P>

Type Parameters:
  `R` - the return type of this visitor's methods. Use `Void` for visitors that do not need to return results.
  `P` - the type of the additional parameter to this visitor's methods. Use `Void` for visitors that do not need an additional parameter.

All Known Implementing Classes:
  `AbstractElementVisitor14`, `AbstractElementVisitor6`, `AbstractElementVisitor7`, `AbstractElementVisitor8`, `AbstractElementVisitor9`, `AbstractElementVisitorPreviewPREVIEW`, `ElementKindVisitor14`, `ElementKindVisitor6`, `ElementKindVisitor7`, `ElementKindVisitor8`, `ElementKindVisitor9`, `ElementKindVisitorPreviewPREVIEW`, `ElementScanner14`, `ElementScanner6`, `ElementScanner7`, `ElementScanner8`, `ElementScanner9`, `ElementScannerPreviewPREVIEW`, `SimpleElementVisitor14`, `SimpleElementVisitor6`, `SimpleElementVisitor7`, `SimpleElementVisitor8`, `SimpleElementVisitor9`, `SimpleElementVisitorPreviewPREVIEW`

public interface ElementVisitor<R,P>
A visitor of program elements, in the style of the visitor design pattern. Classes implementing this interface are used to operate on an element when the kind of element is unknown at compile time. When a visitor is passed to an element's `accept` method, the `visitXyz` method most applicable to that element is invoked.

Classes implementing this interface may or may not throw a `NullPointerException` if the additional parameter `p` is `null`; see documentation of the implementing class for details.

API Note:
  WARNING: It is possible that methods will be added to this interface to accommodate new, currently unknown, language structures added to future versions of the Java programming language. Such additions have already occurred to support language features added after this API was introduced. Visitor classes directly implementing this interface may be source incompatible with future versions of the platform. To avoid this source incompatibility, visitor implementations are encouraged to instead extend the appropriate abstract visitor class that implements this interface. However, an API should generally use this visitor interface as the type for parameters, return type, etc. rather than one of the abstract classes.

  Methods to accommodate new language constructs are expected to be added as default methods to provide strong source compatibility, as done for `visitModule` and `visitRecordComponent`. The implementations of the default methods will in turn call `visitUnknown`, behavior that will be overridden in concrete visitors supporting the source version with the new language construct.

  There are several families of classes implementing this visitor interface in the util package. The families follow a naming pattern along the lines of `FooVisitor`N where N indicates the source version the visitor is appropriate for. In particular, a `FooVisitor`N is expected to handle all language constructs present in source version N. If there are no new language constructs added in version N + 1 (or subsequent releases), `FooVisitor`N may also handle that later source version; in that case, the `SupportedSourceVersion` annotation on the `FooVisitor`N class will indicate a later version. When visiting an element representing a language construct introduced after source version N, a `FooVisitor`N will throw an `UnknownElementException` unless that behavior is overridden.

  When choosing which member of a visitor family to subclass, subclassing the most recent one increases the range of source versions covered. When choosing which visitor family to subclass, consider their built-in capabilities:

  - `AbstractElementVisitor`s: Skeletal visitor implementations.
  - `SimpleElementVisitor`s: Support default actions and a default return value.
  - `ElementKindVisitor`s: Visit methods provided on a per-kind granularity as some categories of elements can have more than one kind.
  - `ElementScanner`s: Scanners are visitors which traverse an element and the elements enclosed by it and associated with it.
Since:
  1.6

- ## Method Summary

  Modifier and Type
  Method
  Description
  `default R`
  `visit(Element e)`
  A convenience method equivalent to `visit(e, null)`.
  `R`
  `visit(Element e, P p)`
  Visits an element.
  `R`
  `visitExecutable(ExecutableElement e, P p)`
  Visits an executable element.
  `default R`
  `visitModule(ModuleElement e, P p)`
  Visits a module element.
  `R`
  `visitPackage(PackageElement e, P p)`
  Visits a package element.
  `default R`
  `visitRecordComponent(RecordComponentElement e, P p)`
  Visits a record component element.
  `R`
  `visitType(TypeElement e, P p)`
  Visits a type element.
  `R`
  `visitTypeParameter(TypeParameterElement e, P p)`
  Visits a type parameter element.
  `R`
  `visitUnknown(Element e, P p)`
  Visits an unknown kind of element.
  `R`
  `visitVariable(VariableElement e, P p)`
  Visits a variable element.

- ## Method Details

  - ### visit

    R visit(Element e, P p)
    Visits an element.
    Parameters:
      `e` - the element to visit
      `p` - a visitor-specified parameter
    Returns:
      a visitor-specified result

  - ### visit

    default R visit(Element e)
    A convenience method equivalent to `visit(e, null)`.
    Implementation Requirements:
      The default implementation is `visit(e, null)`.
    Parameters:
      `e` - the element to visit
    Returns:
      a visitor-specified result

  - ### visitPackage

    R visitPackage(PackageElement e, P p)
    Visits a package element.
    Parameters:
      `e` - the element to visit
      `p` - a visitor-specified parameter
    Returns:
      a visitor-specified result

  - ### visitType

    R visitType(TypeElement e, P p)
    Visits a type element.
    Parameters:
      `e` - the element to visit
      `p` - a visitor-specified parameter
    Returns:
      a visitor-specified result

  - ### visitVariable

    R visitVariable(VariableElement e, P p)
    Visits a variable element.
    Parameters:
      `e` - the element to visit
      `p` - a visitor-specified parameter
    Returns:
      a visitor-specified result

  - ### visitExecutable

    R visitExecutable(ExecutableElement e, P p)
    Visits an executable element.
    Parameters:
      `e` - the element to visit
      `p` - a visitor-specified parameter
    Returns:
      a visitor-specified result

  - ### visitTypeParameter

    R visitTypeParameter(TypeParameterElement e, P p)
    Visits a type parameter element.
    Parameters:
      `e` - the element to visit
      `p` - a visitor-specified parameter
    Returns:
      a visitor-specified result

  - ### visitUnknown

    R visitUnknown(Element e, P p)
    Visits an unknown kind of element. This can occur if the language evolves and new kinds of elements are added to the `Element` hierarchy.
    Parameters:
      `e` - the element to visit
      `p` - a visitor-specified parameter
    Returns:
      a visitor-specified result
    Throws:
      `UnknownElementException` - a visitor implementation may optionally throw this exception

  - ### visitModule

    default R visitModule(ModuleElement e, P p)
    Visits a module element.
    Implementation Requirements:
      The default implementation visits a `ModuleElement` by calling `visitUnknown(e, p)`.
    Parameters:
      `e` - the element to visit
      `p` - a visitor-specified parameter
    Returns:
      a visitor-specified result
    Since:
      9

  - ### visitRecordComponent

    default R visitRecordComponent(RecordComponentElement e, P p)
    Visits a record component element.
    Implementation Requirements:
      The default implementation visits a `RecordComponentElement` by calling `visitUnknown(e, p)`.
    Parameters:
      `e` - the element to visit
      `p` - a visitor-specified parameter
    Returns:
      a visitor-specified result
    Since:
      16