|
1 /* |
|
2 * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 package com.sun.tools.classfile; |
|
27 |
|
28 import java.io.IOException; |
|
29 |
|
30 /** |
|
31 * See JVMS3, section 4.8.3. |
|
32 * |
|
33 * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
|
34 * you write code that depends on this, you do so at your own risk. |
|
35 * This code and its internal interfaces are subject to change or |
|
36 * deletion without notice.</b> |
|
37 */ |
|
38 public class Code_attribute extends Attribute { |
|
39 public class InvalidIndex extends AttributeException { |
|
40 InvalidIndex(int index) { |
|
41 this.index = index; |
|
42 } |
|
43 |
|
44 @Override |
|
45 public String getMessage() { |
|
46 // i18n |
|
47 return "invalid index " + index + " in Code attribute"; |
|
48 } |
|
49 |
|
50 public final int index; |
|
51 } |
|
52 |
|
53 Code_attribute(ClassReader cr, int name_index, int length) |
|
54 throws IOException, ConstantPoolException { |
|
55 super(name_index, length); |
|
56 max_stack = cr.readUnsignedShort(); |
|
57 max_locals = cr.readUnsignedShort(); |
|
58 code_length = cr.readInt(); |
|
59 code = new byte[code_length]; |
|
60 cr.readFully(code); |
|
61 exception_table_langth = cr.readUnsignedShort(); |
|
62 exception_table = new Exception_data[exception_table_langth]; |
|
63 for (int i = 0; i < exception_table_langth; i++) |
|
64 exception_table[i] = new Exception_data(cr); |
|
65 attributes = new Attributes(cr); |
|
66 } |
|
67 |
|
68 public int getByte(int offset) throws InvalidIndex { |
|
69 if (offset < 0 || offset >= code.length) |
|
70 throw new InvalidIndex(offset); |
|
71 return code[offset]; |
|
72 } |
|
73 |
|
74 public int getUnsignedByte(int offset) throws InvalidIndex { |
|
75 if (offset < 0 || offset >= code.length) |
|
76 throw new InvalidIndex(offset); |
|
77 return code[offset] & 0xff; |
|
78 } |
|
79 |
|
80 public int getShort(int offset) throws InvalidIndex { |
|
81 if (offset < 0 || offset + 1 >= code.length) |
|
82 throw new InvalidIndex(offset); |
|
83 return (code[offset] << 8) | (code[offset + 1] & 0xFF); |
|
84 } |
|
85 |
|
86 public int getUnsignedShort(int offset) throws InvalidIndex { |
|
87 if (offset < 0 || offset + 1 >= code.length) |
|
88 throw new InvalidIndex(offset); |
|
89 return ((code[offset] << 8) | (code[offset + 1] & 0xFF)) & 0xFFFF; |
|
90 } |
|
91 |
|
92 public int getInt(int offset) throws InvalidIndex { |
|
93 if (offset < 0 || offset + 3 >= code.length) |
|
94 throw new InvalidIndex(offset); |
|
95 return (getShort(offset) << 16) | (getShort(offset + 2) & 0xFFFF); |
|
96 } |
|
97 |
|
98 public <R, D> R accept(Visitor<R, D> visitor, D data) { |
|
99 return visitor.visitCode(this, data); |
|
100 } |
|
101 |
|
102 public final int max_stack; |
|
103 public final int max_locals; |
|
104 public final int code_length; |
|
105 public final byte[] code; |
|
106 public final int exception_table_langth; |
|
107 public final Exception_data[] exception_table; |
|
108 public final Attributes attributes; |
|
109 |
|
110 public class Exception_data { |
|
111 Exception_data(ClassReader cr) throws IOException { |
|
112 start_pc = cr.readUnsignedShort(); |
|
113 end_pc = cr.readUnsignedShort(); |
|
114 handler_pc = cr.readUnsignedShort(); |
|
115 catch_type = cr.readUnsignedShort(); |
|
116 } |
|
117 |
|
118 public final int start_pc; |
|
119 public final int end_pc; |
|
120 public final int handler_pc; |
|
121 public final int catch_type; |
|
122 } |
|
123 } |