Class Attribute

java.lang.Object
org.apache.bcel.classfile.Attribute
All Implemented Interfaces:
Cloneable, Node
Direct Known Subclasses:
AnnotationDefault, Annotations, BootstrapMethods, Code, ConstantValue, Deprecated, EnclosingMethod, ExceptionTable, InnerClasses, LineNumberTable, LocalVariableTable, LocalVariableTypeTable, MethodParameters, Module, ModuleMainClass, ModulePackages, NestHost, NestMembers, ParameterAnnotations, PermittedSubclasses, PMGClass, Record, Signature, SourceFile, StackMap, Synthetic, TypeAnnotations, Unknown

public abstract class Attribute extends Object implements Cloneable, Node
Abstract superclass for Attribute objects. Currently the ConstantValue, SourceFile, Code, Exceptiontable, LineNumberTable, LocalVariableTable, InnerClasses and Synthetic attributes are supported. The Unknown attribute stands for non-standard-attributes.
attribute_info {
  u2 attribute_name_index;
  u4 attribute_length;
  u1 info[attribute_length];
}

The default maximum number of attribute nesting levels in readAttribute(DataInput, ConstantPool) is 64 before throwing a ClassFormatException. This is configurable through the system property org.apache.bcel.classfile.Attribute.maxNestingDepth. Attributes may legitimately nest (for example, a Code attribute carries its own attribute table, and Record components carry theirs), but a malicious class file can nest such attributes deeply enough to overflow the parser's stack.

See Also:
  • Field Details

    • EMPTY_ARRAY

      public static final Attribute[] EMPTY_ARRAY
      Empty array.
      Since:
      6.6.0
    • name_index

      @Deprecated protected int name_index
      Deprecated.
      (since 6.0) will be made private; do not access directly, use getter/setter.
    • length

      @Deprecated protected int length
      Deprecated.
      (since 6.0) (since 6.0) will be made private; do not access directly, use getter/setter.
    • tag

      @Deprecated protected byte tag
      Deprecated.
      (since 6.0) will be made private; do not access directly, use getter/setter.
    • constant_pool

      Deprecated.
      (since 6.0) will be made private; do not access directly, use getter/setter.
  • Constructor Details

    • Attribute

      protected Attribute(byte tag, int nameIndex, int length, ConstantPool constantPool)
      Constructs an instance.
      attribute_info {
        u2 attribute_name_index;
        u4 attribute_length;
        u1 info[attribute_length];
      }
      
      Parameters:
      tag - tag.
      nameIndex - u2 name index.
      length - u4 length.
      constantPool - constant pool.
  • Method Details

    • addAttributeReader

      @Deprecated public static void addAttributeReader(String name, AttributeReader attributeReader)
      Add an Attribute reader capable of parsing (user-defined) attributes named "name". You should not add readers for the standard attributes such as "LineNumberTable", because those are handled internally.
      Parameters:
      name - The name of the attribute as stored in the class file.
      attributeReader - The reader object.
    • addAttributeReader

      public static void addAttributeReader(String name, UnknownAttributeReader unknownAttributeReader)
      Add an Attribute reader capable of parsing (user-defined) attributes named "name". You should not add readers for the standard attributes such as "LineNumberTable", because those are handled internally.
      Parameters:
      name - The name of the attribute as stored in the class file.
      unknownAttributeReader - The reader object.
    • println

      protected static void println(String msg)
      Prints a message to stderr if debug mode is enabled.
      Parameters:
      msg - The message to print.
    • readAttribute

      public static Attribute readAttribute(DataInput dataInput, ConstantPool constantPool) throws IOException
      Reads one attribute using standard eight-byte Code headers.
      Parameters:
      dataInput - Input stream.
      constantPool - Array of constants.
      Returns:
      Attribute.
      Throws:
      IOException - Thrown if an I/O error occurs.
      Since:
      6.0
      See Also:
    • readAttribute

      public static Attribute readAttribute(DataInput dataInput, ConstantPool constantPool, boolean compactCodeHeader) throws IOException
      Reads one attribute using the selected Code header layout.

      The compact layout uses u1, u1, and u2 for max_stack, max_locals, and code_length, respectively. These fields occupy 4 bytes instead of the standard 8 bytes; the rest of the Code attribute layout is unchanged. This layout is intended for historical class files with version 45.0 through 45.2. This method has no enclosing class version to check, so the caller must select the layout. The selection also applies to Code attributes encountered while reading nested attributes.

      Parameters:
      dataInput - Input stream.
      constantPool - Constant pool.
      compactCodeHeader - Whether to read compact four-byte Code headers (true) or standard eight-byte headers (false).
      Returns:
      The attribute.
      Throws:
      IOException - Thrown if an I/O error occurs.
      ClassFormatException - Thrown if the attribute is malformed or exceeds the nesting limit.
      Since:
      6.14.0
      See Also:
    • readAttribute

      public static Attribute readAttribute(DataInputStream dataInputStream, ConstantPool constantPool) throws IOException
      Reads one attribute using standard eight-byte Code headers.
      Parameters:
      dataInputStream - Input stream.
      constantPool - Array of constants.
      Returns:
      Attribute.
      Throws:
      IOException - Thrown if an I/O error occurs.
      See Also:
    • removeAttributeReader

      public static void removeAttributeReader(String name)
      Remove attribute reader
      Parameters:
      name - The name of the attribute as stored in the class file.
    • accept

      public abstract void accept(Visitor v)
      Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. That is, the hierarchy of methods, fields, attributes, and other elements forms a tree of objects.
      Specified by:
      accept in interface Node
      Parameters:
      v - Visitor object.
    • clone

      public Object clone()
      Use copy() if you want to have a deep copy(), that is, with all references copied correctly.
      Overrides:
      clone in class Object
      Returns:
      shallow copy of this attribute.
    • copy

      public abstract Attribute copy(ConstantPool constantPool)
      Creates a deep copy of this attribute.
      Parameters:
      constantPool - constant pool to save.
      Returns:
      deep copy of this attribute.
    • dump

      public void dump(DataOutputStream file) throws IOException
      Dumps attribute to file stream in binary format.
      Parameters:
      file - Output file stream.
      Throws:
      IOException - Thrown if an I/O error occurs.
    • getConstantPool

      public final ConstantPool getConstantPool()
      Gets the constant pool used by this object.
      Returns:
      Constant pool used by this object.
      See Also:
    • getLength

      public final int getLength()
      Gets the length of attribute field in bytes.
      Returns:
      Length of attribute field in bytes.
    • getName

      public String getName()
      Gets the name of the attribute.
      Returns:
      Name of attribute.
      Since:
      6.0
    • getNameIndex

      public final int getNameIndex()
      Gets the name index in constant pool of attribute name.
      Returns:
      Name index in constant pool of attribute name.
    • getTag

      public final byte getTag()
      Gets the tag of attribute, that is, its type.
      Returns:
      Tag of attribute, that is, its type. Value may not be altered, thus there is no setTag() method.
    • setConstantPool

      public final void setConstantPool(ConstantPool constantPool)
      Sets the constant pool to be used for this object.
      Parameters:
      constantPool - Constant pool to be used for this object.
      See Also:
    • setLength

      public final void setLength(int length)
      Sets the length in bytes.
      Parameters:
      length - length in bytes.
    • setNameIndex

      public final void setNameIndex(int nameIndex)
      Sets the name index of attribute.
      Parameters:
      nameIndex - of attribute.
    • toString

      public String toString()
      Overrides:
      toString in class Object
      Returns:
      attribute name.