KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > vladium > jcd > cls > attribute > CodeAttribute_info


1 /* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
2  *
3  * This program and the accompanying materials are made available under
4  * the terms of the Common Public License v1.0 which accompanies this distribution,
5  * and is available at http://www.eclipse.org/legal/cpl-v10.html
6  *
7  * $Id: CodeAttribute_info.java,v 1.1.1.1 2004/05/09 16:57:47 vlad_r Exp $
8  */

9 package com.vladium.jcd.cls.attribute;
10
11 import java.io.IOException JavaDoc;
12
13 import com.vladium.jcd.cls.ElementFactory;
14 import com.vladium.jcd.cls.IAttributeCollection;
15 import com.vladium.jcd.cls.IConstantCollection;
16 import com.vladium.jcd.lib.UDataInputStream;
17 import com.vladium.jcd.lib.UDataOutputStream;
18
19 // ----------------------------------------------------------------------------
20
/**
21  * The Code attribute is a variable-length attribute used in the attributes
22  * table of {@link com.vladium.jcd.cls.Method_info} structures. A Code attribute
23  * contains the JVM instructions and auxiliary information for a single Java method,
24  * instance initialization method, or class or interface initialization method.
25  * Every Java Virtual Machine implementation must recognize Code attributes. There
26  * must be exactly one Code attribute in each method_info structure.<P>
27  *
28  * The Code attribute has the format
29  * <PRE>
30  * Code_attribute {
31  * u2 attribute_name_index;
32  * u4 attribute_length;
33  * u2 max_stack;
34  * u2 max_locals;
35  * u4 code_length;
36  * u1 code[code_length];
37  * u2 exception_table_length;
38  * { u2 start_pc;
39  * u2 end_pc;
40  * u2 handler_pc;
41  * u2 catch_type;
42  * } exception_table[exception_table_length];
43  *
44  * u2 attributes_count;
45  * attribute_info attributes[attributes_count];
46  * }
47  * </PRE>
48  *
49  * The value of the max_stack item gives the maximum number of words on the operand
50  * stack at any point during execution of this method.<P>
51  *
52  * The value of the max_locals item gives the number of local variables used by this
53  * method, including the parameters passed to the method on invocation. The index of
54  * the first local variable is 0 . The greatest local variable index for a one-word
55  * value is max_locals-1 . The greatest local variable index for a two-word value is
56  * max_locals-2.<P>
57  *
58  * The value of the code_length item gives the number of bytes in the code array for
59  * this method. The value of code_length must be greater than zero; the code array must
60  * not be empty.The code array gives the actual bytes of Java Virtual Machine code that
61  * implement the method.<P>
62  *
63  * The value of the exception_table_length item gives the number of entries in the
64  * exception_table table. Each entry in the exception_table array describes one
65  * exception handler in the code array: see {@link Exception_info}.<P>
66  *
67  * The value of the attributes_count item indicates the number of attributes of the Code
68  * attribute. Each value of the attributes table must be a variable-length attribute
69  * structure. A Code attribute can have any number of optional attributes associated
70  * with it.
71  *
72  * @author (C) 2001, Vlad Roubtsov
73  */

74 public
75 final class CodeAttribute_info extends Attribute_info
76 {
77     // public: ................................................................
78

79
80     public static final byte [] EMPTY_BYTE_ARRAY = new byte [0];
81     
82     public int m_max_stack, m_max_locals;
83     
84     
85     
86     public CodeAttribute_info (final int attribute_name_index,
87                                final int max_stack, int max_locals,
88                                final byte [] code,
89                                final IExceptionHandlerTable exceptionHandlerTable,
90                                final IAttributeCollection attributes)
91     {
92         super (attribute_name_index, 8 + (code != null ? code.length : 0) + exceptionHandlerTable.length () + attributes.length ());
93         
94         m_max_stack = max_stack;
95         m_max_locals = max_locals;
96         
97         m_code = (code != null ? code : EMPTY_BYTE_ARRAY);
98         m_codeSize = m_code.length;
99         
100         m_exceptionHandlerTable = exceptionHandlerTable;
101         m_attributes = attributes;
102     }
103     
104     /**
105      * NOTE: must also use getCodeSize()
106      * @return
107      */

108     public final byte [] getCode ()
109     {
110         return m_code;
111     }
112     
113     public final int getCodeSize ()
114     {
115         return m_codeSize;
116     }
117     
118     public IAttributeCollection getAttributes ()
119     {
120         return m_attributes;
121     }
122     
123     public IExceptionHandlerTable getExceptionTable ()
124     {
125         return m_exceptionHandlerTable;
126     }
127     
128     public long length ()
129     {
130         return 14 + m_codeSize + m_exceptionHandlerTable.length () + m_attributes.length ();
131     }
132     
133     // Visitor:
134

135     public void accept (final IAttributeVisitor visitor, final Object JavaDoc ctx)
136     {
137         visitor.visit (this, ctx);
138     }
139     
140     
141     public String JavaDoc toString ()
142     {
143         String JavaDoc eol = System.getProperty ("line.separator");
144         
145         StringBuffer JavaDoc s = new StringBuffer JavaDoc ();
146         
147         s.append ("CodeAttribute_info: [attribute_name_index = " + m_name_index + ", attribute_length = " + m_attribute_length + "]" + eol);
148         s.append (" max_stack/max_locals = " + m_max_stack + '/' + m_max_locals + eol);
149         s.append (" code [length " + m_codeSize + "]" + eol);
150         
151         for (int a = 0; a < m_attributes.size (); ++ a)
152         {
153             s.append (" " + m_attributes.get (a) + eol);
154         }
155        
156         
157         return s.toString ();
158     }
159     
160     // Cloneable:
161

162     /**
163      * Performs a deep copy.
164      */

165     public Object JavaDoc clone ()
166     {
167         final CodeAttribute_info _clone = (CodeAttribute_info) super.clone ();
168         
169         // do deep copy:
170

171         _clone.m_code = (m_codeSize == 0 ? EMPTY_BYTE_ARRAY : (byte []) m_code.clone ()); // does not trim
172
_clone.m_exceptionHandlerTable = (IExceptionHandlerTable) m_exceptionHandlerTable.clone ();
173         _clone.m_attributes = (IAttributeCollection) m_attributes.clone ();
174         
175         return _clone;
176     }
177     
178     
179     // IClassFormatOutput:
180

181     public void writeInClassFormat (final UDataOutputStream out) throws IOException JavaDoc
182     {
183         super.writeInClassFormat (out);
184         
185         out.writeU2 (m_max_stack);
186         out.writeU2 (m_max_locals);
187     
188         out.writeU4 (m_codeSize);
189         out.write (m_code, 0, m_codeSize); // TODO: THIS IS WRONG
190

191         m_exceptionHandlerTable.writeInClassFormat (out);
192         m_attributes.writeInClassFormat (out);
193     }
194     
195     
196     public void setCode (final byte [] code, final int codeSize)
197     {
198         m_code = code;
199         m_codeSize = codeSize;
200     }
201         
202     // protected: .............................................................
203

204     // package: ...............................................................
205

206
207     CodeAttribute_info (final IConstantCollection constants,
208                         final int attribute_name_index, final long attribute_length,
209                         final UDataInputStream bytes)
210         throws IOException JavaDoc
211     {
212         super (attribute_name_index, attribute_length);
213         
214         m_max_stack = bytes.readU2 ();
215         m_max_locals = bytes.readU2 ();
216         
217         final long code_length = bytes.readU4 ();
218         
219         m_code = new byte [(int) code_length];
220         bytes.readFully (m_code);
221         m_codeSize = (int) code_length;
222         
223         
224         final int exception_table_length = bytes.readU2 ();
225         m_exceptionHandlerTable = AttributeElementFactory.newExceptionHandlerTable (exception_table_length);
226         
227         for (int i = 0; i < exception_table_length; ++ i)
228         {
229             Exception_info exception_info = new Exception_info (bytes);
230             if (DEBUG) System.out.println ("\t[" + i + "] exception: " + exception_info);
231             
232             m_exceptionHandlerTable.add (exception_info);
233         }
234         
235         
236         // TODO: put this logic into AttributeCollection
237
final int attributes_count = bytes.readU2 ();
238         m_attributes = ElementFactory.newAttributeCollection (attributes_count);
239         
240         for (int i = 0; i < attributes_count; ++ i)
241         {
242             Attribute_info attribute_info = Attribute_info.new_Attribute_info (constants, bytes);
243             if (DEBUG) System.out.println ("\t[" + i + "] attribute: " + attribute_info);
244             
245             m_attributes.add (attribute_info);
246         }
247     }
248
249     // private: ...............................................................
250

251
252     private byte [] m_code; // never null [valid content extent is m_codeSize]
253
private int m_codeSize;
254     
255     private IExceptionHandlerTable m_exceptionHandlerTable; // never null
256
private IAttributeCollection m_attributes; // never null
257

258         
259     private static final boolean DEBUG = false;
260     
261 } // end of class
262
// ----------------------------------------------------------------------------
263
Popular Tags