2 * Copyright (c) 2002, 2004, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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).
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.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
25 package org.apidesign.vm4brwsr;
27 import java.io.ByteArrayInputStream;
28 import java.io.DataInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import org.apidesign.bck2brwsr.core.JavaScriptBody;
32 import org.apidesign.bck2brwsr.core.JavaScriptPrototype;
34 /** This is a byte code parser heavily based on original code of JavaP utility.
35 * As such I decided to keep the original Oracle's GPLv2 header.
37 * @author Jaroslav Tulach <jtulach@netbeans.org>
39 final class ByteCodeParser {
40 private ByteCodeParser() {
43 /* Class File Constants */
44 public static final int JAVA_MAGIC = 0xcafebabe;
45 public static final int JAVA_VERSION = 45;
46 public static final int JAVA_MINOR_VERSION = 3;
49 public static final int CONSTANT_UTF8 = 1;
50 public static final int CONSTANT_UNICODE = 2;
51 public static final int CONSTANT_INTEGER = 3;
52 public static final int CONSTANT_FLOAT = 4;
53 public static final int CONSTANT_LONG = 5;
54 public static final int CONSTANT_DOUBLE = 6;
55 public static final int CONSTANT_CLASS = 7;
56 public static final int CONSTANT_STRING = 8;
57 public static final int CONSTANT_FIELD = 9;
58 public static final int CONSTANT_METHOD = 10;
59 public static final int CONSTANT_INTERFACEMETHOD = 11;
60 public static final int CONSTANT_NAMEANDTYPE = 12;
63 public static final int ACC_PUBLIC = 0x00000001;
64 public static final int ACC_PRIVATE = 0x00000002;
65 public static final int ACC_PROTECTED = 0x00000004;
66 public static final int ACC_STATIC = 0x00000008;
67 public static final int ACC_FINAL = 0x00000010;
68 public static final int ACC_SYNCHRONIZED = 0x00000020;
69 public static final int ACC_SUPER = 0x00000020;
70 public static final int ACC_VOLATILE = 0x00000040;
71 public static final int ACC_TRANSIENT = 0x00000080;
72 public static final int ACC_NATIVE = 0x00000100;
73 public static final int ACC_INTERFACE = 0x00000200;
74 public static final int ACC_ABSTRACT = 0x00000400;
75 public static final int ACC_STRICT = 0x00000800;
76 public static final int ACC_EXPLICIT = 0x00001000;
77 public static final int ACC_SYNTHETIC = 0x00010000; // actually, this is an attribute
79 /* Type codes for StackMap attribute */
80 public static final int ITEM_Bogus =0; // an unknown or uninitialized value
81 public static final int ITEM_Integer =1; // a 32-bit integer
82 public static final int ITEM_Float =2; // not used
83 public static final int ITEM_Double =3; // not used
84 public static final int ITEM_Long =4; // a 64-bit integer
85 public static final int ITEM_Null =5; // the type of null
86 public static final int ITEM_InitObject =6; // "this" in constructor
87 public static final int ITEM_Object =7; // followed by 2-byte index of class name
88 public static final int ITEM_NewObject =8; // followed by 2-byte ref to "new"
90 /* Constants used in StackMapTable attribute */
91 public static final int SAME_FRAME_BOUND = 64;
92 public static final int SAME_LOCALS_1_STACK_ITEM_BOUND = 128;
93 public static final int SAME_LOCALS_1_STACK_ITEM_EXTENDED = 247;
94 public static final int SAME_FRAME_EXTENDED = 251;
95 public static final int FULL_FRAME = 255;
98 public static final int opc_dead = -2;
99 public static final int opc_label = -1;
100 public static final int opc_nop = 0;
101 public static final int opc_aconst_null = 1;
102 public static final int opc_iconst_m1 = 2;
103 public static final int opc_iconst_0 = 3;
104 public static final int opc_iconst_1 = 4;
105 public static final int opc_iconst_2 = 5;
106 public static final int opc_iconst_3 = 6;
107 public static final int opc_iconst_4 = 7;
108 public static final int opc_iconst_5 = 8;
109 public static final int opc_lconst_0 = 9;
110 public static final int opc_lconst_1 = 10;
111 public static final int opc_fconst_0 = 11;
112 public static final int opc_fconst_1 = 12;
113 public static final int opc_fconst_2 = 13;
114 public static final int opc_dconst_0 = 14;
115 public static final int opc_dconst_1 = 15;
116 public static final int opc_bipush = 16;
117 public static final int opc_sipush = 17;
118 public static final int opc_ldc = 18;
119 public static final int opc_ldc_w = 19;
120 public static final int opc_ldc2_w = 20;
121 public static final int opc_iload = 21;
122 public static final int opc_lload = 22;
123 public static final int opc_fload = 23;
124 public static final int opc_dload = 24;
125 public static final int opc_aload = 25;
126 public static final int opc_iload_0 = 26;
127 public static final int opc_iload_1 = 27;
128 public static final int opc_iload_2 = 28;
129 public static final int opc_iload_3 = 29;
130 public static final int opc_lload_0 = 30;
131 public static final int opc_lload_1 = 31;
132 public static final int opc_lload_2 = 32;
133 public static final int opc_lload_3 = 33;
134 public static final int opc_fload_0 = 34;
135 public static final int opc_fload_1 = 35;
136 public static final int opc_fload_2 = 36;
137 public static final int opc_fload_3 = 37;
138 public static final int opc_dload_0 = 38;
139 public static final int opc_dload_1 = 39;
140 public static final int opc_dload_2 = 40;
141 public static final int opc_dload_3 = 41;
142 public static final int opc_aload_0 = 42;
143 public static final int opc_aload_1 = 43;
144 public static final int opc_aload_2 = 44;
145 public static final int opc_aload_3 = 45;
146 public static final int opc_iaload = 46;
147 public static final int opc_laload = 47;
148 public static final int opc_faload = 48;
149 public static final int opc_daload = 49;
150 public static final int opc_aaload = 50;
151 public static final int opc_baload = 51;
152 public static final int opc_caload = 52;
153 public static final int opc_saload = 53;
154 public static final int opc_istore = 54;
155 public static final int opc_lstore = 55;
156 public static final int opc_fstore = 56;
157 public static final int opc_dstore = 57;
158 public static final int opc_astore = 58;
159 public static final int opc_istore_0 = 59;
160 public static final int opc_istore_1 = 60;
161 public static final int opc_istore_2 = 61;
162 public static final int opc_istore_3 = 62;
163 public static final int opc_lstore_0 = 63;
164 public static final int opc_lstore_1 = 64;
165 public static final int opc_lstore_2 = 65;
166 public static final int opc_lstore_3 = 66;
167 public static final int opc_fstore_0 = 67;
168 public static final int opc_fstore_1 = 68;
169 public static final int opc_fstore_2 = 69;
170 public static final int opc_fstore_3 = 70;
171 public static final int opc_dstore_0 = 71;
172 public static final int opc_dstore_1 = 72;
173 public static final int opc_dstore_2 = 73;
174 public static final int opc_dstore_3 = 74;
175 public static final int opc_astore_0 = 75;
176 public static final int opc_astore_1 = 76;
177 public static final int opc_astore_2 = 77;
178 public static final int opc_astore_3 = 78;
179 public static final int opc_iastore = 79;
180 public static final int opc_lastore = 80;
181 public static final int opc_fastore = 81;
182 public static final int opc_dastore = 82;
183 public static final int opc_aastore = 83;
184 public static final int opc_bastore = 84;
185 public static final int opc_castore = 85;
186 public static final int opc_sastore = 86;
187 public static final int opc_pop = 87;
188 public static final int opc_pop2 = 88;
189 public static final int opc_dup = 89;
190 public static final int opc_dup_x1 = 90;
191 public static final int opc_dup_x2 = 91;
192 public static final int opc_dup2 = 92;
193 public static final int opc_dup2_x1 = 93;
194 public static final int opc_dup2_x2 = 94;
195 public static final int opc_swap = 95;
196 public static final int opc_iadd = 96;
197 public static final int opc_ladd = 97;
198 public static final int opc_fadd = 98;
199 public static final int opc_dadd = 99;
200 public static final int opc_isub = 100;
201 public static final int opc_lsub = 101;
202 public static final int opc_fsub = 102;
203 public static final int opc_dsub = 103;
204 public static final int opc_imul = 104;
205 public static final int opc_lmul = 105;
206 public static final int opc_fmul = 106;
207 public static final int opc_dmul = 107;
208 public static final int opc_idiv = 108;
209 public static final int opc_ldiv = 109;
210 public static final int opc_fdiv = 110;
211 public static final int opc_ddiv = 111;
212 public static final int opc_irem = 112;
213 public static final int opc_lrem = 113;
214 public static final int opc_frem = 114;
215 public static final int opc_drem = 115;
216 public static final int opc_ineg = 116;
217 public static final int opc_lneg = 117;
218 public static final int opc_fneg = 118;
219 public static final int opc_dneg = 119;
220 public static final int opc_ishl = 120;
221 public static final int opc_lshl = 121;
222 public static final int opc_ishr = 122;
223 public static final int opc_lshr = 123;
224 public static final int opc_iushr = 124;
225 public static final int opc_lushr = 125;
226 public static final int opc_iand = 126;
227 public static final int opc_land = 127;
228 public static final int opc_ior = 128;
229 public static final int opc_lor = 129;
230 public static final int opc_ixor = 130;
231 public static final int opc_lxor = 131;
232 public static final int opc_iinc = 132;
233 public static final int opc_i2l = 133;
234 public static final int opc_i2f = 134;
235 public static final int opc_i2d = 135;
236 public static final int opc_l2i = 136;
237 public static final int opc_l2f = 137;
238 public static final int opc_l2d = 138;
239 public static final int opc_f2i = 139;
240 public static final int opc_f2l = 140;
241 public static final int opc_f2d = 141;
242 public static final int opc_d2i = 142;
243 public static final int opc_d2l = 143;
244 public static final int opc_d2f = 144;
245 public static final int opc_i2b = 145;
246 public static final int opc_int2byte = 145;
247 public static final int opc_i2c = 146;
248 public static final int opc_int2char = 146;
249 public static final int opc_i2s = 147;
250 public static final int opc_int2short = 147;
251 public static final int opc_lcmp = 148;
252 public static final int opc_fcmpl = 149;
253 public static final int opc_fcmpg = 150;
254 public static final int opc_dcmpl = 151;
255 public static final int opc_dcmpg = 152;
256 public static final int opc_ifeq = 153;
257 public static final int opc_ifne = 154;
258 public static final int opc_iflt = 155;
259 public static final int opc_ifge = 156;
260 public static final int opc_ifgt = 157;
261 public static final int opc_ifle = 158;
262 public static final int opc_if_icmpeq = 159;
263 public static final int opc_if_icmpne = 160;
264 public static final int opc_if_icmplt = 161;
265 public static final int opc_if_icmpge = 162;
266 public static final int opc_if_icmpgt = 163;
267 public static final int opc_if_icmple = 164;
268 public static final int opc_if_acmpeq = 165;
269 public static final int opc_if_acmpne = 166;
270 public static final int opc_goto = 167;
271 public static final int opc_jsr = 168;
272 public static final int opc_ret = 169;
273 public static final int opc_tableswitch = 170;
274 public static final int opc_lookupswitch = 171;
275 public static final int opc_ireturn = 172;
276 public static final int opc_lreturn = 173;
277 public static final int opc_freturn = 174;
278 public static final int opc_dreturn = 175;
279 public static final int opc_areturn = 176;
280 public static final int opc_return = 177;
281 public static final int opc_getstatic = 178;
282 public static final int opc_putstatic = 179;
283 public static final int opc_getfield = 180;
284 public static final int opc_putfield = 181;
285 public static final int opc_invokevirtual = 182;
286 public static final int opc_invokenonvirtual = 183;
287 public static final int opc_invokespecial = 183;
288 public static final int opc_invokestatic = 184;
289 public static final int opc_invokeinterface = 185;
290 // public static final int opc_xxxunusedxxx = 186;
291 public static final int opc_new = 187;
292 public static final int opc_newarray = 188;
293 public static final int opc_anewarray = 189;
294 public static final int opc_arraylength = 190;
295 public static final int opc_athrow = 191;
296 public static final int opc_checkcast = 192;
297 public static final int opc_instanceof = 193;
298 public static final int opc_monitorenter = 194;
299 public static final int opc_monitorexit = 195;
300 public static final int opc_wide = 196;
301 public static final int opc_multianewarray = 197;
302 public static final int opc_ifnull = 198;
303 public static final int opc_ifnonnull = 199;
304 public static final int opc_goto_w = 200;
305 public static final int opc_jsr_w = 201;
306 /* Pseudo-instructions */
307 public static final int opc_bytecode = 203;
308 public static final int opc_try = 204;
309 public static final int opc_endtry = 205;
310 public static final int opc_catch = 206;
311 public static final int opc_var = 207;
312 public static final int opc_endvar = 208;
313 public static final int opc_localsmap = 209;
314 public static final int opc_stackmap = 210;
315 /* PicoJava prefixes */
316 public static final int opc_nonpriv = 254;
317 public static final int opc_priv = 255;
319 /* Wide instructions *
320 public static final int opc_iload_w = (opc_wide<<8)|opc_iload;
321 public static final int opc_lload_w = (opc_wide<<8)|opc_lload;
322 public static final int opc_fload_w = (opc_wide<<8)|opc_fload;
323 public static final int opc_dload_w = (opc_wide<<8)|opc_dload;
324 public static final int opc_aload_w = (opc_wide<<8)|opc_aload;
325 public static final int opc_istore_w = (opc_wide<<8)|opc_istore;
326 public static final int opc_lstore_w = (opc_wide<<8)|opc_lstore;
327 public static final int opc_fstore_w = (opc_wide<<8)|opc_fstore;
328 public static final int opc_dstore_w = (opc_wide<<8)|opc_dstore;
329 public static final int opc_astore_w = (opc_wide<<8)|opc_astore;
330 public static final int opc_ret_w = (opc_wide<<8)|opc_ret;
331 public static final int opc_iinc_w = (opc_wide<<8)|opc_iinc;
333 static class AnnotationParser {
335 private final boolean textual;
336 private final boolean iterateArray;
338 protected AnnotationParser(boolean textual, boolean iterateArray) {
339 this.textual = textual;
340 this.iterateArray = iterateArray;
343 protected void visitAnnotationStart(String type, boolean top) throws IOException {
346 protected void visitAnnotationEnd(String type, boolean top) throws IOException {
349 protected void visitValueStart(String attrName, char type) throws IOException {
352 protected void visitValueEnd(String attrName, char type) throws IOException {
355 protected void visitAttr(
356 String annoType, String attr, String attrType, String value) throws IOException {
359 protected void visitEnumAttr(
360 String annoType, String attr, String attrType, String value) throws IOException {
361 visitAttr(annoType, attr, attrType, value);
365 * Initialize the parsing with constant pool from
368 * @param attr the attribute defining annotations
369 * @param cd constant pool
370 * @throws IOException in case I/O fails
372 public final void parse(byte[] attr, ClassData cd) throws IOException {
373 ByteArrayInputStream is = new ByteArrayInputStream(attr);
374 DataInputStream dis = new DataInputStream(is);
382 private void read(DataInputStream dis, ClassData cd) throws IOException {
383 int cnt = dis.readUnsignedShort();
384 for (int i = 0; i < cnt; i++) {
385 readAnno(dis, cd, true);
389 private void readAnno(DataInputStream dis, ClassData cd, boolean top) throws IOException {
390 int type = dis.readUnsignedShort();
391 String typeName = cd.StringValue(type);
392 visitAnnotationStart(typeName, top);
393 int cnt = dis.readUnsignedShort();
394 for (int i = 0; i < cnt; i++) {
395 String attrName = cd.StringValue(dis.readUnsignedShort());
396 readValue(dis, cd, typeName, attrName);
398 visitAnnotationEnd(typeName, top);
400 visitAttr(typeName, null, null, null);
404 private void readValue(
405 DataInputStream dis, ClassData cd, String typeName, String attrName) throws IOException {
406 char type = (char) dis.readByte();
407 visitValueStart(attrName, type);
409 readAnno(dis, cd, false);
410 } else if ("CFJZsSIDB".indexOf(type) >= 0) { // NOI18N
411 int primitive = dis.readUnsignedShort();
412 String val = cd.stringValue(primitive, textual);
415 attrType = "Ljava_lang_String_2";
417 val = '"' + val + '"';
420 attrType = "" + type;
422 visitAttr(typeName, attrName, attrType, val);
423 } else if (type == 'c') {
424 int cls = dis.readUnsignedShort();
425 } else if (type == '[') {
426 int cnt = dis.readUnsignedShort();
427 for (int i = 0; i < cnt; i++) {
428 readValue(dis, cd, typeName, iterateArray ? attrName : null);
430 } else if (type == 'e') {
431 int enumT = dis.readUnsignedShort();
432 String attrType = cd.stringValue(enumT, textual);
433 int enumN = dis.readUnsignedShort();
434 String val = cd.stringValue(enumN, textual);
435 visitEnumAttr(typeName, attrName, attrType, val);
437 throw new IOException("Unknown type " + type);
439 visitValueEnd(attrName, type);
444 * Reads and stores attribute information.
446 * @author Sucheta Dambalkar (Adopted code from jdis)
448 private static class AttrData {
455 public AttrData(ClassData cls) {
460 * Reads unknown attribute.
462 public void read(int name_cpx, DataInputStream in) throws IOException {
463 this.name_cpx = name_cpx;
464 datalen = in.readInt();
465 data = new byte[datalen];
470 * Reads just the name of known attribute.
472 public void read(int name_cpx) {
473 this.name_cpx = name_cpx;
477 * Returns attribute name.
479 public String getAttrName() {
480 return cls.getString(name_cpx);
484 * Returns attribute data.
486 public byte[] getData() {
492 * Stores constant pool entry information with one field.
494 * @author Sucheta Dambalkar (Adopted code from jdis)
496 private static class CPX {
506 * Stores constant pool entry information with two fields.
508 * @author Sucheta Dambalkar (Adopted code from jdis)
510 private static class CPX2 {
514 CPX2(int cpx1, int cpx2) {
521 * Central data repository of the Java Disassembler. Stores all the
522 * information in java class file.
524 * @author Sucheta Dambalkar (Adopted code from jdis)
526 static final class ClassData {
529 private int minor_version;
530 private int major_version;
531 private int cpool_count;
532 private Object cpool[];
534 private int this_class = 0;
535 private int super_class;
536 private int interfaces_count;
537 private int[] interfaces = new int[0];
538 private FieldData[] fields;
539 private MethodData[] methods;
540 private InnerClassData[] innerClasses;
541 private int attributes_count;
542 private AttrData[] attrs;
543 private int source_cpx = 0;
545 private Hashtable indexHashAscii = new Hashtable();
546 private String pkgPrefix = "";
547 private int pkgPrefixLen = 0;
550 * Read classfile to disassemble.
552 public ClassData(InputStream infile) throws IOException {
553 this.read(new DataInputStream(infile));
557 * Reads and stores class file information.
559 public void read(DataInputStream in) throws IOException {
561 magic = in.readInt();
562 if (magic != JAVA_MAGIC) {
563 throw new ClassFormatError("wrong magic: "
564 + toHex(magic) + ", expected "
565 + toHex(JAVA_MAGIC));
567 minor_version = in.readShort();
568 major_version = in.readShort();
569 if (major_version != JAVA_VERSION) {
572 // Read the constant pool
574 access = in.readUnsignedShort();
575 this_class = in.readUnsignedShort();
576 super_class = in.readUnsignedShort();
579 interfaces_count = in.readUnsignedShort();
580 if (interfaces_count > 0) {
581 interfaces = new int[interfaces_count];
583 for (int i = 0; i < interfaces_count; i++) {
584 interfaces[i] = in.readShort();
593 // Read the attributes
594 attributes_count = in.readUnsignedShort();
595 attrs = new AttrData[attributes_count];
596 for (int k = 0; k < attributes_count; k++) {
597 int name_cpx = in.readUnsignedShort();
598 if (getTag(name_cpx) == CONSTANT_UTF8
599 && getString(name_cpx).equals("SourceFile")) {
600 if (in.readInt() != 2) {
601 throw new ClassFormatError("invalid attr length");
603 source_cpx = in.readUnsignedShort();
604 AttrData attr = new AttrData(this);
608 } else if (getTag(name_cpx) == CONSTANT_UTF8
609 && getString(name_cpx).equals("InnerClasses")) {
610 int length = in.readInt();
611 int num = in.readUnsignedShort();
612 if (2 + num * 8 != length) {
613 throw new ClassFormatError("invalid attr length");
615 innerClasses = new InnerClassData[num];
616 for (int j = 0; j < num; j++) {
617 InnerClassData innerClass = new InnerClassData(this);
619 innerClasses[j] = innerClass;
621 AttrData attr = new AttrData(this);
625 AttrData attr = new AttrData(this);
626 attr.read(name_cpx, in);
631 } // end ClassData.read()
634 * Reads and stores constant pool info.
636 void readCP(DataInputStream in) throws IOException {
637 cpool_count = in.readUnsignedShort();
638 tags = new byte[cpool_count];
639 cpool = new Object[cpool_count];
640 for (int i = 1; i < cpool_count; i++) {
641 byte tag = in.readByte();
643 switch (tags[i] = tag) {
645 String str = in.readUTF();
646 indexHashAscii.put(cpool[i] = str, new Integer(i));
648 case CONSTANT_INTEGER:
649 cpool[i] = new Integer(in.readInt());
652 cpool[i] = new Float(in.readFloat());
655 cpool[i++] = new Long(in.readLong());
657 case CONSTANT_DOUBLE:
658 cpool[i++] = new Double(in.readDouble());
661 case CONSTANT_STRING:
662 cpool[i] = new CPX(in.readUnsignedShort());
666 case CONSTANT_METHOD:
667 case CONSTANT_INTERFACEMETHOD:
668 case CONSTANT_NAMEANDTYPE:
669 cpool[i] = new CPX2(in.readUnsignedShort(), in.readUnsignedShort());
674 throw new ClassFormatError("invalid constant type: " + (int) tags[i]);
680 * Reads and strores field info.
682 protected void readFields(DataInputStream in) throws IOException {
683 int fields_count = in.readUnsignedShort();
684 fields = new FieldData[fields_count];
685 for (int k = 0; k < fields_count; k++) {
686 FieldData field = new FieldData(this);
693 * Reads and strores Method info.
695 protected void readMethods(DataInputStream in) throws IOException {
696 int methods_count = in.readUnsignedShort();
697 methods = new MethodData[methods_count];
698 for (int k = 0; k < methods_count; k++) {
699 MethodData method = new MethodData(this);
708 public String getString(int n) {
712 return (String) cpool[n];
717 * get the type of constant given an index
719 public byte getTag(int n) {
722 } catch (ArrayIndexOutOfBoundsException e) {
726 static final String hexString = "0123456789ABCDEF";
727 public static char hexTable[] = hexString.toCharArray();
729 static String toHex(long val, int width) {
730 StringBuffer s = new StringBuffer();
731 for (int i = width - 1; i >= 0; i--) {
732 s.append(hexTable[((int) (val >> (4 * i))) & 0xF]);
734 return "0x" + s.toString();
737 static String toHex(long val) {
739 for (width = 16; width > 0; width--) {
740 if ((val >> (width - 1) * 4) != 0) {
744 return toHex(val, width);
747 static String toHex(int val) {
749 for (width = 8; width > 0; width--) {
750 if ((val >> (width - 1) * 4) != 0) {
754 return toHex(val, width);
758 * Returns the name of this class.
760 public String getClassName() {
762 if (this_class == 0) {
767 if (tags[this_class] != CONSTANT_CLASS) {
768 return res; //"<CP["+cpx+"] is not a Class> ";
770 tcpx = ((CPX) cpool[this_class]).cpx;
771 } catch (ArrayIndexOutOfBoundsException e) {
772 return res; // "#"+cpx+"// invalid constant pool index";
773 } catch (Throwable e) {
774 return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
778 return (String) (cpool[tcpx]);
779 } catch (ArrayIndexOutOfBoundsException e) {
780 return res; // "class #"+scpx+"// invalid constant pool index";
781 } catch (ClassCastException e) {
782 return res; // "class #"+scpx+"// invalid constant pool reference";
783 } catch (Throwable e) {
784 return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
790 * Returns the name of class at perticular index.
792 public String getClassName(int cpx) {
793 String res = "#" + cpx;
799 if (tags[cpx] != CONSTANT_CLASS) {
800 return res; //"<CP["+cpx+"] is not a Class> ";
802 scpx = ((CPX) cpool[cpx]).cpx;
803 } catch (ArrayIndexOutOfBoundsException e) {
804 return res; // "#"+cpx+"// invalid constant pool index";
805 } catch (Throwable e) {
806 return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
810 return (String) (cpool[scpx]);
811 } catch (ArrayIndexOutOfBoundsException e) {
812 return res; // "class #"+scpx+"// invalid constant pool index";
813 } catch (ClassCastException e) {
814 return res; // "class #"+scpx+"// invalid constant pool reference";
815 } catch (Throwable e) {
816 return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
820 public int getAccessFlags() {
825 * Returns true if it is a class
827 public boolean isClass() {
828 if ((access & ACC_INTERFACE) == 0) {
835 * Returns true if it is a interface.
837 public boolean isInterface() {
838 if ((access & ACC_INTERFACE) != 0) {
845 * Returns true if this member is public, false otherwise.
847 public boolean isPublic() {
848 return (access & ACC_PUBLIC) != 0;
852 * Returns the access of this class or interface.
854 public String[] getAccess() {
855 Vector v = new Vector();
856 if ((access & ACC_PUBLIC) != 0) {
857 v.addElement("public");
859 if ((access & ACC_FINAL) != 0) {
860 v.addElement("final");
862 if ((access & ACC_ABSTRACT) != 0) {
863 v.addElement("abstract");
865 String[] accflags = new String[v.size()];
866 v.copyInto(accflags);
871 * Returns list of innerclasses.
873 public InnerClassData[] getInnerClasses() {
878 * Returns list of attributes.
880 final AttrData[] getAttributes() {
884 public byte[] findAnnotationData(boolean classRetention) {
885 String n = classRetention
886 ? "RuntimeInvisibleAnnotations" : // NOI18N
887 "RuntimeVisibleAnnotations"; // NOI18N
888 return findAttr(n, attrs);
892 * Returns true if superbit is set.
894 public boolean isSuperSet() {
895 if ((access & ACC_SUPER) != 0) {
902 * Returns super class name.
904 public String getSuperClassName() {
906 if (super_class == 0) {
911 if (tags[super_class] != CONSTANT_CLASS) {
912 return res; //"<CP["+cpx+"] is not a Class> ";
914 scpx = ((CPX) cpool[super_class]).cpx;
915 } catch (ArrayIndexOutOfBoundsException e) {
916 return res; // "#"+cpx+"// invalid constant pool index";
917 } catch (Throwable e) {
918 return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
922 return (String) (cpool[scpx]);
923 } catch (ArrayIndexOutOfBoundsException e) {
924 return res; // "class #"+scpx+"// invalid constant pool index";
925 } catch (ClassCastException e) {
926 return res; // "class #"+scpx+"// invalid constant pool reference";
927 } catch (Throwable e) {
928 return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
933 * Returns list of super interfaces.
935 public String[] getSuperInterfaces() {
936 String interfacenames[] = new String[interfaces.length];
937 int interfacecpx = -1;
938 for (int i = 0; i < interfaces.length; i++) {
939 interfacecpx = ((CPX) cpool[interfaces[i]]).cpx;
940 interfacenames[i] = (String) (cpool[interfacecpx]);
942 return interfacenames;
946 * Returns string at prticular constant pool index.
948 public String getStringValue(int cpoolx) {
950 return ((String) cpool[cpoolx]);
951 } catch (ArrayIndexOutOfBoundsException e) {
952 return "//invalid constant pool index:" + cpoolx;
953 } catch (ClassCastException e) {
954 return "//invalid constant pool ref:" + cpoolx;
959 * Returns list of field info.
961 public FieldData[] getFields() {
966 * Returns list of method info.
968 public MethodData[] getMethods() {
973 * Returns constant pool entry at that index.
975 public CPX2 getCpoolEntry(int cpx) {
976 return ((CPX2) (cpool[cpx]));
979 public Object getCpoolEntryobj(int cpx) {
984 * Returns index of this class.
986 public int getthis_cpx() {
991 * Returns string at that index.
993 public String StringValue(int cpx) {
994 return stringValue(cpx, false);
997 public String stringValue(int cpx, boolean textual) {
998 return stringValue(cpx, textual, null);
1001 public String stringValue(int cpx, String[] classRefs) {
1002 return stringValue(cpx, true, classRefs);
1005 private String stringValue(int cpx, boolean textual, String[] refs) {
1015 } catch (IndexOutOfBoundsException e) {
1016 return "<Incorrect CP index:" + cpx + ">";
1023 case CONSTANT_UTF8: {
1027 StringBuilder sb = new StringBuilder();
1028 String s = (String) x;
1029 for (int k = 0; k < s.length(); k++) {
1030 char c = s.charAt(k);
1033 sb.append('\\').append('\\');
1036 sb.append('\\').append('t');
1039 sb.append('\\').append('n');
1042 sb.append('\\').append('r');
1045 sb.append('\\').append('\"');
1048 sb.append("\\u2028");
1051 sb.append("\\u2029");
1057 return sb.toString();
1059 case CONSTANT_DOUBLE: {
1060 Double d = (Double) x;
1061 String sd = d.toString();
1067 case CONSTANT_FLOAT: {
1068 Float f = (Float) x;
1069 String sf = (f).toString();
1075 case CONSTANT_LONG: {
1078 return ln.toString();
1080 return ln.toString() + 'l';
1082 case CONSTANT_INTEGER: {
1083 Integer in = (Integer) x;
1084 return in.toString();
1086 case CONSTANT_CLASS:
1087 String jn = getClassName(cpx);
1094 return javaName(jn);
1095 case CONSTANT_STRING:
1096 String sv = stringValue(((CPX) x).cpx, textual);
1098 return '"' + sv + '"';
1102 case CONSTANT_FIELD:
1103 case CONSTANT_METHOD:
1104 case CONSTANT_INTERFACEMETHOD:
1105 //return getShortClassName(((CPX2)x).cpx1)+"."+StringValue(((CPX2)x).cpx2);
1106 return javaName(getClassName(((CPX2) x).cpx1)) + "." + StringValue(((CPX2) x).cpx2);
1108 case CONSTANT_NAMEANDTYPE:
1109 return getName(((CPX2) x).cpx1) + ":" + StringValue(((CPX2) x).cpx2);
1111 return "UnknownTag"; //TBD
1116 * Returns resolved java type name.
1118 public String javaName(String name) {
1122 int len = name.length();
1130 for (int k = 0; k < len; k += Character.charCount(cp)) {
1131 cp = name.codePointAt(k);
1133 if (!isJavaIdentifierStart(cp)) {
1136 } else if (cp != '/') {
1137 if (!isJavaIdentifierPart(cp)) {
1145 return "\"" + name + "\"";
1148 public String getName(int cpx) {
1151 return javaName((String) cpool[cpx]); //.replace('/','.');
1152 } catch (ArrayIndexOutOfBoundsException e) {
1153 return "<invalid constant pool index:" + cpx + ">";
1154 } catch (ClassCastException e) {
1155 return "<invalid constant pool ref:" + cpx + ">";
1160 * Returns unqualified class name.
1162 public String getShortClassName(int cpx) {
1163 String classname = javaName(getClassName(cpx));
1164 pkgPrefixLen = classname.lastIndexOf("/") + 1;
1165 if (pkgPrefixLen != 0) {
1166 pkgPrefix = classname.substring(0, pkgPrefixLen);
1167 if (classname.startsWith(pkgPrefix)) {
1168 return classname.substring(pkgPrefixLen);
1175 * Returns source file name.
1177 public String getSourceName() {
1178 return getName(source_cpx);
1182 * Returns package name.
1184 public String getPkgName() {
1185 String classname = getClassName(this_class);
1186 pkgPrefixLen = classname.lastIndexOf("/") + 1;
1187 if (pkgPrefixLen != 0) {
1188 pkgPrefix = classname.substring(0, pkgPrefixLen);
1189 return ("package " + pkgPrefix.substring(0, pkgPrefixLen - 1) + ";\n");
1196 * Returns total constant pool entry count.
1198 public int getCpoolCount() {
1203 * Returns minor version of class file.
1205 public int getMinor_version() {
1206 return minor_version;
1210 * Returns major version of class file.
1212 public int getMajor_version() {
1213 return major_version;
1216 private boolean isJavaIdentifierStart(int cp) {
1217 return ('a' <= cp && cp <= 'z') || ('A' <= cp && cp <= 'Z');
1220 private boolean isJavaIdentifierPart(int cp) {
1221 return isJavaIdentifierStart(cp) || ('0' <= cp && cp <= '9');
1224 public String[] getNameAndType(int indx) {
1225 return getNameAndType(indx, 0, new String[2]);
1228 private String[] getNameAndType(int indx, int at, String[] arr) {
1229 CPX2 c2 = getCpoolEntry(indx);
1230 arr[at] = StringValue(c2.cpx1);
1231 arr[at + 1] = StringValue(c2.cpx2);
1235 public String[] getFieldInfoName(int indx) {
1236 CPX2 c2 = getCpoolEntry(indx);
1237 String[] arr = new String[3];
1238 arr[0] = getClassName(c2.cpx1);
1239 return getNameAndType(c2.cpx2, 1, arr);
1242 static byte[] findAttr(String n, AttrData[] attrs) {
1243 for (AttrData ad : attrs) {
1244 if (n.equals(ad.getAttrName())) {
1245 return ad.getData();
1253 * Strores field data informastion.
1255 * @author Sucheta Dambalkar (Adopted code from jdis)
1257 static class FieldData {
1262 int descriptor_index;
1263 int attributes_count;
1265 boolean isSynthetic = false;
1266 boolean isDeprecated = false;
1269 public FieldData(ClassData cls) {
1274 * Read and store field info.
1276 public void read(DataInputStream in) throws IOException {
1277 access = in.readUnsignedShort();
1278 name_index = in.readUnsignedShort();
1279 descriptor_index = in.readUnsignedShort();
1280 // Read the attributes
1281 int attributes_count = in.readUnsignedShort();
1282 attrs = new Vector(attributes_count);
1283 for (int i = 0; i < attributes_count; i++) {
1284 int attr_name_index = in.readUnsignedShort();
1285 if (cls.getTag(attr_name_index) != CONSTANT_UTF8) {
1288 String attr_name = cls.getString(attr_name_index);
1289 if (attr_name.equals("ConstantValue")) {
1290 if (in.readInt() != 2) {
1291 throw new ClassFormatError("invalid ConstantValue attr length");
1293 value_cpx = in.readUnsignedShort();
1294 AttrData attr = new AttrData(cls);
1295 attr.read(attr_name_index);
1296 attrs.addElement(attr);
1297 } else if (attr_name.equals("Synthetic")) {
1298 if (in.readInt() != 0) {
1299 throw new ClassFormatError("invalid Synthetic attr length");
1302 AttrData attr = new AttrData(cls);
1303 attr.read(attr_name_index);
1304 attrs.addElement(attr);
1305 } else if (attr_name.equals("Deprecated")) {
1306 if (in.readInt() != 0) {
1307 throw new ClassFormatError("invalid Synthetic attr length");
1309 isDeprecated = true;
1310 AttrData attr = new AttrData(cls);
1311 attr.read(attr_name_index);
1312 attrs.addElement(attr);
1314 AttrData attr = new AttrData(cls);
1315 attr.read(attr_name_index, in);
1316 attrs.addElement(attr);
1322 public boolean isStatic() {
1323 return (access & ACC_STATIC) != 0;
1327 * Returns access of a field.
1329 public String[] getAccess() {
1330 Vector v = new Vector();
1331 if ((access & ACC_PUBLIC) != 0) {
1332 v.addElement("public");
1334 if ((access & ACC_PRIVATE) != 0) {
1335 v.addElement("private");
1337 if ((access & ACC_PROTECTED) != 0) {
1338 v.addElement("protected");
1340 if ((access & ACC_STATIC) != 0) {
1341 v.addElement("static");
1343 if ((access & ACC_FINAL) != 0) {
1344 v.addElement("final");
1346 if ((access & ACC_VOLATILE) != 0) {
1347 v.addElement("volatile");
1349 if ((access & ACC_TRANSIENT) != 0) {
1350 v.addElement("transient");
1352 String[] accflags = new String[v.size()];
1353 v.copyInto(accflags);
1358 * Returns name of a field.
1360 public String getName() {
1361 return cls.getStringValue(name_index);
1365 * Returns internal signature of a field
1367 public String getInternalSig() {
1368 return cls.getStringValue(descriptor_index);
1372 * Returns true if field is synthetic.
1374 public boolean isSynthetic() {
1379 * Returns true if field is deprecated.
1381 public boolean isDeprecated() {
1382 return isDeprecated;
1385 public boolean hasConstantValue() {
1386 return value_cpx != -1;
1390 * Returns list of attributes of field.
1392 public Vector getAttributes() {
1396 public byte[] findAnnotationData(boolean classRetention) {
1397 String n = classRetention
1398 ? "RuntimeInvisibleAnnotations" : // NOI18N
1399 "RuntimeVisibleAnnotations"; // NOI18N
1400 AttrData[] arr = new AttrData[attrs.size()];
1401 attrs.copyInto(arr);
1402 return ClassData.findAttr(n, arr);
1407 * A JavaScript optimized replacement for Hashtable.
1409 * @author Jaroslav Tulach <jtulach@netbeans.org>
1411 private static final class Hashtable {
1413 private Object[] keys;
1414 private Object[] values;
1420 Hashtable(int i, double d) {
1427 synchronized void put(Object key, Object val) {
1428 int[] where = {-1, -1};
1429 Object found = get(key, where);
1430 if (where[0] != -1) {
1432 values[where[0]] = val;
1434 if (where[1] != -1) {
1436 keys[where[1]] = key;
1437 values[where[1]] = val;
1440 keys = new Object[11];
1441 values = new Object[11];
1445 Object[] newKeys = new Object[keys.length * 2];
1446 Object[] newValues = new Object[values.length * 2];
1447 for (int i = 0; i < keys.length; i++) {
1448 newKeys[i] = keys[i];
1449 newValues[i] = values[i];
1451 newKeys[keys.length] = key;
1452 newValues[keys.length] = val;
1460 Object get(Object key) {
1461 return get(key, null);
1464 private synchronized Object get(Object key, int[] foundAndNull) {
1468 for (int i = 0; i < keys.length; i++) {
1469 if (keys[i] == null) {
1470 if (foundAndNull != null) {
1471 foundAndNull[1] = i;
1473 } else if (keys[i].equals(key)) {
1474 if (foundAndNull != null) {
1475 foundAndNull[0] = i;
1485 * Strores InnerClass data informastion.
1487 * @author Sucheta Dambalkar (Adopted code from jdis)
1489 private static class InnerClassData {
1492 int inner_class_info_index, outer_class_info_index, inner_name_index, access;
1494 public InnerClassData(ClassData cls) {
1500 * Read Innerclass attribute data.
1502 public void read(DataInputStream in) throws IOException {
1503 inner_class_info_index = in.readUnsignedShort();
1504 outer_class_info_index = in.readUnsignedShort();
1505 inner_name_index = in.readUnsignedShort();
1506 access = in.readUnsignedShort();
1510 * Returns the access of this class or interface.
1512 public String[] getAccess() {
1513 Vector v = new Vector();
1514 if ((access & ACC_PUBLIC) != 0) {
1515 v.addElement("public");
1517 if ((access & ACC_FINAL) != 0) {
1518 v.addElement("final");
1520 if ((access & ACC_ABSTRACT) != 0) {
1521 v.addElement("abstract");
1523 String[] accflags = new String[v.size()];
1524 v.copyInto(accflags);
1527 } // end InnerClassData
1530 * Strores LineNumberTable data information.
1532 * @author Sucheta Dambalkar (Adopted code from jdis)
1534 private static class LineNumData {
1536 short start_pc, line_number;
1538 public LineNumData() {
1542 * Read LineNumberTable attribute.
1544 public LineNumData(DataInputStream in) throws IOException {
1545 start_pc = in.readShort();
1546 line_number = in.readShort();
1552 * Strores LocalVariableTable data information.
1554 * @author Sucheta Dambalkar (Adopted code from jdis)
1556 private static class LocVarData {
1558 short start_pc, length, name_cpx, sig_cpx, slot;
1560 public LocVarData() {
1564 * Read LocalVariableTable attribute.
1566 public LocVarData(DataInputStream in) throws IOException {
1567 start_pc = in.readShort();
1568 length = in.readShort();
1569 name_cpx = in.readShort();
1570 sig_cpx = in.readShort();
1571 slot = in.readShort();
1576 * Strores method data informastion.
1578 * @author Sucheta Dambalkar (Adopted code from jdis)
1580 static class MethodData {
1585 int descriptor_index;
1586 int attributes_count;
1588 Vector exception_table = new Vector(0);
1589 Vector lin_num_tb = new Vector(0);
1590 Vector loc_var_tb = new Vector(0);
1591 StackMapTableData[] stackMapTable;
1592 StackMapData[] stackMap;
1593 int[] exc_index_table = null;
1594 Vector attrs = new Vector(0);
1595 Vector code_attrs = new Vector(0);
1596 int max_stack, max_locals;
1597 boolean isSynthetic = false;
1598 boolean isDeprecated = false;
1600 public MethodData(ClassData cls) {
1607 public void read(DataInputStream in) throws IOException {
1608 access = in.readUnsignedShort();
1609 name_index = in.readUnsignedShort();
1610 descriptor_index = in.readUnsignedShort();
1611 int attributes_count = in.readUnsignedShort();
1612 for (int i = 0; i < attributes_count; i++) {
1613 int attr_name_index = in.readUnsignedShort();
1617 if (cls.getTag(attr_name_index) == CONSTANT_UTF8) {
1618 String attr_name = cls.getString(attr_name_index);
1619 if (attr_name.equals("Code")) {
1621 AttrData attr = new AttrData(cls);
1622 attr.read(attr_name_index);
1623 attrs.addElement(attr);
1625 } else if (attr_name.equals("Exceptions")) {
1627 AttrData attr = new AttrData(cls);
1628 attr.read(attr_name_index);
1629 attrs.addElement(attr);
1631 } else if (attr_name.equals("Synthetic")) {
1632 if (in.readInt() != 0) {
1633 throw new ClassFormatError("invalid Synthetic attr length");
1636 AttrData attr = new AttrData(cls);
1637 attr.read(attr_name_index);
1638 attrs.addElement(attr);
1640 } else if (attr_name.equals("Deprecated")) {
1641 if (in.readInt() != 0) {
1642 throw new ClassFormatError("invalid Synthetic attr length");
1644 isDeprecated = true;
1645 AttrData attr = new AttrData(cls);
1646 attr.read(attr_name_index);
1647 attrs.addElement(attr);
1651 AttrData attr = new AttrData(cls);
1652 attr.read(attr_name_index, in);
1653 attrs.addElement(attr);
1659 * Read code attribute info.
1661 public void readCode(DataInputStream in) throws IOException {
1663 int attr_length = in.readInt();
1664 max_stack = in.readUnsignedShort();
1665 max_locals = in.readUnsignedShort();
1666 int codelen = in.readInt();
1668 code = new byte[codelen];
1670 while (totalread < codelen) {
1671 totalread += in.read(code, totalread, codelen - totalread);
1673 // in.read(code, 0, codelen);
1675 readExceptionTable(in);
1676 int code_attributes_count = in.readUnsignedShort();
1678 for (int k = 0; k < code_attributes_count; k++) {
1679 int table_name_index = in.readUnsignedShort();
1680 int table_name_tag = cls.getTag(table_name_index);
1681 AttrData attr = new AttrData(cls);
1682 if (table_name_tag == CONSTANT_UTF8) {
1683 String table_name_tstr = cls.getString(table_name_index);
1684 if (table_name_tstr.equals("LineNumberTable")) {
1685 readLineNumTable(in);
1686 attr.read(table_name_index);
1687 } else if (table_name_tstr.equals("LocalVariableTable")) {
1688 readLocVarTable(in);
1689 attr.read(table_name_index);
1690 } else if (table_name_tstr.equals("StackMapTable")) {
1691 readStackMapTable(in);
1692 attr.read(table_name_index);
1693 } else if (table_name_tstr.equals("StackMap")) {
1695 attr.read(table_name_index);
1697 attr.read(table_name_index, in);
1699 code_attrs.addElement(attr);
1703 attr.read(table_name_index, in);
1704 code_attrs.addElement(attr);
1709 * Read exception table info.
1711 void readExceptionTable(DataInputStream in) throws IOException {
1712 int exception_table_len = in.readUnsignedShort();
1713 exception_table = new Vector(exception_table_len);
1714 for (int l = 0; l < exception_table_len; l++) {
1715 exception_table.addElement(new TrapData(in, l));
1720 * Read LineNumberTable attribute info.
1722 void readLineNumTable(DataInputStream in) throws IOException {
1723 int attr_len = in.readInt(); // attr_length
1724 int lin_num_tb_len = in.readUnsignedShort();
1725 lin_num_tb = new Vector(lin_num_tb_len);
1726 for (int l = 0; l < lin_num_tb_len; l++) {
1727 lin_num_tb.addElement(new LineNumData(in));
1732 * Read LocalVariableTable attribute info.
1734 void readLocVarTable(DataInputStream in) throws IOException {
1735 int attr_len = in.readInt(); // attr_length
1736 int loc_var_tb_len = in.readUnsignedShort();
1737 loc_var_tb = new Vector(loc_var_tb_len);
1738 for (int l = 0; l < loc_var_tb_len; l++) {
1739 loc_var_tb.addElement(new LocVarData(in));
1744 * Read Exception attribute info.
1746 public void readExceptions(DataInputStream in) throws IOException {
1747 int attr_len = in.readInt(); // attr_length in prog
1748 int num_exceptions = in.readUnsignedShort();
1749 exc_index_table = new int[num_exceptions];
1750 for (int l = 0; l < num_exceptions; l++) {
1751 int exc = in.readShort();
1752 exc_index_table[l] = exc;
1757 * Read StackMapTable attribute info.
1759 void readStackMapTable(DataInputStream in) throws IOException {
1760 int attr_len = in.readInt(); //attr_length
1761 int stack_map_tb_len = in.readUnsignedShort();
1762 stackMapTable = new StackMapTableData[stack_map_tb_len];
1763 for (int i = 0; i < stack_map_tb_len; i++) {
1764 stackMapTable[i] = StackMapTableData.getInstance(in, this);
1769 * Read StackMap attribute info.
1771 void readStackMap(DataInputStream in) throws IOException {
1772 int attr_len = in.readInt(); //attr_length
1773 int stack_map_len = in.readUnsignedShort();
1774 stackMap = new StackMapData[stack_map_len];
1775 for (int i = 0; i < stack_map_len; i++) {
1776 stackMap[i] = new StackMapData(in, this);
1781 * Return access of the method.
1783 public int getAccess() {
1788 * Return name of the method.
1790 public String getName() {
1791 return cls.getStringValue(name_index);
1795 * Return internal siganature of the method.
1797 public String getInternalSig() {
1798 return cls.getStringValue(descriptor_index);
1802 * Return code attribute data of a method.
1804 public byte[] getCode() {
1809 * Return LineNumberTable size.
1811 public int getnumlines() {
1812 return lin_num_tb.size();
1816 * Return LineNumberTable
1818 public Vector getlin_num_tb() {
1823 * Return LocalVariableTable size.
1825 public int getloc_var_tbsize() {
1826 return loc_var_tb.size();
1830 * Return LocalVariableTable.
1832 public Vector getloc_var_tb() {
1839 public StackMapData[] getStackMap() {
1844 * Return StackMapTable.
1846 public StackMapTableData[] getStackMapTable() {
1847 return stackMapTable;
1850 public StackMapIterator createStackMapIterator() {
1851 return new StackMapIterator(this);
1855 * Return true if method is static
1857 public boolean isStatic() {
1858 if ((access & ACC_STATIC) != 0) {
1865 * Return max depth of operand stack.
1867 public int getMaxStack() {
1872 * Return number of local variables.
1874 public int getMaxLocals() {
1879 * Return exception index table in Exception attribute.
1881 public int[] get_exc_index_table() {
1882 return exc_index_table;
1886 * Return exception table in code attributre.
1888 public TrapDataIterator getTrapDataIterator() {
1889 return new TrapDataIterator(exception_table);
1893 * Return method attributes.
1895 public Vector getAttributes() {
1900 * Return code attributes.
1902 public Vector getCodeAttributes() {
1907 * Return true if method id synthetic.
1909 public boolean isSynthetic() {
1914 * Return true if method is deprecated.
1916 public boolean isDeprecated() {
1917 return isDeprecated;
1920 public byte[] findAnnotationData(boolean classRetention) {
1921 String n = classRetention
1922 ? "RuntimeInvisibleAnnotations" : // NOI18N
1923 "RuntimeVisibleAnnotations"; // NOI18N
1924 AttrData[] arr = new AttrData[attrs.size()];
1925 attrs.copyInto(arr);
1926 return ClassData.findAttr(n, arr);
1929 public boolean isConstructor() {
1930 return "<init>".equals(getName());
1934 /* represents one entry of StackMap attribute
1936 private static class StackMapData {
1942 StackMapData(int offset, int[] locals, int[] stack) {
1943 this.offset = offset;
1944 this.locals = locals;
1948 StackMapData(DataInputStream in, MethodData method) throws IOException {
1949 offset = in.readUnsignedShort();
1950 int local_size = in.readUnsignedShort();
1951 locals = readTypeArray(in, local_size, method);
1952 int stack_size = in.readUnsignedShort();
1953 stack = readTypeArray(in, stack_size, method);
1956 static final int[] readTypeArray(DataInputStream in, int length, MethodData method) throws IOException {
1957 int[] types = new int[length];
1958 for (int i = 0; i < length; i++) {
1959 types[i] = readType(in, method);
1964 static final int readType(DataInputStream in, MethodData method) throws IOException {
1965 int type = in.readUnsignedByte();
1966 if (type == ITEM_Object || type == ITEM_NewObject) {
1967 type = type | (in.readUnsignedShort() << 8);
1973 static final class StackMapIterator {
1975 private final StackMapTableData[] stackMapTable;
1976 private final TypeArray argTypes;
1977 private final TypeArray localTypes;
1978 private final TypeArray stackTypes;
1979 private int nextFrameIndex;
1980 private int lastFrameByteCodeOffset;
1981 private int byteCodeOffset;
1983 StackMapIterator(final MethodData methodData) {
1984 this(methodData.getStackMapTable(),
1985 methodData.getInternalSig(),
1986 methodData.isStatic());
1989 StackMapIterator(final StackMapTableData[] stackMapTable,
1990 final String methodSignature,
1991 final boolean isStaticMethod) {
1992 this.stackMapTable = (stackMapTable != null)
1994 : new StackMapTableData[0];
1996 argTypes = getArgTypes(methodSignature, isStaticMethod);
1997 localTypes = new TypeArray();
1998 stackTypes = new TypeArray();
2000 localTypes.addAll(argTypes);
2002 lastFrameByteCodeOffset = -1;
2006 public boolean isEmpty() {
2007 return stackMapTable.length == 0;
2010 public String getFrameAsString() {
2011 return (nextFrameIndex == 0)
2012 ? StackMapTableData.toString("INITIAL", 0, null, null)
2013 : stackMapTable[nextFrameIndex - 1].toString();
2016 public int getFrameIndex() {
2017 return nextFrameIndex;
2020 public TypeArray getFrameStack() {
2024 public TypeArray getFrameLocals() {
2028 public TypeArray getArguments() {
2032 public void advanceBy(final int numByteCodes) {
2033 if (numByteCodes < 0) {
2034 throw new IllegalStateException("Forward only iterator");
2037 byteCodeOffset += numByteCodes;
2038 while ((nextFrameIndex < stackMapTable.length)
2039 && ((byteCodeOffset - lastFrameByteCodeOffset)
2040 >= (stackMapTable[nextFrameIndex].offsetDelta
2042 final StackMapTableData nextFrame = stackMapTable[nextFrameIndex];
2044 lastFrameByteCodeOffset += nextFrame.offsetDelta + 1;
2045 nextFrame.applyTo(localTypes, stackTypes);
2051 public void advanceTo(final int nextByteCodeOffset) {
2052 advanceBy(nextByteCodeOffset - byteCodeOffset);
2055 private static TypeArray getArgTypes(final String methodSignature,
2056 final boolean isStaticMethod) {
2057 final TypeArray argTypes = new TypeArray();
2059 if (!isStaticMethod) {
2060 argTypes.add(ITEM_Object);
2063 if (methodSignature.charAt(0) != '(') {
2064 throw new IllegalArgumentException("Invalid method signature");
2067 final int length = methodSignature.length();
2068 boolean skipType = false;
2070 for (int i = 1; i < length; ++i) {
2071 switch (methodSignature.charAt(i)) {
2077 argType = ITEM_Integer;
2080 argType = ITEM_Long;
2083 argType = ITEM_Float;
2086 argType = ITEM_Double;
2089 i = methodSignature.indexOf(';', i + 1);
2091 throw new IllegalArgumentException(
2092 "Invalid method signature");
2094 argType = ITEM_Object;
2098 // not interested in the return value type
2102 argTypes.add(ITEM_Object);
2108 throw new IllegalArgumentException(
2109 "Invalid method signature");
2113 argTypes.add(argType);
2122 /* represents one entry of StackMapTable attribute
2125 private static abstract class StackMapTableData {
2127 final int frameType;
2130 StackMapTableData(int frameType) {
2131 this.frameType = frameType;
2134 abstract void applyTo(TypeArray localTypes, TypeArray stackTypes);
2136 protected static String toString(
2137 final String frameType,
2139 final int[] localTypes,
2140 final int[] stackTypes) {
2141 final StringBuilder sb = new StringBuilder(frameType);
2143 sb.append("(off: +").append(offset);
2144 if (localTypes != null) {
2145 sb.append(", locals: ");
2146 appendTypes(sb, localTypes);
2148 if (stackTypes != null) {
2149 sb.append(", stack: ");
2150 appendTypes(sb, stackTypes);
2154 return sb.toString();
2157 private static void appendTypes(final StringBuilder sb, final int[] types) {
2159 if (types.length > 0) {
2160 sb.append(TypeArray.typeString(types[0]));
2161 for (int i = 1; i < types.length; ++i) {
2163 sb.append(TypeArray.typeString(types[i]));
2169 private static class SameFrame extends StackMapTableData {
2171 SameFrame(int frameType, int offsetDelta) {
2173 this.offsetDelta = offsetDelta;
2177 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
2182 public String toString() {
2183 return toString("SAME" + ((frameType == SAME_FRAME_EXTENDED)
2184 ? "_FRAME_EXTENDED" : ""),
2190 private static class SameLocals1StackItem extends StackMapTableData {
2194 SameLocals1StackItem(int frameType, int offsetDelta, int[] stack) {
2196 this.offsetDelta = offsetDelta;
2201 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
2202 stackTypes.setAll(stack);
2206 public String toString() {
2208 "SAME_LOCALS_1_STACK_ITEM"
2209 + ((frameType == SAME_LOCALS_1_STACK_ITEM_EXTENDED)
2210 ? "_EXTENDED" : ""),
2216 private static class ChopFrame extends StackMapTableData {
2218 ChopFrame(int frameType, int offsetDelta) {
2220 this.offsetDelta = offsetDelta;
2224 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
2225 localTypes.setSize(localTypes.getSize()
2226 - (SAME_FRAME_EXTENDED - frameType));
2231 public String toString() {
2232 return toString("CHOP", offsetDelta, null, null);
2236 private static class AppendFrame extends StackMapTableData {
2240 AppendFrame(int frameType, int offsetDelta, int[] locals) {
2242 this.offsetDelta = offsetDelta;
2243 this.locals = locals;
2247 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
2248 localTypes.addAll(locals);
2253 public String toString() {
2254 return toString("APPEND", offsetDelta, locals, null);
2258 private static class FullFrame extends StackMapTableData {
2263 FullFrame(int offsetDelta, int[] locals, int[] stack) {
2265 this.offsetDelta = offsetDelta;
2266 this.locals = locals;
2271 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
2272 localTypes.setAll(locals);
2273 stackTypes.setAll(stack);
2277 public String toString() {
2278 return toString("FULL", offsetDelta, locals, stack);
2282 static StackMapTableData getInstance(DataInputStream in, MethodData method)
2283 throws IOException {
2284 int frameType = in.readUnsignedByte();
2286 if (frameType < SAME_FRAME_BOUND) {
2288 return new SameFrame(frameType, frameType);
2289 } else if (SAME_FRAME_BOUND <= frameType && frameType < SAME_LOCALS_1_STACK_ITEM_BOUND) {
2290 // same_locals_1_stack_item_frame
2291 // read additional single stack element
2292 return new SameLocals1StackItem(frameType,
2293 (frameType - SAME_FRAME_BOUND),
2294 StackMapData.readTypeArray(in, 1, method));
2295 } else if (frameType == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
2296 // same_locals_1_stack_item_extended
2297 return new SameLocals1StackItem(frameType,
2298 in.readUnsignedShort(),
2299 StackMapData.readTypeArray(in, 1, method));
2300 } else if (SAME_LOCALS_1_STACK_ITEM_EXTENDED < frameType && frameType < SAME_FRAME_EXTENDED) {
2301 // chop_frame or same_frame_extended
2302 return new ChopFrame(frameType, in.readUnsignedShort());
2303 } else if (frameType == SAME_FRAME_EXTENDED) {
2304 // chop_frame or same_frame_extended
2305 return new SameFrame(frameType, in.readUnsignedShort());
2306 } else if (SAME_FRAME_EXTENDED < frameType && frameType < FULL_FRAME) {
2308 return new AppendFrame(frameType, in.readUnsignedShort(),
2309 StackMapData.readTypeArray(in, frameType - SAME_FRAME_EXTENDED, method));
2310 } else if (frameType == FULL_FRAME) {
2312 int offsetDelta = in.readUnsignedShort();
2313 int locals_size = in.readUnsignedShort();
2314 int[] locals = StackMapData.readTypeArray(in, locals_size, method);
2315 int stack_size = in.readUnsignedShort();
2316 int[] stack = StackMapData.readTypeArray(in, stack_size, method);
2317 return new FullFrame(offsetDelta, locals, stack);
2319 throw new ClassFormatError("unrecognized frame_type in StackMapTable");
2325 * Stores exception table data in code attribute.
2327 * @author Sucheta Dambalkar (Adopted code from jdis)
2329 static final class TrapData {
2331 public final short start_pc;
2332 public final short end_pc;
2333 public final short handler_pc;
2334 public final short catch_cpx;
2338 * Read and store exception table data in code attribute.
2340 TrapData(DataInputStream in, int num) throws IOException {
2342 start_pc = in.readShort();
2343 end_pc = in.readShort();
2344 handler_pc = in.readShort();
2345 catch_cpx = in.readShort();
2349 * returns recommended identifier
2351 public String ident() {
2357 * @author Jaroslav Tulach <jtulach@netbeans.org>
2359 static final class TrapDataIterator {
2361 private final Hashtable exStart = new Hashtable();
2362 private final Hashtable exStop = new Hashtable();
2363 private TrapData[] current = new TrapData[10];
2364 private int currentCount;
2366 TrapDataIterator(Vector exceptionTable) {
2367 for (int i = 0; i < exceptionTable.size(); i++) {
2368 final TrapData td = (TrapData) exceptionTable.elementAt(i);
2369 put(exStart, td.start_pc, td);
2370 put(exStop, td.end_pc, td);
2374 private static void put(Hashtable h, short key, TrapData td) {
2375 Short s = Short.valueOf((short) key);
2376 Vector v = (Vector) h.get(s);
2384 private boolean processAll(Hashtable h, Short key, boolean add) {
2385 boolean change = false;
2386 Vector v = (Vector) h.get(key);
2389 for (int i = 0; i < s; i++) {
2390 TrapData td = (TrapData) v.elementAt(i);
2403 public boolean advanceTo(int i) {
2404 Short s = Short.valueOf((short) i);
2405 boolean ch1 = processAll(exStart, s, true);
2406 boolean ch2 = processAll(exStop, s, false);
2410 public boolean useTry() {
2411 return currentCount > 0;
2414 public TrapData[] current() {
2415 TrapData[] copy = new TrapData[currentCount];
2416 for (int i = 0; i < currentCount; i++) {
2417 copy[i] = current[i];
2422 private void add(TrapData e) {
2423 if (currentCount == current.length) {
2424 TrapData[] data = new TrapData[currentCount * 2];
2425 for (int i = 0; i < currentCount; i++) {
2426 data[i] = current[i];
2430 current[currentCount++] = e;
2433 private void remove(TrapData e) {
2434 if (currentCount == 0) {
2438 while (from < currentCount) {
2439 if (e == current[from++]) {
2443 while (from < currentCount) {
2444 current[from - 1] = current[from];
2445 current[from] = null;
2451 static final class TypeArray {
2453 private static final int CAPACITY_INCREMENT = 16;
2454 private int[] types;
2457 public TypeArray() {
2460 public TypeArray(final TypeArray initialTypes) {
2461 setAll(initialTypes);
2464 public void add(final int newType) {
2465 ensureCapacity(size + 1);
2466 types[size++] = newType;
2469 public void addAll(final TypeArray newTypes) {
2470 addAll(newTypes.types, 0, newTypes.size);
2473 public void addAll(final int[] newTypes) {
2474 addAll(newTypes, 0, newTypes.length);
2477 public void addAll(final int[] newTypes,
2481 ensureCapacity(size + count);
2482 arraycopy(newTypes, offset, types, size, count);
2487 public void set(final int index, final int newType) {
2488 types[index] = newType;
2491 public void setAll(final TypeArray newTypes) {
2492 setAll(newTypes.types, 0, newTypes.size);
2495 public void setAll(final int[] newTypes) {
2496 setAll(newTypes, 0, newTypes.length);
2499 public void setAll(final int[] newTypes,
2503 ensureCapacity(count);
2504 arraycopy(newTypes, offset, types, 0, count);
2511 public void setSize(final int newSize) {
2512 if (size != newSize) {
2513 ensureCapacity(newSize);
2515 for (int i = size; i < newSize; ++i) {
2522 public void clear() {
2526 public int getSize() {
2530 public int get(final int index) {
2531 return types[index];
2534 public static String typeString(final int type) {
2535 switch (type & 0xff) {
2548 case ITEM_InitObject: // UninitializedThis
2552 case ITEM_NewObject: // Uninitialized
2555 throw new IllegalArgumentException("Unknown type");
2560 public String toString() {
2561 final StringBuilder sb = new StringBuilder("[");
2563 sb.append(typeString(types[0]));
2564 for (int i = 1; i < size; ++i) {
2566 sb.append(typeString(types[i]));
2570 return sb.append(']').toString();
2573 private void ensureCapacity(final int minCapacity) {
2574 if ((minCapacity == 0)
2575 || (types != null) && (minCapacity <= types.length)) {
2579 final int newCapacity =
2580 ((minCapacity + CAPACITY_INCREMENT - 1) / CAPACITY_INCREMENT)
2581 * CAPACITY_INCREMENT;
2582 final int[] newTypes = new int[newCapacity];
2585 arraycopy(types, 0, newTypes, 0, size);
2591 // no System.arraycopy
2592 private void arraycopy(final int[] src, final int srcPos,
2593 final int[] dest, final int destPos,
2595 for (int i = 0; i < length; ++i) {
2596 dest[destPos + i] = src[srcPos + i];
2601 * A JavaScript ready replacement for java.util.Vector
2603 * @author Jaroslav Tulach <jtulach@netbeans.org>
2605 @JavaScriptPrototype(prototype = "new Array")
2606 private static final class Vector {
2608 private Object[] arr;
2616 void add(Object objectType) {
2617 addElement(objectType);
2620 @JavaScriptBody(args = {"obj"}, body =
2622 void addElement(Object obj) {
2623 final int s = size();
2625 setElementAt(obj, s);
2628 @JavaScriptBody(args = {}, body =
2629 "return this.length;")
2631 return arr == null ? 0 : arr.length;
2634 @JavaScriptBody(args = {"newArr"}, body =
2635 "for (var i = 0; i < this.length; i++) {\n"
2636 + " newArr[i] = this[i];\n"
2638 void copyInto(Object[] newArr) {
2642 int min = Math.min(newArr.length, arr.length);
2643 for (int i = 0; i < min; i++) {
2648 @JavaScriptBody(args = {"index"}, body =
2649 "return this[index];")
2650 Object elementAt(int index) {
2654 private void setSize(int len) {
2655 Object[] newArr = new Object[len];
2660 @JavaScriptBody(args = {"val", "index"}, body =
2661 "this[index] = val;")
2662 void setElementAt(Object val, int index) {