KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > classfile > Code


1 /*
2  * Code.java
3  *
4  * The contents of this file are subject to the terms of the Common Development
5  * and Distribution License (the License). You may not use this file except in
6  * compliance with the License.
7  *
8  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
9  * or http://www.netbeans.org/cddl.txt.
10  *
11  * When distributing Covered Code, include this CDDL Header Notice in each file
12  * and include the License file at http://www.netbeans.org/cddl.txt.
13  * If applicable, add the following below the CDDL Header, with the fields
14  * enclosed by brackets [] replaced by your own identifying information:
15  * "Portions Copyrighted [year] [name of copyright owner]"
16  *
17  * The Original Software is NetBeans. The Initial Developer of the Original
18  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
19  * Microsystems, Inc. All Rights Reserved.
20  *
21  * Contributor(s): Thomas Ball
22  *
23  * Version: $Revision: 1.14 $
24  */

25
26 package org.netbeans.modules.classfile;
27
28 import java.io.DataInputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 /**
32  * The Code attribute of a method.
33  *
34  * @author Thomas Ball
35  */

36 public final class Code {
37     private static final boolean debug = false;
38
39     private int maxStack;
40     private int maxLocals;
41     private byte[] byteCodes;
42     private ExceptionTableEntry[] exceptionTable;
43     private int[] lineNumberTable;
44     private LocalVariableTableEntry[] localVariableTable;
45     private LocalVariableTypeTableEntry[] localVariableTypeTable;
46     private StackMapFrame[] stackMapTable;
47
48     /** Creates a new Code object */
49     /* package-private */ Code(DataInputStream JavaDoc in, ConstantPool pool) throws IOException JavaDoc {
50         if (in == null)
51             throw new IllegalArgumentException JavaDoc("input stream not specified");
52         if (pool == null)
53             throw new IllegalArgumentException JavaDoc("constant pool not specified");
54         try {
55             loadCode(in, pool);
56         } catch (IndexOutOfBoundsException JavaDoc e) {
57             throw new InvalidClassFileAttributeException("invalid code attribute", e);
58         }
59     }
60
61     private void loadCode(DataInputStream JavaDoc in, ConstantPool pool)
62       throws IOException JavaDoc {
63         maxStack = in.readUnsignedShort();
64         maxLocals = in.readUnsignedShort();
65         int len = in.readInt();
66         byteCodes = new byte[len];
67         in.readFully(byteCodes);
68         exceptionTable = ExceptionTableEntry.loadExceptionTable(in, pool);
69         loadCodeAttributes(in, pool);
70     }
71         
72     private void loadCodeAttributes(DataInputStream JavaDoc in, ConstantPool pool)
73       throws IOException JavaDoc {
74         int count = in.readUnsignedShort();
75         for (int i = 0; i < count; i++) {
76             Object JavaDoc o = pool.get(in.readUnsignedShort());
77             if (!(o instanceof CPUTF8Info))
78                 throw new InvalidClassFormatException();
79             CPUTF8Info entry = (CPUTF8Info)o;
80             int len = in.readInt();
81             String JavaDoc name = entry.getName();
82             if (name.equals("LineNumberTable")) //NOI18N
83
loadLineNumberTable(in, pool);
84             else if (name.equals("LocalVariableTable")) //NOI18N
85
localVariableTable =
86                     LocalVariableTableEntry.loadLocalVariableTable(in, pool);
87             else if (name.equals("LocalVariableTypeTable")) //NOI18N
88
localVariableTypeTable =
89                     LocalVariableTypeTableEntry.loadLocalVariableTypeTable(in, pool);
90             else if (name.equals("StackMapTable")) //NOI18N
91
stackMapTable = StackMapFrame.loadStackMapTable(in, pool);
92             else {
93         if (debug)
94             System.out.println("skipped unknown code attribute: " + name);
95                 // ignore unknown attribute...
96
int n;
97         while ((n = (int)in.skip(len)) > 0 && n < len)
98             len -= n;
99             }
100         }
101         if (lineNumberTable == null)
102             lineNumberTable = new int[0];
103         if (localVariableTable == null)
104             localVariableTable = new LocalVariableTableEntry[0];
105         if (localVariableTypeTable == null)
106             localVariableTypeTable = new LocalVariableTypeTableEntry[0];
107         if (stackMapTable == null)
108             stackMapTable = new StackMapFrame[0];
109     }
110     
111     private void loadLineNumberTable(DataInputStream JavaDoc in, ConstantPool pool) throws IOException JavaDoc {
112         int n = in.readUnsignedShort();
113         lineNumberTable = new int[n * 2];
114         for (int i = 0; i < n; i++) {
115             lineNumberTable[i * 2] = in.readUnsignedShort(); // start_pc
116
lineNumberTable[i * 2 + 1] = in.readUnsignedShort(); // line_number
117
}
118     }
119
120     public final int getMaxStack() {
121         return maxStack;
122     }
123     
124     public final int getMaxLocals() {
125         return maxLocals;
126     }
127     
128     public final byte[] getByteCodes() {
129         return byteCodes.clone();
130     }
131     
132     public final ExceptionTableEntry[] getExceptionTable() {
133         return exceptionTable.clone();
134     }
135
136     /**
137      * Returns an array of int pairs consisting of a start_pc and a
138      * line_number. For example, [0] = first pc, [1] = first line,
139      * [2] = second pc, etc.
140      */

141     public final int[] getLineNumberTable() {
142         return lineNumberTable.clone();
143     }
144
145     /**
146      * Returns the local variable table for this code.
147      */

148     public final LocalVariableTableEntry[] getLocalVariableTable() {
149         return localVariableTable.clone();
150     }
151
152     /**
153      * Returns the local variable type table for this code, which
154      * describes the generic reference type for those variables which
155      * are generic.
156      */

157     public final LocalVariableTypeTableEntry[] getLocalVariableTypeTable() {
158         return localVariableTypeTable.clone();
159     }
160     
161     /**
162      * Returns the stack map table for this code, which defines the stack frame
163      * information needed by the new classfile verifier in Java 6.
164      */

165     public final StackMapFrame[] getStackMapTable() {
166         return stackMapTable.clone();
167     }
168
169     public String JavaDoc toString() {
170     StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Code: bytes=");
171     sb.append(byteCodes.length);
172     sb.append(", stack=");
173     sb.append(maxStack);
174     sb.append(", locals=");
175     sb.append(maxLocals);
176     return sb.toString();
177     }
178 }
179
Popular Tags