Class TypeTarget

java.lang.Object
org.jboss.jandex.TypeTarget
All Implemented Interfaces:
AnnotationTarget
Direct Known Subclasses:
EmptyTypeTarget, PositionBasedTypeTarget

public abstract class TypeTarget extends Object implements AnnotationTarget
Represents a type that is the target of a type annotation. Type annotations can occur at any nesting level on any type declaration. For this reason, an enclosing target is provided, as well as other usage specific information to determine the starting point for locating the type.

It is expected that callers will traverse the full tree from the specified starting point, since this context is important in interpreting the meaning of the type annotation

Since:
2.0
See Also:
  • Field Details

  • Constructor Details

  • Method Details

    • setTarget

      void setTarget(Type target)
    • kind

      public final AnnotationTarget.Kind kind()
      Description copied from interface: AnnotationTarget
      Returns the kind of object this target represents.
      Specified by:
      kind in interface AnnotationTarget
      Returns:
      the target kind.
    • isDeclaration

      public boolean isDeclaration()
      Description copied from interface: AnnotationTarget
      Returns whether this annotation target is a declaration.
      Specified by:
      isDeclaration in interface AnnotationTarget
      Returns:
      whether this annotation target is a declaration
    • asDeclaration

      public Declaration asDeclaration()
      Description copied from interface: AnnotationTarget
      Casts and returns this annotation target as a Declaration if it is a declaration.
      Specified by:
      asDeclaration in interface AnnotationTarget
      Returns:
      this instance cast to a declaration
    • enclosingTarget

      public AnnotationTarget enclosingTarget()
      Returns the enclosing target that contains the type referred to by the target() method.
      Returns:
      the enclosing target
    • target

      public Type target()
      Returns the type which contains the respective annotation. In some cases this may be null (e.g. in the case of a bridge method, which erases type information so there is no target to map to)
      Returns:
      the type containing the respective annotation
    • usage

      public abstract TypeTarget.Usage usage()
      Returns the kind of usage of this type target. This allows a caller to use a switch statement as opposed to getClass() comparisons.
      Returns:
      the kind of usage of this type target
    • asEmpty

      public EmptyTypeTarget asEmpty()
    • asClassExtends

      public ClassExtendsTypeTarget asClassExtends()
      Casts and returns this type target as a ClassExtendsTypeTarget. If this type target is not a ClassExtendsTypeTarget, then an exception will be thrown.
      Returns:
      an instance of ClassExtendsTypeTarget
      Throws:
      IllegalArgumentException - if this is not a ClassExtendsTypeTarget
    • asMethodParameterType

      public MethodParameterTypeTarget asMethodParameterType()
      Casts and returns this type target as a MethodParameterTypeTarget. If this type target is not a MethodParameterTypeTarget, then an exception will be thrown.
      Returns:
      an instance of MethodParameterTypeTarget
      Throws:
      IllegalArgumentException - if this is not a MethodParameterTypeTarget
    • asTypeParameter

      public TypeParameterTypeTarget asTypeParameter()
      Casts and returns this type target as a TypeParameterTypeTarget. If this type target is not a TypeParameterTypeTarget, then an exception will be thrown.
      Returns:
      an instance of TypeParameterTypeTarget
      Throws:
      IllegalArgumentException - if this is not a TypeParameterTypeTarget
    • asTypeParameterBound

      public TypeParameterBoundTypeTarget asTypeParameterBound()
      Casts and returns this type target as a TypeParameterBoundTypeTarget. If this type target is not a TypeParameterBoundTypeTarget, then an exception will be thrown.
      Returns:
      an instance of TypeParameterBoundTypeTarget
      Throws:
      IllegalArgumentException - if this is not a TypeParameterBoundTypeTarget
    • asThrows

      public ThrowsTypeTarget asThrows()
      Casts and returns this type target as a ThrowsTypeTarget. If this type target is not a ThrowsTypeTarget, then an exception will be thrown.
      Returns:
      an instance of ThrowsTypeTarget
      Throws:
      IllegalArgumentException - if this is not a TypeParameterBoundTypeTarget
    • asClass

      public final ClassInfo asClass()
      Description copied from interface: AnnotationTarget
      Casts and returns this target as a ClassInfo if it is of kind CLASS
      Specified by:
      asClass in interface AnnotationTarget
      Returns:
      this instance cast to a class
    • asField

      public final FieldInfo asField()
      Description copied from interface: AnnotationTarget
      Casts and returns this target as a FieldInfo if it is of kind FIELD
      Specified by:
      asField in interface AnnotationTarget
      Returns:
      this instance cast to a field
    • asMethod

      public final MethodInfo asMethod()
      Description copied from interface: AnnotationTarget
      Casts and returns this target as a MethodInfo if it is of kind METHOD
      Specified by:
      asMethod in interface AnnotationTarget
      Returns:
      this instance cast to a method
    • asMethodParameter

      public final MethodParameterInfo asMethodParameter()
      Description copied from interface: AnnotationTarget
      Casts and returns this target as a MethodParameterInfo if it is of kind METHOD_PARAMETER
      Specified by:
      asMethodParameter in interface AnnotationTarget
      Returns:
      this instance cast to a method parameter
    • asType

      public final TypeTarget asType()
      Description copied from interface: AnnotationTarget
      Casts and returns this target as a TypeTarget if it is of kind TYPE
      Specified by:
      asType in interface AnnotationTarget
      Returns:
      this instance cast to a type target
    • asRecordComponent

      public final RecordComponentInfo asRecordComponent()
      Description copied from interface: AnnotationTarget
      Casts and returns this target as a RecordComponentInfo if it is of kind RECORD_COMPONENT
      Specified by:
      asRecordComponent in interface AnnotationTarget
      Returns:
      this instance cast to a record component
    • hasAnnotation

      public boolean hasAnnotation(DotName name)
      Returns whether an annotation instance with given name is declared on this type usage.

      Note that unlike other AnnotationTargets, this method doesn't inspect nested annotation targets, even though array types, parameterized types, type variables and wildcard types may contain other types inside them. In other words, this method is equivalent to hasDeclaredAnnotation(DotName).

      Specified by:
      hasAnnotation in interface AnnotationTarget
      Parameters:
      name - name of the annotation type to look for, must not be null
      Returns:
      true if the annotation is present, false otherwise
      Since:
      3.0
      See Also:
    • annotation

      public AnnotationInstance annotation(DotName name)
      Returns the annotation instance with given name declared on this type usage.

      Note that unlike other AnnotationTargets, this method doesn't inspect nested annotation targets, even though array types, parameterized types, type variables and wildcard types may contain other types inside them. In other words, this method is equivalent to declaredAnnotation(DotName).

      Specified by:
      annotation in interface AnnotationTarget
      Parameters:
      name - name of the annotation type to look for, must not be null
      Returns:
      the annotation instance, or null if not found
      Since:
      3.0
      See Also:
    • annotations

      public List<AnnotationInstance> annotations(DotName name)
      Returns the annotation instances with given name declared on this type usage.

      Note that unlike other AnnotationTargets, this method doesn't inspect nested annotation targets, even though array types, parameterized types, type variables and wildcard types may contain other types inside them. In other words, this method is equivalent to annotation(DotName) and declaredAnnotation(DotName), except it returns a list.

      Specified by:
      annotations in interface AnnotationTarget
      Parameters:
      name - name of the annotation type, must not be null
      Returns:
      immutable list of annotation instances, never null
      Since:
      3.0
      See Also:
    • annotationsWithRepeatable

      public List<AnnotationInstance> annotationsWithRepeatable(DotName name, IndexView index)
      Returns the annotation instances with given name declared on this type usage.

      If the specified annotation is repeatable, the result also contains all values from the container annotation instance. In this case, the AnnotationInstance.target() returns the target of the container annotation instance.

      Note that unlike other AnnotationTargets, this method doesn't inspect nested annotation targets, even though array types, parameterized types, type variables and wildcard types may contain other types inside them. In other words, this method is equivalent to declaredAnnotationsWithRepeatable(DotName, IndexView).

      Specified by:
      annotationsWithRepeatable in interface AnnotationTarget
      Parameters:
      name - name of the annotation type, must not be null
      index - index used to obtain the annotation type, must not be null
      Returns:
      immutable list of annotation instances, never null
      Throws:
      IllegalArgumentException - if the index is null, if the index does not contain the annotation type or if name does not identify an annotation type
      Since:
      3.0
      See Also:
    • annotations

      public List<AnnotationInstance> annotations()
      Returns the annotation instances declared on this type usage.

      Note that unlike other AnnotationTargets, this method doesn't inspect nested annotation targets, even though array types, parameterized types, type variables and wildcard types may contain other types inside them. In other words, this method is equivalent to declaredAnnotations().

      Specified by:
      annotations in interface AnnotationTarget
      Returns:
      immutable list of annotation instances, never null
      Since:
      3.0
    • hasDeclaredAnnotation

      public boolean hasDeclaredAnnotation(DotName name)
      Returns whether an annotation instance with given name is declared on this type usage.
      Specified by:
      hasDeclaredAnnotation in interface AnnotationTarget
      Parameters:
      name - name of the annotation type to look for, must not be null
      Returns:
      true if the annotation is present, false otherwise
      Since:
      3.0
      See Also:
    • declaredAnnotation

      public AnnotationInstance declaredAnnotation(DotName name)
      Returns the annotation instance with given name declared on this type usage.
      Specified by:
      declaredAnnotation in interface AnnotationTarget
      Parameters:
      name - name of the annotation type to look for, must not be null
      Returns:
      the annotation instance, or null if not found
      Since:
      3.0
      See Also:
    • declaredAnnotationsWithRepeatable

      public List<AnnotationInstance> declaredAnnotationsWithRepeatable(DotName name, IndexView index)
      Returns the annotation instances with given name declared on this type usage.

      If the specified annotation is repeatable, the result also contains all values from the container annotation instance. In this case, the AnnotationInstance.target() returns the target of the container annotation instance.

      Specified by:
      declaredAnnotationsWithRepeatable in interface AnnotationTarget
      Parameters:
      name - name of the annotation type, must not be null
      index - index used to obtain the annotation type, must not be null
      Returns:
      immutable list of annotation instances, never null
      Throws:
      IllegalArgumentException - if the index is null, if the index does not contain the annotation type or if name does not identify an annotation type
      Since:
      3.0
      See Also:
    • declaredAnnotations

      public List<AnnotationInstance> declaredAnnotations()
      Returns the annotation instances declared on this type usage.
      Specified by:
      declaredAnnotations in interface AnnotationTarget
      Returns:
      immutable list of annotation instances, never null
      Since:
      3.0
      See Also: