Flush mapper before pushing back a result of (virtual) method invocation. The static method call was already doing that.
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)
516 final int cpx1, 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 BootMethodData[] bootMethods;
546 private int attributes_count;
547 private AttrData[] attrs;
548 private int source_cpx = 0;
550 private Hashtable indexHashAscii = new Hashtable();
551 private String pkgPrefix = "";
552 private int pkgPrefixLen = 0;
555 * Read classfile to disassemble.
557 public ClassData(InputStream infile) throws IOException {
558 this.read(new DataInputStream(infile));
562 * Reads and stores class file information.
564 public void read(DataInputStream in) throws IOException {
566 magic = in.readInt();
567 if (magic != JAVA_MAGIC) {
568 throw new ClassFormatError("wrong magic: "
569 + toHex(magic) + ", expected "
570 + toHex(JAVA_MAGIC));
572 minor_version = in.readShort();
573 major_version = in.readShort();
574 if (major_version != JAVA_VERSION) {
577 // Read the constant pool
579 access = in.readUnsignedShort();
580 this_class = in.readUnsignedShort();
581 super_class = in.readUnsignedShort();
584 interfaces_count = in.readUnsignedShort();
585 if (interfaces_count > 0) {
586 interfaces = new int[interfaces_count];
588 for (int i = 0; i < interfaces_count; i++) {
589 interfaces[i] = in.readShort();
598 // Read the attributes
599 attributes_count = in.readUnsignedShort();
600 attrs = new AttrData[attributes_count];
601 for (int k = 0; k < attributes_count; k++) {
602 int name_cpx = in.readUnsignedShort();
603 if (getTag(name_cpx) == CONSTANT_UTF8
604 && getString(name_cpx).equals("SourceFile")) {
605 if (in.readInt() != 2) {
606 throw new ClassFormatError("invalid attr length");
608 source_cpx = in.readUnsignedShort();
609 AttrData attr = new AttrData(this);
613 } else if (getTag(name_cpx) == CONSTANT_UTF8
614 && getString(name_cpx).equals("InnerClasses")) {
615 int length = in.readInt();
616 int num = in.readUnsignedShort();
617 if (2 + num * 8 != length) {
618 throw new ClassFormatError("invalid attr length");
620 innerClasses = new InnerClassData[num];
621 for (int j = 0; j < num; j++) {
622 InnerClassData innerClass = new InnerClassData(this);
624 innerClasses[j] = innerClass;
626 AttrData attr = new AttrData(this);
629 } else if (getTag(name_cpx) == CONSTANT_UTF8
630 && getString(name_cpx).equals("BootstrapMethods")) {
631 AttrData attr = new AttrData(this);
632 bootMethods = readBootstrapMethods(in);
636 AttrData attr = new AttrData(this);
637 attr.read(name_cpx, in);
642 } // end ClassData.read()
644 BootMethodData[] readBootstrapMethods(DataInputStream in) throws IOException {
645 int attr_len = in.readInt(); //attr_lengt
646 int number = in.readShort();
647 BootMethodData[] arr = new BootMethodData[number];
648 for (int i = 0; i < number; i++) {
649 int ref = in.readShort();
650 int len = in.readShort();
651 int[] args = new int[len];
652 for (int j = 0; j < len; j++) {
653 args[j] = in.readShort();
655 arr[i] = new BootMethodData(this, ref, args);
661 * Reads and stores constant pool info.
663 void readCP(DataInputStream in) throws IOException {
664 cpool_count = in.readUnsignedShort();
665 tags = new byte[cpool_count];
666 cpool = new Object[cpool_count];
667 for (int i = 1; i < cpool_count; i++) {
668 byte tag = in.readByte();
670 switch (tags[i] = tag) {
672 String str = in.readUTF();
673 indexHashAscii.put(cpool[i] = str, new Integer(i));
675 case CONSTANT_INTEGER:
676 cpool[i] = new Integer(in.readInt());
679 cpool[i] = new Float(in.readFloat());
682 cpool[i++] = new Long(in.readLong());
684 case CONSTANT_DOUBLE:
685 cpool[i++] = new Double(in.readDouble());
688 case CONSTANT_STRING:
689 cpool[i] = new CPX(in.readUnsignedShort());
693 case CONSTANT_METHOD:
694 case CONSTANT_INTERFACEMETHOD:
695 case CONSTANT_NAMEANDTYPE:
696 cpool[i] = new CPX2(in.readUnsignedShort(), in.readUnsignedShort());
698 case CONSTANT_METHODHANDLE:
699 cpool[i] = new CPX2(in.readByte(), in.readUnsignedShort());
701 case CONSTANT_METHODTYPE:
702 cpool[i] = new CPX(in.readUnsignedShort());
704 case CONSTANT_INVOKEDYNAMIC:
705 cpool[i] = new CPX2(in.readUnsignedShort(), in.readUnsignedShort());
709 throw new ClassFormatError("invalid constant type: " + (int) tags[i]);
715 * Reads and strores field info.
717 protected void readFields(DataInputStream in) throws IOException {
718 int fields_count = in.readUnsignedShort();
719 fields = new FieldData[fields_count];
720 for (int k = 0; k < fields_count; k++) {
721 FieldData field = new FieldData(this);
728 * Reads and strores Method info.
730 protected void readMethods(DataInputStream in) throws IOException {
731 int methods_count = in.readUnsignedShort();
732 methods = new MethodData[methods_count];
733 for (int k = 0; k < methods_count; k++) {
734 MethodData method = new MethodData(this);
743 public String getString(int n) {
747 return (String) cpool[n];
752 * get the type of constant given an index
754 public byte getTag(int n) {
757 } catch (ArrayIndexOutOfBoundsException e) {
761 static final String hexString = "0123456789ABCDEF";
762 public static char hexTable[] = hexString.toCharArray();
764 static String toHex(long val, int width) {
765 StringBuffer s = new StringBuffer();
766 for (int i = width - 1; i >= 0; i--) {
767 s.append(hexTable[((int) (val >> (4 * i))) & 0xF]);
769 return "0x" + s.toString();
772 static String toHex(long val) {
774 for (width = 16; width > 0; width--) {
775 if ((val >> (width - 1) * 4) != 0) {
779 return toHex(val, width);
782 static String toHex(int val) {
784 for (width = 8; width > 0; width--) {
785 if ((val >> (width - 1) * 4) != 0) {
789 return toHex(val, width);
793 * Returns the name of this class.
795 public String getClassName() {
797 if (this_class == 0) {
802 if (tags[this_class] != CONSTANT_CLASS) {
803 return res; //"<CP["+cpx+"] is not a Class> ";
805 tcpx = ((CPX) cpool[this_class]).cpx;
806 } catch (ArrayIndexOutOfBoundsException e) {
807 return res; // "#"+cpx+"// invalid constant pool index";
808 } catch (Throwable e) {
809 return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
813 return (String) (cpool[tcpx]);
814 } catch (ArrayIndexOutOfBoundsException e) {
815 return res; // "class #"+scpx+"// invalid constant pool index";
816 } catch (ClassCastException e) {
817 return res; // "class #"+scpx+"// invalid constant pool reference";
818 } catch (Throwable e) {
819 return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
825 * Returns the name of class at perticular index.
827 public String getClassName(int cpx) {
828 String res = "#" + cpx;
834 if (tags[cpx] != CONSTANT_CLASS) {
835 return res; //"<CP["+cpx+"] is not a Class> ";
837 scpx = ((CPX) cpool[cpx]).cpx;
838 } catch (ArrayIndexOutOfBoundsException e) {
839 return res; // "#"+cpx+"// invalid constant pool index";
840 } catch (Throwable e) {
841 return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
845 return (String) (cpool[scpx]);
846 } catch (ArrayIndexOutOfBoundsException e) {
847 return res; // "class #"+scpx+"// invalid constant pool index";
848 } catch (ClassCastException e) {
849 return res; // "class #"+scpx+"// invalid constant pool reference";
850 } catch (Throwable e) {
851 return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
855 public int getAccessFlags() {
860 * Returns true if it is a class
862 public boolean isClass() {
863 if ((access & ACC_INTERFACE) == 0) {
870 * Returns true if it is a interface.
872 public boolean isInterface() {
873 if ((access & ACC_INTERFACE) != 0) {
880 * Returns true if this member is public, false otherwise.
882 public boolean isPublic() {
883 return (access & ACC_PUBLIC) != 0;
887 * Returns the access of this class or interface.
889 public String[] getAccess() {
890 Vector v = new Vector();
891 if ((access & ACC_PUBLIC) != 0) {
892 v.addElement("public");
894 if ((access & ACC_FINAL) != 0) {
895 v.addElement("final");
897 if ((access & ACC_ABSTRACT) != 0) {
898 v.addElement("abstract");
900 String[] accflags = new String[v.size()];
901 v.copyInto(accflags);
906 * Returns list of innerclasses.
908 public InnerClassData[] getInnerClasses() {
913 * Returns list of attributes.
915 final AttrData[] getAttributes() {
919 public byte[] findAnnotationData(boolean classRetention) {
920 String n = classRetention
921 ? "RuntimeInvisibleAnnotations" : // NOI18N
922 "RuntimeVisibleAnnotations"; // NOI18N
923 return findAttr(n, attrs);
927 * Returns true if superbit is set.
929 public boolean isSuperSet() {
930 if ((access & ACC_SUPER) != 0) {
937 * Returns super class name.
939 public String getSuperClassName() {
941 if (super_class == 0) {
946 if (tags[super_class] != CONSTANT_CLASS) {
947 return res; //"<CP["+cpx+"] is not a Class> ";
949 scpx = ((CPX) cpool[super_class]).cpx;
950 } catch (ArrayIndexOutOfBoundsException e) {
951 return res; // "#"+cpx+"// invalid constant pool index";
952 } catch (Throwable e) {
953 return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
957 return (String) (cpool[scpx]);
958 } catch (ArrayIndexOutOfBoundsException e) {
959 return res; // "class #"+scpx+"// invalid constant pool index";
960 } catch (ClassCastException e) {
961 return res; // "class #"+scpx+"// invalid constant pool reference";
962 } catch (Throwable e) {
963 return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
968 * Returns list of super interfaces.
970 public String[] getSuperInterfaces() {
971 String interfacenames[] = new String[interfaces.length];
972 int interfacecpx = -1;
973 for (int i = 0; i < interfaces.length; i++) {
974 interfacecpx = ((CPX) cpool[interfaces[i]]).cpx;
975 interfacenames[i] = (String) (cpool[interfacecpx]);
977 return interfacenames;
981 * Returns string at prticular constant pool index.
983 public String getStringValue(int cpoolx) {
985 return ((String) cpool[cpoolx]);
986 } catch (ArrayIndexOutOfBoundsException e) {
987 return "//invalid constant pool index:" + cpoolx;
988 } catch (ClassCastException e) {
989 return "//invalid constant pool ref:" + cpoolx;
994 * Returns list of field info.
996 public FieldData[] getFields() {
1001 * Returns list of method info.
1003 public MethodData[] getMethods() {
1008 * Returns constant pool entry at that index.
1010 public CPX2 getCpoolEntry(int cpx) {
1011 return ((CPX2) (cpool[cpx]));
1014 public Object getCpoolEntryobj(int cpx) {
1015 return (cpool[cpx]);
1019 * Returns index of this class.
1021 public int getthis_cpx() {
1026 * Returns string at that index.
1028 public String StringValue(int cpx) {
1029 return stringValue(cpx, false);
1032 public String stringValue(int cpx, boolean textual) {
1033 return stringValue(cpx, textual, null);
1036 public String stringValue(int cpx, String[] classRefs) {
1037 return stringValue(cpx, true, classRefs);
1040 private String stringValue(int cpx, boolean textual, String[] refs) {
1050 } catch (IndexOutOfBoundsException e) {
1051 return "<Incorrect CP index:" + cpx + ">";
1058 case CONSTANT_UTF8: {
1062 StringBuilder sb = new StringBuilder();
1063 String s = (String) x;
1064 for (int k = 0; k < s.length(); k++) {
1065 char c = s.charAt(k);
1068 sb.append('\\').append('\\');
1071 sb.append('\\').append('t');
1074 sb.append('\\').append('n');
1077 sb.append('\\').append('r');
1080 sb.append('\\').append('\"');
1083 sb.append("\\u2028");
1086 sb.append("\\u2029");
1092 return sb.toString();
1094 case CONSTANT_DOUBLE: {
1095 Double d = (Double) x;
1096 String sd = d.toString();
1102 case CONSTANT_FLOAT: {
1103 Float f = (Float) x;
1104 String sf = (f).toString();
1110 case CONSTANT_LONG: {
1113 return ln.toString();
1115 return ln.toString() + 'l';
1117 case CONSTANT_INTEGER: {
1118 Integer in = (Integer) x;
1119 return in.toString();
1121 case CONSTANT_CLASS:
1122 String jn = getClassName(cpx);
1129 return javaName(jn);
1130 case CONSTANT_STRING:
1131 String sv = stringValue(((CPX) x).cpx, textual);
1133 return '"' + sv + '"';
1137 case CONSTANT_FIELD:
1138 case CONSTANT_METHOD:
1139 case CONSTANT_INTERFACEMETHOD:
1140 //return getShortClassName(((CPX2)x).cpx1)+"."+StringValue(((CPX2)x).cpx2);
1141 return javaName(getClassName(((CPX2) x).cpx1)) + "." + StringValue(((CPX2) x).cpx2);
1143 case CONSTANT_NAMEANDTYPE:
1144 return getName(((CPX2) x).cpx1) + ":" + StringValue(((CPX2) x).cpx2);
1145 case CONSTANT_METHODHANDLE:
1146 return "K" + ((CPX2)x).cpx1 + "@" + stringValue(((CPX2)x).cpx2, textual);
1147 case CONSTANT_METHODTYPE:
1148 return stringValue(((CPX)x).cpx, true);
1150 return "UnknownTag" + tag; //TBD
1155 * Returns resolved java type name.
1157 public String javaName(String name) {
1161 int len = name.length();
1169 for (int k = 0; k < len; k += Character.charCount(cp)) {
1170 cp = name.codePointAt(k);
1172 if (!isJavaIdentifierStart(cp)) {
1175 } else if (cp != '/') {
1176 if (!isJavaIdentifierPart(cp)) {
1184 return "\"" + name + "\"";
1187 public String getName(int cpx) {
1190 return javaName((String) cpool[cpx]); //.replace('/','.');
1191 } catch (ArrayIndexOutOfBoundsException e) {
1192 return "<invalid constant pool index:" + cpx + ">";
1193 } catch (ClassCastException e) {
1194 return "<invalid constant pool ref:" + cpx + ">";
1199 * Returns unqualified class name.
1201 public String getShortClassName(int cpx) {
1202 String classname = javaName(getClassName(cpx));
1203 pkgPrefixLen = classname.lastIndexOf("/") + 1;
1204 if (pkgPrefixLen != 0) {
1205 pkgPrefix = classname.substring(0, pkgPrefixLen);
1206 if (classname.startsWith(pkgPrefix)) {
1207 return classname.substring(pkgPrefixLen);
1214 * Returns source file name.
1216 public String getSourceName() {
1217 return getName(source_cpx);
1221 * Returns package name.
1223 public String getPkgName() {
1224 String classname = getClassName(this_class);
1225 pkgPrefixLen = classname.lastIndexOf("/") + 1;
1226 if (pkgPrefixLen != 0) {
1227 pkgPrefix = classname.substring(0, pkgPrefixLen);
1228 return /* ("package " + */ pkgPrefix.substring(0, pkgPrefixLen - 1) /* + ";\n") */;
1234 public BootMethodData getBootMethod(int indx) {
1235 return bootMethods != null ? bootMethods[indx] : null;
1239 * Returns total constant pool entry count.
1241 public int getCpoolCount() {
1246 * Returns minor version of class file.
1248 public int getMinor_version() {
1249 return minor_version;
1253 * Returns major version of class file.
1255 public int getMajor_version() {
1256 return major_version;
1259 private boolean isJavaIdentifierStart(int cp) {
1260 return ('a' <= cp && cp <= 'z') || ('A' <= cp && cp <= 'Z');
1263 private boolean isJavaIdentifierPart(int cp) {
1264 return isJavaIdentifierStart(cp) || ('0' <= cp && cp <= '9');
1267 public String[] getNameAndType(int indx) {
1268 return getNameAndType(indx, 0, new String[2]);
1271 private String[] getNameAndType(int indx, int at, String[] arr) {
1272 CPX2 c2 = getCpoolEntry(indx);
1273 arr[at] = StringValue(c2.cpx1);
1274 arr[at + 1] = StringValue(c2.cpx2);
1278 public String[] getFieldInfoName(int indx) {
1279 CPX2 c2 = getCpoolEntry(indx);
1280 String[] arr = new String[3];
1281 arr[0] = getClassName(c2.cpx1);
1282 return getNameAndType(c2.cpx2, 1, arr);
1285 public MethodData findMethod(String name, String signature) {
1286 for (MethodData md: methods) {
1287 if (md.getName().equals(name)
1288 && md.getInternalSig().equals(signature)) {
1297 public FieldData findField(String name, String signature) {
1298 for (FieldData fd: fields) {
1299 if (fd.getName().equals(name)
1300 && fd.getInternalSig().equals(signature)) {
1309 static byte[] findAttr(String n, AttrData[] attrs) {
1310 for (AttrData ad : attrs) {
1311 if (n.equals(ad.getAttrName())) {
1312 return ad.getData();
1320 * Strores field data informastion.
1322 * @author Sucheta Dambalkar (Adopted code from jdis)
1324 static class FieldData {
1329 int descriptor_index;
1330 int attributes_count;
1332 boolean isSynthetic = false;
1333 boolean isDeprecated = false;
1336 public FieldData(ClassData cls) {
1341 * Read and store field info.
1343 public void read(DataInputStream in) throws IOException {
1344 access = in.readUnsignedShort();
1345 name_index = in.readUnsignedShort();
1346 descriptor_index = in.readUnsignedShort();
1347 // Read the attributes
1348 int attributes_count = in.readUnsignedShort();
1349 attrs = new Vector(attributes_count);
1350 for (int i = 0; i < attributes_count; i++) {
1351 int attr_name_index = in.readUnsignedShort();
1352 if (cls.getTag(attr_name_index) != CONSTANT_UTF8) {
1355 String attr_name = cls.getString(attr_name_index);
1356 if (attr_name.equals("ConstantValue")) {
1357 if (in.readInt() != 2) {
1358 throw new ClassFormatError("invalid ConstantValue attr length");
1360 value_cpx = in.readUnsignedShort();
1361 AttrData attr = new AttrData(cls);
1362 attr.read(attr_name_index);
1363 attrs.addElement(attr);
1364 } else if (attr_name.equals("Synthetic")) {
1365 if (in.readInt() != 0) {
1366 throw new ClassFormatError("invalid Synthetic attr length");
1369 AttrData attr = new AttrData(cls);
1370 attr.read(attr_name_index);
1371 attrs.addElement(attr);
1372 } else if (attr_name.equals("Deprecated")) {
1373 if (in.readInt() != 0) {
1374 throw new ClassFormatError("invalid Synthetic attr length");
1376 isDeprecated = true;
1377 AttrData attr = new AttrData(cls);
1378 attr.read(attr_name_index);
1379 attrs.addElement(attr);
1381 AttrData attr = new AttrData(cls);
1382 attr.read(attr_name_index, in);
1383 attrs.addElement(attr);
1389 public boolean isStatic() {
1390 return (access & ACC_STATIC) != 0;
1394 * Returns access of a field.
1396 public String[] getAccess() {
1397 Vector v = new Vector();
1398 if ((access & ACC_PUBLIC) != 0) {
1399 v.addElement("public");
1401 if ((access & ACC_PRIVATE) != 0) {
1402 v.addElement("private");
1404 if ((access & ACC_PROTECTED) != 0) {
1405 v.addElement("protected");
1407 if ((access & ACC_STATIC) != 0) {
1408 v.addElement("static");
1410 if ((access & ACC_FINAL) != 0) {
1411 v.addElement("final");
1413 if ((access & ACC_VOLATILE) != 0) {
1414 v.addElement("volatile");
1416 if ((access & ACC_TRANSIENT) != 0) {
1417 v.addElement("transient");
1419 String[] accflags = new String[v.size()];
1420 v.copyInto(accflags);
1425 * Returns name of a field.
1427 public String getName() {
1428 return cls.getStringValue(name_index);
1432 * Returns internal signature of a field
1434 public String getInternalSig() {
1435 return cls.getStringValue(descriptor_index);
1439 * Returns true if field is synthetic.
1441 public boolean isSynthetic() {
1446 * Returns true if field is deprecated.
1448 public boolean isDeprecated() {
1449 return isDeprecated;
1452 public boolean hasConstantValue() {
1453 return value_cpx != -1;
1457 * Returns list of attributes of field.
1459 public Vector getAttributes() {
1463 public byte[] findAnnotationData(boolean classRetention) {
1464 String n = classRetention
1465 ? "RuntimeInvisibleAnnotations" : // NOI18N
1466 "RuntimeVisibleAnnotations"; // NOI18N
1467 AttrData[] arr = new AttrData[attrs.size()];
1468 attrs.copyInto(arr);
1469 return ClassData.findAttr(n, arr);
1474 * A JavaScript optimized replacement for Hashtable.
1476 * @author Jaroslav Tulach <jtulach@netbeans.org>
1478 private static final class Hashtable {
1480 private Object[] keys;
1481 private Object[] values;
1487 Hashtable(int i, double d) {
1494 synchronized void put(Object key, Object val) {
1495 int[] where = {-1, -1};
1496 Object found = get(key, where);
1497 if (where[0] != -1) {
1499 values[where[0]] = val;
1501 if (where[1] != -1) {
1503 keys[where[1]] = key;
1504 values[where[1]] = val;
1507 keys = new Object[11];
1508 values = new Object[11];
1512 Object[] newKeys = new Object[keys.length * 2];
1513 Object[] newValues = new Object[values.length * 2];
1514 for (int i = 0; i < keys.length; i++) {
1515 newKeys[i] = keys[i];
1516 newValues[i] = values[i];
1518 newKeys[keys.length] = key;
1519 newValues[keys.length] = val;
1527 Object get(Object key) {
1528 return get(key, null);
1531 private synchronized Object get(Object key, int[] foundAndNull) {
1535 for (int i = 0; i < keys.length; i++) {
1536 if (keys[i] == null) {
1537 if (foundAndNull != null) {
1538 foundAndNull[1] = i;
1540 } else if (keys[i].equals(key)) {
1541 if (foundAndNull != null) {
1542 foundAndNull[0] = i;
1552 * Strores InnerClass data informastion.
1554 * @author Sucheta Dambalkar (Adopted code from jdis)
1556 private static class InnerClassData {
1559 int inner_class_info_index, outer_class_info_index, inner_name_index, access;
1561 public InnerClassData(ClassData cls) {
1567 * Read Innerclass attribute data.
1569 public void read(DataInputStream in) throws IOException {
1570 inner_class_info_index = in.readUnsignedShort();
1571 outer_class_info_index = in.readUnsignedShort();
1572 inner_name_index = in.readUnsignedShort();
1573 access = in.readUnsignedShort();
1577 * Returns the access of this class or interface.
1579 public String[] getAccess() {
1580 Vector v = new Vector();
1581 if ((access & ACC_PUBLIC) != 0) {
1582 v.addElement("public");
1584 if ((access & ACC_FINAL) != 0) {
1585 v.addElement("final");
1587 if ((access & ACC_ABSTRACT) != 0) {
1588 v.addElement("abstract");
1590 String[] accflags = new String[v.size()];
1591 v.copyInto(accflags);
1594 } // end InnerClassData
1596 static class BootMethodData {
1597 private final ClassData clazz;
1599 private final int[] args;
1601 private BootMethodData(ClassData clazz, int method, int[] args) {
1603 this.method = method;
1608 public String toString() {
1609 StringBuilder sb = new StringBuilder();
1610 sb.append(clazz.stringValue(method, true));
1612 for (int indx : args) {
1614 sb.append(clazz.stringValue(indx, true));
1617 return sb.toString();
1622 * Strores LineNumberTable data information.
1624 * @author Sucheta Dambalkar (Adopted code from jdis)
1626 private static class LineNumData {
1628 short start_pc, line_number;
1630 public LineNumData() {
1634 * Read LineNumberTable attribute.
1636 public LineNumData(DataInputStream in) throws IOException {
1637 start_pc = in.readShort();
1638 line_number = in.readShort();
1644 * Strores LocalVariableTable data information.
1646 * @author Sucheta Dambalkar (Adopted code from jdis)
1648 private static class LocVarData {
1650 short start_pc, length, name_cpx, sig_cpx, slot;
1652 public LocVarData() {
1656 * Read LocalVariableTable attribute.
1658 public LocVarData(DataInputStream in) throws IOException {
1659 start_pc = in.readShort();
1660 length = in.readShort();
1661 name_cpx = in.readShort();
1662 sig_cpx = in.readShort();
1663 slot = in.readShort();
1668 * Strores method data informastion.
1670 * @author Sucheta Dambalkar (Adopted code from jdis)
1672 static class MethodData {
1677 int descriptor_index;
1678 int attributes_count;
1680 Vector exception_table = new Vector(0);
1681 Vector lin_num_tb = new Vector(0);
1682 Vector loc_var_tb = new Vector(0);
1683 StackMapTableData[] stackMapTable;
1684 StackMapData[] stackMap;
1685 int[] exc_index_table = null;
1686 Vector attrs = new Vector(0);
1687 Vector code_attrs = new Vector(0);
1688 int max_stack, max_locals;
1689 boolean isSynthetic = false;
1690 boolean isDeprecated = false;
1692 public MethodData(ClassData cls) {
1699 public void read(DataInputStream in) throws IOException {
1700 access = in.readUnsignedShort();
1701 name_index = in.readUnsignedShort();
1702 descriptor_index = in.readUnsignedShort();
1703 int attributes_count = in.readUnsignedShort();
1704 for (int i = 0; i < attributes_count; i++) {
1705 int attr_name_index = in.readUnsignedShort();
1709 if (cls.getTag(attr_name_index) == CONSTANT_UTF8) {
1710 String attr_name = cls.getString(attr_name_index);
1711 if (attr_name.equals("Code")) {
1713 AttrData attr = new AttrData(cls);
1714 attr.read(attr_name_index);
1715 attrs.addElement(attr);
1717 } else if (attr_name.equals("Exceptions")) {
1719 AttrData attr = new AttrData(cls);
1720 attr.read(attr_name_index);
1721 attrs.addElement(attr);
1723 } else if (attr_name.equals("Synthetic")) {
1724 if (in.readInt() != 0) {
1725 throw new ClassFormatError("invalid Synthetic attr length");
1728 AttrData attr = new AttrData(cls);
1729 attr.read(attr_name_index);
1730 attrs.addElement(attr);
1732 } else if (attr_name.equals("Deprecated")) {
1733 if (in.readInt() != 0) {
1734 throw new ClassFormatError("invalid Synthetic attr length");
1736 isDeprecated = true;
1737 AttrData attr = new AttrData(cls);
1738 attr.read(attr_name_index);
1739 attrs.addElement(attr);
1743 AttrData attr = new AttrData(cls);
1744 attr.read(attr_name_index, in);
1745 attrs.addElement(attr);
1751 * Read code attribute info.
1753 public void readCode(DataInputStream in) throws IOException {
1755 int attr_length = in.readInt();
1756 max_stack = in.readUnsignedShort();
1757 max_locals = in.readUnsignedShort();
1758 int codelen = in.readInt();
1760 code = new byte[codelen];
1762 while (totalread < codelen) {
1763 totalread += in.read(code, totalread, codelen - totalread);
1765 // in.read(code, 0, codelen);
1767 readExceptionTable(in);
1768 int code_attributes_count = in.readUnsignedShort();
1770 for (int k = 0; k < code_attributes_count; k++) {
1771 int table_name_index = in.readUnsignedShort();
1772 int table_name_tag = cls.getTag(table_name_index);
1773 AttrData attr = new AttrData(cls);
1774 if (table_name_tag == CONSTANT_UTF8) {
1775 String table_name_tstr = cls.getString(table_name_index);
1776 if (table_name_tstr.equals("LineNumberTable")) {
1777 readLineNumTable(in);
1778 attr.read(table_name_index);
1779 } else if (table_name_tstr.equals("LocalVariableTable")) {
1780 readLocVarTable(in);
1781 attr.read(table_name_index);
1782 } else if (table_name_tstr.equals("StackMapTable")) {
1783 readStackMapTable(in);
1784 attr.read(table_name_index);
1785 } else if (table_name_tstr.equals("StackMap")) {
1787 attr.read(table_name_index);
1789 attr.read(table_name_index, in);
1791 code_attrs.addElement(attr);
1795 attr.read(table_name_index, in);
1796 code_attrs.addElement(attr);
1801 * Read exception table info.
1803 void readExceptionTable(DataInputStream in) throws IOException {
1804 int exception_table_len = in.readUnsignedShort();
1805 exception_table = new Vector(exception_table_len);
1806 for (int l = 0; l < exception_table_len; l++) {
1807 exception_table.addElement(new TrapData(in, l));
1812 * Read LineNumberTable attribute info.
1814 void readLineNumTable(DataInputStream in) throws IOException {
1815 int attr_len = in.readInt(); // attr_length
1816 int lin_num_tb_len = in.readUnsignedShort();
1817 lin_num_tb = new Vector(lin_num_tb_len);
1818 for (int l = 0; l < lin_num_tb_len; l++) {
1819 lin_num_tb.addElement(new LineNumData(in));
1824 * Read LocalVariableTable attribute info.
1826 void readLocVarTable(DataInputStream in) throws IOException {
1827 int attr_len = in.readInt(); // attr_length
1828 int loc_var_tb_len = in.readUnsignedShort();
1829 loc_var_tb = new Vector(loc_var_tb_len);
1830 for (int l = 0; l < loc_var_tb_len; l++) {
1831 loc_var_tb.addElement(new LocVarData(in));
1836 * Read Exception attribute info.
1838 public void readExceptions(DataInputStream in) throws IOException {
1839 int attr_len = in.readInt(); // attr_length in prog
1840 int num_exceptions = in.readUnsignedShort();
1841 exc_index_table = new int[num_exceptions];
1842 for (int l = 0; l < num_exceptions; l++) {
1843 int exc = in.readShort();
1844 exc_index_table[l] = exc;
1849 * Read StackMapTable attribute info.
1851 void readStackMapTable(DataInputStream in) throws IOException {
1852 int attr_len = in.readInt(); //attr_length
1853 int stack_map_tb_len = in.readUnsignedShort();
1854 stackMapTable = new StackMapTableData[stack_map_tb_len];
1855 for (int i = 0; i < stack_map_tb_len; i++) {
1856 stackMapTable[i] = StackMapTableData.getInstance(in, this);
1861 * Read StackMap attribute info.
1863 void readStackMap(DataInputStream in) throws IOException {
1864 int attr_len = in.readInt(); //attr_length
1865 int stack_map_len = in.readUnsignedShort();
1866 stackMap = new StackMapData[stack_map_len];
1867 for (int i = 0; i < stack_map_len; i++) {
1868 stackMap[i] = new StackMapData(in, this);
1873 * Return access of the method.
1875 public int getAccess() {
1880 * Return name of the method.
1882 public String getName() {
1883 return cls.getStringValue(name_index);
1887 * Return internal siganature of the method.
1889 public String getInternalSig() {
1890 return cls.getStringValue(descriptor_index);
1894 * Return code attribute data of a method.
1896 public byte[] getCode() {
1901 * Return LineNumberTable size.
1903 public int getnumlines() {
1904 return lin_num_tb.size();
1908 * Return LineNumberTable
1910 public Vector getlin_num_tb() {
1915 * Return LocalVariableTable size.
1917 public int getloc_var_tbsize() {
1918 return loc_var_tb.size();
1922 * Return LocalVariableTable.
1924 public Vector getloc_var_tb() {
1931 public StackMapData[] getStackMap() {
1936 * Return StackMapTable.
1938 public StackMapTableData[] getStackMapTable() {
1939 return stackMapTable;
1942 public StackMapIterator createStackMapIterator() {
1943 return new StackMapIterator(this);
1947 * Return true if method is static
1949 public boolean isStatic() {
1950 if ((access & ACC_STATIC) != 0) {
1957 * Return max depth of operand stack.
1959 public int getMaxStack() {
1964 * Return number of local variables.
1966 public int getMaxLocals() {
1971 * Return exception index table in Exception attribute.
1973 public int[] get_exc_index_table() {
1974 return exc_index_table;
1978 * Return exception table in code attributre.
1980 public TrapDataIterator getTrapDataIterator() {
1981 return new TrapDataIterator(exception_table);
1985 * Return method attributes.
1987 public Vector getAttributes() {
1992 * Return code attributes.
1994 public Vector getCodeAttributes() {
1999 * Return true if method id synthetic.
2001 public boolean isSynthetic() {
2006 * Return true if method is deprecated.
2008 public boolean isDeprecated() {
2009 return isDeprecated;
2012 public byte[] findAnnotationData(boolean classRetention) {
2013 String n = classRetention
2014 ? "RuntimeInvisibleAnnotations" : // NOI18N
2015 "RuntimeVisibleAnnotations"; // NOI18N
2016 AttrData[] arr = new AttrData[attrs.size()];
2017 attrs.copyInto(arr);
2018 return ClassData.findAttr(n, arr);
2021 public boolean isConstructor() {
2022 return "<init>".equals(getName());
2026 /* represents one entry of StackMap attribute
2028 private static class StackMapData {
2034 StackMapData(int offset, int[] locals, int[] stack) {
2035 this.offset = offset;
2036 this.locals = locals;
2040 StackMapData(DataInputStream in, MethodData method) throws IOException {
2041 offset = in.readUnsignedShort();
2042 int local_size = in.readUnsignedShort();
2043 locals = readTypeArray(in, local_size, method);
2044 int stack_size = in.readUnsignedShort();
2045 stack = readTypeArray(in, stack_size, method);
2048 static final int[] readTypeArray(DataInputStream in, int length, MethodData method) throws IOException {
2049 int[] types = new int[length];
2050 for (int i = 0; i < length; i++) {
2051 types[i] = readType(in, method);
2056 static final int readType(DataInputStream in, MethodData method) throws IOException {
2057 int type = in.readUnsignedByte();
2058 if (type == ITEM_Object || type == ITEM_NewObject) {
2059 type = type | (in.readUnsignedShort() << 8);
2065 static final class StackMapIterator {
2067 private final StackMapTableData[] stackMapTable;
2068 private final TypeArray argTypes;
2069 private final TypeArray localTypes;
2070 private final TypeArray stackTypes;
2071 private int nextFrameIndex;
2072 private int lastFrameByteCodeOffset;
2073 private int byteCodeOffset;
2075 StackMapIterator(final MethodData methodData) {
2076 this(methodData.getStackMapTable(),
2077 methodData.getInternalSig(),
2078 methodData.isStatic());
2081 StackMapIterator(final StackMapTableData[] stackMapTable,
2082 final String methodSignature,
2083 final boolean isStaticMethod) {
2084 this.stackMapTable = (stackMapTable != null)
2086 : new StackMapTableData[0];
2088 argTypes = getArgTypes(methodSignature, isStaticMethod);
2089 localTypes = new TypeArray();
2090 stackTypes = new TypeArray();
2092 localTypes.addAll(argTypes);
2094 lastFrameByteCodeOffset = -1;
2098 public boolean isEmpty() {
2099 return stackMapTable.length == 0;
2102 public String getFrameAsString() {
2103 return (nextFrameIndex == 0)
2104 ? StackMapTableData.toString("INITIAL", 0, null, null)
2105 : stackMapTable[nextFrameIndex - 1].toString();
2108 public int getFrameIndex() {
2109 return nextFrameIndex;
2112 public TypeArray getFrameStack() {
2116 public TypeArray getFrameLocals() {
2120 public TypeArray getArguments() {
2124 public void advanceBy(final int numByteCodes) {
2125 if (numByteCodes < 0) {
2126 throw new IllegalStateException("Forward only iterator");
2129 byteCodeOffset += numByteCodes;
2130 while ((nextFrameIndex < stackMapTable.length)
2131 && ((byteCodeOffset - lastFrameByteCodeOffset)
2132 >= (stackMapTable[nextFrameIndex].offsetDelta
2134 final StackMapTableData nextFrame = stackMapTable[nextFrameIndex];
2136 lastFrameByteCodeOffset += nextFrame.offsetDelta + 1;
2137 nextFrame.applyTo(localTypes, stackTypes);
2143 public void advanceTo(final int nextByteCodeOffset) {
2144 advanceBy(nextByteCodeOffset - byteCodeOffset);
2147 private static TypeArray getArgTypes(final String methodSignature,
2148 final boolean isStaticMethod) {
2149 final TypeArray argTypes = new TypeArray();
2151 if (!isStaticMethod) {
2152 argTypes.add(ITEM_Object);
2155 if (methodSignature.charAt(0) != '(') {
2156 throw new IllegalArgumentException("Invalid method signature");
2159 final int length = methodSignature.length();
2160 boolean skipType = false;
2162 for (int i = 1; i < length; ++i) {
2163 switch (methodSignature.charAt(i)) {
2169 argType = ITEM_Integer;
2172 argType = ITEM_Long;
2175 argType = ITEM_Float;
2178 argType = ITEM_Double;
2181 i = methodSignature.indexOf(';', i + 1);
2183 throw new IllegalArgumentException(
2184 "Invalid method signature");
2186 argType = ITEM_Object;
2190 // not interested in the return value type
2194 argTypes.add(ITEM_Object);
2200 throw new IllegalArgumentException(
2201 "Invalid method signature");
2205 argTypes.add(argType);
2214 /* represents one entry of StackMapTable attribute
2217 private static abstract class StackMapTableData {
2219 final int frameType;
2222 StackMapTableData(int frameType) {
2223 this.frameType = frameType;
2226 abstract void applyTo(TypeArray localTypes, TypeArray stackTypes);
2228 protected static String toString(
2229 final String frameType,
2231 final int[] localTypes,
2232 final int[] stackTypes) {
2233 final StringBuilder sb = new StringBuilder(frameType);
2235 sb.append("(off: +").append(offset);
2236 if (localTypes != null) {
2237 sb.append(", locals: ");
2238 appendTypes(sb, localTypes);
2240 if (stackTypes != null) {
2241 sb.append(", stack: ");
2242 appendTypes(sb, stackTypes);
2246 return sb.toString();
2249 private static void appendTypes(final StringBuilder sb, final int[] types) {
2251 if (types.length > 0) {
2252 sb.append(TypeArray.typeString(types[0]));
2253 for (int i = 1; i < types.length; ++i) {
2255 sb.append(TypeArray.typeString(types[i]));
2261 private static class SameFrame extends StackMapTableData {
2263 SameFrame(int frameType, int offsetDelta) {
2265 this.offsetDelta = offsetDelta;
2269 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
2274 public String toString() {
2275 return toString("SAME" + ((frameType == SAME_FRAME_EXTENDED)
2276 ? "_FRAME_EXTENDED" : ""),
2282 private static class SameLocals1StackItem extends StackMapTableData {
2286 SameLocals1StackItem(int frameType, int offsetDelta, int[] stack) {
2288 this.offsetDelta = offsetDelta;
2293 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
2294 stackTypes.setAll(stack);
2298 public String toString() {
2300 "SAME_LOCALS_1_STACK_ITEM"
2301 + ((frameType == SAME_LOCALS_1_STACK_ITEM_EXTENDED)
2302 ? "_EXTENDED" : ""),
2308 private static class ChopFrame extends StackMapTableData {
2310 ChopFrame(int frameType, int offsetDelta) {
2312 this.offsetDelta = offsetDelta;
2316 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
2317 localTypes.setSize(localTypes.getSize()
2318 - (SAME_FRAME_EXTENDED - frameType));
2323 public String toString() {
2324 return toString("CHOP", offsetDelta, null, null);
2328 private static class AppendFrame extends StackMapTableData {
2332 AppendFrame(int frameType, int offsetDelta, int[] locals) {
2334 this.offsetDelta = offsetDelta;
2335 this.locals = locals;
2339 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
2340 localTypes.addAll(locals);
2345 public String toString() {
2346 return toString("APPEND", offsetDelta, locals, null);
2350 private static class FullFrame extends StackMapTableData {
2355 FullFrame(int offsetDelta, int[] locals, int[] stack) {
2357 this.offsetDelta = offsetDelta;
2358 this.locals = locals;
2363 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
2364 localTypes.setAll(locals);
2365 stackTypes.setAll(stack);
2369 public String toString() {
2370 return toString("FULL", offsetDelta, locals, stack);
2374 static StackMapTableData getInstance(DataInputStream in, MethodData method)
2375 throws IOException {
2376 int frameType = in.readUnsignedByte();
2378 if (frameType < SAME_FRAME_BOUND) {
2380 return new SameFrame(frameType, frameType);
2381 } else if (SAME_FRAME_BOUND <= frameType && frameType < SAME_LOCALS_1_STACK_ITEM_BOUND) {
2382 // same_locals_1_stack_item_frame
2383 // read additional single stack element
2384 return new SameLocals1StackItem(frameType,
2385 (frameType - SAME_FRAME_BOUND),
2386 StackMapData.readTypeArray(in, 1, method));
2387 } else if (frameType == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
2388 // same_locals_1_stack_item_extended
2389 return new SameLocals1StackItem(frameType,
2390 in.readUnsignedShort(),
2391 StackMapData.readTypeArray(in, 1, method));
2392 } else if (SAME_LOCALS_1_STACK_ITEM_EXTENDED < frameType && frameType < SAME_FRAME_EXTENDED) {
2393 // chop_frame or same_frame_extended
2394 return new ChopFrame(frameType, in.readUnsignedShort());
2395 } else if (frameType == SAME_FRAME_EXTENDED) {
2396 // chop_frame or same_frame_extended
2397 return new SameFrame(frameType, in.readUnsignedShort());
2398 } else if (SAME_FRAME_EXTENDED < frameType && frameType < FULL_FRAME) {
2400 return new AppendFrame(frameType, in.readUnsignedShort(),
2401 StackMapData.readTypeArray(in, frameType - SAME_FRAME_EXTENDED, method));
2402 } else if (frameType == FULL_FRAME) {
2404 int offsetDelta = in.readUnsignedShort();
2405 int locals_size = in.readUnsignedShort();
2406 int[] locals = StackMapData.readTypeArray(in, locals_size, method);
2407 int stack_size = in.readUnsignedShort();
2408 int[] stack = StackMapData.readTypeArray(in, stack_size, method);
2409 return new FullFrame(offsetDelta, locals, stack);
2411 throw new ClassFormatError("unrecognized frame_type in StackMapTable");
2417 * Stores exception table data in code attribute.
2419 * @author Sucheta Dambalkar (Adopted code from jdis)
2421 static final class TrapData {
2423 public final short start_pc;
2424 public final short end_pc;
2425 public final short handler_pc;
2426 public final short catch_cpx;
2430 * Read and store exception table data in code attribute.
2432 TrapData(DataInputStream in, int num) throws IOException {
2434 start_pc = in.readShort();
2435 end_pc = in.readShort();
2436 handler_pc = in.readShort();
2437 catch_cpx = in.readShort();
2441 * returns recommended identifier
2443 public String ident() {
2449 * @author Jaroslav Tulach <jtulach@netbeans.org>
2451 static final class TrapDataIterator {
2453 private final Hashtable exStart = new Hashtable();
2454 private final Hashtable exStop = new Hashtable();
2455 private TrapData[] current = new TrapData[10];
2456 private int currentCount;
2458 TrapDataIterator(Vector exceptionTable) {
2459 for (int i = 0; i < exceptionTable.size(); i++) {
2460 final TrapData td = (TrapData) exceptionTable.elementAt(i);
2461 put(exStart, td.start_pc, td);
2462 put(exStop, td.end_pc, td);
2466 private static void put(Hashtable h, short key, TrapData td) {
2467 Short s = Short.valueOf((short) key);
2468 Vector v = (Vector) h.get(s);
2476 private boolean processAll(Hashtable h, Short key, boolean add) {
2477 boolean change = false;
2478 Vector v = (Vector) h.get(key);
2481 for (int i = 0; i < s; i++) {
2482 TrapData td = (TrapData) v.elementAt(i);
2495 public boolean advanceTo(int i) {
2496 Short s = Short.valueOf((short) i);
2497 boolean ch1 = processAll(exStart, s, true);
2498 boolean ch2 = processAll(exStop, s, false);
2502 public boolean useTry() {
2503 return currentCount > 0;
2506 public TrapData[] current() {
2507 TrapData[] copy = new TrapData[currentCount];
2508 for (int i = 0; i < currentCount; i++) {
2509 copy[i] = current[i];
2514 private void add(TrapData e) {
2515 if (currentCount == current.length) {
2516 TrapData[] data = new TrapData[currentCount * 2];
2517 for (int i = 0; i < currentCount; i++) {
2518 data[i] = current[i];
2522 current[currentCount++] = e;
2525 private void remove(TrapData e) {
2526 if (currentCount == 0) {
2530 while (from < currentCount) {
2531 if (e == current[from++]) {
2535 while (from < currentCount) {
2536 current[from - 1] = current[from];
2537 current[from] = null;
2543 static final class TypeArray {
2545 private static final int CAPACITY_INCREMENT = 16;
2546 private int[] types;
2549 public TypeArray() {
2552 public TypeArray(final TypeArray initialTypes) {
2553 setAll(initialTypes);
2556 public void add(final int newType) {
2557 ensureCapacity(size + 1);
2558 types[size++] = newType;
2561 public void addAll(final TypeArray newTypes) {
2562 addAll(newTypes.types, 0, newTypes.size);
2565 public void addAll(final int[] newTypes) {
2566 addAll(newTypes, 0, newTypes.length);
2569 public void addAll(final int[] newTypes,
2573 ensureCapacity(size + count);
2574 arraycopy(newTypes, offset, types, size, count);
2579 public void set(final int index, final int newType) {
2580 types[index] = newType;
2583 public void setAll(final TypeArray newTypes) {
2584 setAll(newTypes.types, 0, newTypes.size);
2587 public void setAll(final int[] newTypes) {
2588 setAll(newTypes, 0, newTypes.length);
2591 public void setAll(final int[] newTypes,
2595 ensureCapacity(count);
2596 arraycopy(newTypes, offset, types, 0, count);
2603 public void setSize(final int newSize) {
2604 if (size != newSize) {
2605 ensureCapacity(newSize);
2607 for (int i = size; i < newSize; ++i) {
2614 public void clear() {
2618 public int getSize() {
2622 public int get(final int index) {
2623 return types[index];
2626 public static String typeString(final int type) {
2627 switch (type & 0xff) {
2640 case ITEM_InitObject: // UninitializedThis
2644 case ITEM_NewObject: // Uninitialized
2647 throw new IllegalArgumentException("Unknown type");
2652 public String toString() {
2653 final StringBuilder sb = new StringBuilder("[");
2655 for (int i = 0; i < size; ++i) {
2656 sb.append(sep).append(VarType.toString(types[i] & 0xff));
2659 return sb.append(']').toString();
2662 private void ensureCapacity(final int minCapacity) {
2663 if ((minCapacity == 0)
2664 || (types != null) && (minCapacity <= types.length)) {
2668 final int newCapacity =
2669 ((minCapacity + CAPACITY_INCREMENT - 1) / CAPACITY_INCREMENT)
2670 * CAPACITY_INCREMENT;
2671 final int[] newTypes = new int[newCapacity];
2674 arraycopy(types, 0, newTypes, 0, size);
2680 // no System.arraycopy
2681 private void arraycopy(final int[] src, final int srcPos,
2682 final int[] dest, final int destPos,
2684 for (int i = 0; i < length; ++i) {
2685 dest[destPos + i] = src[srcPos + i];
2690 * A JavaScript ready replacement for java.util.Vector
2692 * @author Jaroslav Tulach <jtulach@netbeans.org>
2694 @JavaScriptPrototype(prototype = "new Array")
2695 private static final class Vector {
2697 private Object[] arr;
2705 void add(Object objectType) {
2706 addElement(objectType);
2709 @JavaScriptBody(args = {"obj"}, body =
2711 void addElement(Object obj) {
2712 final int s = size();
2714 setElementAt(obj, s);
2717 @JavaScriptBody(args = {}, body =
2718 "return this.length;")
2720 return arr == null ? 0 : arr.length;
2723 @JavaScriptBody(args = {"newArr"}, body =
2724 "for (var i = 0; i < this.length; i++) {\n"
2725 + " newArr[i] = this[i];\n"
2727 void copyInto(Object[] newArr) {
2731 int min = Math.min(newArr.length, arr.length);
2732 for (int i = 0; i < min; i++) {
2737 @JavaScriptBody(args = {"index"}, body =
2738 "return this[index];")
2739 Object elementAt(int index) {
2743 private void setSize(int len) {
2744 Object[] newArr = new Object[len];
2749 @JavaScriptBody(args = {"val", "index"}, body =
2750 "this[index] = val;")
2751 void setElementAt(Object val, int index) {