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 java.util.Arrays;
32 import org.apidesign.bck2brwsr.core.JavaScriptBody;
33 import org.apidesign.bck2brwsr.core.JavaScriptPrototype;
35 /** This is a byte code parser heavily based on original code of JavaP utility.
36 * As such I decided to keep the original Oracle's GPLv2 header.
38 * @author Jaroslav Tulach <jtulach@netbeans.org>
40 final class ByteCodeParser {
41 private ByteCodeParser() {
44 /* Class File Constants */
45 public static final int JAVA_MAGIC = 0xcafebabe;
46 public static final int JAVA_VERSION = 45;
47 public static final int JAVA_MINOR_VERSION = 3;
50 public static final int CONSTANT_UTF8 = 1;
51 public static final int CONSTANT_UNICODE = 2;
52 public static final int CONSTANT_INTEGER = 3;
53 public static final int CONSTANT_FLOAT = 4;
54 public static final int CONSTANT_LONG = 5;
55 public static final int CONSTANT_DOUBLE = 6;
56 public static final int CONSTANT_CLASS = 7;
57 public static final int CONSTANT_STRING = 8;
58 public static final int CONSTANT_FIELD = 9;
59 public static final int CONSTANT_METHOD = 10;
60 public static final int CONSTANT_INTERFACEMETHOD = 11;
61 public static final int CONSTANT_NAMEANDTYPE = 12;
62 public static final int CONSTANT_METHODHANDLE = 15;
63 public static final int CONSTANT_METHODTYPE = 16;
64 public static final int CONSTANT_INVOKEDYNAMIC = 18;
67 public static final int ACC_PUBLIC = 0x00000001;
68 public static final int ACC_PRIVATE = 0x00000002;
69 public static final int ACC_PROTECTED = 0x00000004;
70 public static final int ACC_STATIC = 0x00000008;
71 public static final int ACC_FINAL = 0x00000010;
72 public static final int ACC_SYNCHRONIZED = 0x00000020;
73 public static final int ACC_SUPER = 0x00000020;
74 public static final int ACC_VOLATILE = 0x00000040;
75 public static final int ACC_TRANSIENT = 0x00000080;
76 public static final int ACC_NATIVE = 0x00000100;
77 public static final int ACC_INTERFACE = 0x00000200;
78 public static final int ACC_ABSTRACT = 0x00000400;
79 public static final int ACC_STRICT = 0x00000800;
80 public static final int ACC_EXPLICIT = 0x00001000;
81 public static final int ACC_SYNTHETIC = 0x00010000; // actually, this is an attribute
83 /* Type codes for StackMap attribute */
84 public static final int ITEM_Bogus =0; // an unknown or uninitialized value
85 public static final int ITEM_Integer =1; // a 32-bit integer
86 public static final int ITEM_Float =2; // not used
87 public static final int ITEM_Double =3; // not used
88 public static final int ITEM_Long =4; // a 64-bit integer
89 public static final int ITEM_Null =5; // the type of null
90 public static final int ITEM_InitObject =6; // "this" in constructor
91 public static final int ITEM_Object =7; // followed by 2-byte index of class name
92 public static final int ITEM_NewObject =8; // followed by 2-byte ref to "new"
94 /* Constants used in StackMapTable attribute */
95 public static final int SAME_FRAME_BOUND = 64;
96 public static final int SAME_LOCALS_1_STACK_ITEM_BOUND = 128;
97 public static final int SAME_LOCALS_1_STACK_ITEM_EXTENDED = 247;
98 public static final int SAME_FRAME_EXTENDED = 251;
99 public static final int FULL_FRAME = 255;
102 public static final int opc_dead = -2;
103 public static final int opc_label = -1;
104 public static final int opc_nop = 0;
105 public static final int opc_aconst_null = 1;
106 public static final int opc_iconst_m1 = 2;
107 public static final int opc_iconst_0 = 3;
108 public static final int opc_iconst_1 = 4;
109 public static final int opc_iconst_2 = 5;
110 public static final int opc_iconst_3 = 6;
111 public static final int opc_iconst_4 = 7;
112 public static final int opc_iconst_5 = 8;
113 public static final int opc_lconst_0 = 9;
114 public static final int opc_lconst_1 = 10;
115 public static final int opc_fconst_0 = 11;
116 public static final int opc_fconst_1 = 12;
117 public static final int opc_fconst_2 = 13;
118 public static final int opc_dconst_0 = 14;
119 public static final int opc_dconst_1 = 15;
120 public static final int opc_bipush = 16;
121 public static final int opc_sipush = 17;
122 public static final int opc_ldc = 18;
123 public static final int opc_ldc_w = 19;
124 public static final int opc_ldc2_w = 20;
125 public static final int opc_iload = 21;
126 public static final int opc_lload = 22;
127 public static final int opc_fload = 23;
128 public static final int opc_dload = 24;
129 public static final int opc_aload = 25;
130 public static final int opc_iload_0 = 26;
131 public static final int opc_iload_1 = 27;
132 public static final int opc_iload_2 = 28;
133 public static final int opc_iload_3 = 29;
134 public static final int opc_lload_0 = 30;
135 public static final int opc_lload_1 = 31;
136 public static final int opc_lload_2 = 32;
137 public static final int opc_lload_3 = 33;
138 public static final int opc_fload_0 = 34;
139 public static final int opc_fload_1 = 35;
140 public static final int opc_fload_2 = 36;
141 public static final int opc_fload_3 = 37;
142 public static final int opc_dload_0 = 38;
143 public static final int opc_dload_1 = 39;
144 public static final int opc_dload_2 = 40;
145 public static final int opc_dload_3 = 41;
146 public static final int opc_aload_0 = 42;
147 public static final int opc_aload_1 = 43;
148 public static final int opc_aload_2 = 44;
149 public static final int opc_aload_3 = 45;
150 public static final int opc_iaload = 46;
151 public static final int opc_laload = 47;
152 public static final int opc_faload = 48;
153 public static final int opc_daload = 49;
154 public static final int opc_aaload = 50;
155 public static final int opc_baload = 51;
156 public static final int opc_caload = 52;
157 public static final int opc_saload = 53;
158 public static final int opc_istore = 54;
159 public static final int opc_lstore = 55;
160 public static final int opc_fstore = 56;
161 public static final int opc_dstore = 57;
162 public static final int opc_astore = 58;
163 public static final int opc_istore_0 = 59;
164 public static final int opc_istore_1 = 60;
165 public static final int opc_istore_2 = 61;
166 public static final int opc_istore_3 = 62;
167 public static final int opc_lstore_0 = 63;
168 public static final int opc_lstore_1 = 64;
169 public static final int opc_lstore_2 = 65;
170 public static final int opc_lstore_3 = 66;
171 public static final int opc_fstore_0 = 67;
172 public static final int opc_fstore_1 = 68;
173 public static final int opc_fstore_2 = 69;
174 public static final int opc_fstore_3 = 70;
175 public static final int opc_dstore_0 = 71;
176 public static final int opc_dstore_1 = 72;
177 public static final int opc_dstore_2 = 73;
178 public static final int opc_dstore_3 = 74;
179 public static final int opc_astore_0 = 75;
180 public static final int opc_astore_1 = 76;
181 public static final int opc_astore_2 = 77;
182 public static final int opc_astore_3 = 78;
183 public static final int opc_iastore = 79;
184 public static final int opc_lastore = 80;
185 public static final int opc_fastore = 81;
186 public static final int opc_dastore = 82;
187 public static final int opc_aastore = 83;
188 public static final int opc_bastore = 84;
189 public static final int opc_castore = 85;
190 public static final int opc_sastore = 86;
191 public static final int opc_pop = 87;
192 public static final int opc_pop2 = 88;
193 public static final int opc_dup = 89;
194 public static final int opc_dup_x1 = 90;
195 public static final int opc_dup_x2 = 91;
196 public static final int opc_dup2 = 92;
197 public static final int opc_dup2_x1 = 93;
198 public static final int opc_dup2_x2 = 94;
199 public static final int opc_swap = 95;
200 public static final int opc_iadd = 96;
201 public static final int opc_ladd = 97;
202 public static final int opc_fadd = 98;
203 public static final int opc_dadd = 99;
204 public static final int opc_isub = 100;
205 public static final int opc_lsub = 101;
206 public static final int opc_fsub = 102;
207 public static final int opc_dsub = 103;
208 public static final int opc_imul = 104;
209 public static final int opc_lmul = 105;
210 public static final int opc_fmul = 106;
211 public static final int opc_dmul = 107;
212 public static final int opc_idiv = 108;
213 public static final int opc_ldiv = 109;
214 public static final int opc_fdiv = 110;
215 public static final int opc_ddiv = 111;
216 public static final int opc_irem = 112;
217 public static final int opc_lrem = 113;
218 public static final int opc_frem = 114;
219 public static final int opc_drem = 115;
220 public static final int opc_ineg = 116;
221 public static final int opc_lneg = 117;
222 public static final int opc_fneg = 118;
223 public static final int opc_dneg = 119;
224 public static final int opc_ishl = 120;
225 public static final int opc_lshl = 121;
226 public static final int opc_ishr = 122;
227 public static final int opc_lshr = 123;
228 public static final int opc_iushr = 124;
229 public static final int opc_lushr = 125;
230 public static final int opc_iand = 126;
231 public static final int opc_land = 127;
232 public static final int opc_ior = 128;
233 public static final int opc_lor = 129;
234 public static final int opc_ixor = 130;
235 public static final int opc_lxor = 131;
236 public static final int opc_iinc = 132;
237 public static final int opc_i2l = 133;
238 public static final int opc_i2f = 134;
239 public static final int opc_i2d = 135;
240 public static final int opc_l2i = 136;
241 public static final int opc_l2f = 137;
242 public static final int opc_l2d = 138;
243 public static final int opc_f2i = 139;
244 public static final int opc_f2l = 140;
245 public static final int opc_f2d = 141;
246 public static final int opc_d2i = 142;
247 public static final int opc_d2l = 143;
248 public static final int opc_d2f = 144;
249 public static final int opc_i2b = 145;
250 public static final int opc_int2byte = 145;
251 public static final int opc_i2c = 146;
252 public static final int opc_int2char = 146;
253 public static final int opc_i2s = 147;
254 public static final int opc_int2short = 147;
255 public static final int opc_lcmp = 148;
256 public static final int opc_fcmpl = 149;
257 public static final int opc_fcmpg = 150;
258 public static final int opc_dcmpl = 151;
259 public static final int opc_dcmpg = 152;
260 public static final int opc_ifeq = 153;
261 public static final int opc_ifne = 154;
262 public static final int opc_iflt = 155;
263 public static final int opc_ifge = 156;
264 public static final int opc_ifgt = 157;
265 public static final int opc_ifle = 158;
266 public static final int opc_if_icmpeq = 159;
267 public static final int opc_if_icmpne = 160;
268 public static final int opc_if_icmplt = 161;
269 public static final int opc_if_icmpge = 162;
270 public static final int opc_if_icmpgt = 163;
271 public static final int opc_if_icmple = 164;
272 public static final int opc_if_acmpeq = 165;
273 public static final int opc_if_acmpne = 166;
274 public static final int opc_goto = 167;
275 public static final int opc_jsr = 168;
276 public static final int opc_ret = 169;
277 public static final int opc_tableswitch = 170;
278 public static final int opc_lookupswitch = 171;
279 public static final int opc_ireturn = 172;
280 public static final int opc_lreturn = 173;
281 public static final int opc_freturn = 174;
282 public static final int opc_dreturn = 175;
283 public static final int opc_areturn = 176;
284 public static final int opc_return = 177;
285 public static final int opc_getstatic = 178;
286 public static final int opc_putstatic = 179;
287 public static final int opc_getfield = 180;
288 public static final int opc_putfield = 181;
289 public static final int opc_invokevirtual = 182;
290 public static final int opc_invokenonvirtual = 183;
291 public static final int opc_invokespecial = 183;
292 public static final int opc_invokestatic = 184;
293 public static final int opc_invokeinterface = 185;
294 public static final int opc_invokedynamic = 186;
295 public static final int opc_new = 187;
296 public static final int opc_newarray = 188;
297 public static final int opc_anewarray = 189;
298 public static final int opc_arraylength = 190;
299 public static final int opc_athrow = 191;
300 public static final int opc_checkcast = 192;
301 public static final int opc_instanceof = 193;
302 public static final int opc_monitorenter = 194;
303 public static final int opc_monitorexit = 195;
304 public static final int opc_wide = 196;
305 public static final int opc_multianewarray = 197;
306 public static final int opc_ifnull = 198;
307 public static final int opc_ifnonnull = 199;
308 public static final int opc_goto_w = 200;
309 public static final int opc_jsr_w = 201;
310 /* Pseudo-instructions */
311 public static final int opc_bytecode = 203;
312 public static final int opc_try = 204;
313 public static final int opc_endtry = 205;
314 public static final int opc_catch = 206;
315 public static final int opc_var = 207;
316 public static final int opc_endvar = 208;
317 public static final int opc_localsmap = 209;
318 public static final int opc_stackmap = 210;
319 /* PicoJava prefixes */
320 public static final int opc_nonpriv = 254;
321 public static final int opc_priv = 255;
323 /* Wide instructions *
324 public static final int opc_iload_w = (opc_wide<<8)|opc_iload;
325 public static final int opc_lload_w = (opc_wide<<8)|opc_lload;
326 public static final int opc_fload_w = (opc_wide<<8)|opc_fload;
327 public static final int opc_dload_w = (opc_wide<<8)|opc_dload;
328 public static final int opc_aload_w = (opc_wide<<8)|opc_aload;
329 public static final int opc_istore_w = (opc_wide<<8)|opc_istore;
330 public static final int opc_lstore_w = (opc_wide<<8)|opc_lstore;
331 public static final int opc_fstore_w = (opc_wide<<8)|opc_fstore;
332 public static final int opc_dstore_w = (opc_wide<<8)|opc_dstore;
333 public static final int opc_astore_w = (opc_wide<<8)|opc_astore;
334 public static final int opc_ret_w = (opc_wide<<8)|opc_ret;
335 public static final int opc_iinc_w = (opc_wide<<8)|opc_iinc;
337 static class AnnotationParser {
339 private final boolean textual;
340 private final boolean iterateArray;
342 protected AnnotationParser(boolean textual, boolean iterateArray) {
343 this.textual = textual;
344 this.iterateArray = iterateArray;
347 protected void visitAnnotationStart(String type, boolean top) throws IOException {
350 protected void visitAnnotationEnd(String type, boolean top) throws IOException {
353 protected void visitValueStart(String attrName, char type) throws IOException {
356 protected void visitValueEnd(String attrName, char type) throws IOException {
359 protected void visitAttr(
360 String annoType, String attr, String attrType, String value) throws IOException {
363 protected void visitEnumAttr(
364 String annoType, String attr, String attrType, String value) throws IOException {
365 visitAttr(annoType, attr, attrType, value);
369 * Initialize the parsing with constant pool from
372 * @param attr the attribute defining annotations
373 * @param cd constant pool
374 * @throws IOException in case I/O fails
376 public final void parse(byte[] attr, ClassData cd) throws IOException {
377 ByteArrayInputStream is = new ByteArrayInputStream(attr);
378 DataInputStream dis = new DataInputStream(is);
386 private void read(DataInputStream dis, ClassData cd) throws IOException {
387 int cnt = dis.readUnsignedShort();
388 for (int i = 0; i < cnt; i++) {
389 readAnno(dis, cd, true);
393 private void readAnno(DataInputStream dis, ClassData cd, boolean top) throws IOException {
394 int type = dis.readUnsignedShort();
395 String typeName = cd.StringValue(type);
396 visitAnnotationStart(typeName, top);
397 int cnt = dis.readUnsignedShort();
398 for (int i = 0; i < cnt; i++) {
399 String attrName = cd.StringValue(dis.readUnsignedShort());
400 readValue(dis, cd, typeName, attrName);
402 visitAnnotationEnd(typeName, top);
404 visitAttr(typeName, null, null, null);
408 private void readValue(
409 DataInputStream dis, ClassData cd, String typeName, String attrName) throws IOException {
410 char type = (char) dis.readByte();
411 visitValueStart(attrName, type);
413 readAnno(dis, cd, false);
414 } else if ("CFJZsSIDB".indexOf(type) >= 0) { // NOI18N
415 int primitive = dis.readUnsignedShort();
416 String val = cd.stringValue(primitive, textual);
419 attrType = "Ljava_lang_String_2";
421 val = '"' + val + '"';
424 attrType = "" + type;
426 visitAttr(typeName, attrName, attrType, val);
427 } else if (type == 'c') {
428 int cls = dis.readUnsignedShort();
429 } else if (type == '[') {
430 int cnt = dis.readUnsignedShort();
431 for (int i = 0; i < cnt; i++) {
432 readValue(dis, cd, typeName, iterateArray ? attrName : null);
434 } else if (type == 'e') {
435 int enumT = dis.readUnsignedShort();
436 String attrType = cd.stringValue(enumT, textual);
437 int enumN = dis.readUnsignedShort();
438 String val = cd.stringValue(enumN, textual);
439 visitEnumAttr(typeName, attrName, attrType, val);
441 throw new IOException("Unknown type " + type);
443 visitValueEnd(attrName, type);
448 * Reads and stores attribute information.
450 * @author Sucheta Dambalkar (Adopted code from jdis)
452 private static class AttrData {
459 public AttrData(ClassData cls) {
464 * Reads unknown attribute.
466 public void read(int name_cpx, DataInputStream in) throws IOException {
467 this.name_cpx = name_cpx;
468 datalen = in.readInt();
469 data = new byte[datalen];
474 * Reads just the name of known attribute.
476 public void read(int name_cpx) {
477 this.name_cpx = name_cpx;
481 * Returns attribute name.
483 public String getAttrName() {
484 return cls.getString(name_cpx);
488 * Returns attribute data.
490 public byte[] getData() {
496 * Stores constant pool entry information with one field.
498 * @author Sucheta Dambalkar (Adopted code from jdis)
500 private static class CPX {
510 * Stores constant pool entry information with two fields.
512 * @author Sucheta Dambalkar (Adopted code from jdis)
514 private static class CPX2 {
518 CPX2(int cpx1, int cpx2) {
525 * Central data repository of the Java Disassembler. Stores all the
526 * information in java class file.
528 * @author Sucheta Dambalkar (Adopted code from jdis)
530 static final class ClassData {
533 private int minor_version;
534 private int major_version;
535 private int cpool_count;
536 private Object cpool[];
538 private int this_class = 0;
539 private int super_class;
540 private int interfaces_count;
541 private int[] interfaces = new int[0];
542 private FieldData[] fields;
543 private MethodData[] methods;
544 private InnerClassData[] innerClasses;
545 private int attributes_count;
546 private AttrData[] attrs;
547 private int source_cpx = 0;
549 private Hashtable indexHashAscii = new Hashtable();
550 private String pkgPrefix = "";
551 private int pkgPrefixLen = 0;
554 * Read classfile to disassemble.
556 public ClassData(InputStream infile) throws IOException {
557 this.read(new DataInputStream(infile));
561 * Reads and stores class file information.
563 public void read(DataInputStream in) throws IOException {
565 magic = in.readInt();
566 if (magic != JAVA_MAGIC) {
567 throw new ClassFormatError("wrong magic: "
568 + toHex(magic) + ", expected "
569 + toHex(JAVA_MAGIC));
571 minor_version = in.readShort();
572 major_version = in.readShort();
573 if (major_version != JAVA_VERSION) {
576 // Read the constant pool
578 access = in.readUnsignedShort();
579 this_class = in.readUnsignedShort();
580 super_class = in.readUnsignedShort();
583 interfaces_count = in.readUnsignedShort();
584 if (interfaces_count > 0) {
585 interfaces = new int[interfaces_count];
587 for (int i = 0; i < interfaces_count; i++) {
588 interfaces[i] = in.readShort();
597 // Read the attributes
598 attributes_count = in.readUnsignedShort();
599 attrs = new AttrData[attributes_count];
600 for (int k = 0; k < attributes_count; k++) {
601 int name_cpx = in.readUnsignedShort();
602 if (getTag(name_cpx) == CONSTANT_UTF8
603 && getString(name_cpx).equals("SourceFile")) {
604 if (in.readInt() != 2) {
605 throw new ClassFormatError("invalid attr length");
607 source_cpx = in.readUnsignedShort();
608 AttrData attr = new AttrData(this);
612 } else if (getTag(name_cpx) == CONSTANT_UTF8
613 && getString(name_cpx).equals("InnerClasses")) {
614 int length = in.readInt();
615 int num = in.readUnsignedShort();
616 if (2 + num * 8 != length) {
617 throw new ClassFormatError("invalid attr length");
619 innerClasses = new InnerClassData[num];
620 for (int j = 0; j < num; j++) {
621 InnerClassData innerClass = new InnerClassData(this);
623 innerClasses[j] = innerClass;
625 AttrData attr = new AttrData(this);
628 } else if (getTag(name_cpx) == CONSTANT_UTF8
629 && getString(name_cpx).equals("BootstrapMethods")) {
630 AttrData attr = new AttrData(this);
631 readBootstrapMethods(in);
635 AttrData attr = new AttrData(this);
636 attr.read(name_cpx, in);
641 } // end ClassData.read()
643 void readBootstrapMethods(DataInputStream in) throws IOException {
644 int attr_len = in.readInt(); //attr_length
645 int number = in.readShort();
646 for (int i = 0; i < number; i++) {
647 int ref = in.readShort();
648 int len = in.readShort();
649 int[] args = new int[len];
650 for (int j = 0; j < len; j++) {
651 args[j] = in.readShort();
654 System.err.print("ref: " + ref + " len: " + len + ":");
655 for (int j = 0; j < len; j++) {
656 System.err.print(" " + args[j]);
658 System.err.println();
663 * Reads and stores constant pool info.
665 void readCP(DataInputStream in) throws IOException {
666 cpool_count = in.readUnsignedShort();
667 tags = new byte[cpool_count];
668 cpool = new Object[cpool_count];
669 for (int i = 1; i < cpool_count; i++) {
670 byte tag = in.readByte();
672 switch (tags[i] = tag) {
674 String str = in.readUTF();
675 indexHashAscii.put(cpool[i] = str, new Integer(i));
677 case CONSTANT_INTEGER:
678 cpool[i] = new Integer(in.readInt());
681 cpool[i] = new Float(in.readFloat());
684 cpool[i++] = new Long(in.readLong());
686 case CONSTANT_DOUBLE:
687 cpool[i++] = new Double(in.readDouble());
690 case CONSTANT_STRING:
691 cpool[i] = new CPX(in.readUnsignedShort());
695 case CONSTANT_METHOD:
696 case CONSTANT_INTERFACEMETHOD:
697 case CONSTANT_NAMEANDTYPE:
698 cpool[i] = new CPX2(in.readUnsignedShort(), in.readUnsignedShort());
700 case CONSTANT_METHODHANDLE:
701 cpool[i] = new CPX2(in.readByte(), in.readUnsignedShort());
703 case CONSTANT_METHODTYPE:
704 cpool[i] = new CPX(in.readUnsignedShort());
706 case CONSTANT_INVOKEDYNAMIC:
707 cpool[i] = new CPX2(in.readUnsignedShort(), in.readUnsignedShort());
711 throw new ClassFormatError("invalid constant type: " + (int) tags[i]);
717 * Reads and strores field info.
719 protected void readFields(DataInputStream in) throws IOException {
720 int fields_count = in.readUnsignedShort();
721 fields = new FieldData[fields_count];
722 for (int k = 0; k < fields_count; k++) {
723 FieldData field = new FieldData(this);
730 * Reads and strores Method info.
732 protected void readMethods(DataInputStream in) throws IOException {
733 int methods_count = in.readUnsignedShort();
734 methods = new MethodData[methods_count];
735 for (int k = 0; k < methods_count; k++) {
736 MethodData method = new MethodData(this);
745 public String getString(int n) {
749 return (String) cpool[n];
754 * get the type of constant given an index
756 public byte getTag(int n) {
759 } catch (ArrayIndexOutOfBoundsException e) {
763 static final String hexString = "0123456789ABCDEF";
764 public static char hexTable[] = hexString.toCharArray();
766 static String toHex(long val, int width) {
767 StringBuffer s = new StringBuffer();
768 for (int i = width - 1; i >= 0; i--) {
769 s.append(hexTable[((int) (val >> (4 * i))) & 0xF]);
771 return "0x" + s.toString();
774 static String toHex(long val) {
776 for (width = 16; width > 0; width--) {
777 if ((val >> (width - 1) * 4) != 0) {
781 return toHex(val, width);
784 static String toHex(int val) {
786 for (width = 8; width > 0; width--) {
787 if ((val >> (width - 1) * 4) != 0) {
791 return toHex(val, width);
795 * Returns the name of this class.
797 public String getClassName() {
799 if (this_class == 0) {
804 if (tags[this_class] != CONSTANT_CLASS) {
805 return res; //"<CP["+cpx+"] is not a Class> ";
807 tcpx = ((CPX) cpool[this_class]).cpx;
808 } catch (ArrayIndexOutOfBoundsException e) {
809 return res; // "#"+cpx+"// invalid constant pool index";
810 } catch (Throwable e) {
811 return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
815 return (String) (cpool[tcpx]);
816 } catch (ArrayIndexOutOfBoundsException e) {
817 return res; // "class #"+scpx+"// invalid constant pool index";
818 } catch (ClassCastException e) {
819 return res; // "class #"+scpx+"// invalid constant pool reference";
820 } catch (Throwable e) {
821 return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
827 * Returns the name of class at perticular index.
829 public String getClassName(int cpx) {
830 String res = "#" + cpx;
836 if (tags[cpx] != CONSTANT_CLASS) {
837 return res; //"<CP["+cpx+"] is not a Class> ";
839 scpx = ((CPX) cpool[cpx]).cpx;
840 } catch (ArrayIndexOutOfBoundsException e) {
841 return res; // "#"+cpx+"// invalid constant pool index";
842 } catch (Throwable e) {
843 return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
847 return (String) (cpool[scpx]);
848 } catch (ArrayIndexOutOfBoundsException e) {
849 return res; // "class #"+scpx+"// invalid constant pool index";
850 } catch (ClassCastException e) {
851 return res; // "class #"+scpx+"// invalid constant pool reference";
852 } catch (Throwable e) {
853 return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
857 public int getAccessFlags() {
862 * Returns true if it is a class
864 public boolean isClass() {
865 if ((access & ACC_INTERFACE) == 0) {
872 * Returns true if it is a interface.
874 public boolean isInterface() {
875 if ((access & ACC_INTERFACE) != 0) {
882 * Returns true if this member is public, false otherwise.
884 public boolean isPublic() {
885 return (access & ACC_PUBLIC) != 0;
889 * Returns the access of this class or interface.
891 public String[] getAccess() {
892 Vector v = new Vector();
893 if ((access & ACC_PUBLIC) != 0) {
894 v.addElement("public");
896 if ((access & ACC_FINAL) != 0) {
897 v.addElement("final");
899 if ((access & ACC_ABSTRACT) != 0) {
900 v.addElement("abstract");
902 String[] accflags = new String[v.size()];
903 v.copyInto(accflags);
908 * Returns list of innerclasses.
910 public InnerClassData[] getInnerClasses() {
915 * Returns list of attributes.
917 final AttrData[] getAttributes() {
921 public byte[] findAnnotationData(boolean classRetention) {
922 String n = classRetention
923 ? "RuntimeInvisibleAnnotations" : // NOI18N
924 "RuntimeVisibleAnnotations"; // NOI18N
925 return findAttr(n, attrs);
929 * Returns true if superbit is set.
931 public boolean isSuperSet() {
932 if ((access & ACC_SUPER) != 0) {
939 * Returns super class name.
941 public String getSuperClassName() {
943 if (super_class == 0) {
948 if (tags[super_class] != CONSTANT_CLASS) {
949 return res; //"<CP["+cpx+"] is not a Class> ";
951 scpx = ((CPX) cpool[super_class]).cpx;
952 } catch (ArrayIndexOutOfBoundsException e) {
953 return res; // "#"+cpx+"// invalid constant pool index";
954 } catch (Throwable e) {
955 return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
959 return (String) (cpool[scpx]);
960 } catch (ArrayIndexOutOfBoundsException e) {
961 return res; // "class #"+scpx+"// invalid constant pool index";
962 } catch (ClassCastException e) {
963 return res; // "class #"+scpx+"// invalid constant pool reference";
964 } catch (Throwable e) {
965 return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
970 * Returns list of super interfaces.
972 public String[] getSuperInterfaces() {
973 String interfacenames[] = new String[interfaces.length];
974 int interfacecpx = -1;
975 for (int i = 0; i < interfaces.length; i++) {
976 interfacecpx = ((CPX) cpool[interfaces[i]]).cpx;
977 interfacenames[i] = (String) (cpool[interfacecpx]);
979 return interfacenames;
983 * Returns string at prticular constant pool index.
985 public String getStringValue(int cpoolx) {
987 return ((String) cpool[cpoolx]);
988 } catch (ArrayIndexOutOfBoundsException e) {
989 return "//invalid constant pool index:" + cpoolx;
990 } catch (ClassCastException e) {
991 return "//invalid constant pool ref:" + cpoolx;
996 * Returns list of field info.
998 public FieldData[] getFields() {
1003 * Returns list of method info.
1005 public MethodData[] getMethods() {
1010 * Returns constant pool entry at that index.
1012 public CPX2 getCpoolEntry(int cpx) {
1013 return ((CPX2) (cpool[cpx]));
1016 public Object getCpoolEntryobj(int cpx) {
1017 return (cpool[cpx]);
1021 * Returns index of this class.
1023 public int getthis_cpx() {
1028 * Returns string at that index.
1030 public String StringValue(int cpx) {
1031 return stringValue(cpx, false);
1034 public String stringValue(int cpx, boolean textual) {
1035 return stringValue(cpx, textual, null);
1038 public String stringValue(int cpx, String[] classRefs) {
1039 return stringValue(cpx, true, classRefs);
1042 private String stringValue(int cpx, boolean textual, String[] refs) {
1052 } catch (IndexOutOfBoundsException e) {
1053 return "<Incorrect CP index:" + cpx + ">";
1060 case CONSTANT_UTF8: {
1064 StringBuilder sb = new StringBuilder();
1065 String s = (String) x;
1066 for (int k = 0; k < s.length(); k++) {
1067 char c = s.charAt(k);
1070 sb.append('\\').append('\\');
1073 sb.append('\\').append('t');
1076 sb.append('\\').append('n');
1079 sb.append('\\').append('r');
1082 sb.append('\\').append('\"');
1085 sb.append("\\u2028");
1088 sb.append("\\u2029");
1094 return sb.toString();
1096 case CONSTANT_DOUBLE: {
1097 Double d = (Double) x;
1098 String sd = d.toString();
1104 case CONSTANT_FLOAT: {
1105 Float f = (Float) x;
1106 String sf = (f).toString();
1112 case CONSTANT_LONG: {
1115 return ln.toString();
1117 return ln.toString() + 'l';
1119 case CONSTANT_INTEGER: {
1120 Integer in = (Integer) x;
1121 return in.toString();
1123 case CONSTANT_CLASS:
1124 String jn = getClassName(cpx);
1131 return javaName(jn);
1132 case CONSTANT_STRING:
1133 String sv = stringValue(((CPX) x).cpx, textual);
1135 return '"' + sv + '"';
1139 case CONSTANT_FIELD:
1140 case CONSTANT_METHOD:
1141 case CONSTANT_INTERFACEMETHOD:
1142 //return getShortClassName(((CPX2)x).cpx1)+"."+StringValue(((CPX2)x).cpx2);
1143 return javaName(getClassName(((CPX2) x).cpx1)) + "." + StringValue(((CPX2) x).cpx2);
1145 case CONSTANT_NAMEANDTYPE:
1146 return getName(((CPX2) x).cpx1) + ":" + StringValue(((CPX2) x).cpx2);
1148 return "UnknownTag"; //TBD
1153 * Returns resolved java type name.
1155 public String javaName(String name) {
1159 int len = name.length();
1167 for (int k = 0; k < len; k += Character.charCount(cp)) {
1168 cp = name.codePointAt(k);
1170 if (!isJavaIdentifierStart(cp)) {
1173 } else if (cp != '/') {
1174 if (!isJavaIdentifierPart(cp)) {
1182 return "\"" + name + "\"";
1185 public String getName(int cpx) {
1188 return javaName((String) cpool[cpx]); //.replace('/','.');
1189 } catch (ArrayIndexOutOfBoundsException e) {
1190 return "<invalid constant pool index:" + cpx + ">";
1191 } catch (ClassCastException e) {
1192 return "<invalid constant pool ref:" + cpx + ">";
1197 * Returns unqualified class name.
1199 public String getShortClassName(int cpx) {
1200 String classname = javaName(getClassName(cpx));
1201 pkgPrefixLen = classname.lastIndexOf("/") + 1;
1202 if (pkgPrefixLen != 0) {
1203 pkgPrefix = classname.substring(0, pkgPrefixLen);
1204 if (classname.startsWith(pkgPrefix)) {
1205 return classname.substring(pkgPrefixLen);
1212 * Returns source file name.
1214 public String getSourceName() {
1215 return getName(source_cpx);
1219 * Returns package name.
1221 public String getPkgName() {
1222 String classname = getClassName(this_class);
1223 pkgPrefixLen = classname.lastIndexOf("/") + 1;
1224 if (pkgPrefixLen != 0) {
1225 pkgPrefix = classname.substring(0, pkgPrefixLen);
1226 return /* ("package " + */ pkgPrefix.substring(0, pkgPrefixLen - 1) /* + ";\n") */;
1233 * Returns total constant pool entry count.
1235 public int getCpoolCount() {
1240 * Returns minor version of class file.
1242 public int getMinor_version() {
1243 return minor_version;
1247 * Returns major version of class file.
1249 public int getMajor_version() {
1250 return major_version;
1253 private boolean isJavaIdentifierStart(int cp) {
1254 return ('a' <= cp && cp <= 'z') || ('A' <= cp && cp <= 'Z');
1257 private boolean isJavaIdentifierPart(int cp) {
1258 return isJavaIdentifierStart(cp) || ('0' <= cp && cp <= '9');
1261 public String[] getNameAndType(int indx) {
1262 return getNameAndType(indx, 0, new String[2]);
1265 private String[] getNameAndType(int indx, int at, String[] arr) {
1266 CPX2 c2 = getCpoolEntry(indx);
1267 arr[at] = StringValue(c2.cpx1);
1268 arr[at + 1] = StringValue(c2.cpx2);
1272 public String[] getFieldInfoName(int indx) {
1273 CPX2 c2 = getCpoolEntry(indx);
1274 String[] arr = new String[3];
1275 arr[0] = getClassName(c2.cpx1);
1276 return getNameAndType(c2.cpx2, 1, arr);
1279 public MethodData findMethod(String name, String signature) {
1280 for (MethodData md: methods) {
1281 if (md.getName().equals(name)
1282 && md.getInternalSig().equals(signature)) {
1291 public FieldData findField(String name, String signature) {
1292 for (FieldData fd: fields) {
1293 if (fd.getName().equals(name)
1294 && fd.getInternalSig().equals(signature)) {
1303 static byte[] findAttr(String n, AttrData[] attrs) {
1304 for (AttrData ad : attrs) {
1305 if (n.equals(ad.getAttrName())) {
1306 return ad.getData();
1314 * Strores field data informastion.
1316 * @author Sucheta Dambalkar (Adopted code from jdis)
1318 static class FieldData {
1323 int descriptor_index;
1324 int attributes_count;
1326 boolean isSynthetic = false;
1327 boolean isDeprecated = false;
1330 public FieldData(ClassData cls) {
1335 * Read and store field info.
1337 public void read(DataInputStream in) throws IOException {
1338 access = in.readUnsignedShort();
1339 name_index = in.readUnsignedShort();
1340 descriptor_index = in.readUnsignedShort();
1341 // Read the attributes
1342 int attributes_count = in.readUnsignedShort();
1343 attrs = new Vector(attributes_count);
1344 for (int i = 0; i < attributes_count; i++) {
1345 int attr_name_index = in.readUnsignedShort();
1346 if (cls.getTag(attr_name_index) != CONSTANT_UTF8) {
1349 String attr_name = cls.getString(attr_name_index);
1350 if (attr_name.equals("ConstantValue")) {
1351 if (in.readInt() != 2) {
1352 throw new ClassFormatError("invalid ConstantValue attr length");
1354 value_cpx = in.readUnsignedShort();
1355 AttrData attr = new AttrData(cls);
1356 attr.read(attr_name_index);
1357 attrs.addElement(attr);
1358 } else if (attr_name.equals("Synthetic")) {
1359 if (in.readInt() != 0) {
1360 throw new ClassFormatError("invalid Synthetic attr length");
1363 AttrData attr = new AttrData(cls);
1364 attr.read(attr_name_index);
1365 attrs.addElement(attr);
1366 } else if (attr_name.equals("Deprecated")) {
1367 if (in.readInt() != 0) {
1368 throw new ClassFormatError("invalid Synthetic attr length");
1370 isDeprecated = true;
1371 AttrData attr = new AttrData(cls);
1372 attr.read(attr_name_index);
1373 attrs.addElement(attr);
1375 AttrData attr = new AttrData(cls);
1376 attr.read(attr_name_index, in);
1377 attrs.addElement(attr);
1383 public boolean isStatic() {
1384 return (access & ACC_STATIC) != 0;
1388 * Returns access of a field.
1390 public String[] getAccess() {
1391 Vector v = new Vector();
1392 if ((access & ACC_PUBLIC) != 0) {
1393 v.addElement("public");
1395 if ((access & ACC_PRIVATE) != 0) {
1396 v.addElement("private");
1398 if ((access & ACC_PROTECTED) != 0) {
1399 v.addElement("protected");
1401 if ((access & ACC_STATIC) != 0) {
1402 v.addElement("static");
1404 if ((access & ACC_FINAL) != 0) {
1405 v.addElement("final");
1407 if ((access & ACC_VOLATILE) != 0) {
1408 v.addElement("volatile");
1410 if ((access & ACC_TRANSIENT) != 0) {
1411 v.addElement("transient");
1413 String[] accflags = new String[v.size()];
1414 v.copyInto(accflags);
1419 * Returns name of a field.
1421 public String getName() {
1422 return cls.getStringValue(name_index);
1426 * Returns internal signature of a field
1428 public String getInternalSig() {
1429 return cls.getStringValue(descriptor_index);
1433 * Returns true if field is synthetic.
1435 public boolean isSynthetic() {
1440 * Returns true if field is deprecated.
1442 public boolean isDeprecated() {
1443 return isDeprecated;
1446 public boolean hasConstantValue() {
1447 return value_cpx != -1;
1451 * Returns list of attributes of field.
1453 public Vector getAttributes() {
1457 public byte[] findAnnotationData(boolean classRetention) {
1458 String n = classRetention
1459 ? "RuntimeInvisibleAnnotations" : // NOI18N
1460 "RuntimeVisibleAnnotations"; // NOI18N
1461 AttrData[] arr = new AttrData[attrs.size()];
1462 attrs.copyInto(arr);
1463 return ClassData.findAttr(n, arr);
1468 * A JavaScript optimized replacement for Hashtable.
1470 * @author Jaroslav Tulach <jtulach@netbeans.org>
1472 private static final class Hashtable {
1474 private Object[] keys;
1475 private Object[] values;
1481 Hashtable(int i, double d) {
1488 synchronized void put(Object key, Object val) {
1489 int[] where = {-1, -1};
1490 Object found = get(key, where);
1491 if (where[0] != -1) {
1493 values[where[0]] = val;
1495 if (where[1] != -1) {
1497 keys[where[1]] = key;
1498 values[where[1]] = val;
1501 keys = new Object[11];
1502 values = new Object[11];
1506 Object[] newKeys = new Object[keys.length * 2];
1507 Object[] newValues = new Object[values.length * 2];
1508 for (int i = 0; i < keys.length; i++) {
1509 newKeys[i] = keys[i];
1510 newValues[i] = values[i];
1512 newKeys[keys.length] = key;
1513 newValues[keys.length] = val;
1521 Object get(Object key) {
1522 return get(key, null);
1525 private synchronized Object get(Object key, int[] foundAndNull) {
1529 for (int i = 0; i < keys.length; i++) {
1530 if (keys[i] == null) {
1531 if (foundAndNull != null) {
1532 foundAndNull[1] = i;
1534 } else if (keys[i].equals(key)) {
1535 if (foundAndNull != null) {
1536 foundAndNull[0] = i;
1546 * Strores InnerClass data informastion.
1548 * @author Sucheta Dambalkar (Adopted code from jdis)
1550 private static class InnerClassData {
1553 int inner_class_info_index, outer_class_info_index, inner_name_index, access;
1555 public InnerClassData(ClassData cls) {
1561 * Read Innerclass attribute data.
1563 public void read(DataInputStream in) throws IOException {
1564 inner_class_info_index = in.readUnsignedShort();
1565 outer_class_info_index = in.readUnsignedShort();
1566 inner_name_index = in.readUnsignedShort();
1567 access = in.readUnsignedShort();
1571 * Returns the access of this class or interface.
1573 public String[] getAccess() {
1574 Vector v = new Vector();
1575 if ((access & ACC_PUBLIC) != 0) {
1576 v.addElement("public");
1578 if ((access & ACC_FINAL) != 0) {
1579 v.addElement("final");
1581 if ((access & ACC_ABSTRACT) != 0) {
1582 v.addElement("abstract");
1584 String[] accflags = new String[v.size()];
1585 v.copyInto(accflags);
1588 } // end InnerClassData
1591 * Strores LineNumberTable data information.
1593 * @author Sucheta Dambalkar (Adopted code from jdis)
1595 private static class LineNumData {
1597 short start_pc, line_number;
1599 public LineNumData() {
1603 * Read LineNumberTable attribute.
1605 public LineNumData(DataInputStream in) throws IOException {
1606 start_pc = in.readShort();
1607 line_number = in.readShort();
1613 * Strores LocalVariableTable data information.
1615 * @author Sucheta Dambalkar (Adopted code from jdis)
1617 private static class LocVarData {
1619 short start_pc, length, name_cpx, sig_cpx, slot;
1621 public LocVarData() {
1625 * Read LocalVariableTable attribute.
1627 public LocVarData(DataInputStream in) throws IOException {
1628 start_pc = in.readShort();
1629 length = in.readShort();
1630 name_cpx = in.readShort();
1631 sig_cpx = in.readShort();
1632 slot = in.readShort();
1637 * Strores method data informastion.
1639 * @author Sucheta Dambalkar (Adopted code from jdis)
1641 static class MethodData {
1646 int descriptor_index;
1647 int attributes_count;
1649 Vector exception_table = new Vector(0);
1650 Vector lin_num_tb = new Vector(0);
1651 Vector loc_var_tb = new Vector(0);
1652 StackMapTableData[] stackMapTable;
1653 StackMapData[] stackMap;
1654 int[] exc_index_table = null;
1655 Vector attrs = new Vector(0);
1656 Vector code_attrs = new Vector(0);
1657 int max_stack, max_locals;
1658 boolean isSynthetic = false;
1659 boolean isDeprecated = false;
1661 public MethodData(ClassData cls) {
1668 public void read(DataInputStream in) throws IOException {
1669 access = in.readUnsignedShort();
1670 name_index = in.readUnsignedShort();
1671 descriptor_index = in.readUnsignedShort();
1672 int attributes_count = in.readUnsignedShort();
1673 for (int i = 0; i < attributes_count; i++) {
1674 int attr_name_index = in.readUnsignedShort();
1678 if (cls.getTag(attr_name_index) == CONSTANT_UTF8) {
1679 String attr_name = cls.getString(attr_name_index);
1680 if (attr_name.equals("Code")) {
1682 AttrData attr = new AttrData(cls);
1683 attr.read(attr_name_index);
1684 attrs.addElement(attr);
1686 } else if (attr_name.equals("Exceptions")) {
1688 AttrData attr = new AttrData(cls);
1689 attr.read(attr_name_index);
1690 attrs.addElement(attr);
1692 } else if (attr_name.equals("Synthetic")) {
1693 if (in.readInt() != 0) {
1694 throw new ClassFormatError("invalid Synthetic attr length");
1697 AttrData attr = new AttrData(cls);
1698 attr.read(attr_name_index);
1699 attrs.addElement(attr);
1701 } else if (attr_name.equals("Deprecated")) {
1702 if (in.readInt() != 0) {
1703 throw new ClassFormatError("invalid Synthetic attr length");
1705 isDeprecated = true;
1706 AttrData attr = new AttrData(cls);
1707 attr.read(attr_name_index);
1708 attrs.addElement(attr);
1712 AttrData attr = new AttrData(cls);
1713 attr.read(attr_name_index, in);
1714 attrs.addElement(attr);
1720 * Read code attribute info.
1722 public void readCode(DataInputStream in) throws IOException {
1724 int attr_length = in.readInt();
1725 max_stack = in.readUnsignedShort();
1726 max_locals = in.readUnsignedShort();
1727 int codelen = in.readInt();
1729 code = new byte[codelen];
1731 while (totalread < codelen) {
1732 totalread += in.read(code, totalread, codelen - totalread);
1734 // in.read(code, 0, codelen);
1736 readExceptionTable(in);
1737 int code_attributes_count = in.readUnsignedShort();
1739 for (int k = 0; k < code_attributes_count; k++) {
1740 int table_name_index = in.readUnsignedShort();
1741 int table_name_tag = cls.getTag(table_name_index);
1742 AttrData attr = new AttrData(cls);
1743 if (table_name_tag == CONSTANT_UTF8) {
1744 String table_name_tstr = cls.getString(table_name_index);
1745 if (table_name_tstr.equals("LineNumberTable")) {
1746 readLineNumTable(in);
1747 attr.read(table_name_index);
1748 } else if (table_name_tstr.equals("LocalVariableTable")) {
1749 readLocVarTable(in);
1750 attr.read(table_name_index);
1751 } else if (table_name_tstr.equals("StackMapTable")) {
1752 readStackMapTable(in);
1753 attr.read(table_name_index);
1754 } else if (table_name_tstr.equals("StackMap")) {
1756 attr.read(table_name_index);
1758 attr.read(table_name_index, in);
1760 code_attrs.addElement(attr);
1764 attr.read(table_name_index, in);
1765 code_attrs.addElement(attr);
1770 * Read exception table info.
1772 void readExceptionTable(DataInputStream in) throws IOException {
1773 int exception_table_len = in.readUnsignedShort();
1774 exception_table = new Vector(exception_table_len);
1775 for (int l = 0; l < exception_table_len; l++) {
1776 exception_table.addElement(new TrapData(in, l));
1781 * Read LineNumberTable attribute info.
1783 void readLineNumTable(DataInputStream in) throws IOException {
1784 int attr_len = in.readInt(); // attr_length
1785 int lin_num_tb_len = in.readUnsignedShort();
1786 lin_num_tb = new Vector(lin_num_tb_len);
1787 for (int l = 0; l < lin_num_tb_len; l++) {
1788 lin_num_tb.addElement(new LineNumData(in));
1793 * Read LocalVariableTable attribute info.
1795 void readLocVarTable(DataInputStream in) throws IOException {
1796 int attr_len = in.readInt(); // attr_length
1797 int loc_var_tb_len = in.readUnsignedShort();
1798 loc_var_tb = new Vector(loc_var_tb_len);
1799 for (int l = 0; l < loc_var_tb_len; l++) {
1800 loc_var_tb.addElement(new LocVarData(in));
1805 * Read Exception attribute info.
1807 public void readExceptions(DataInputStream in) throws IOException {
1808 int attr_len = in.readInt(); // attr_length in prog
1809 int num_exceptions = in.readUnsignedShort();
1810 exc_index_table = new int[num_exceptions];
1811 for (int l = 0; l < num_exceptions; l++) {
1812 int exc = in.readShort();
1813 exc_index_table[l] = exc;
1818 * Read StackMapTable attribute info.
1820 void readStackMapTable(DataInputStream in) throws IOException {
1821 int attr_len = in.readInt(); //attr_length
1822 int stack_map_tb_len = in.readUnsignedShort();
1823 stackMapTable = new StackMapTableData[stack_map_tb_len];
1824 for (int i = 0; i < stack_map_tb_len; i++) {
1825 stackMapTable[i] = StackMapTableData.getInstance(in, this);
1830 * Read StackMap attribute info.
1832 void readStackMap(DataInputStream in) throws IOException {
1833 int attr_len = in.readInt(); //attr_length
1834 int stack_map_len = in.readUnsignedShort();
1835 stackMap = new StackMapData[stack_map_len];
1836 for (int i = 0; i < stack_map_len; i++) {
1837 stackMap[i] = new StackMapData(in, this);
1842 * Return access of the method.
1844 public int getAccess() {
1849 * Return name of the method.
1851 public String getName() {
1852 return cls.getStringValue(name_index);
1856 * Return internal siganature of the method.
1858 public String getInternalSig() {
1859 return cls.getStringValue(descriptor_index);
1863 * Return code attribute data of a method.
1865 public byte[] getCode() {
1870 * Return LineNumberTable size.
1872 public int getnumlines() {
1873 return lin_num_tb.size();
1877 * Return LineNumberTable
1879 public Vector getlin_num_tb() {
1884 * Return LocalVariableTable size.
1886 public int getloc_var_tbsize() {
1887 return loc_var_tb.size();
1891 * Return LocalVariableTable.
1893 public Vector getloc_var_tb() {
1900 public StackMapData[] getStackMap() {
1905 * Return StackMapTable.
1907 public StackMapTableData[] getStackMapTable() {
1908 return stackMapTable;
1911 public StackMapIterator createStackMapIterator() {
1912 return new StackMapIterator(this);
1916 * Return true if method is static
1918 public boolean isStatic() {
1919 if ((access & ACC_STATIC) != 0) {
1926 * Return max depth of operand stack.
1928 public int getMaxStack() {
1933 * Return number of local variables.
1935 public int getMaxLocals() {
1940 * Return exception index table in Exception attribute.
1942 public int[] get_exc_index_table() {
1943 return exc_index_table;
1947 * Return exception table in code attributre.
1949 public TrapDataIterator getTrapDataIterator() {
1950 return new TrapDataIterator(exception_table);
1954 * Return method attributes.
1956 public Vector getAttributes() {
1961 * Return code attributes.
1963 public Vector getCodeAttributes() {
1968 * Return true if method id synthetic.
1970 public boolean isSynthetic() {
1975 * Return true if method is deprecated.
1977 public boolean isDeprecated() {
1978 return isDeprecated;
1981 public byte[] findAnnotationData(boolean classRetention) {
1982 String n = classRetention
1983 ? "RuntimeInvisibleAnnotations" : // NOI18N
1984 "RuntimeVisibleAnnotations"; // NOI18N
1985 AttrData[] arr = new AttrData[attrs.size()];
1986 attrs.copyInto(arr);
1987 return ClassData.findAttr(n, arr);
1990 public boolean isConstructor() {
1991 return "<init>".equals(getName());
1995 /* represents one entry of StackMap attribute
1997 private static class StackMapData {
2003 StackMapData(int offset, int[] locals, int[] stack) {
2004 this.offset = offset;
2005 this.locals = locals;
2009 StackMapData(DataInputStream in, MethodData method) throws IOException {
2010 offset = in.readUnsignedShort();
2011 int local_size = in.readUnsignedShort();
2012 locals = readTypeArray(in, local_size, method);
2013 int stack_size = in.readUnsignedShort();
2014 stack = readTypeArray(in, stack_size, method);
2017 static final int[] readTypeArray(DataInputStream in, int length, MethodData method) throws IOException {
2018 int[] types = new int[length];
2019 for (int i = 0; i < length; i++) {
2020 types[i] = readType(in, method);
2025 static final int readType(DataInputStream in, MethodData method) throws IOException {
2026 int type = in.readUnsignedByte();
2027 if (type == ITEM_Object || type == ITEM_NewObject) {
2028 type = type | (in.readUnsignedShort() << 8);
2034 static final class StackMapIterator {
2036 private final StackMapTableData[] stackMapTable;
2037 private final TypeArray argTypes;
2038 private final TypeArray localTypes;
2039 private final TypeArray stackTypes;
2040 private int nextFrameIndex;
2041 private int lastFrameByteCodeOffset;
2042 private int byteCodeOffset;
2044 StackMapIterator(final MethodData methodData) {
2045 this(methodData.getStackMapTable(),
2046 methodData.getInternalSig(),
2047 methodData.isStatic());
2050 StackMapIterator(final StackMapTableData[] stackMapTable,
2051 final String methodSignature,
2052 final boolean isStaticMethod) {
2053 this.stackMapTable = (stackMapTable != null)
2055 : new StackMapTableData[0];
2057 argTypes = getArgTypes(methodSignature, isStaticMethod);
2058 localTypes = new TypeArray();
2059 stackTypes = new TypeArray();
2061 localTypes.addAll(argTypes);
2063 lastFrameByteCodeOffset = -1;
2067 public boolean isEmpty() {
2068 return stackMapTable.length == 0;
2071 public String getFrameAsString() {
2072 return (nextFrameIndex == 0)
2073 ? StackMapTableData.toString("INITIAL", 0, null, null)
2074 : stackMapTable[nextFrameIndex - 1].toString();
2077 public int getFrameIndex() {
2078 return nextFrameIndex;
2081 public TypeArray getFrameStack() {
2085 public TypeArray getFrameLocals() {
2089 public TypeArray getArguments() {
2093 public void advanceBy(final int numByteCodes) {
2094 if (numByteCodes < 0) {
2095 throw new IllegalStateException("Forward only iterator");
2098 byteCodeOffset += numByteCodes;
2099 while ((nextFrameIndex < stackMapTable.length)
2100 && ((byteCodeOffset - lastFrameByteCodeOffset)
2101 >= (stackMapTable[nextFrameIndex].offsetDelta
2103 final StackMapTableData nextFrame = stackMapTable[nextFrameIndex];
2105 lastFrameByteCodeOffset += nextFrame.offsetDelta + 1;
2106 nextFrame.applyTo(localTypes, stackTypes);
2112 public void advanceTo(final int nextByteCodeOffset) {
2113 advanceBy(nextByteCodeOffset - byteCodeOffset);
2116 private static TypeArray getArgTypes(final String methodSignature,
2117 final boolean isStaticMethod) {
2118 final TypeArray argTypes = new TypeArray();
2120 if (!isStaticMethod) {
2121 argTypes.add(ITEM_Object);
2124 if (methodSignature.charAt(0) != '(') {
2125 throw new IllegalArgumentException("Invalid method signature");
2128 final int length = methodSignature.length();
2129 boolean skipType = false;
2131 for (int i = 1; i < length; ++i) {
2132 switch (methodSignature.charAt(i)) {
2138 argType = ITEM_Integer;
2141 argType = ITEM_Long;
2144 argType = ITEM_Float;
2147 argType = ITEM_Double;
2150 i = methodSignature.indexOf(';', i + 1);
2152 throw new IllegalArgumentException(
2153 "Invalid method signature");
2155 argType = ITEM_Object;
2159 // not interested in the return value type
2163 argTypes.add(ITEM_Object);
2169 throw new IllegalArgumentException(
2170 "Invalid method signature");
2174 argTypes.add(argType);
2183 /* represents one entry of StackMapTable attribute
2186 private static abstract class StackMapTableData {
2188 final int frameType;
2191 StackMapTableData(int frameType) {
2192 this.frameType = frameType;
2195 abstract void applyTo(TypeArray localTypes, TypeArray stackTypes);
2197 protected static String toString(
2198 final String frameType,
2200 final int[] localTypes,
2201 final int[] stackTypes) {
2202 final StringBuilder sb = new StringBuilder(frameType);
2204 sb.append("(off: +").append(offset);
2205 if (localTypes != null) {
2206 sb.append(", locals: ");
2207 appendTypes(sb, localTypes);
2209 if (stackTypes != null) {
2210 sb.append(", stack: ");
2211 appendTypes(sb, stackTypes);
2215 return sb.toString();
2218 private static void appendTypes(final StringBuilder sb, final int[] types) {
2220 if (types.length > 0) {
2221 sb.append(TypeArray.typeString(types[0]));
2222 for (int i = 1; i < types.length; ++i) {
2224 sb.append(TypeArray.typeString(types[i]));
2230 private static class SameFrame extends StackMapTableData {
2232 SameFrame(int frameType, int offsetDelta) {
2234 this.offsetDelta = offsetDelta;
2238 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
2243 public String toString() {
2244 return toString("SAME" + ((frameType == SAME_FRAME_EXTENDED)
2245 ? "_FRAME_EXTENDED" : ""),
2251 private static class SameLocals1StackItem extends StackMapTableData {
2255 SameLocals1StackItem(int frameType, int offsetDelta, int[] stack) {
2257 this.offsetDelta = offsetDelta;
2262 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
2263 stackTypes.setAll(stack);
2267 public String toString() {
2269 "SAME_LOCALS_1_STACK_ITEM"
2270 + ((frameType == SAME_LOCALS_1_STACK_ITEM_EXTENDED)
2271 ? "_EXTENDED" : ""),
2277 private static class ChopFrame extends StackMapTableData {
2279 ChopFrame(int frameType, int offsetDelta) {
2281 this.offsetDelta = offsetDelta;
2285 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
2286 localTypes.setSize(localTypes.getSize()
2287 - (SAME_FRAME_EXTENDED - frameType));
2292 public String toString() {
2293 return toString("CHOP", offsetDelta, null, null);
2297 private static class AppendFrame extends StackMapTableData {
2301 AppendFrame(int frameType, int offsetDelta, int[] locals) {
2303 this.offsetDelta = offsetDelta;
2304 this.locals = locals;
2308 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
2309 localTypes.addAll(locals);
2314 public String toString() {
2315 return toString("APPEND", offsetDelta, locals, null);
2319 private static class FullFrame extends StackMapTableData {
2324 FullFrame(int offsetDelta, int[] locals, int[] stack) {
2326 this.offsetDelta = offsetDelta;
2327 this.locals = locals;
2332 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
2333 localTypes.setAll(locals);
2334 stackTypes.setAll(stack);
2338 public String toString() {
2339 return toString("FULL", offsetDelta, locals, stack);
2343 static StackMapTableData getInstance(DataInputStream in, MethodData method)
2344 throws IOException {
2345 int frameType = in.readUnsignedByte();
2347 if (frameType < SAME_FRAME_BOUND) {
2349 return new SameFrame(frameType, frameType);
2350 } else if (SAME_FRAME_BOUND <= frameType && frameType < SAME_LOCALS_1_STACK_ITEM_BOUND) {
2351 // same_locals_1_stack_item_frame
2352 // read additional single stack element
2353 return new SameLocals1StackItem(frameType,
2354 (frameType - SAME_FRAME_BOUND),
2355 StackMapData.readTypeArray(in, 1, method));
2356 } else if (frameType == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
2357 // same_locals_1_stack_item_extended
2358 return new SameLocals1StackItem(frameType,
2359 in.readUnsignedShort(),
2360 StackMapData.readTypeArray(in, 1, method));
2361 } else if (SAME_LOCALS_1_STACK_ITEM_EXTENDED < frameType && frameType < SAME_FRAME_EXTENDED) {
2362 // chop_frame or same_frame_extended
2363 return new ChopFrame(frameType, in.readUnsignedShort());
2364 } else if (frameType == SAME_FRAME_EXTENDED) {
2365 // chop_frame or same_frame_extended
2366 return new SameFrame(frameType, in.readUnsignedShort());
2367 } else if (SAME_FRAME_EXTENDED < frameType && frameType < FULL_FRAME) {
2369 return new AppendFrame(frameType, in.readUnsignedShort(),
2370 StackMapData.readTypeArray(in, frameType - SAME_FRAME_EXTENDED, method));
2371 } else if (frameType == FULL_FRAME) {
2373 int offsetDelta = in.readUnsignedShort();
2374 int locals_size = in.readUnsignedShort();
2375 int[] locals = StackMapData.readTypeArray(in, locals_size, method);
2376 int stack_size = in.readUnsignedShort();
2377 int[] stack = StackMapData.readTypeArray(in, stack_size, method);
2378 return new FullFrame(offsetDelta, locals, stack);
2380 throw new ClassFormatError("unrecognized frame_type in StackMapTable");
2386 * Stores exception table data in code attribute.
2388 * @author Sucheta Dambalkar (Adopted code from jdis)
2390 static final class TrapData {
2392 public final short start_pc;
2393 public final short end_pc;
2394 public final short handler_pc;
2395 public final short catch_cpx;
2399 * Read and store exception table data in code attribute.
2401 TrapData(DataInputStream in, int num) throws IOException {
2403 start_pc = in.readShort();
2404 end_pc = in.readShort();
2405 handler_pc = in.readShort();
2406 catch_cpx = in.readShort();
2410 * returns recommended identifier
2412 public String ident() {
2418 * @author Jaroslav Tulach <jtulach@netbeans.org>
2420 static final class TrapDataIterator {
2422 private final Hashtable exStart = new Hashtable();
2423 private final Hashtable exStop = new Hashtable();
2424 private TrapData[] current = new TrapData[10];
2425 private int currentCount;
2427 TrapDataIterator(Vector exceptionTable) {
2428 for (int i = 0; i < exceptionTable.size(); i++) {
2429 final TrapData td = (TrapData) exceptionTable.elementAt(i);
2430 put(exStart, td.start_pc, td);
2431 put(exStop, td.end_pc, td);
2435 private static void put(Hashtable h, short key, TrapData td) {
2436 Short s = Short.valueOf((short) key);
2437 Vector v = (Vector) h.get(s);
2445 private boolean processAll(Hashtable h, Short key, boolean add) {
2446 boolean change = false;
2447 Vector v = (Vector) h.get(key);
2450 for (int i = 0; i < s; i++) {
2451 TrapData td = (TrapData) v.elementAt(i);
2464 public boolean advanceTo(int i) {
2465 Short s = Short.valueOf((short) i);
2466 boolean ch1 = processAll(exStart, s, true);
2467 boolean ch2 = processAll(exStop, s, false);
2471 public boolean useTry() {
2472 return currentCount > 0;
2475 public TrapData[] current() {
2476 TrapData[] copy = new TrapData[currentCount];
2477 for (int i = 0; i < currentCount; i++) {
2478 copy[i] = current[i];
2483 private void add(TrapData e) {
2484 if (currentCount == current.length) {
2485 TrapData[] data = new TrapData[currentCount * 2];
2486 for (int i = 0; i < currentCount; i++) {
2487 data[i] = current[i];
2491 current[currentCount++] = e;
2494 private void remove(TrapData e) {
2495 if (currentCount == 0) {
2499 while (from < currentCount) {
2500 if (e == current[from++]) {
2504 while (from < currentCount) {
2505 current[from - 1] = current[from];
2506 current[from] = null;
2512 static final class TypeArray {
2514 private static final int CAPACITY_INCREMENT = 16;
2515 private int[] types;
2518 public TypeArray() {
2521 public TypeArray(final TypeArray initialTypes) {
2522 setAll(initialTypes);
2525 public void add(final int newType) {
2526 ensureCapacity(size + 1);
2527 types[size++] = newType;
2530 public void addAll(final TypeArray newTypes) {
2531 addAll(newTypes.types, 0, newTypes.size);
2534 public void addAll(final int[] newTypes) {
2535 addAll(newTypes, 0, newTypes.length);
2538 public void addAll(final int[] newTypes,
2542 ensureCapacity(size + count);
2543 arraycopy(newTypes, offset, types, size, count);
2548 public void set(final int index, final int newType) {
2549 types[index] = newType;
2552 public void setAll(final TypeArray newTypes) {
2553 setAll(newTypes.types, 0, newTypes.size);
2556 public void setAll(final int[] newTypes) {
2557 setAll(newTypes, 0, newTypes.length);
2560 public void setAll(final int[] newTypes,
2564 ensureCapacity(count);
2565 arraycopy(newTypes, offset, types, 0, count);
2572 public void setSize(final int newSize) {
2573 if (size != newSize) {
2574 ensureCapacity(newSize);
2576 for (int i = size; i < newSize; ++i) {
2583 public void clear() {
2587 public int getSize() {
2591 public int get(final int index) {
2592 return types[index];
2595 public static String typeString(final int type) {
2596 switch (type & 0xff) {
2609 case ITEM_InitObject: // UninitializedThis
2613 case ITEM_NewObject: // Uninitialized
2616 throw new IllegalArgumentException("Unknown type");
2621 public String toString() {
2622 final StringBuilder sb = new StringBuilder("[");
2624 sb.append(typeString(types[0]));
2625 for (int i = 1; i < size; ++i) {
2627 sb.append(typeString(types[i]));
2631 return sb.append(']').toString();
2634 private void ensureCapacity(final int minCapacity) {
2635 if ((minCapacity == 0)
2636 || (types != null) && (minCapacity <= types.length)) {
2640 final int newCapacity =
2641 ((minCapacity + CAPACITY_INCREMENT - 1) / CAPACITY_INCREMENT)
2642 * CAPACITY_INCREMENT;
2643 final int[] newTypes = new int[newCapacity];
2646 arraycopy(types, 0, newTypes, 0, size);
2652 // no System.arraycopy
2653 private void arraycopy(final int[] src, final int srcPos,
2654 final int[] dest, final int destPos,
2656 for (int i = 0; i < length; ++i) {
2657 dest[destPos + i] = src[srcPos + i];
2662 * A JavaScript ready replacement for java.util.Vector
2664 * @author Jaroslav Tulach <jtulach@netbeans.org>
2666 @JavaScriptPrototype(prototype = "new Array")
2667 private static final class Vector {
2669 private Object[] arr;
2677 void add(Object objectType) {
2678 addElement(objectType);
2681 @JavaScriptBody(args = {"obj"}, body =
2683 void addElement(Object obj) {
2684 final int s = size();
2686 setElementAt(obj, s);
2689 @JavaScriptBody(args = {}, body =
2690 "return this.length;")
2692 return arr == null ? 0 : arr.length;
2695 @JavaScriptBody(args = {"newArr"}, body =
2696 "for (var i = 0; i < this.length; i++) {\n"
2697 + " newArr[i] = this[i];\n"
2699 void copyInto(Object[] newArr) {
2703 int min = Math.min(newArr.length, arr.length);
2704 for (int i = 0; i < min; i++) {
2709 @JavaScriptBody(args = {"index"}, body =
2710 "return this[index];")
2711 Object elementAt(int index) {
2715 private void setSize(int len) {
2716 Object[] newArr = new Object[len];
2721 @JavaScriptBody(args = {"val", "index"}, body =
2722 "this[index] = val;")
2723 void setElementAt(Object val, int index) {