TypeAnnotation (Java SE 26 & JDK 26)
Description: declaration: module: java.base, package: java.lang.classfile, interface: TypeAnnotation
Keywords: java.lang.classfile.TypeAnnotation interface

# Interface TypeAnnotation

public sealed interface TypeAnnotation
Models a `type_annotation` structure (JVMS 4.7.20). This model indicates the annotated type within a declaration or expression and the part of the indicated type that is annotated, in addition to what is available in an `Annotation`.

This model can reconstruct an annotation on a type or a part of a type, given the location of the `type_annotation` structure in the class file and the definition of the annotation interface.

Two `TypeAnnotation` objects should be compared using the `equals` method.

Since:
  24
See Also:
  - `Annotation`
  - `RuntimeVisibleTypeAnnotationsAttribute`
  - `RuntimeInvisibleTypeAnnotationsAttribute`

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Interface
  Description
  `static interface`
  `TypeAnnotation.CatchTarget`
  Indicates that an annotation appears on the i'th type in an exception parameter declaration.
  `static interface`
  `TypeAnnotation.EmptyTarget`
  Indicates that an annotation appears on either the type in a field declaration, the return type of a method, the type of a newly constructed object, or the receiver type of a method or constructor.
  `static interface`
  `TypeAnnotation.FormalParameterTarget`
  Indicates that an annotation appears on the type in a formal parameter declaration of a method, constructor, or lambda expression.
  `static interface`
  `TypeAnnotation.LocalVarTarget`
  Indicates that an annotation appears on the type in a local variable declaration, including a variable declared as a resource in a try-with-resources statement.
  `static interface`
  `TypeAnnotation.LocalVarTargetInfo`
  Indicates a range of code array offsets within which a local variable has a value, and the index into the local variable array of the current frame at which that local variable can be found.
  `static interface`
  `TypeAnnotation.OffsetTarget`
  Indicates that an annotation appears on either the type in an instanceof expression or a new expression, or the type before the :: in a method reference expression.
  `static interface`
  `TypeAnnotation.SupertypeTarget`
  Indicates that an annotation appears on a type in the extends or implements clause of a class or interface declaration.
  `static interface`
  `TypeAnnotation.TargetInfo`
  Specifies which type in a declaration or expression is being annotated.
  `static enum`
  `TypeAnnotation.TargetType`
  The kind of target on which the annotation appears, as defined in JVMS 4.7.20.1.
  `static interface`
  `TypeAnnotation.ThrowsTarget`
  Indicates that an annotation appears on the i'th type in the throws clause of a method or constructor declaration.
  `static interface`
  `TypeAnnotation.TypeArgumentTarget`
  Indicates that an annotation appears either on the i'th type in a cast expression, or on the i'th type argument in the explicit type argument list for any of the following: a new expression, an explicit constructor invocation statement, a method invocation expression, or a method reference expression.
  `static interface`
  `TypeAnnotation.TypeParameterBoundTarget`
  Indicates that an annotation appears on the i'th bound of the j'th type parameter declaration of a generic class, interface, method, or constructor.
  `static interface`
  `TypeAnnotation.TypeParameterTarget`
  Indicates that an annotation appears on the declaration of the i'th type parameter of a generic class, generic interface, generic method, or generic constructor.
  `static interface`
  `TypeAnnotation.TypePathComponent`
  JVMS: Type_path structure identifies which part of the type is annotated, as defined in JVMS 4.7.20.2

- ## Method Summary

  Modifier and Type
  Method
  Description
  `Annotation`
  `annotation()`
  Returns the annotation applied to the part indicated by `targetPath()`.
  `static TypeAnnotation`
  `of(TypeAnnotation.TargetInfo targetInfo, List<TypeAnnotation.TypePathComponent> targetPath, Annotation annotation)`
  Returns a `type_annotation` structure.
  `TypeAnnotation.TargetInfo`
  `targetInfo()`
  Returns information describing precisely which type in a declaration or expression is annotated.
  `List<TypeAnnotation.TypePathComponent>`
  `targetPath()`
  Returns which part of the type indicated by `targetInfo()` is annotated.

- ## Method Details

  - ### targetInfo

    TypeAnnotation.TargetInfo targetInfo()
    Returns information describing precisely which type in a declaration or expression is annotated. This models the `target_type` and `target_info` items.
    Returns:
      information describing precisely which type in a declaration or expression is annotated

  - ### targetPath

    List<TypeAnnotation.TypePathComponent> targetPath()
    Returns which part of the type indicated by `targetInfo()` is annotated.
    Returns:
      which part of the type indicated by `targetInfo()` is annotated

  - ### annotation

    Annotation annotation()
    Returns the annotation applied to the part indicated by `targetPath()`. This models the interface of the annotation and the set of element-value pairs, the subset of the `type_annotation` structure that is identical to the `annotation` structure.
    Returns:
      the annotation applied to the part indicated by `targetPath()`

  - ### of

    static TypeAnnotation of(TypeAnnotation.TargetInfo targetInfo, List<TypeAnnotation.TypePathComponent> targetPath, Annotation annotation)
    Returns a `type_annotation` structure.
    Parameters:
      `targetInfo` - which type in a declaration or expression is annotated
      `targetPath` - which part of the type is annotated
      `annotation` - the annotation
    Returns:
      a `type_annotation` structure
    Throws:
      `IllegalArgumentException` - if the size of `targetPath` exceeds the limit of `u1`