Class TypeAnnotationEntry

java.lang.Object
org.apache.bcel.classfile.TypeAnnotationEntry

public class TypeAnnotationEntry extends Object
Represents one type annotation of a TypeAnnotations attribute (JVMS 4.7.20): an annotation, the kind of program element it applies to (the target type and its target info), and the path to the annotated type inside that element.
Since:
6.14.0
See Also:
  • Field Details

    • TARGET_CLASS_TYPE_PARAMETER

      public static final int TARGET_CLASS_TYPE_PARAMETER
      Target type: type parameter declaration of a generic class or interface.
      See Also:
    • TARGET_METHOD_TYPE_PARAMETER

      public static final int TARGET_METHOD_TYPE_PARAMETER
      Target type: type parameter declaration of a generic method or constructor.
      See Also:
    • TARGET_CLASS_EXTENDS

      public static final int TARGET_CLASS_EXTENDS
      Target type: type in an extends or implements clause of a class or interface declaration.
      See Also:
    • TARGET_CLASS_TYPE_PARAMETER_BOUND

      public static final int TARGET_CLASS_TYPE_PARAMETER_BOUND
      Target type: bound of a type parameter of a generic class or interface.
      See Also:
    • TARGET_METHOD_TYPE_PARAMETER_BOUND

      public static final int TARGET_METHOD_TYPE_PARAMETER_BOUND
      Target type: bound of a type parameter of a generic method or constructor.
      See Also:
    • TARGET_FIELD

      public static final int TARGET_FIELD
      Target type: type in a field or record component declaration.
      See Also:
    • TARGET_METHOD_RETURN

      public static final int TARGET_METHOD_RETURN
      Target type: return type of a method, or type of a newly constructed object.
      See Also:
    • TARGET_METHOD_RECEIVER

      public static final int TARGET_METHOD_RECEIVER
      Target type: receiver type of a method or constructor.
      See Also:
    • TARGET_METHOD_FORMAL_PARAMETER

      public static final int TARGET_METHOD_FORMAL_PARAMETER
      Target type: type in a formal parameter declaration of a method, constructor, or lambda expression.
      See Also:
    • TARGET_THROWS

      public static final int TARGET_THROWS
      Target type: type in a throws clause of a method or constructor.
      See Also:
    • TARGET_LOCAL_VARIABLE

      public static final int TARGET_LOCAL_VARIABLE
      Target type: type in a local variable declaration.
      See Also:
    • TARGET_RESOURCE_VARIABLE

      public static final int TARGET_RESOURCE_VARIABLE
      Target type: type in a resource variable declaration.
      See Also:
    • TARGET_EXCEPTION_PARAMETER

      public static final int TARGET_EXCEPTION_PARAMETER
      Target type: type in an exception parameter declaration.
      See Also:
    • TARGET_INSTANCEOF

      public static final int TARGET_INSTANCEOF
      Target type: type in an instanceof expression.
      See Also:
    • TARGET_NEW

      public static final int TARGET_NEW
      Target type: type in a new expression.
      See Also:
    • TARGET_CONSTRUCTOR_REFERENCE

      public static final int TARGET_CONSTRUCTOR_REFERENCE
      Target type: type in a method reference expression using ::new.
      See Also:
    • TARGET_METHOD_REFERENCE

      public static final int TARGET_METHOD_REFERENCE
      Target type: type in a method reference expression using ::Identifier.
      See Also:
    • TARGET_CAST

      public static final int TARGET_CAST
      Target type: type in a cast expression.
      See Also:
    • TARGET_CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT

      public static final int TARGET_CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT
      Target type: type argument for a generic constructor in a new expression or an explicit constructor invocation.
      See Also:
    • TARGET_METHOD_INVOCATION_TYPE_ARGUMENT

      public static final int TARGET_METHOD_INVOCATION_TYPE_ARGUMENT
      Target type: type argument for a generic method in a method invocation expression.
      See Also:
    • TARGET_CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT

      public static final int TARGET_CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT
      Target type: type argument for a generic constructor in a method reference expression using ::new.
      See Also:
    • TARGET_METHOD_REFERENCE_TYPE_ARGUMENT

      public static final int TARGET_METHOD_REFERENCE_TYPE_ARGUMENT
      Target type: type argument for a generic method in a method reference expression using ::Identifier.
      See Also:
    • EMPTY_ARRAY

      public static final TypeAnnotationEntry[] EMPTY_ARRAY
      Empty array of TypeAnnotationEntry objects.
    • MAX_TYPE_PATH_LENGTH

      private static final int MAX_TYPE_PATH_LENGTH
      Maximum number of steps of a type path, which its length of one byte limits.
      See Also:
    • LOCAL_VARIABLE_RANGE_SIZE

      private static final int LOCAL_VARIABLE_RANGE_SIZE
      Number of bytes of a localvar_target entry: start_pc, length and index.
      See Also:
    • targetType

      private final int targetType
    • targetInfo

      private final byte[] targetInfo
    • typePath

      private final byte[] typePath
    • annotationEntry

      private final AnnotationEntry annotationEntry
  • Constructor Details

    • TypeAnnotationEntry

      public TypeAnnotationEntry(int targetType, byte[] targetInfo, byte[] typePath, AnnotationEntry annotationEntry)
      Constructs a TypeAnnotationEntry.
      Parameters:
      targetType - The target type, one of the TARGET_* constants.
      targetInfo - The target info, as it is stored in the class file.
      typePath - The type path, as it is stored in the class file without its length: a pair of bytes (path kind, type argument index) per step.
      annotationEntry - The annotation.
      Throws:
      ClassFormatException - Thrown if the target type is not defined.
      IllegalArgumentException - Thrown if the target info does not have the size its target type requires, or if the type path is not a sequence of pairs of bytes of at most 255 steps.
  • Method Details

    • checkTargetInfo

      private static void checkTargetInfo(int targetType, byte[] targetInfo)
    • read

      public static TypeAnnotationEntry read(DataInput input, ConstantPool constantPool, boolean isRuntimeVisible) throws IOException
      Factory method to create a TypeAnnotationEntry from a DataInput.
      Parameters:
      input - The input stream.
      constantPool - The constant pool.
      isRuntimeVisible - whether the annotation is runtime visible.
      Returns:
      The entry.
      Throws:
      IOException - Thrown if an I/O error occurs.
    • targetInfoSize

      private static int targetInfoSize(int targetType)
      Gets the number of bytes of the target info that follows a target type.
      Parameters:
      targetType - The target type.
      Returns:
      the size of the target info, or -1 if it depends on its content (the table of a localvar_target).
      Throws:
      ClassFormatException - Thrown if the target type is not defined.
    • copy

      public TypeAnnotationEntry copy(ConstantPool constantPool)
      Creates a deep copy of this entry, whose annotation is independent of this one and refers to the given constant pool. The indexes it holds are kept, as they are in Attribute.copy(ConstantPool): the constant pool must have the same constants as the one of this entry at these indexes.
      Parameters:
      constantPool - The constant pool of the copy.
      Returns:
      A deep copy of this entry.
    • dump

      public void dump(DataOutputStream dos) throws IOException
      Writes this entry to a DataOutputStream.
      Parameters:
      dos - The data output stream.
      Throws:
      IOException - Thrown if an I/O error occurs.
    • getAnnotationEntry

      public AnnotationEntry getAnnotationEntry()
      Gets the annotation.
      Returns:
      The annotation.
    • getTargetInfo

      public byte[] getTargetInfo()
      Gets the target info, which depends on the target type (JVMS table 4.7.20-A): for instance the index of the type parameter for TARGET_CLASS_TYPE_PARAMETER, the index of the type parameter then the index of the bound for TARGET_CLASS_TYPE_PARAMETER_BOUND, or the index of the parameter for TARGET_METHOD_FORMAL_PARAMETER.
      Returns:
      A copy of the target info, as it is stored in the class file.
    • getTargetType

      public int getTargetType()
      Gets the kind of program element the annotation applies to.
      Returns:
      One of the TARGET_* constants.
    • getTypePathArgumentIndex

      public int getTypePathArgumentIndex(int step)
      Gets the index of the type argument for a step of the type path of kind 3, else 0.
      Parameters:
      step - Index of the step, from 0 to getTypePathLength() excluded.
      Returns:
      The index of the type argument.
    • getTypePathKind

      public int getTypePathKind(int step)
      Gets the kind of a step of the type path: 0 deeper in an array type, 1 deeper in a nested type, 2 on the bound of a wildcard type argument, 3 on a type argument of a parameterized type.
      Parameters:
      step - Index of the step, from 0 to getTypePathLength() excluded.
      Returns:
      The kind of the step.
    • getTypePathLength

      public int getTypePathLength()
      Gets the number of steps in the type path.

      The path leads from the type of the target to the part of it that is annotated. An empty path identifies the outermost part of the type where an annotation is allowed. For a non-static inner class, @A Outer.Inner has an empty path, while Outer.@A Inner requires one step. For a static nested class, Outer.@A Nested has an empty path.

      Returns:
      The number of steps of the type path.
    • toString

      public String toString()
      Overrides:
      toString in class Object