KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > structures > attributes > CodeAttribute


1 /*
2     This library is free software; you can redistribute it and/or
3     modify it under the terms of the GNU General Public
4     License as published by the Free Software Foundation; either
5     version 2 of the license, or (at your option) any later version.
6 */

7
8 package org.gjt.jclasslib.structures.attributes;
9
10 import org.gjt.jclasslib.structures.AttributeInfo;
11 import org.gjt.jclasslib.structures.InvalidByteCodeException;
12
13 import java.io.*;
14
15 /**
16     Describes a <tt>Code</tt> attribute structure.
17  
18     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
19     @version $Revision: 1.4 $ $Date: 2003/08/18 07:52:05 $
20 */

21 public class CodeAttribute extends AttributeInfo {
22
23     /** Name of the attribute as in the corresponding constant pool entry. */
24     public static final String JavaDoc ATTRIBUTE_NAME = "Code";
25
26     private static final int INITIAL_LENGTH = 12;
27     
28     private int maxStack;
29     private int maxLocals;
30     private byte[] code;
31     private ExceptionTableEntry[] exceptionTable;
32
33     /**
34         Get the maximum stack depth of this code attribute.
35         @return the stack depth
36      */

37     public int getMaxStack() {
38         return maxStack;
39     }
40
41     /**
42         Set the maximum stack depth of this code attribute.
43         @param maxStack the stack depth
44      */

45     public void setMaxStack(int maxStack) {
46         this.maxStack = maxStack;
47     }
48
49     /**
50         Get the maximum number of local variables of this code attribute.
51         @return the maximum number
52      */

53     public int getMaxLocals() {
54         return maxLocals;
55     }
56
57     /**
58         Set the maximum number of local variables of this code attribute.
59         @param maxLocals the maximum number
60      */

61     public void setMaxLocals(int maxLocals) {
62         this.maxLocals = maxLocals;
63     }
64
65     /**
66         Get the code of this code attribute as an array of bytes .
67         @return the array
68      */

69     public byte[] getCode() {
70         return code;
71     }
72
73     /**
74         Set the code of this code attribute as an array of bytes .
75         @param code the array
76      */

77     public void setCode(byte[] code) {
78         this.code = code;
79     }
80
81     /**
82         Get the exception table of this code attribute as an array of
83         <tt>ExceptionTableEntry</tt> elements.
84         @return the array
85      */

86     public ExceptionTableEntry[] getExceptionTable() {
87         return exceptionTable;
88     }
89     
90     /**
91         Set the exception table of this code attribute as an array of
92         <tt>ExceptionTableEntry</tt> elements.
93         @param exceptionTable the array
94      */

95     public void setExceptionTable(ExceptionTableEntry[] exceptionTable) {
96         this.exceptionTable = exceptionTable;
97     }
98
99     public void read(DataInput in)
100         throws InvalidByteCodeException, IOException {
101             
102         maxStack = in.readUnsignedShort();
103         maxLocals = in.readUnsignedShort();
104         int codeLength = in.readInt();
105         code = new byte[codeLength];
106         in.readFully(code);
107         
108         readExceptionTable(in);
109         readAttributes(in);
110         if (debug) debug("read ");
111     }
112
113     public void write(DataOutput out)
114         throws InvalidByteCodeException, IOException {
115         
116         super.write(out);
117         out.writeShort(maxStack);
118         out.writeShort(maxLocals);
119         out.writeInt(getLength(code));
120         out.write(code);
121         
122         writeExceptionTable(out);
123         writeAttributes(out);
124       
125         if (debug) debug("wrote ");
126     }
127
128     private void readExceptionTable(DataInput in)
129         throws InvalidByteCodeException, IOException {
130             
131         int exceptionTableLength = in.readUnsignedShort();
132         exceptionTable = new ExceptionTableEntry[exceptionTableLength];
133         for (int i = 0; i < exceptionTableLength; i++) {
134             exceptionTable[i] = ExceptionTableEntry.create(in, classFile);
135         }
136     
137     }
138     
139     private void writeExceptionTable(DataOutput out)
140         throws InvalidByteCodeException, IOException {
141             
142         int exceptionTableLength = getLength(exceptionTable);
143
144         out.writeShort(exceptionTableLength);
145         for (int i = 0; i < exceptionTableLength; i++) {
146             exceptionTable[i].write(out);
147         }
148     
149     }
150
151     public int getAttributeLength() {
152         return INITIAL_LENGTH + getLength(code) +
153                getLength(exceptionTable) * ExceptionTableEntry.LENGTH +
154                6 * getLength(attributes) +
155                getTotalAttributesLength() ;
156     }
157
158     protected void debug(String JavaDoc message) {
159         super.debug(message + "Code attribute with max_stack " + maxStack +
160               ", max_locals " + maxLocals + ", code_length " + getLength(code));
161     }
162
163 }
164
Popular Tags