FileDocCategorySizeDatePackage
CodeAttribute_info.javaAPI DocAndroid 1.5 API9092Wed May 06 22:41:16 BST 2009com.vladium.jcd.cls.attribute

CodeAttribute_info

public final class CodeAttribute_info extends Attribute_info
The Code attribute is a variable-length attribute used in the attributes table of {@link com.vladium.jcd.cls.Method_info} structures. A Code attribute contains the JVM instructions and auxiliary information for a single Java method, instance initialization method, or class or interface initialization method. Every Java Virtual Machine implementation must recognize Code attributes. There must be exactly one Code attribute in each method_info structure.

The Code attribute has the format

Code_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 max_stack;
u2 max_locals;
u4 code_length;
u1 code[code_length];
u2 exception_table_length;
{ u2 start_pc;
u2 end_pc;
u2 handler_pc;
u2 catch_type;
} exception_table[exception_table_length];

u2 attributes_count;
attribute_info attributes[attributes_count];
}
The value of the max_stack item gives the maximum number of words on the operand stack at any point during execution of this method.

The value of the max_locals item gives the number of local variables used by this method, including the parameters passed to the method on invocation. The index of the first local variable is 0 . The greatest local variable index for a one-word value is max_locals-1 . The greatest local variable index for a two-word value is max_locals-2.

The value of the code_length item gives the number of bytes in the code array for this method. The value of code_length must be greater than zero; the code array must not be empty.The code array gives the actual bytes of Java Virtual Machine code that implement the method.

The value of the exception_table_length item gives the number of entries in the exception_table table. Each entry in the exception_table array describes one exception handler in the code array: see {@link Exception_info}.

The value of the attributes_count item indicates the number of attributes of the Code attribute. Each value of the attributes table must be a variable-length attribute structure. A Code attribute can have any number of optional attributes associated with it.

author
(C) 2001, Vlad Roubtsov

Fields Summary
public static final byte[]
EMPTY_BYTE_ARRAY
public int
m_max_stack
public int
m_max_locals
private byte[]
m_code
private int
m_codeSize
private IExceptionHandlerTable
m_exceptionHandlerTable
private com.vladium.jcd.cls.IAttributeCollection
m_attributes
private static final boolean
DEBUG
Constructors Summary
public CodeAttribute_info(int attribute_name_index, int max_stack, int max_locals, byte[] code, IExceptionHandlerTable exceptionHandlerTable, com.vladium.jcd.cls.IAttributeCollection attributes)

    
    
    
        
                                   
                                   
                                 
                                 
    
        super (attribute_name_index, 8 + (code != null ? code.length : 0) + exceptionHandlerTable.length () + attributes.length ());
        
        m_max_stack = max_stack;
        m_max_locals = max_locals;
        
        m_code = (code != null ? code : EMPTY_BYTE_ARRAY);
        m_codeSize = m_code.length;
        
        m_exceptionHandlerTable = exceptionHandlerTable;
        m_attributes = attributes;
    
CodeAttribute_info(com.vladium.jcd.cls.IConstantCollection constants, int attribute_name_index, long attribute_length, com.vladium.jcd.lib.UDataInputStream bytes)

        super (attribute_name_index, attribute_length);
        
        m_max_stack = bytes.readU2 ();
        m_max_locals = bytes.readU2 ();
        
        final long code_length = bytes.readU4 ();
        
        m_code = new byte [(int) code_length];
        bytes.readFully (m_code);
        m_codeSize = (int) code_length;
        
        
        final int exception_table_length = bytes.readU2 ();
        m_exceptionHandlerTable = AttributeElementFactory.newExceptionHandlerTable (exception_table_length);
        
        for (int i = 0; i < exception_table_length; ++ i)
        {
            Exception_info exception_info = new Exception_info (bytes);
            if (DEBUG) System.out.println ("\t[" + i + "] exception: " + exception_info);
            
            m_exceptionHandlerTable.add (exception_info);
        }
        
        
        // TODO: put this logic into AttributeCollection
        final int attributes_count = bytes.readU2 ();
        m_attributes = ElementFactory.newAttributeCollection (attributes_count);
        
        for (int i = 0; i < attributes_count; ++ i)
        {
            Attribute_info attribute_info = Attribute_info.new_Attribute_info (constants, bytes);
            if (DEBUG) System.out.println ("\t[" + i + "] attribute: " + attribute_info);
            
            m_attributes.add (attribute_info);
        }
    
Methods Summary
public voidaccept(IAttributeVisitor visitor, java.lang.Object ctx)

        visitor.visit (this, ctx);
    
public java.lang.Objectclone()
Performs a deep copy.

        final CodeAttribute_info _clone = (CodeAttribute_info) super.clone ();
        
        // do deep copy:
        
        _clone.m_code = (m_codeSize == 0 ? EMPTY_BYTE_ARRAY : (byte []) m_code.clone ()); // does not trim
        _clone.m_exceptionHandlerTable = (IExceptionHandlerTable) m_exceptionHandlerTable.clone ();
        _clone.m_attributes = (IAttributeCollection) m_attributes.clone ();
        
        return _clone;
    
public com.vladium.jcd.cls.IAttributeCollectiongetAttributes()

        return m_attributes;
    
public final byte[]getCode()
NOTE: must also use getCodeSize()

return

        return m_code;
    
public final intgetCodeSize()

        return m_codeSize;
    
public IExceptionHandlerTablegetExceptionTable()

        return m_exceptionHandlerTable;
    
public longlength()

        return 14 + m_codeSize + m_exceptionHandlerTable.length () + m_attributes.length ();
    
public voidsetCode(byte[] code, int codeSize)

        m_code = code;
        m_codeSize = codeSize;
    
public java.lang.StringtoString()

        String eol = System.getProperty ("line.separator");
        
        StringBuffer s = new StringBuffer ();
        
        s.append ("CodeAttribute_info: [attribute_name_index = " + m_name_index + ", attribute_length = " + m_attribute_length + "]" + eol);
        s.append ("    max_stack/max_locals = " + m_max_stack + '/" + m_max_locals + eol);
        s.append ("    code [length " + m_codeSize + "]" + eol);
        
        for (int a = 0; a < m_attributes.size (); ++ a)
        {
            s.append ("         " + m_attributes.get (a) + eol);
        }
       
        
        return s.toString ();
    
public voidwriteInClassFormat(com.vladium.jcd.lib.UDataOutputStream out)

        super.writeInClassFormat (out);
        
        out.writeU2 (m_max_stack);
        out.writeU2 (m_max_locals);
    
        out.writeU4 (m_codeSize);
        out.write (m_code, 0, m_codeSize); // TODO: THIS IS WRONG
        
        m_exceptionHandlerTable.writeInClassFormat (out);
        m_attributes.writeInClassFormat (out);