2 * Copyright (c) 2002, 2004, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
25 package org.apidesign.vm4brwsr;
27 import java.io.ByteArrayInputStream;
28 import java.io.DataInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import org.apidesign.bck2brwsr.core.JavaScriptBody;
32 import org.apidesign.bck2brwsr.core.JavaScriptPrototype;
34 /** This is a byte code parser heavily based on original code of JavaP utility.
35 * As such I decided to keep the original Oracle's GPLv2 header.
37 * @author Jaroslav Tulach <jtulach@netbeans.org>
39 final class ByteCodeParser {
40 private ByteCodeParser() {
43 /* Class File Constants */
44 public static final int JAVA_MAGIC = 0xcafebabe;
45 public static final int JAVA_VERSION = 45;
46 public static final int JAVA_MINOR_VERSION = 3;
49 public static final int CONSTANT_UTF8 = 1;
50 public static final int CONSTANT_UNICODE = 2;
51 public static final int CONSTANT_INTEGER = 3;
52 public static final int CONSTANT_FLOAT = 4;
53 public static final int CONSTANT_LONG = 5;
54 public static final int CONSTANT_DOUBLE = 6;
55 public static final int CONSTANT_CLASS = 7;
56 public static final int CONSTANT_STRING = 8;
57 public static final int CONSTANT_FIELD = 9;
58 public static final int CONSTANT_METHOD = 10;
59 public static final int CONSTANT_INTERFACEMETHOD = 11;
60 public static final int CONSTANT_NAMEANDTYPE = 12;
61 public static final int CONSTANT_METHODHANDLE = 15;
62 public static final int CONSTANT_METHODTYPE = 16;
63 public static final int CONSTANT_INVOKEDYNAMIC = 18;
66 public static final int ACC_PUBLIC = 0x00000001;
67 public static final int ACC_PRIVATE = 0x00000002;
68 public static final int ACC_PROTECTED = 0x00000004;
69 public static final int ACC_STATIC = 0x00000008;
70 public static final int ACC_FINAL = 0x00000010;
71 public static final int ACC_SYNCHRONIZED = 0x00000020;
72 public static final int ACC_SUPER = 0x00000020;
73 public static final int ACC_VOLATILE = 0x00000040;
74 public static final int ACC_TRANSIENT = 0x00000080;
75 public static final int ACC_NATIVE = 0x00000100;
76 public static final int ACC_INTERFACE = 0x00000200;
77 public static final int ACC_ABSTRACT = 0x00000400;
78 public static final int ACC_STRICT = 0x00000800;
79 public static final int ACC_EXPLICIT = 0x00001000;
80 public static final int ACC_SYNTHETIC = 0x00010000; // actually, this is an attribute
81 private static final int ACC_ANNOTATION = 0x00020000;
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
361 ) throws IOException {
364 protected void visitEnumAttr(
365 String annoType, String attr, String attrType, String value
366 ) throws IOException {
367 visitAttr(annoType, attr, attrType, value);
370 protected void visitClassAttr(
371 String annoType, String attr, String className
372 ) throws IOException {
373 visitAttr(annoType, attr, className, className);
377 * Initialize the parsing with constant pool from
380 * @param attr the attribute defining annotations
381 * @param cd constant pool
382 * @throws IOException in case I/O fails
384 public final void parse(byte[] attr, ClassData cd) throws IOException {
385 ByteArrayInputStream is = new ByteArrayInputStream(attr);
386 DataInputStream dis = new DataInputStream(is);
394 private void read(DataInputStream dis, ClassData cd) throws IOException {
395 int cnt = dis.readUnsignedShort();
396 for (int i = 0; i < cnt; i++) {
397 readAnno(dis, cd, true);
401 private void readAnno(DataInputStream dis, ClassData cd, boolean top) throws IOException {
402 int type = dis.readUnsignedShort();
403 String typeName = cd.StringValue(type);
404 visitAnnotationStart(typeName, top);
405 int cnt = dis.readUnsignedShort();
406 for (int i = 0; i < cnt; i++) {
407 String attrName = cd.StringValue(dis.readUnsignedShort());
408 readValue(dis, cd, typeName, attrName);
410 visitAnnotationEnd(typeName, top);
412 visitAttr(typeName, null, null, null);
416 public void parseDefault(byte[] defaultAttribute, ClassData cd) throws IOException {
417 ByteArrayInputStream is = new ByteArrayInputStream(defaultAttribute);
418 DataInputStream dis = new DataInputStream(is);
420 readValue(dis, cd, null, null);
426 private void readValue(
427 DataInputStream dis, ClassData cd, String typeName, String attrName) throws IOException {
428 char type = (char) dis.readByte();
429 visitValueStart(attrName, type);
431 readAnno(dis, cd, false);
432 } else if ("CFJZsSIDB".indexOf(type) >= 0) { // NOI18N
433 int primitive = dis.readUnsignedShort();
434 String val = cd.stringValue(primitive, textual);
437 attrType = "Ljava_lang_String_2";
439 val = '"' + val + '"';
442 attrType = "" + type;
444 visitAttr(typeName, attrName, attrType, val);
445 } else if (type == 'c') {
446 int cls = dis.readUnsignedShort();
447 String attrType = cd.stringValue(cls, textual);
448 visitClassAttr(typeName, attrName, attrType);
449 } else if (type == '[') {
450 int cnt = dis.readUnsignedShort();
451 for (int i = 0; i < cnt; i++) {
452 readValue(dis, cd, typeName, iterateArray ? attrName : null);
454 } else if (type == 'e') {
455 int enumT = dis.readUnsignedShort();
456 String attrType = cd.stringValue(enumT, textual);
457 int enumN = dis.readUnsignedShort();
458 String val = cd.stringValue(enumN, textual);
459 visitEnumAttr(typeName, attrName, attrType, val);
461 throw new IOException("Unknown type " + type);
463 visitValueEnd(attrName, type);
468 * Reads and stores attribute information.
470 * @author Sucheta Dambalkar (Adopted code from jdis)
472 private static class AttrData {
479 public AttrData(ClassData cls) {
484 * Reads unknown attribute.
486 public void read(int name_cpx, DataInputStream in) throws IOException {
487 this.name_cpx = name_cpx;
488 datalen = in.readInt();
489 data = new byte[datalen];
494 * Reads just the name of known attribute.
496 public void read(int name_cpx) {
497 this.name_cpx = name_cpx;
501 * Returns attribute name.
503 public String getAttrName() {
504 return cls.getString(name_cpx);
508 * Returns attribute data.
510 public byte[] getData() {
516 * Stores constant pool entry information with one field.
518 * @author Sucheta Dambalkar (Adopted code from jdis)
520 private static class CPX {
530 * Stores constant pool entry information with two fields.
532 * @author Sucheta Dambalkar (Adopted code from jdis)
536 final int cpx1, cpx2;
538 CPX2(int cpx1, int cpx2) {
545 * Central data repository of the Java Disassembler. Stores all the
546 * information in java class file.
548 * @author Sucheta Dambalkar (Adopted code from jdis)
550 static final class ClassData {
553 private int minor_version;
554 private int major_version;
555 private int cpool_count;
556 private Object cpool[];
558 private int this_class = 0;
559 private int super_class;
560 private int interfaces_count;
561 private int[] interfaces = new int[0];
562 private FieldData[] fields;
563 private MethodData[] methods;
564 private InnerClassData[] innerClasses;
565 private BootMethodData[] bootMethods;
566 private int attributes_count;
567 private AttrData[] attrs;
568 private int source_cpx = 0;
570 private Hashtable indexHashAscii = new Hashtable();
571 private String pkgPrefix = "";
572 private int pkgPrefixLen = 0;
575 * Read classfile to disassemble.
577 public ClassData(InputStream infile) throws IOException {
578 this.read(new DataInputStream(infile));
582 * Reads and stores class file information.
584 public void read(DataInputStream in) throws IOException {
586 magic = in.readInt();
587 if (magic != JAVA_MAGIC) {
588 throw new ClassFormatError("wrong magic: "
589 + toHex(magic) + ", expected "
590 + toHex(JAVA_MAGIC));
592 minor_version = in.readShort();
593 major_version = in.readShort();
594 if (major_version != JAVA_VERSION) {
597 // Read the constant pool
599 access = in.readUnsignedShort();
600 this_class = in.readUnsignedShort();
601 super_class = in.readUnsignedShort();
604 interfaces_count = in.readUnsignedShort();
605 if (interfaces_count > 0) {
606 interfaces = new int[interfaces_count];
608 for (int i = 0; i < interfaces_count; i++) {
609 interfaces[i] = in.readShort();
618 // Read the attributes
619 attributes_count = in.readUnsignedShort();
620 attrs = new AttrData[attributes_count];
621 for (int k = 0; k < attributes_count; k++) {
622 int name_cpx = in.readUnsignedShort();
623 if (getTag(name_cpx) == CONSTANT_UTF8
624 && getString(name_cpx).equals("SourceFile")) {
625 if (in.readInt() != 2) {
626 throw new ClassFormatError("invalid attr length");
628 source_cpx = in.readUnsignedShort();
629 AttrData attr = new AttrData(this);
633 } else if (getTag(name_cpx) == CONSTANT_UTF8
634 && getString(name_cpx).equals("InnerClasses")) {
635 int length = in.readInt();
636 int num = in.readUnsignedShort();
637 if (2 + num * 8 != length) {
638 throw new ClassFormatError("invalid attr length");
640 innerClasses = new InnerClassData[num];
641 for (int j = 0; j < num; j++) {
642 InnerClassData innerClass = new InnerClassData(this);
644 innerClasses[j] = innerClass;
646 AttrData attr = new AttrData(this);
649 } else if (getTag(name_cpx) == CONSTANT_UTF8
650 && getString(name_cpx).equals("BootstrapMethods")) {
651 AttrData attr = new AttrData(this);
652 bootMethods = readBootstrapMethods(in);
656 AttrData attr = new AttrData(this);
657 attr.read(name_cpx, in);
662 } // end ClassData.read()
664 BootMethodData[] readBootstrapMethods(DataInputStream in) throws IOException {
665 int attr_len = in.readInt(); //attr_lengt
666 int number = in.readShort();
667 BootMethodData[] arr = new BootMethodData[number];
668 for (int i = 0; i < number; i++) {
669 int ref = in.readShort();
670 int len = in.readShort();
671 int[] args = new int[len];
672 for (int j = 0; j < len; j++) {
673 args[j] = in.readShort();
675 arr[i] = new BootMethodData(this, ref, args);
681 * Reads and stores constant pool info.
683 void readCP(DataInputStream in) throws IOException {
684 cpool_count = in.readUnsignedShort();
685 tags = new byte[cpool_count];
686 cpool = new Object[cpool_count];
687 for (int i = 1; i < cpool_count; i++) {
688 byte tag = in.readByte();
690 switch (tags[i] = tag) {
692 String str = in.readUTF();
693 indexHashAscii.put(cpool[i] = str, new Integer(i));
695 case CONSTANT_INTEGER:
696 cpool[i] = new Integer(in.readInt());
699 cpool[i] = new Float(in.readFloat());
702 cpool[i++] = new Long(in.readLong());
704 case CONSTANT_DOUBLE:
705 cpool[i++] = new Double(in.readDouble());
708 case CONSTANT_STRING:
709 cpool[i] = new CPX(in.readUnsignedShort());
713 case CONSTANT_METHOD:
714 case CONSTANT_INTERFACEMETHOD:
715 case CONSTANT_NAMEANDTYPE:
716 cpool[i] = new CPX2(in.readUnsignedShort(), in.readUnsignedShort());
718 case CONSTANT_METHODHANDLE:
719 cpool[i] = new CPX2(in.readByte(), in.readUnsignedShort());
721 case CONSTANT_METHODTYPE:
722 cpool[i] = new CPX(in.readUnsignedShort());
724 case CONSTANT_INVOKEDYNAMIC:
725 cpool[i] = new CPX2(in.readUnsignedShort(), in.readUnsignedShort());
729 throw new ClassFormatError("invalid constant type: " + (int) tags[i]);
735 * Reads and strores field info.
737 protected void readFields(DataInputStream in) throws IOException {
738 int fields_count = in.readUnsignedShort();
739 fields = new FieldData[fields_count];
740 for (int k = 0; k < fields_count; k++) {
741 FieldData field = new FieldData(this);
748 * Reads and strores Method info.
750 protected void readMethods(DataInputStream in) throws IOException {
751 int methods_count = in.readUnsignedShort();
752 methods = new MethodData[methods_count];
753 for (int k = 0; k < methods_count; k++) {
754 MethodData method = new MethodData(this);
763 public String getString(int n) {
767 return (String) cpool[n];
772 * get the type of constant given an index
774 public byte getTag(int n) {
777 } catch (ArrayIndexOutOfBoundsException e) {
781 static final String hexString = "0123456789ABCDEF";
782 public static char hexTable[] = hexString.toCharArray();
784 static String toHex(long val, int width) {
785 StringBuffer s = new StringBuffer();
786 for (int i = width - 1; i >= 0; i--) {
787 s.append(hexTable[((int) (val >> (4 * i))) & 0xF]);
789 return "0x" + s.toString();
792 static String toHex(long val) {
794 for (width = 16; width > 0; width--) {
795 if ((val >> (width - 1) * 4) != 0) {
799 return toHex(val, width);
802 static String toHex(int val) {
804 for (width = 8; width > 0; width--) {
805 if ((val >> (width - 1) * 4) != 0) {
809 return toHex(val, width);
813 * Returns the name of this class.
815 public String getClassName() {
817 if (this_class == 0) {
822 if (tags[this_class] != CONSTANT_CLASS) {
823 return res; //"<CP["+cpx+"] is not a Class> ";
825 tcpx = ((CPX) cpool[this_class]).cpx;
826 } catch (ArrayIndexOutOfBoundsException e) {
827 return res; // "#"+cpx+"// invalid constant pool index";
828 } catch (Throwable e) {
829 return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
833 return (String) (cpool[tcpx]);
834 } catch (ArrayIndexOutOfBoundsException e) {
835 return res; // "class #"+scpx+"// invalid constant pool index";
836 } catch (ClassCastException e) {
837 return res; // "class #"+scpx+"// invalid constant pool reference";
838 } catch (Throwable e) {
839 return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
845 * Returns the name of class at perticular index.
847 public String getClassName(int cpx) {
848 String res = "#" + cpx;
854 if (tags[cpx] != CONSTANT_CLASS) {
855 return res; //"<CP["+cpx+"] is not a Class> ";
857 scpx = ((CPX) cpool[cpx]).cpx;
858 } catch (ArrayIndexOutOfBoundsException e) {
859 return res; // "#"+cpx+"// invalid constant pool index";
860 } catch (Throwable e) {
861 return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
865 return (String) (cpool[scpx]);
866 } catch (ArrayIndexOutOfBoundsException e) {
867 return res; // "class #"+scpx+"// invalid constant pool index";
868 } catch (ClassCastException e) {
869 return res; // "class #"+scpx+"// invalid constant pool reference";
870 } catch (Throwable e) {
871 return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
875 public int getAccessFlags() {
880 * Returns true if it is a class
882 public boolean isClass() {
883 if ((access & ACC_INTERFACE) == 0) {
890 * Returns true if it is a interface.
892 public boolean isInterface() {
893 if ((access & ACC_INTERFACE) != 0) {
899 public boolean isAnnotation() {
900 return (access & ACC_ANNOTATION) != 0;
904 * Returns true if this member is public, false otherwise.
906 public boolean isPublic() {
907 return (access & ACC_PUBLIC) != 0;
911 * Returns the access of this class or interface.
913 public String[] getAccess() {
914 Vector v = new Vector();
915 if ((access & ACC_PUBLIC) != 0) {
916 v.addElement("public");
918 if ((access & ACC_FINAL) != 0) {
919 v.addElement("final");
921 if ((access & ACC_ABSTRACT) != 0) {
922 v.addElement("abstract");
924 String[] accflags = new String[v.size()];
925 v.copyInto(accflags);
930 * Returns list of innerclasses.
932 public InnerClassData[] getInnerClasses() {
937 * Returns list of attributes.
939 final AttrData[] getAttributes() {
943 public byte[] findAnnotationData(boolean classRetention) {
944 String n = classRetention
945 ? "RuntimeInvisibleAnnotations" : // NOI18N
946 "RuntimeVisibleAnnotations"; // NOI18N
947 return findAttr(n, attrs);
951 * Returns true if superbit is set.
953 public boolean isSuperSet() {
954 if ((access & ACC_SUPER) != 0) {
961 * Returns super class name.
963 public String getSuperClassName() {
965 if (super_class == 0) {
970 if (tags[super_class] != CONSTANT_CLASS) {
971 return res; //"<CP["+cpx+"] is not a Class> ";
973 scpx = ((CPX) cpool[super_class]).cpx;
974 } catch (ArrayIndexOutOfBoundsException e) {
975 return res; // "#"+cpx+"// invalid constant pool index";
976 } catch (Throwable e) {
977 return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
981 return (String) (cpool[scpx]);
982 } catch (ArrayIndexOutOfBoundsException e) {
983 return res; // "class #"+scpx+"// invalid constant pool index";
984 } catch (ClassCastException e) {
985 return res; // "class #"+scpx+"// invalid constant pool reference";
986 } catch (Throwable e) {
987 return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
992 * Returns list of super interfaces.
994 public String[] getSuperInterfaces() {
995 String interfacenames[] = new String[interfaces.length];
996 int interfacecpx = -1;
997 for (int i = 0; i < interfaces.length; i++) {
998 interfacecpx = ((CPX) cpool[interfaces[i]]).cpx;
999 interfacenames[i] = (String) (cpool[interfacecpx]);
1001 return interfacenames;
1005 * Returns string at prticular constant pool index.
1007 public String getStringValue(int cpoolx) {
1009 return ((String) cpool[cpoolx]);
1010 } catch (ArrayIndexOutOfBoundsException e) {
1011 return "//invalid constant pool index:" + cpoolx;
1012 } catch (ClassCastException e) {
1013 return "//invalid constant pool ref:" + cpoolx;
1018 * Returns list of field info.
1020 public FieldData[] getFields() {
1025 * Returns list of method info.
1027 public MethodData[] getMethods() {
1032 * Returns constant pool entry at that index.
1034 public CPX2 getCpoolEntry(int cpx) {
1035 return ((CPX2) (cpool[cpx]));
1038 public Object getCpoolEntryobj(int cpx) {
1039 return (cpool[cpx]);
1043 * Returns index of this class.
1045 public int getthis_cpx() {
1050 * Returns string at that index.
1052 public String StringValue(int cpx) {
1053 return stringValue(cpx, false);
1056 public String stringValue(int cpx, boolean textual) {
1057 return stringValue(cpx, textual, null);
1060 public String stringValue(int cpx, String[] classRefs) {
1061 return stringValue(cpx, true, classRefs);
1064 private String stringValue(int cpx, boolean textual, String[] refs) {
1074 } catch (IndexOutOfBoundsException e) {
1075 return "<Incorrect CP index:" + cpx + ">";
1082 case CONSTANT_UTF8: {
1086 StringBuilder sb = new StringBuilder();
1087 String s = (String) x;
1088 for (int k = 0; k < s.length(); k++) {
1089 char c = s.charAt(k);
1092 sb.append('\\').append('\\');
1095 sb.append('\\').append('t');
1098 sb.append('\\').append('n');
1101 sb.append('\\').append('r');
1104 sb.append('\\').append('\"');
1107 sb.append("\\u2028");
1110 sb.append("\\u2029");
1116 return sb.toString();
1118 case CONSTANT_DOUBLE: {
1119 Double d = (Double) x;
1120 String sd = d.toString();
1126 case CONSTANT_FLOAT: {
1127 Float f = (Float) x;
1128 String sf = (f).toString();
1134 case CONSTANT_LONG: {
1137 return ln.toString();
1139 return ln.toString() + 'l';
1141 case CONSTANT_INTEGER: {
1142 Integer in = (Integer) x;
1143 return in.toString();
1145 case CONSTANT_CLASS:
1146 String jn = getClassName(cpx);
1153 return javaName(jn);
1154 case CONSTANT_STRING:
1155 String sv = stringValue(((CPX) x).cpx, textual);
1157 return '"' + sv + '"';
1161 case CONSTANT_FIELD:
1162 case CONSTANT_METHOD:
1163 case CONSTANT_INTERFACEMETHOD:
1164 //return getShortClassName(((CPX2)x).cpx1)+"."+StringValue(((CPX2)x).cpx2);
1165 return javaName(getClassName(((CPX2) x).cpx1)) + "." + StringValue(((CPX2) x).cpx2);
1167 case CONSTANT_NAMEANDTYPE:
1168 return getName(((CPX2) x).cpx1) + ":" + StringValue(((CPX2) x).cpx2);
1169 case CONSTANT_METHODHANDLE:
1170 return "K" + ((CPX2)x).cpx1 + "@" + stringValue(((CPX2)x).cpx2, textual);
1171 case CONSTANT_METHODTYPE:
1172 return stringValue(((CPX)x).cpx, true);
1174 return "UnknownTag" + tag; //TBD
1179 * Returns resolved java type name.
1181 public String javaName(String name) {
1185 int len = name.length();
1193 for (int k = 0; k < len; k += Character.charCount(cp)) {
1194 cp = name.codePointAt(k);
1196 if (!isJavaIdentifierStart(cp)) {
1199 } else if (cp != '/') {
1200 if (!isJavaIdentifierPart(cp)) {
1208 return "\"" + name + "\"";
1211 public String getName(int cpx) {
1214 return javaName((String) cpool[cpx]); //.replace('/','.');
1215 } catch (ArrayIndexOutOfBoundsException e) {
1216 return "<invalid constant pool index:" + cpx + ">";
1217 } catch (ClassCastException e) {
1218 return "<invalid constant pool ref:" + cpx + ">";
1223 * Returns unqualified class name.
1225 public String getShortClassName(int cpx) {
1226 String classname = javaName(getClassName(cpx));
1227 pkgPrefixLen = classname.lastIndexOf("/") + 1;
1228 if (pkgPrefixLen != 0) {
1229 pkgPrefix = classname.substring(0, pkgPrefixLen);
1230 if (classname.startsWith(pkgPrefix)) {
1231 return classname.substring(pkgPrefixLen);
1238 * Returns source file name.
1240 public String getSourceName() {
1241 return getName(source_cpx);
1245 * Returns package name.
1247 public String getPkgName() {
1248 String classname = getClassName(this_class);
1249 pkgPrefixLen = classname.lastIndexOf("/") + 1;
1250 if (pkgPrefixLen != 0) {
1251 pkgPrefix = classname.substring(0, pkgPrefixLen);
1252 return /* ("package " + */ pkgPrefix.substring(0, pkgPrefixLen - 1) /* + ";\n") */;
1258 public BootMethodData getBootMethod(int indx) {
1259 return bootMethods != null ? bootMethods[indx] : null;
1263 * Returns total constant pool entry count.
1265 public int getCpoolCount() {
1270 * Returns minor version of class file.
1272 public int getMinor_version() {
1273 return minor_version;
1277 * Returns major version of class file.
1279 public int getMajor_version() {
1280 return major_version;
1283 private boolean isJavaIdentifierStart(int cp) {
1284 return ('a' <= cp && cp <= 'z') || ('A' <= cp && cp <= 'Z');
1287 private boolean isJavaIdentifierPart(int cp) {
1288 return isJavaIdentifierStart(cp) || ('0' <= cp && cp <= '9');
1291 public String[] getNameAndType(int indx) {
1292 return getNameAndType(indx, 0, new String[2]);
1295 private String[] getNameAndType(int indx, int at, String[] arr) {
1296 CPX2 c2 = getCpoolEntry(indx);
1297 arr[at] = StringValue(c2.cpx1);
1298 arr[at + 1] = StringValue(c2.cpx2);
1302 public String[] getFieldInfoName(int indx) {
1303 CPX2 c2 = getCpoolEntry(indx);
1304 String[] arr = new String[3];
1305 arr[0] = getClassName(c2.cpx1);
1306 return getNameAndType(c2.cpx2, 1, arr);
1309 public MethodData findMethod(String name, String signature) {
1310 for (MethodData md: methods) {
1311 if (md.getName().equals(name)
1312 && md.getInternalSig().equals(signature)) {
1321 public FieldData findField(String name, String signature) {
1322 for (FieldData fd: fields) {
1323 if (fd.getName().equals(name)
1324 && fd.getInternalSig().equals(signature)) {
1333 static byte[] findAttr(String n, AttrData[] attrs) {
1334 for (AttrData ad : attrs) {
1335 if (n.equals(ad.getAttrName())) {
1336 return ad.getData();
1344 * Strores field data informastion.
1346 * @author Sucheta Dambalkar (Adopted code from jdis)
1348 static class FieldData {
1353 int descriptor_index;
1354 int attributes_count;
1356 boolean isSynthetic = false;
1357 boolean isDeprecated = false;
1360 public FieldData(ClassData cls) {
1365 * Read and store field info.
1367 public void read(DataInputStream in) throws IOException {
1368 access = in.readUnsignedShort();
1369 name_index = in.readUnsignedShort();
1370 descriptor_index = in.readUnsignedShort();
1371 // Read the attributes
1372 int attributes_count = in.readUnsignedShort();
1373 attrs = new Vector(attributes_count);
1374 for (int i = 0; i < attributes_count; i++) {
1375 int attr_name_index = in.readUnsignedShort();
1376 if (cls.getTag(attr_name_index) != CONSTANT_UTF8) {
1379 String attr_name = cls.getString(attr_name_index);
1380 if (attr_name.equals("ConstantValue")) {
1381 if (in.readInt() != 2) {
1382 throw new ClassFormatError("invalid ConstantValue attr length");
1384 value_cpx = in.readUnsignedShort();
1385 AttrData attr = new AttrData(cls);
1386 attr.read(attr_name_index);
1387 attrs.addElement(attr);
1388 } else if (attr_name.equals("Synthetic")) {
1389 if (in.readInt() != 0) {
1390 throw new ClassFormatError("invalid Synthetic attr length");
1393 AttrData attr = new AttrData(cls);
1394 attr.read(attr_name_index);
1395 attrs.addElement(attr);
1396 } else if (attr_name.equals("Deprecated")) {
1397 if (in.readInt() != 0) {
1398 throw new ClassFormatError("invalid Synthetic attr length");
1400 isDeprecated = true;
1401 AttrData attr = new AttrData(cls);
1402 attr.read(attr_name_index);
1403 attrs.addElement(attr);
1405 AttrData attr = new AttrData(cls);
1406 attr.read(attr_name_index, in);
1407 attrs.addElement(attr);
1413 public boolean isStatic() {
1414 return (access & ACC_STATIC) != 0;
1418 * Returns access of a field.
1420 public String[] getAccess() {
1421 Vector v = new Vector();
1422 if ((access & ACC_PUBLIC) != 0) {
1423 v.addElement("public");
1425 if ((access & ACC_PRIVATE) != 0) {
1426 v.addElement("private");
1428 if ((access & ACC_PROTECTED) != 0) {
1429 v.addElement("protected");
1431 if ((access & ACC_STATIC) != 0) {
1432 v.addElement("static");
1434 if ((access & ACC_FINAL) != 0) {
1435 v.addElement("final");
1437 if ((access & ACC_VOLATILE) != 0) {
1438 v.addElement("volatile");
1440 if ((access & ACC_TRANSIENT) != 0) {
1441 v.addElement("transient");
1443 String[] accflags = new String[v.size()];
1444 v.copyInto(accflags);
1449 * Returns name of a field.
1451 public String getName() {
1452 return cls.getStringValue(name_index);
1456 * Returns internal signature of a field
1458 public String getInternalSig() {
1459 return cls.getStringValue(descriptor_index);
1463 * Returns true if field is synthetic.
1465 public boolean isSynthetic() {
1470 * Returns true if field is deprecated.
1472 public boolean isDeprecated() {
1473 return isDeprecated;
1476 public boolean hasConstantValue() {
1477 return value_cpx != -1;
1481 * Returns list of attributes of field.
1483 public Vector getAttributes() {
1487 public byte[] findAnnotationData(boolean classRetention) {
1488 String n = classRetention
1489 ? "RuntimeInvisibleAnnotations" : // NOI18N
1490 "RuntimeVisibleAnnotations"; // NOI18N
1491 AttrData[] arr = new AttrData[attrs.size()];
1492 attrs.copyInto(arr);
1493 return ClassData.findAttr(n, arr);
1498 * A JavaScript optimized replacement for Hashtable.
1500 * @author Jaroslav Tulach <jtulach@netbeans.org>
1502 private static final class Hashtable {
1504 private Object[] keys;
1505 private Object[] values;
1511 Hashtable(int i, double d) {
1518 synchronized void put(Object key, Object val) {
1519 int[] where = {-1, -1};
1520 Object found = get(key, where);
1521 if (where[0] != -1) {
1523 values[where[0]] = val;
1525 if (where[1] != -1) {
1527 keys[where[1]] = key;
1528 values[where[1]] = val;
1531 keys = new Object[11];
1532 values = new Object[11];
1536 Object[] newKeys = new Object[keys.length * 2];
1537 Object[] newValues = new Object[values.length * 2];
1538 for (int i = 0; i < keys.length; i++) {
1539 newKeys[i] = keys[i];
1540 newValues[i] = values[i];
1542 newKeys[keys.length] = key;
1543 newValues[keys.length] = val;
1551 Object get(Object key) {
1552 return get(key, null);
1555 private synchronized Object get(Object key, int[] foundAndNull) {
1559 for (int i = 0; i < keys.length; i++) {
1560 if (keys[i] == null) {
1561 if (foundAndNull != null) {
1562 foundAndNull[1] = i;
1564 } else if (keys[i].equals(key)) {
1565 if (foundAndNull != null) {
1566 foundAndNull[0] = i;
1576 * Strores InnerClass data informastion.
1578 * @author Sucheta Dambalkar (Adopted code from jdis)
1580 private static class InnerClassData {
1583 int inner_class_info_index, outer_class_info_index, inner_name_index, access;
1585 public InnerClassData(ClassData cls) {
1591 * Read Innerclass attribute data.
1593 public void read(DataInputStream in) throws IOException {
1594 inner_class_info_index = in.readUnsignedShort();
1595 outer_class_info_index = in.readUnsignedShort();
1596 inner_name_index = in.readUnsignedShort();
1597 access = in.readUnsignedShort();
1601 * Returns the access of this class or interface.
1603 public String[] getAccess() {
1604 Vector v = new Vector();
1605 if ((access & ACC_PUBLIC) != 0) {
1606 v.addElement("public");
1608 if ((access & ACC_FINAL) != 0) {
1609 v.addElement("final");
1611 if ((access & ACC_ABSTRACT) != 0) {
1612 v.addElement("abstract");
1614 String[] accflags = new String[v.size()];
1615 v.copyInto(accflags);
1618 } // end InnerClassData
1620 static class BootMethodData {
1621 private final ClassData clazz;
1623 private final int[] args;
1625 private BootMethodData(ClassData clazz, int method, int[] args) {
1627 this.method = method;
1632 public String toString() {
1633 StringBuilder sb = new StringBuilder();
1634 sb.append(clazz.stringValue(method, true));
1636 for (int indx : args) {
1638 sb.append(clazz.stringValue(indx, true));
1641 return sb.toString();
1646 * Strores LineNumberTable data information.
1648 * @author Sucheta Dambalkar (Adopted code from jdis)
1650 private static class LineNumData {
1652 short start_pc, line_number;
1654 public LineNumData() {
1658 * Read LineNumberTable attribute.
1660 public LineNumData(DataInputStream in) throws IOException {
1661 start_pc = in.readShort();
1662 line_number = in.readShort();
1668 * Strores LocalVariableTable data information.
1670 * @author Sucheta Dambalkar (Adopted code from jdis)
1672 private static class LocVarData {
1674 short start_pc, length, name_cpx, sig_cpx, slot;
1676 public LocVarData() {
1680 * Read LocalVariableTable attribute.
1682 public LocVarData(DataInputStream in) throws IOException {
1683 start_pc = in.readShort();
1684 length = in.readShort();
1685 name_cpx = in.readShort();
1686 sig_cpx = in.readShort();
1687 slot = in.readShort();
1692 * Strores method data informastion.
1694 * @author Sucheta Dambalkar (Adopted code from jdis)
1696 static class MethodData {
1701 int descriptor_index;
1702 int attributes_count;
1704 Vector exception_table = new Vector(0);
1705 Vector lin_num_tb = new Vector(0);
1706 Vector loc_var_tb = new Vector(0);
1707 StackMapTableData[] stackMapTable;
1708 StackMapData[] stackMap;
1709 int[] exc_index_table = null;
1710 Vector attrs = new Vector(0);
1711 Vector code_attrs = new Vector(0);
1712 int max_stack, max_locals;
1713 boolean isSynthetic = false;
1714 boolean isDeprecated = false;
1715 private AttrData annotationDefault;
1717 public MethodData(ClassData cls) {
1724 public void read(DataInputStream in) throws IOException {
1725 access = in.readUnsignedShort();
1726 name_index = in.readUnsignedShort();
1727 descriptor_index = in.readUnsignedShort();
1728 int attributes_count = in.readUnsignedShort();
1729 for (int i = 0; i < attributes_count; i++) {
1730 int attr_name_index = in.readUnsignedShort();
1734 if (cls.getTag(attr_name_index) == CONSTANT_UTF8) {
1735 String attr_name = cls.getString(attr_name_index);
1736 if (attr_name.equals("Code")) {
1738 AttrData attr = new AttrData(cls);
1739 attr.read(attr_name_index);
1740 attrs.addElement(attr);
1742 } else if (attr_name.equals("Exceptions")) {
1744 AttrData attr = new AttrData(cls);
1745 attr.read(attr_name_index);
1746 attrs.addElement(attr);
1748 } else if (attr_name.equals("Synthetic")) {
1749 if (in.readInt() != 0) {
1750 throw new ClassFormatError("invalid Synthetic attr length");
1753 AttrData attr = new AttrData(cls);
1754 attr.read(attr_name_index);
1755 attrs.addElement(attr);
1757 } else if (attr_name.equals("Deprecated")) {
1758 if (in.readInt() != 0) {
1759 throw new ClassFormatError("invalid Synthetic attr length");
1761 isDeprecated = true;
1762 AttrData attr = new AttrData(cls);
1763 attr.read(attr_name_index);
1764 attrs.addElement(attr);
1766 } else if (attr_name.equals("AnnotationDefault")) {
1767 AttrData attr = new AttrData(cls);
1768 attr.read(attr_name_index, in);
1769 attrs.addElement(attr);
1770 annotationDefault = attr;
1774 AttrData attr = new AttrData(cls);
1775 attr.read(attr_name_index, in);
1776 attrs.addElement(attr);
1782 * Read code attribute info.
1784 public void readCode(DataInputStream in) throws IOException {
1786 int attr_length = in.readInt();
1787 max_stack = in.readUnsignedShort();
1788 max_locals = in.readUnsignedShort();
1789 int codelen = in.readInt();
1791 code = new byte[codelen];
1793 while (totalread < codelen) {
1794 totalread += in.read(code, totalread, codelen - totalread);
1796 // in.read(code, 0, codelen);
1798 readExceptionTable(in);
1799 int code_attributes_count = in.readUnsignedShort();
1801 for (int k = 0; k < code_attributes_count; k++) {
1802 int table_name_index = in.readUnsignedShort();
1803 int table_name_tag = cls.getTag(table_name_index);
1804 AttrData attr = new AttrData(cls);
1805 if (table_name_tag == CONSTANT_UTF8) {
1806 String table_name_tstr = cls.getString(table_name_index);
1807 if (table_name_tstr.equals("LineNumberTable")) {
1808 readLineNumTable(in);
1809 attr.read(table_name_index);
1810 } else if (table_name_tstr.equals("LocalVariableTable")) {
1811 readLocVarTable(in);
1812 attr.read(table_name_index);
1813 } else if (table_name_tstr.equals("StackMapTable")) {
1814 readStackMapTable(in);
1815 attr.read(table_name_index);
1816 } else if (table_name_tstr.equals("StackMap")) {
1818 attr.read(table_name_index);
1820 attr.read(table_name_index, in);
1822 code_attrs.addElement(attr);
1826 attr.read(table_name_index, in);
1827 code_attrs.addElement(attr);
1832 * Read exception table info.
1834 void readExceptionTable(DataInputStream in) throws IOException {
1835 int exception_table_len = in.readUnsignedShort();
1836 exception_table = new Vector(exception_table_len);
1837 for (int l = 0; l < exception_table_len; l++) {
1838 exception_table.addElement(new TrapData(in, l));
1843 * Read LineNumberTable attribute info.
1845 void readLineNumTable(DataInputStream in) throws IOException {
1846 int attr_len = in.readInt(); // attr_length
1847 int lin_num_tb_len = in.readUnsignedShort();
1848 lin_num_tb = new Vector(lin_num_tb_len);
1849 for (int l = 0; l < lin_num_tb_len; l++) {
1850 lin_num_tb.addElement(new LineNumData(in));
1855 * Read LocalVariableTable attribute info.
1857 void readLocVarTable(DataInputStream in) throws IOException {
1858 int attr_len = in.readInt(); // attr_length
1859 int loc_var_tb_len = in.readUnsignedShort();
1860 loc_var_tb = new Vector(loc_var_tb_len);
1861 for (int l = 0; l < loc_var_tb_len; l++) {
1862 loc_var_tb.addElement(new LocVarData(in));
1867 * Read Exception attribute info.
1869 public void readExceptions(DataInputStream in) throws IOException {
1870 int attr_len = in.readInt(); // attr_length in prog
1871 int num_exceptions = in.readUnsignedShort();
1872 exc_index_table = new int[num_exceptions];
1873 for (int l = 0; l < num_exceptions; l++) {
1874 int exc = in.readShort();
1875 exc_index_table[l] = exc;
1880 * Read StackMapTable attribute info.
1882 void readStackMapTable(DataInputStream in) throws IOException {
1883 int attr_len = in.readInt(); //attr_length
1884 int stack_map_tb_len = in.readUnsignedShort();
1885 stackMapTable = new StackMapTableData[stack_map_tb_len];
1886 for (int i = 0; i < stack_map_tb_len; i++) {
1887 stackMapTable[i] = StackMapTableData.getInstance(in, this);
1892 * Read StackMap attribute info.
1894 void readStackMap(DataInputStream in) throws IOException {
1895 int attr_len = in.readInt(); //attr_length
1896 int stack_map_len = in.readUnsignedShort();
1897 stackMap = new StackMapData[stack_map_len];
1898 for (int i = 0; i < stack_map_len; i++) {
1899 stackMap[i] = new StackMapData(in, this);
1904 * Return access of the method.
1906 public int getAccess() {
1911 * Return name of the method.
1913 public String getName() {
1914 return cls.getStringValue(name_index);
1918 * Return internal siganature of the method.
1920 public String getInternalSig() {
1921 return cls.getStringValue(descriptor_index);
1925 * Return code attribute data of a method.
1927 public byte[] getCode() {
1932 * Return LineNumberTable size.
1934 public int getnumlines() {
1935 return lin_num_tb.size();
1939 * Return LineNumberTable
1941 public Vector getlin_num_tb() {
1946 * Return LocalVariableTable size.
1948 public int getloc_var_tbsize() {
1949 return loc_var_tb.size();
1953 * Return LocalVariableTable.
1955 public Vector getloc_var_tb() {
1962 public StackMapData[] getStackMap() {
1967 * Return StackMapTable.
1969 public StackMapTableData[] getStackMapTable() {
1970 return stackMapTable;
1973 public StackMapIterator createStackMapIterator() {
1974 return new StackMapIterator(this);
1978 * Return true if method is static
1980 public boolean isStatic() {
1981 if ((access & ACC_STATIC) != 0) {
1988 * Return max depth of operand stack.
1990 public int getMaxStack() {
1995 * Return number of local variables.
1997 public int getMaxLocals() {
2002 * Return exception index table in Exception attribute.
2004 public int[] get_exc_index_table() {
2005 return exc_index_table;
2009 * Return exception table in code attributre.
2011 public TrapDataIterator getTrapDataIterator() {
2012 return new TrapDataIterator(exception_table);
2016 * Return method attributes.
2018 public Vector getAttributes() {
2023 * Return code attributes.
2025 public Vector getCodeAttributes() {
2029 byte[] getDefaultAttribute() {
2030 return annotationDefault == null ? null : annotationDefault.getData();
2034 * Return true if method id synthetic.
2036 public boolean isSynthetic() {
2041 * Return true if method is deprecated.
2043 public boolean isDeprecated() {
2044 return isDeprecated;
2047 public byte[] findAnnotationData(boolean classRetention) {
2048 String n = classRetention
2049 ? "RuntimeInvisibleAnnotations" : // NOI18N
2050 "RuntimeVisibleAnnotations"; // NOI18N
2051 AttrData[] arr = new AttrData[attrs.size()];
2052 attrs.copyInto(arr);
2053 return ClassData.findAttr(n, arr);
2056 public boolean isConstructor() {
2057 return "<init>".equals(getName());
2061 /* represents one entry of StackMap attribute
2063 private static class StackMapData {
2069 StackMapData(int offset, int[] locals, int[] stack) {
2070 this.offset = offset;
2071 this.locals = locals;
2075 StackMapData(DataInputStream in, MethodData method) throws IOException {
2076 offset = in.readUnsignedShort();
2077 int local_size = in.readUnsignedShort();
2078 locals = readTypeArray(in, local_size, method);
2079 int stack_size = in.readUnsignedShort();
2080 stack = readTypeArray(in, stack_size, method);
2083 static final int[] readTypeArray(DataInputStream in, int length, MethodData method) throws IOException {
2084 int[] types = new int[length];
2085 for (int i = 0; i < length; i++) {
2086 types[i] = readType(in, method);
2091 static final int readType(DataInputStream in, MethodData method) throws IOException {
2092 int type = in.readUnsignedByte();
2093 if (type == ITEM_Object || type == ITEM_NewObject) {
2094 type = type | (in.readUnsignedShort() << 8);
2100 static final class StackMapIterator {
2102 private final StackMapTableData[] stackMapTable;
2103 private final TypeArray argTypes;
2104 private final TypeArray localTypes;
2105 private final TypeArray stackTypes;
2106 private int nextFrameIndex;
2107 private int lastFrameByteCodeOffset;
2108 private int byteCodeOffset;
2110 StackMapIterator(final MethodData methodData) {
2111 this(methodData.getStackMapTable(),
2112 methodData.getInternalSig(),
2113 methodData.isStatic());
2116 StackMapIterator(final StackMapTableData[] stackMapTable,
2117 final String methodSignature,
2118 final boolean isStaticMethod) {
2119 this.stackMapTable = (stackMapTable != null)
2121 : new StackMapTableData[0];
2123 argTypes = getArgTypes(methodSignature, isStaticMethod);
2124 localTypes = new TypeArray();
2125 stackTypes = new TypeArray();
2127 localTypes.addAll(argTypes);
2129 lastFrameByteCodeOffset = -1;
2133 public boolean isEmpty() {
2134 return stackMapTable.length == 0;
2137 public String getFrameAsString() {
2138 return (nextFrameIndex == 0)
2139 ? StackMapTableData.toString("INITIAL", 0, null, null)
2140 : stackMapTable[nextFrameIndex - 1].toString();
2143 public int getFrameIndex() {
2144 return nextFrameIndex;
2147 public TypeArray getFrameStack() {
2151 public TypeArray getFrameLocals() {
2155 public TypeArray getArguments() {
2159 public void advanceBy(final int numByteCodes) {
2160 if (numByteCodes < 0) {
2161 throw new IllegalStateException("Forward only iterator");
2164 byteCodeOffset += numByteCodes;
2165 while ((nextFrameIndex < stackMapTable.length)
2166 && ((byteCodeOffset - lastFrameByteCodeOffset)
2167 >= (stackMapTable[nextFrameIndex].offsetDelta
2169 final StackMapTableData nextFrame = stackMapTable[nextFrameIndex];
2171 lastFrameByteCodeOffset += nextFrame.offsetDelta + 1;
2172 nextFrame.applyTo(localTypes, stackTypes);
2178 public void advanceTo(final int nextByteCodeOffset) {
2179 advanceBy(nextByteCodeOffset - byteCodeOffset);
2182 private static TypeArray getArgTypes(final String methodSignature,
2183 final boolean isStaticMethod) {
2184 final TypeArray argTypes = new TypeArray();
2186 if (!isStaticMethod) {
2187 argTypes.add(ITEM_Object);
2190 if (methodSignature.charAt(0) != '(') {
2191 throw new IllegalArgumentException("Invalid method signature");
2194 final int length = methodSignature.length();
2195 boolean skipType = false;
2197 for (int i = 1; i < length; ++i) {
2198 switch (methodSignature.charAt(i)) {
2204 argType = ITEM_Integer;
2207 argType = ITEM_Long;
2210 argType = ITEM_Float;
2213 argType = ITEM_Double;
2216 i = methodSignature.indexOf(';', i + 1);
2218 throw new IllegalArgumentException(
2219 "Invalid method signature");
2221 argType = ITEM_Object;
2225 // not interested in the return value type
2229 argTypes.add(ITEM_Object);
2235 throw new IllegalArgumentException(
2236 "Invalid method signature");
2240 argTypes.add(argType);
2249 /* represents one entry of StackMapTable attribute
2252 private static abstract class StackMapTableData {
2254 final int frameType;
2257 StackMapTableData(int frameType) {
2258 this.frameType = frameType;
2261 abstract void applyTo(TypeArray localTypes, TypeArray stackTypes);
2263 protected static String toString(
2264 final String frameType,
2266 final int[] localTypes,
2267 final int[] stackTypes) {
2268 final StringBuilder sb = new StringBuilder(frameType);
2270 sb.append("(off: +").append(offset);
2271 if (localTypes != null) {
2272 sb.append(", locals: ");
2273 appendTypes(sb, localTypes);
2275 if (stackTypes != null) {
2276 sb.append(", stack: ");
2277 appendTypes(sb, stackTypes);
2281 return sb.toString();
2284 private static void appendTypes(final StringBuilder sb, final int[] types) {
2286 if (types.length > 0) {
2287 sb.append(TypeArray.typeString(types[0]));
2288 for (int i = 1; i < types.length; ++i) {
2290 sb.append(TypeArray.typeString(types[i]));
2296 private static class SameFrame extends StackMapTableData {
2298 SameFrame(int frameType, int offsetDelta) {
2300 this.offsetDelta = offsetDelta;
2304 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
2309 public String toString() {
2310 return toString("SAME" + ((frameType == SAME_FRAME_EXTENDED)
2311 ? "_FRAME_EXTENDED" : ""),
2317 private static class SameLocals1StackItem extends StackMapTableData {
2321 SameLocals1StackItem(int frameType, int offsetDelta, int[] stack) {
2323 this.offsetDelta = offsetDelta;
2328 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
2329 stackTypes.setAll(stack);
2333 public String toString() {
2335 "SAME_LOCALS_1_STACK_ITEM"
2336 + ((frameType == SAME_LOCALS_1_STACK_ITEM_EXTENDED)
2337 ? "_EXTENDED" : ""),
2343 private static class ChopFrame extends StackMapTableData {
2345 ChopFrame(int frameType, int offsetDelta) {
2347 this.offsetDelta = offsetDelta;
2351 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
2352 localTypes.setSize(localTypes.getSize()
2353 - (SAME_FRAME_EXTENDED - frameType));
2358 public String toString() {
2359 return toString("CHOP", offsetDelta, null, null);
2363 private static class AppendFrame extends StackMapTableData {
2367 AppendFrame(int frameType, int offsetDelta, int[] locals) {
2369 this.offsetDelta = offsetDelta;
2370 this.locals = locals;
2374 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
2375 localTypes.addAll(locals);
2380 public String toString() {
2381 return toString("APPEND", offsetDelta, locals, null);
2385 private static class FullFrame extends StackMapTableData {
2390 FullFrame(int offsetDelta, int[] locals, int[] stack) {
2392 this.offsetDelta = offsetDelta;
2393 this.locals = locals;
2398 void applyTo(TypeArray localTypes, TypeArray stackTypes) {
2399 localTypes.setAll(locals);
2400 stackTypes.setAll(stack);
2404 public String toString() {
2405 return toString("FULL", offsetDelta, locals, stack);
2409 static StackMapTableData getInstance(DataInputStream in, MethodData method)
2410 throws IOException {
2411 int frameType = in.readUnsignedByte();
2413 if (frameType < SAME_FRAME_BOUND) {
2415 return new SameFrame(frameType, frameType);
2416 } else if (SAME_FRAME_BOUND <= frameType && frameType < SAME_LOCALS_1_STACK_ITEM_BOUND) {
2417 // same_locals_1_stack_item_frame
2418 // read additional single stack element
2419 return new SameLocals1StackItem(frameType,
2420 (frameType - SAME_FRAME_BOUND),
2421 StackMapData.readTypeArray(in, 1, method));
2422 } else if (frameType == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
2423 // same_locals_1_stack_item_extended
2424 return new SameLocals1StackItem(frameType,
2425 in.readUnsignedShort(),
2426 StackMapData.readTypeArray(in, 1, method));
2427 } else if (SAME_LOCALS_1_STACK_ITEM_EXTENDED < frameType && frameType < SAME_FRAME_EXTENDED) {
2428 // chop_frame or same_frame_extended
2429 return new ChopFrame(frameType, in.readUnsignedShort());
2430 } else if (frameType == SAME_FRAME_EXTENDED) {
2431 // chop_frame or same_frame_extended
2432 return new SameFrame(frameType, in.readUnsignedShort());
2433 } else if (SAME_FRAME_EXTENDED < frameType && frameType < FULL_FRAME) {
2435 return new AppendFrame(frameType, in.readUnsignedShort(),
2436 StackMapData.readTypeArray(in, frameType - SAME_FRAME_EXTENDED, method));
2437 } else if (frameType == FULL_FRAME) {
2439 int offsetDelta = in.readUnsignedShort();
2440 int locals_size = in.readUnsignedShort();
2441 int[] locals = StackMapData.readTypeArray(in, locals_size, method);
2442 int stack_size = in.readUnsignedShort();
2443 int[] stack = StackMapData.readTypeArray(in, stack_size, method);
2444 return new FullFrame(offsetDelta, locals, stack);
2446 throw new ClassFormatError("unrecognized frame_type in StackMapTable");
2452 * Stores exception table data in code attribute.
2454 * @author Sucheta Dambalkar (Adopted code from jdis)
2456 static final class TrapData {
2458 public final short start_pc;
2459 public final short end_pc;
2460 public final short handler_pc;
2461 public final short catch_cpx;
2465 * Read and store exception table data in code attribute.
2467 TrapData(DataInputStream in, int num) throws IOException {
2469 start_pc = in.readShort();
2470 end_pc = in.readShort();
2471 handler_pc = in.readShort();
2472 catch_cpx = in.readShort();
2476 * returns recommended identifier
2478 public String ident() {
2484 * @author Jaroslav Tulach <jtulach@netbeans.org>
2486 static final class TrapDataIterator {
2488 private final Hashtable exStart = new Hashtable();
2489 private final Hashtable exStop = new Hashtable();
2490 private TrapData[] current = new TrapData[10];
2491 private int currentCount;
2493 TrapDataIterator(Vector exceptionTable) {
2494 for (int i = 0; i < exceptionTable.size(); i++) {
2495 final TrapData td = (TrapData) exceptionTable.elementAt(i);
2496 put(exStart, td.start_pc, td);
2497 put(exStop, td.end_pc, td);
2501 private static void put(Hashtable h, short key, TrapData td) {
2502 Short s = Short.valueOf((short) key);
2503 Vector v = (Vector) h.get(s);
2511 private boolean processAll(Hashtable h, Short key, boolean add) {
2512 boolean change = false;
2513 Vector v = (Vector) h.get(key);
2516 for (int i = 0; i < s; i++) {
2517 TrapData td = (TrapData) v.elementAt(i);
2530 public boolean advanceTo(int i) {
2531 Short s = Short.valueOf((short) i);
2532 boolean ch1 = processAll(exStart, s, true);
2533 boolean ch2 = processAll(exStop, s, false);
2537 public boolean useTry() {
2538 return currentCount > 0;
2541 public TrapData[] current() {
2542 TrapData[] copy = new TrapData[currentCount];
2543 for (int i = 0; i < currentCount; i++) {
2544 copy[i] = current[i];
2549 private void add(TrapData e) {
2550 if (currentCount == current.length) {
2551 TrapData[] data = new TrapData[currentCount * 2];
2552 for (int i = 0; i < currentCount; i++) {
2553 data[i] = current[i];
2557 current[currentCount++] = e;
2560 private void remove(TrapData e) {
2561 if (currentCount == 0) {
2565 while (from < currentCount) {
2566 if (e == current[from++]) {
2570 while (from < currentCount) {
2571 current[from - 1] = current[from];
2572 current[from] = null;
2578 static final class TypeArray {
2580 private static final int CAPACITY_INCREMENT = 16;
2581 private int[] types;
2584 public TypeArray() {
2587 public TypeArray(final TypeArray initialTypes) {
2588 setAll(initialTypes);
2591 public void add(final int newType) {
2592 ensureCapacity(size + 1);
2593 types[size++] = newType;
2596 public void addAll(final TypeArray newTypes) {
2597 addAll(newTypes.types, 0, newTypes.size);
2600 public void addAll(final int[] newTypes) {
2601 addAll(newTypes, 0, newTypes.length);
2604 public void addAll(final int[] newTypes,
2608 ensureCapacity(size + count);
2609 arraycopy(newTypes, offset, types, size, count);
2614 public void set(final int index, final int newType) {
2615 types[index] = newType;
2618 public void setAll(final TypeArray newTypes) {
2619 setAll(newTypes.types, 0, newTypes.size);
2622 public void setAll(final int[] newTypes) {
2623 setAll(newTypes, 0, newTypes.length);
2626 public void setAll(final int[] newTypes,
2630 ensureCapacity(count);
2631 arraycopy(newTypes, offset, types, 0, count);
2638 public void setSize(final int newSize) {
2639 if (size != newSize) {
2640 ensureCapacity(newSize);
2642 for (int i = size; i < newSize; ++i) {
2649 public void clear() {
2653 public int getSize() {
2657 public int get(final int index) {
2658 return types[index];
2661 public static String typeString(final int type) {
2662 switch (type & 0xff) {
2675 case ITEM_InitObject: // UninitializedThis
2679 case ITEM_NewObject: // Uninitialized
2682 throw new IllegalArgumentException("Unknown type");
2687 public String toString() {
2688 final StringBuilder sb = new StringBuilder("[");
2690 for (int i = 0; i < size; ++i) {
2691 sb.append(sep).append(VarType.toString(types[i] & 0xff));
2694 return sb.append(']').toString();
2697 private void ensureCapacity(final int minCapacity) {
2698 if ((minCapacity == 0)
2699 || (types != null) && (minCapacity <= types.length)) {
2703 final int newCapacity =
2704 ((minCapacity + CAPACITY_INCREMENT - 1) / CAPACITY_INCREMENT)
2705 * CAPACITY_INCREMENT;
2706 final int[] newTypes = new int[newCapacity];
2709 arraycopy(types, 0, newTypes, 0, size);
2715 // no System.arraycopy
2716 private void arraycopy(final int[] src, final int srcPos,
2717 final int[] dest, final int destPos,
2719 for (int i = 0; i < length; ++i) {
2720 dest[destPos + i] = src[srcPos + i];
2725 * A JavaScript ready replacement for java.util.Vector
2727 * @author Jaroslav Tulach <jtulach@netbeans.org>
2729 @JavaScriptPrototype(prototype = "new Array")
2730 private static final class Vector {
2732 private Object[] arr;
2740 void add(Object objectType) {
2741 addElement(objectType);
2744 @JavaScriptBody(args = {"obj"}, body =
2746 void addElement(Object obj) {
2747 final int s = size();
2749 setElementAt(obj, s);
2752 @JavaScriptBody(args = {}, body =
2753 "return this.length;")
2755 return arr == null ? 0 : arr.length;
2758 @JavaScriptBody(args = {"newArr"}, body =
2759 "for (var i = 0; i < this.length; i++) {\n"
2760 + " newArr[i] = this[i];\n"
2762 void copyInto(Object[] newArr) {
2766 int min = Math.min(newArr.length, arr.length);
2767 for (int i = 0; i < min; i++) {
2772 @JavaScriptBody(args = {"index"}, body =
2773 "return this[index];")
2774 Object elementAt(int index) {
2778 private void setSize(int len) {
2779 Object[] newArr = new Object[len];
2784 @JavaScriptBody(args = {"val", "index"}, body =
2785 "this[index] = val;")
2786 void setElementAt(Object val, int index) {