rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeParser.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 19 Mar 2016 10:31:13 +0100
changeset 1889 e1953d8b8338
parent 1835 9581e8765176
child 1898 cf6d5d357696
permissions -rw-r--r--
Support for default attributes of annotations
     1 /*
     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.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  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.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    25 package org.apidesign.vm4brwsr;
    26 
    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;
    33 
    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.
    36  *
    37  * @author Jaroslav Tulach <jtulach@netbeans.org>
    38  */
    39 final class ByteCodeParser {
    40     private ByteCodeParser() {
    41     }
    42 
    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;
    47 
    48     /* Constant table */
    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;
    64 
    65     /* Access Flags */
    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;
    82 
    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"
    93 
    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;
   100 
   101     /* Opcodes */
   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;
   322 
   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;
   336 */
   337     static class AnnotationParser {
   338 
   339         private final boolean textual;
   340         private final boolean iterateArray;
   341 
   342         protected AnnotationParser(boolean textual, boolean iterateArray) {
   343             this.textual = textual;
   344             this.iterateArray = iterateArray;
   345         }
   346 
   347         protected void visitAnnotationStart(String type, boolean top) throws IOException {
   348         }
   349 
   350         protected void visitAnnotationEnd(String type, boolean top) throws IOException {
   351         }
   352 
   353         protected void visitValueStart(String attrName, char type) throws IOException {
   354         }
   355 
   356         protected void visitValueEnd(String attrName, char type) throws IOException {
   357         }
   358 
   359         protected void visitAttr(
   360             String annoType, String attr, String attrType, String value
   361         ) throws IOException {
   362         }
   363 
   364         protected void visitEnumAttr(
   365             String annoType, String attr, String attrType, String value
   366         ) throws IOException {
   367             visitAttr(annoType, attr, attrType, value);
   368         }
   369 
   370         protected void visitClassAttr(
   371             String annoType, String attr, String className
   372         ) throws IOException {
   373             visitAttr(annoType, attr, className, className);
   374         }
   375 
   376         /**
   377          * Initialize the parsing with constant pool from
   378          * <code>cd</code>.
   379          *
   380          * @param attr the attribute defining annotations
   381          * @param cd constant pool
   382          * @throws IOException in case I/O fails
   383          */
   384         public final void parse(byte[] attr, ClassData cd) throws IOException {
   385             ByteArrayInputStream is = new ByteArrayInputStream(attr);
   386             DataInputStream dis = new DataInputStream(is);
   387             try {
   388                 read(dis, cd);
   389             } finally {
   390                 is.close();
   391             }
   392         }
   393 
   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);
   398             }
   399         }
   400 
   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);
   409             }
   410             visitAnnotationEnd(typeName, top);
   411             if (cnt == 0) {
   412                 visitAttr(typeName, null, null, null);
   413             }
   414         }
   415 
   416         public void parseDefault(byte[] defaultAttribute, ClassData cd) throws IOException {
   417             ByteArrayInputStream is = new ByteArrayInputStream(defaultAttribute);
   418             DataInputStream dis = new DataInputStream(is);
   419             try {
   420                 readValue(dis, cd, null, null);
   421             } finally {
   422                 is.close();
   423             }
   424         }
   425 
   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);
   430             if (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);
   435                 String attrType;
   436                 if (type == 's') {
   437                     attrType = "Ljava_lang_String_2";
   438                     if (textual) {
   439                         val = '"' + val + '"';
   440                     }
   441                 } else {
   442                     attrType = "" + type;
   443                 }
   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);
   453                 }
   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);
   460             } else {
   461                 throw new IOException("Unknown type " + type);
   462             }
   463             visitValueEnd(attrName, type);
   464         }
   465     }
   466     
   467     /**
   468      * Reads and stores attribute information.
   469      *
   470      * @author Sucheta Dambalkar (Adopted code from jdis)
   471      */
   472     private static class AttrData {
   473 
   474         ClassData cls;
   475         int name_cpx;
   476         int datalen;
   477         byte data[];
   478 
   479         public AttrData(ClassData cls) {
   480             this.cls = cls;
   481         }
   482 
   483         /**
   484          * Reads unknown attribute.
   485          */
   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];
   490             in.readFully(data);
   491         }
   492 
   493         /**
   494          * Reads just the name of known attribute.
   495          */
   496         public void read(int name_cpx) {
   497             this.name_cpx = name_cpx;
   498         }
   499 
   500         /**
   501          * Returns attribute name.
   502          */
   503         public String getAttrName() {
   504             return cls.getString(name_cpx);
   505         }
   506 
   507         /**
   508          * Returns attribute data.
   509          */
   510         public byte[] getData() {
   511             return data;
   512         }
   513     }
   514 
   515     /**
   516      * Stores constant pool entry information with one field.
   517      *
   518      * @author Sucheta Dambalkar (Adopted code from jdis)
   519      */
   520     private static class CPX {
   521 
   522         final int cpx;
   523 
   524         CPX(int cpx) {
   525             this.cpx = cpx;
   526         }
   527     }
   528 
   529     /**
   530      * Stores constant pool entry information with two fields.
   531      *
   532      * @author Sucheta Dambalkar (Adopted code from jdis)
   533      */
   534     static class CPX2 {
   535 
   536         final int cpx1, cpx2;
   537 
   538         CPX2(int cpx1, int cpx2) {
   539             this.cpx1 = cpx1;
   540             this.cpx2 = cpx2;
   541         }
   542     }
   543 
   544     /**
   545      * Central data repository of the Java Disassembler. Stores all the
   546      * information in java class file.
   547      *
   548      * @author Sucheta Dambalkar (Adopted code from jdis)
   549      */
   550     static final class ClassData {
   551 
   552         private int magic;
   553         private int minor_version;
   554         private int major_version;
   555         private int cpool_count;
   556         private Object cpool[];
   557         private int access;
   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;
   569         private byte tags[];
   570         private Hashtable indexHashAscii = new Hashtable();
   571         private String pkgPrefix = "";
   572         private int pkgPrefixLen = 0;
   573 
   574         /**
   575          * Read classfile to disassemble.
   576          */
   577         public ClassData(InputStream infile) throws IOException {
   578             this.read(new DataInputStream(infile));
   579         }
   580 
   581         /**
   582          * Reads and stores class file information.
   583          */
   584         public void read(DataInputStream in) throws IOException {
   585             // Read the header
   586             magic = in.readInt();
   587             if (magic != JAVA_MAGIC) {
   588                 throw new ClassFormatError("wrong magic: "
   589                     + toHex(magic) + ", expected "
   590                     + toHex(JAVA_MAGIC));
   591             }
   592             minor_version = in.readShort();
   593             major_version = in.readShort();
   594             if (major_version != JAVA_VERSION) {
   595             }
   596 
   597             // Read the constant pool
   598             readCP(in);
   599             access = in.readUnsignedShort();
   600             this_class = in.readUnsignedShort();
   601             super_class = in.readUnsignedShort();
   602 
   603             //Read interfaces.
   604             interfaces_count = in.readUnsignedShort();
   605             if (interfaces_count > 0) {
   606                 interfaces = new int[interfaces_count];
   607             }
   608             for (int i = 0; i < interfaces_count; i++) {
   609                 interfaces[i] = in.readShort();
   610             }
   611 
   612             // Read the fields
   613             readFields(in);
   614 
   615             // Read the methods
   616             readMethods(in);
   617 
   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");
   627                     }
   628                     source_cpx = in.readUnsignedShort();
   629                     AttrData attr = new AttrData(this);
   630                     attr.read(name_cpx);
   631                     attrs[k] = attr;
   632 
   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");
   639                     }
   640                     innerClasses = new InnerClassData[num];
   641                     for (int j = 0; j < num; j++) {
   642                         InnerClassData innerClass = new InnerClassData(this);
   643                         innerClass.read(in);
   644                         innerClasses[j] = innerClass;
   645                     }
   646                     AttrData attr = new AttrData(this);
   647                     attr.read(name_cpx);
   648                     attrs[k] = attr;
   649                 } else if (getTag(name_cpx) == CONSTANT_UTF8
   650                     && getString(name_cpx).equals("BootstrapMethods")) {
   651                     AttrData attr = new AttrData(this);
   652                     bootMethods = readBootstrapMethods(in);
   653                     attr.read(name_cpx);
   654                     attrs[k] = attr;
   655                 } else {
   656                     AttrData attr = new AttrData(this);
   657                     attr.read(name_cpx, in);
   658                     attrs[k] = attr;
   659                 }
   660             }
   661             in.close();
   662         } // end ClassData.read()
   663 
   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();
   674                 }
   675                 arr[i] = new BootMethodData(this, ref, args);
   676             }
   677             return arr;
   678         }
   679         
   680         /**
   681          * Reads and stores constant pool info.
   682          */
   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();
   689 
   690                 switch (tags[i] = tag) {
   691                     case CONSTANT_UTF8:
   692                         String str = in.readUTF();
   693                         indexHashAscii.put(cpool[i] = str, new Integer(i));
   694                         break;
   695                     case CONSTANT_INTEGER:
   696                         cpool[i] = new Integer(in.readInt());
   697                         break;
   698                     case CONSTANT_FLOAT:
   699                         cpool[i] = new Float(in.readFloat());
   700                         break;
   701                     case CONSTANT_LONG:
   702                         cpool[i++] = new Long(in.readLong());
   703                         break;
   704                     case CONSTANT_DOUBLE:
   705                         cpool[i++] = new Double(in.readDouble());
   706                         break;
   707                     case CONSTANT_CLASS:
   708                     case CONSTANT_STRING:
   709                         cpool[i] = new CPX(in.readUnsignedShort());
   710                         break;
   711 
   712                     case CONSTANT_FIELD:
   713                     case CONSTANT_METHOD:
   714                     case CONSTANT_INTERFACEMETHOD:
   715                     case CONSTANT_NAMEANDTYPE:
   716                         cpool[i] = new CPX2(in.readUnsignedShort(), in.readUnsignedShort());
   717                         break;
   718                     case CONSTANT_METHODHANDLE:
   719                         cpool[i] = new CPX2(in.readByte(), in.readUnsignedShort());
   720                         break;
   721                     case CONSTANT_METHODTYPE:
   722                         cpool[i] = new CPX(in.readUnsignedShort());
   723                         break;
   724                     case CONSTANT_INVOKEDYNAMIC:
   725                         cpool[i] = new CPX2(in.readUnsignedShort(), in.readUnsignedShort());
   726                         break;
   727                     case 0:
   728                     default:
   729                         throw new ClassFormatError("invalid constant type: " + (int) tags[i]);
   730                 }
   731             }
   732         }
   733 
   734         /**
   735          * Reads and strores field info.
   736          */
   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);
   742                 field.read(in);
   743                 fields[k] = field;
   744             }
   745         }
   746 
   747         /**
   748          * Reads and strores Method info.
   749          */
   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);
   755                 method.read(in);
   756                 methods[k] = method;
   757             }
   758         }
   759 
   760         /**
   761          * get a string
   762          */
   763         public String getString(int n) {
   764             if (n == 0) {
   765                 return null;
   766             } else {
   767                 return (String) cpool[n];
   768             }
   769         }
   770 
   771         /**
   772          * get the type of constant given an index
   773          */
   774         public byte getTag(int n) {
   775             try {
   776                 return tags[n];
   777             } catch (ArrayIndexOutOfBoundsException e) {
   778                 return (byte) 100;
   779             }
   780         }
   781         static final String hexString = "0123456789ABCDEF";
   782         public static char hexTable[] = hexString.toCharArray();
   783 
   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]);
   788             }
   789             return "0x" + s.toString();
   790         }
   791 
   792         static String toHex(long val) {
   793             int width;
   794             for (width = 16; width > 0; width--) {
   795                 if ((val >> (width - 1) * 4) != 0) {
   796                     break;
   797                 }
   798             }
   799             return toHex(val, width);
   800         }
   801 
   802         static String toHex(int val) {
   803             int width;
   804             for (width = 8; width > 0; width--) {
   805                 if ((val >> (width - 1) * 4) != 0) {
   806                     break;
   807                 }
   808             }
   809             return toHex(val, width);
   810         }
   811 
   812         /**
   813          * Returns the name of this class.
   814          */
   815         public String getClassName() {
   816             String res = null;
   817             if (this_class == 0) {
   818                 return res;
   819             }
   820             int tcpx;
   821             try {
   822                 if (tags[this_class] != CONSTANT_CLASS) {
   823                     return res; //"<CP["+cpx+"] is not a Class> ";
   824                 }
   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";
   830             }
   831 
   832             try {
   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";
   840             }
   841 
   842         }
   843 
   844         /**
   845          * Returns the name of class at perticular index.
   846          */
   847         public String getClassName(int cpx) {
   848             String res = "#" + cpx;
   849             if (cpx == 0) {
   850                 return res;
   851             }
   852             int scpx;
   853             try {
   854                 if (tags[cpx] != CONSTANT_CLASS) {
   855                     return res; //"<CP["+cpx+"] is not a Class> ";
   856                 }
   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";
   862             }
   863             res = "#" + scpx;
   864             try {
   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";
   872             }
   873         }
   874 
   875         public int getAccessFlags() {
   876             return access;
   877         }
   878 
   879         /**
   880          * Returns true if it is a class
   881          */
   882         public boolean isClass() {
   883             if ((access & ACC_INTERFACE) == 0) {
   884                 return true;
   885             }
   886             return false;
   887         }
   888 
   889         /**
   890          * Returns true if it is a interface.
   891          */
   892         public boolean isInterface() {
   893             if ((access & ACC_INTERFACE) != 0) {
   894                 return true;
   895             }
   896             return false;
   897         }
   898 
   899         public boolean isAnnotation() {
   900             return (access & ACC_ANNOTATION) != 0;
   901         }
   902 
   903         /**
   904          * Returns true if this member is public, false otherwise.
   905          */
   906         public boolean isPublic() {
   907             return (access & ACC_PUBLIC) != 0;
   908         }
   909 
   910         /**
   911          * Returns the access of this class or interface.
   912          */
   913         public String[] getAccess() {
   914             Vector v = new Vector();
   915             if ((access & ACC_PUBLIC) != 0) {
   916                 v.addElement("public");
   917             }
   918             if ((access & ACC_FINAL) != 0) {
   919                 v.addElement("final");
   920             }
   921             if ((access & ACC_ABSTRACT) != 0) {
   922                 v.addElement("abstract");
   923             }
   924             String[] accflags = new String[v.size()];
   925             v.copyInto(accflags);
   926             return accflags;
   927         }
   928 
   929         /**
   930          * Returns list of innerclasses.
   931          */
   932         public InnerClassData[] getInnerClasses() {
   933             return innerClasses;
   934         }
   935 
   936         /**
   937          * Returns list of attributes.
   938          */
   939         final AttrData[] getAttributes() {
   940             return attrs;
   941         }
   942 
   943         public byte[] findAnnotationData(boolean classRetention) {
   944             String n = classRetention
   945                 ? "RuntimeInvisibleAnnotations" : // NOI18N
   946                 "RuntimeVisibleAnnotations"; // NOI18N
   947             return findAttr(n, attrs);
   948         }
   949 
   950         /**
   951          * Returns true if superbit is set.
   952          */
   953         public boolean isSuperSet() {
   954             if ((access & ACC_SUPER) != 0) {
   955                 return true;
   956             }
   957             return false;
   958         }
   959 
   960         /**
   961          * Returns super class name.
   962          */
   963         public String getSuperClassName() {
   964             String res = null;
   965             if (super_class == 0) {
   966                 return res;
   967             }
   968             int scpx;
   969             try {
   970                 if (tags[super_class] != CONSTANT_CLASS) {
   971                     return res; //"<CP["+cpx+"] is not a Class> ";
   972                 }
   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";
   978             }
   979 
   980             try {
   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";
   988             }
   989         }
   990 
   991         /**
   992          * Returns list of super interfaces.
   993          */
   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]);
  1000             }
  1001             return interfacenames;
  1002         }
  1003 
  1004         /**
  1005          * Returns string at prticular constant pool index.
  1006          */
  1007         public String getStringValue(int cpoolx) {
  1008             try {
  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;
  1014             }
  1015         }
  1016 
  1017         /**
  1018          * Returns list of field info.
  1019          */
  1020         public FieldData[] getFields() {
  1021             return fields;
  1022         }
  1023 
  1024         /**
  1025          * Returns list of method info.
  1026          */
  1027         public MethodData[] getMethods() {
  1028             return methods;
  1029         }
  1030 
  1031         /**
  1032          * Returns constant pool entry at that index.
  1033          */
  1034         public CPX2 getCpoolEntry(int cpx) {
  1035             return ((CPX2) (cpool[cpx]));
  1036         }
  1037 
  1038         public Object getCpoolEntryobj(int cpx) {
  1039             return (cpool[cpx]);
  1040         }
  1041 
  1042         /**
  1043          * Returns index of this class.
  1044          */
  1045         public int getthis_cpx() {
  1046             return this_class;
  1047         }
  1048 
  1049         /**
  1050          * Returns string at that index.
  1051          */
  1052         public String StringValue(int cpx) {
  1053             return stringValue(cpx, false);
  1054         }
  1055 
  1056         public String stringValue(int cpx, boolean textual) {
  1057             return stringValue(cpx, textual, null);
  1058         }
  1059 
  1060         public String stringValue(int cpx, String[] classRefs) {
  1061             return stringValue(cpx, true, classRefs);
  1062         }
  1063 
  1064         private String stringValue(int cpx, boolean textual, String[] refs) {
  1065             if (cpx == 0) {
  1066                 return "#0";
  1067             }
  1068             int tag;
  1069             Object x;
  1070             String suffix = "";
  1071             try {
  1072                 tag = tags[cpx];
  1073                 x = cpool[cpx];
  1074             } catch (IndexOutOfBoundsException e) {
  1075                 return "<Incorrect CP index:" + cpx + ">";
  1076             }
  1077 
  1078             if (x == null) {
  1079                 return "<NULL>";
  1080             }
  1081             switch (tag) {
  1082                 case CONSTANT_UTF8: {
  1083                     if (!textual) {
  1084                         return (String) x;
  1085                     }
  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);
  1090                         switch (c) {
  1091                             case '\\':
  1092                                 sb.append('\\').append('\\');
  1093                                 break;
  1094                             case '\t':
  1095                                 sb.append('\\').append('t');
  1096                                 break;
  1097                             case '\n':
  1098                                 sb.append('\\').append('n');
  1099                                 break;
  1100                             case '\r':
  1101                                 sb.append('\\').append('r');
  1102                                 break;
  1103                             case '\"':
  1104                                 sb.append('\\').append('\"');
  1105                                 break;
  1106                             case '\u2028':
  1107                                 sb.append("\\u2028");
  1108                                 break;
  1109                             case '\u2029':
  1110                                 sb.append("\\u2029");
  1111                                 break;
  1112                             default:
  1113                                 sb.append(c);
  1114                         }
  1115                     }
  1116                     return sb.toString();
  1117                 }
  1118                 case CONSTANT_DOUBLE: {
  1119                     Double d = (Double) x;
  1120                     String sd = d.toString();
  1121                     if (textual) {
  1122                         return sd;
  1123                     }
  1124                     return sd + "d";
  1125                 }
  1126                 case CONSTANT_FLOAT: {
  1127                     Float f = (Float) x;
  1128                     String sf = (f).toString();
  1129                     if (textual) {
  1130                         return sf;
  1131                     }
  1132                     return sf + "f";
  1133                 }
  1134                 case CONSTANT_LONG: {
  1135                     Long ln = (Long) x;
  1136                     if (textual) {
  1137                         return ln.toString();
  1138                     }
  1139                     return ln.toString() + 'l';
  1140                 }
  1141                 case CONSTANT_INTEGER: {
  1142                     Integer in = (Integer) x;
  1143                     return in.toString();
  1144                 }
  1145                 case CONSTANT_CLASS:
  1146                     String jn = getClassName(cpx);
  1147                     if (textual) {
  1148                         if (refs != null) {
  1149                             refs[0] = jn;
  1150                         }
  1151                         return jn;
  1152                     }
  1153                     return javaName(jn);
  1154                 case CONSTANT_STRING:
  1155                     String sv = stringValue(((CPX) x).cpx, textual);
  1156                     if (textual) {
  1157                         return '"' + sv + '"';
  1158                     } else {
  1159                         return sv;
  1160                     }
  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);
  1166 
  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);
  1173                 default:
  1174                     return "UnknownTag" + tag; //TBD
  1175             }
  1176         }
  1177 
  1178         /**
  1179          * Returns resolved java type name.
  1180          */
  1181         public String javaName(String name) {
  1182             if (name == null) {
  1183                 return "null";
  1184             }
  1185             int len = name.length();
  1186             if (len == 0) {
  1187                 return "\"\"";
  1188             }
  1189             int cc = '/';
  1190             fullname:
  1191             { // xxx/yyy/zzz
  1192                 int cp;
  1193                 for (int k = 0; k < len; k += Character.charCount(cp)) {
  1194                     cp = name.codePointAt(k);
  1195                     if (cc == '/') {
  1196                         if (!isJavaIdentifierStart(cp)) {
  1197                             break fullname;
  1198                         }
  1199                     } else if (cp != '/') {
  1200                         if (!isJavaIdentifierPart(cp)) {
  1201                             break fullname;
  1202                         }
  1203                     }
  1204                     cc = cp;
  1205                 }
  1206                 return name;
  1207             }
  1208             return "\"" + name + "\"";
  1209         }
  1210 
  1211         public String getName(int cpx) {
  1212             String res;
  1213             try {
  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 + ">";
  1219             }
  1220         }
  1221 
  1222         /**
  1223          * Returns unqualified class name.
  1224          */
  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);
  1232                 }
  1233             }
  1234             return classname;
  1235         }
  1236 
  1237         /**
  1238          * Returns source file name.
  1239          */
  1240         public String getSourceName() {
  1241             return getName(source_cpx);
  1242         }
  1243 
  1244         /**
  1245          * Returns package name.
  1246          */
  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") */;
  1253             } else {
  1254                 return null;
  1255             }
  1256         }
  1257         
  1258         public BootMethodData getBootMethod(int indx) {
  1259             return bootMethods != null ? bootMethods[indx] : null;
  1260         }
  1261 
  1262         /**
  1263          * Returns total constant pool entry count.
  1264          */
  1265         public int getCpoolCount() {
  1266             return cpool_count;
  1267         }
  1268 
  1269         /**
  1270          * Returns minor version of class file.
  1271          */
  1272         public int getMinor_version() {
  1273             return minor_version;
  1274         }
  1275 
  1276         /**
  1277          * Returns major version of class file.
  1278          */
  1279         public int getMajor_version() {
  1280             return major_version;
  1281         }
  1282 
  1283         private boolean isJavaIdentifierStart(int cp) {
  1284             return ('a' <= cp && cp <= 'z') || ('A' <= cp && cp <= 'Z');
  1285         }
  1286 
  1287         private boolean isJavaIdentifierPart(int cp) {
  1288             return isJavaIdentifierStart(cp) || ('0' <= cp && cp <= '9');
  1289         }
  1290 
  1291         public String[] getNameAndType(int indx) {
  1292             return getNameAndType(indx, 0, new String[2]);
  1293         }
  1294 
  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);
  1299             return arr;
  1300         }
  1301 
  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);
  1307         }
  1308 
  1309         public MethodData findMethod(String name, String signature) {
  1310             for (MethodData md: methods) {
  1311                 if (md.getName().equals(name)
  1312                         && md.getInternalSig().equals(signature)) {
  1313                     return md;
  1314                 }
  1315             }
  1316 
  1317             // not found
  1318             return null;
  1319         }
  1320 
  1321         public FieldData findField(String name, String signature) {
  1322             for (FieldData fd: fields) {
  1323                 if (fd.getName().equals(name)
  1324                         && fd.getInternalSig().equals(signature)) {
  1325                     return fd;
  1326                 }
  1327             }
  1328 
  1329             // not found
  1330             return null;
  1331         }
  1332 
  1333         static byte[] findAttr(String n, AttrData[] attrs) {
  1334             for (AttrData ad : attrs) {
  1335                 if (n.equals(ad.getAttrName())) {
  1336                     return ad.getData();
  1337                 }
  1338             }
  1339             return null;
  1340         }
  1341     }
  1342 
  1343     /**
  1344      * Strores field data informastion.
  1345      *
  1346      * @author Sucheta Dambalkar (Adopted code from jdis)
  1347      */
  1348     static class FieldData {
  1349 
  1350         ClassData cls;
  1351         int access;
  1352         int name_index;
  1353         int descriptor_index;
  1354         int attributes_count;
  1355         int value_cpx = -1;
  1356         boolean isSynthetic = false;
  1357         boolean isDeprecated = false;
  1358         Vector attrs;
  1359 
  1360         public FieldData(ClassData cls) {
  1361             this.cls = cls;
  1362         }
  1363 
  1364         /**
  1365          * Read and store field info.
  1366          */
  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) {
  1377                     continue;
  1378                 }
  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");
  1383                     }
  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");
  1391                     }
  1392                     isSynthetic = true;
  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");
  1399                     }
  1400                     isDeprecated = true;
  1401                     AttrData attr = new AttrData(cls);
  1402                     attr.read(attr_name_index);
  1403                     attrs.addElement(attr);
  1404                 } else {
  1405                     AttrData attr = new AttrData(cls);
  1406                     attr.read(attr_name_index, in);
  1407                     attrs.addElement(attr);
  1408                 }
  1409             }
  1410 
  1411         }  // end read
  1412 
  1413         public boolean isStatic() {
  1414             return (access & ACC_STATIC) != 0;
  1415         }
  1416 
  1417         /**
  1418          * Returns access of a field.
  1419          */
  1420         public String[] getAccess() {
  1421             Vector v = new Vector();
  1422             if ((access & ACC_PUBLIC) != 0) {
  1423                 v.addElement("public");
  1424             }
  1425             if ((access & ACC_PRIVATE) != 0) {
  1426                 v.addElement("private");
  1427             }
  1428             if ((access & ACC_PROTECTED) != 0) {
  1429                 v.addElement("protected");
  1430             }
  1431             if ((access & ACC_STATIC) != 0) {
  1432                 v.addElement("static");
  1433             }
  1434             if ((access & ACC_FINAL) != 0) {
  1435                 v.addElement("final");
  1436             }
  1437             if ((access & ACC_VOLATILE) != 0) {
  1438                 v.addElement("volatile");
  1439             }
  1440             if ((access & ACC_TRANSIENT) != 0) {
  1441                 v.addElement("transient");
  1442             }
  1443             String[] accflags = new String[v.size()];
  1444             v.copyInto(accflags);
  1445             return accflags;
  1446         }
  1447 
  1448         /**
  1449          * Returns name of a field.
  1450          */
  1451         public String getName() {
  1452             return cls.getStringValue(name_index);
  1453         }
  1454 
  1455         /**
  1456          * Returns internal signature of a field
  1457          */
  1458         public String getInternalSig() {
  1459             return cls.getStringValue(descriptor_index);
  1460         }
  1461 
  1462         /**
  1463          * Returns true if field is synthetic.
  1464          */
  1465         public boolean isSynthetic() {
  1466             return isSynthetic;
  1467         }
  1468 
  1469         /**
  1470          * Returns true if field is deprecated.
  1471          */
  1472         public boolean isDeprecated() {
  1473             return isDeprecated;
  1474         }
  1475 
  1476         public boolean hasConstantValue() {
  1477             return value_cpx != -1;
  1478         }
  1479 
  1480         /**
  1481          * Returns list of attributes of field.
  1482          */
  1483         public Vector getAttributes() {
  1484             return attrs;
  1485         }
  1486 
  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);
  1494         }
  1495     }
  1496 
  1497     /**
  1498      * A JavaScript optimized replacement for Hashtable.
  1499      *
  1500      * @author Jaroslav Tulach <jtulach@netbeans.org>
  1501      */
  1502     private static final class Hashtable {
  1503 
  1504         private Object[] keys;
  1505         private Object[] values;
  1506 
  1507         Hashtable(int i) {
  1508             this();
  1509         }
  1510 
  1511         Hashtable(int i, double d) {
  1512             this();
  1513         }
  1514 
  1515         Hashtable() {
  1516         }
  1517 
  1518         synchronized void put(Object key, Object val) {
  1519             int[] where = {-1, -1};
  1520             Object found = get(key, where);
  1521             if (where[0] != -1) {
  1522                 // key exists
  1523                 values[where[0]] = val;
  1524             } else {
  1525                 if (where[1] != -1) {
  1526                     // null found
  1527                     keys[where[1]] = key;
  1528                     values[where[1]] = val;
  1529                 } else {
  1530                     if (keys == null) {
  1531                         keys = new Object[11];
  1532                         values = new Object[11];
  1533                         keys[0] = key;
  1534                         values[0] = val;
  1535                     } else {
  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];
  1541                         }
  1542                         newKeys[keys.length] = key;
  1543                         newValues[keys.length] = val;
  1544                         keys = newKeys;
  1545                         values = newValues;
  1546                     }
  1547                 }
  1548             }
  1549         }
  1550 
  1551         Object get(Object key) {
  1552             return get(key, null);
  1553         }
  1554 
  1555         private synchronized Object get(Object key, int[] foundAndNull) {
  1556             if (keys == null) {
  1557                 return null;
  1558             }
  1559             for (int i = 0; i < keys.length; i++) {
  1560                 if (keys[i] == null) {
  1561                     if (foundAndNull != null) {
  1562                         foundAndNull[1] = i;
  1563                     }
  1564                 } else if (keys[i].equals(key)) {
  1565                     if (foundAndNull != null) {
  1566                         foundAndNull[0] = i;
  1567                     }
  1568                     return values[i];
  1569                 }
  1570             }
  1571             return null;
  1572         }
  1573     }
  1574 
  1575     /**
  1576      * Strores InnerClass data informastion.
  1577      *
  1578      * @author Sucheta Dambalkar (Adopted code from jdis)
  1579      */
  1580     private static class InnerClassData {
  1581 
  1582         ClassData cls;
  1583         int inner_class_info_index, outer_class_info_index, inner_name_index, access;
  1584 
  1585         public InnerClassData(ClassData cls) {
  1586             this.cls = cls;
  1587 
  1588         }
  1589 
  1590         /**
  1591          * Read Innerclass attribute data.
  1592          */
  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();
  1598         }  // end read
  1599 
  1600         /**
  1601          * Returns the access of this class or interface.
  1602          */
  1603         public String[] getAccess() {
  1604             Vector v = new Vector();
  1605             if ((access & ACC_PUBLIC) != 0) {
  1606                 v.addElement("public");
  1607             }
  1608             if ((access & ACC_FINAL) != 0) {
  1609                 v.addElement("final");
  1610             }
  1611             if ((access & ACC_ABSTRACT) != 0) {
  1612                 v.addElement("abstract");
  1613             }
  1614             String[] accflags = new String[v.size()];
  1615             v.copyInto(accflags);
  1616             return accflags;
  1617         }
  1618     } // end InnerClassData
  1619     
  1620     static class BootMethodData {
  1621         private final ClassData clazz;
  1622         final int method;
  1623         private final int[] args;
  1624 
  1625         private BootMethodData(ClassData clazz, int method, int[] args) {
  1626             this.clazz = clazz;
  1627             this.method = method;
  1628             this.args = args;
  1629         }
  1630 
  1631         @Override
  1632         public String toString() {
  1633             StringBuilder sb = new StringBuilder();
  1634             sb.append(clazz.stringValue(method, true));
  1635             sb.append('(');
  1636             for (int indx : args) {
  1637                 sb.append("\n  ");
  1638                 sb.append(clazz.stringValue(indx, true));
  1639             }
  1640             sb.append(')');
  1641             return sb.toString();
  1642         }
  1643     }
  1644 
  1645     /**
  1646      * Strores LineNumberTable data information.
  1647      *
  1648      * @author Sucheta Dambalkar (Adopted code from jdis)
  1649      */
  1650     private static class LineNumData {
  1651 
  1652         short start_pc, line_number;
  1653 
  1654         public LineNumData() {
  1655         }
  1656 
  1657         /**
  1658          * Read LineNumberTable attribute.
  1659          */
  1660         public LineNumData(DataInputStream in) throws IOException {
  1661             start_pc = in.readShort();
  1662             line_number = in.readShort();
  1663 
  1664         }
  1665     }
  1666 
  1667     /**
  1668      * Strores LocalVariableTable data information.
  1669      *
  1670      * @author Sucheta Dambalkar (Adopted code from jdis)
  1671      */
  1672     private static class LocVarData {
  1673 
  1674         short start_pc, length, name_cpx, sig_cpx, slot;
  1675 
  1676         public LocVarData() {
  1677         }
  1678 
  1679         /**
  1680          * Read LocalVariableTable attribute.
  1681          */
  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();
  1688 
  1689         }
  1690     }
  1691     /**
  1692      * Strores method data informastion.
  1693      *
  1694      * @author Sucheta Dambalkar (Adopted code from jdis)
  1695      */
  1696     static class MethodData {
  1697 
  1698         ClassData cls;
  1699         int access;
  1700         int name_index;
  1701         int descriptor_index;
  1702         int attributes_count;
  1703         byte[] code;
  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;
  1716 
  1717         public MethodData(ClassData cls) {
  1718             this.cls = cls;
  1719         }
  1720 
  1721         /**
  1722          * Read method info.
  1723          */
  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();
  1731 
  1732                 readAttr:
  1733                 {
  1734                     if (cls.getTag(attr_name_index) == CONSTANT_UTF8) {
  1735                         String attr_name = cls.getString(attr_name_index);
  1736                         if (attr_name.equals("Code")) {
  1737                             readCode(in);
  1738                             AttrData attr = new AttrData(cls);
  1739                             attr.read(attr_name_index);
  1740                             attrs.addElement(attr);
  1741                             break readAttr;
  1742                         } else if (attr_name.equals("Exceptions")) {
  1743                             readExceptions(in);
  1744                             AttrData attr = new AttrData(cls);
  1745                             attr.read(attr_name_index);
  1746                             attrs.addElement(attr);
  1747                             break readAttr;
  1748                         } else if (attr_name.equals("Synthetic")) {
  1749                             if (in.readInt() != 0) {
  1750                                 throw new ClassFormatError("invalid Synthetic attr length");
  1751                             }
  1752                             isSynthetic = true;
  1753                             AttrData attr = new AttrData(cls);
  1754                             attr.read(attr_name_index);
  1755                             attrs.addElement(attr);
  1756                             break readAttr;
  1757                         } else if (attr_name.equals("Deprecated")) {
  1758                             if (in.readInt() != 0) {
  1759                                 throw new ClassFormatError("invalid Synthetic attr length");
  1760                             }
  1761                             isDeprecated = true;
  1762                             AttrData attr = new AttrData(cls);
  1763                             attr.read(attr_name_index);
  1764                             attrs.addElement(attr);
  1765                             break readAttr;
  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;
  1771                             break readAttr;
  1772                         }
  1773                     }
  1774                     AttrData attr = new AttrData(cls);
  1775                     attr.read(attr_name_index, in);
  1776                     attrs.addElement(attr);
  1777                 }
  1778             }
  1779         }
  1780 
  1781         /**
  1782          * Read code attribute info.
  1783          */
  1784         public void readCode(DataInputStream in) throws IOException {
  1785 
  1786             int attr_length = in.readInt();
  1787             max_stack = in.readUnsignedShort();
  1788             max_locals = in.readUnsignedShort();
  1789             int codelen = in.readInt();
  1790 
  1791             code = new byte[codelen];
  1792             int totalread = 0;
  1793             while (totalread < codelen) {
  1794                 totalread += in.read(code, totalread, codelen - totalread);
  1795             }
  1796             //      in.read(code, 0, codelen);
  1797             int clen = 0;
  1798             readExceptionTable(in);
  1799             int code_attributes_count = in.readUnsignedShort();
  1800 
  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")) {
  1817                         readStackMap(in);
  1818                         attr.read(table_name_index);
  1819                     } else {
  1820                         attr.read(table_name_index, in);
  1821                     }
  1822                     code_attrs.addElement(attr);
  1823                     continue;
  1824                 }
  1825 
  1826                 attr.read(table_name_index, in);
  1827                 code_attrs.addElement(attr);
  1828             }
  1829         }
  1830 
  1831         /**
  1832          * Read exception table info.
  1833          */
  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));
  1839             }
  1840         }
  1841 
  1842         /**
  1843          * Read LineNumberTable attribute info.
  1844          */
  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));
  1851             }
  1852         }
  1853 
  1854         /**
  1855          * Read LocalVariableTable attribute info.
  1856          */
  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));
  1863             }
  1864         }
  1865 
  1866         /**
  1867          * Read Exception attribute info.
  1868          */
  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;
  1876             }
  1877         }
  1878 
  1879         /**
  1880          * Read StackMapTable attribute info.
  1881          */
  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);
  1888             }
  1889         }
  1890 
  1891         /**
  1892          * Read StackMap attribute info.
  1893          */
  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);
  1900             }
  1901         }
  1902         
  1903         /**
  1904          * Return access of the method.
  1905          */
  1906         public int getAccess() {
  1907             return access;
  1908         }
  1909 
  1910         /**
  1911          * Return name of the method.
  1912          */
  1913         public String getName() {
  1914             return cls.getStringValue(name_index);
  1915         }
  1916 
  1917         /**
  1918          * Return internal siganature of the method.
  1919          */
  1920         public String getInternalSig() {
  1921             return cls.getStringValue(descriptor_index);
  1922         }
  1923 
  1924         /**
  1925          * Return code attribute data of a method.
  1926          */
  1927         public byte[] getCode() {
  1928             return code;
  1929         }
  1930 
  1931         /**
  1932          * Return LineNumberTable size.
  1933          */
  1934         public int getnumlines() {
  1935             return lin_num_tb.size();
  1936         }
  1937 
  1938         /**
  1939          * Return LineNumberTable
  1940          */
  1941         public Vector getlin_num_tb() {
  1942             return lin_num_tb;
  1943         }
  1944 
  1945         /**
  1946          * Return LocalVariableTable size.
  1947          */
  1948         public int getloc_var_tbsize() {
  1949             return loc_var_tb.size();
  1950         }
  1951 
  1952         /**
  1953          * Return LocalVariableTable.
  1954          */
  1955         public Vector getloc_var_tb() {
  1956             return loc_var_tb;
  1957         }
  1958 
  1959         /**
  1960          * Return StackMap.
  1961          */
  1962         public StackMapData[] getStackMap() {
  1963             return stackMap;
  1964         }
  1965 
  1966         /**
  1967          * Return StackMapTable.
  1968          */
  1969         public StackMapTableData[] getStackMapTable() {
  1970             return stackMapTable;
  1971         }
  1972 
  1973         public StackMapIterator createStackMapIterator() {
  1974             return new StackMapIterator(this);
  1975         }
  1976 
  1977         /**
  1978          * Return true if method is static
  1979          */
  1980         public boolean isStatic() {
  1981             if ((access & ACC_STATIC) != 0) {
  1982                 return true;
  1983             }
  1984             return false;
  1985         }
  1986 
  1987         /**
  1988          * Return max depth of operand stack.
  1989          */
  1990         public int getMaxStack() {
  1991             return max_stack;
  1992         }
  1993 
  1994         /**
  1995          * Return number of local variables.
  1996          */
  1997         public int getMaxLocals() {
  1998             return max_locals;
  1999         }
  2000 
  2001         /**
  2002          * Return exception index table in Exception attribute.
  2003          */
  2004         public int[] get_exc_index_table() {
  2005             return exc_index_table;
  2006         }
  2007 
  2008         /**
  2009          * Return exception table in code attributre.
  2010          */
  2011         public TrapDataIterator getTrapDataIterator() {
  2012             return new TrapDataIterator(exception_table);
  2013         }
  2014 
  2015         /**
  2016          * Return method attributes.
  2017          */
  2018         public Vector getAttributes() {
  2019             return attrs;
  2020         }
  2021 
  2022         /**
  2023          * Return code attributes.
  2024          */
  2025         public Vector getCodeAttributes() {
  2026             return code_attrs;
  2027         }
  2028 
  2029         byte[] getDefaultAttribute() {
  2030             return annotationDefault == null ? null : annotationDefault.getData();
  2031         }
  2032 
  2033         /**
  2034          * Return true if method id synthetic.
  2035          */
  2036         public boolean isSynthetic() {
  2037             return isSynthetic;
  2038         }
  2039 
  2040         /**
  2041          * Return true if method is deprecated.
  2042          */
  2043         public boolean isDeprecated() {
  2044             return isDeprecated;
  2045         }
  2046 
  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);
  2054         }
  2055 
  2056         public boolean isConstructor() {
  2057             return "<init>".equals(getName());
  2058         }
  2059     }
  2060 
  2061     /* represents one entry of StackMap attribute
  2062      */
  2063     private static class StackMapData {
  2064 
  2065         final int offset;
  2066         final int[] locals;
  2067         final int[] stack;
  2068 
  2069         StackMapData(int offset, int[] locals, int[] stack) {
  2070             this.offset = offset;
  2071             this.locals = locals;
  2072             this.stack = stack;
  2073         }
  2074 
  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);
  2081         }
  2082 
  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);
  2087             }
  2088             return types;
  2089         }
  2090 
  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);
  2095             }
  2096             return type;
  2097         }
  2098     }
  2099 
  2100     static final class StackMapIterator {
  2101 
  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;
  2109 
  2110         StackMapIterator(final MethodData methodData) {
  2111             this(methodData.getStackMapTable(),
  2112                 methodData.getInternalSig(),
  2113                 methodData.isStatic());
  2114         }
  2115 
  2116         StackMapIterator(final StackMapTableData[] stackMapTable,
  2117             final String methodSignature,
  2118             final boolean isStaticMethod) {
  2119             this.stackMapTable = (stackMapTable != null)
  2120                 ? stackMapTable
  2121                 : new StackMapTableData[0];
  2122 
  2123             argTypes = getArgTypes(methodSignature, isStaticMethod);
  2124             localTypes = new TypeArray();
  2125             stackTypes = new TypeArray();
  2126 
  2127             localTypes.addAll(argTypes);
  2128 
  2129             lastFrameByteCodeOffset = -1;
  2130             advanceBy(0);
  2131         }
  2132         
  2133         public boolean isEmpty() {
  2134             return stackMapTable.length == 0;
  2135         }
  2136 
  2137         public String getFrameAsString() {
  2138             return (nextFrameIndex == 0)
  2139                 ? StackMapTableData.toString("INITIAL", 0, null, null)
  2140                 : stackMapTable[nextFrameIndex - 1].toString();
  2141         }
  2142 
  2143         public int getFrameIndex() {
  2144             return nextFrameIndex;
  2145         }
  2146 
  2147         public TypeArray getFrameStack() {
  2148             return stackTypes;
  2149         }
  2150 
  2151         public TypeArray getFrameLocals() {
  2152             return localTypes;
  2153         }
  2154 
  2155         public TypeArray getArguments() {
  2156             return argTypes;
  2157         }
  2158 
  2159         public void advanceBy(final int numByteCodes) {
  2160             if (numByteCodes < 0) {
  2161                 throw new IllegalStateException("Forward only iterator");
  2162             }
  2163 
  2164             byteCodeOffset += numByteCodes;
  2165             while ((nextFrameIndex < stackMapTable.length)
  2166                 && ((byteCodeOffset - lastFrameByteCodeOffset)
  2167                 >= (stackMapTable[nextFrameIndex].offsetDelta
  2168                 + 1))) {
  2169                 final StackMapTableData nextFrame = stackMapTable[nextFrameIndex];
  2170 
  2171                 lastFrameByteCodeOffset += nextFrame.offsetDelta + 1;
  2172                 nextFrame.applyTo(localTypes, stackTypes);
  2173 
  2174                 ++nextFrameIndex;
  2175             }
  2176         }
  2177 
  2178         public void advanceTo(final int nextByteCodeOffset) {
  2179             advanceBy(nextByteCodeOffset - byteCodeOffset);
  2180         }
  2181 
  2182         private static TypeArray getArgTypes(final String methodSignature,
  2183             final boolean isStaticMethod) {
  2184             final TypeArray argTypes = new TypeArray();
  2185 
  2186             if (!isStaticMethod) {
  2187                 argTypes.add(ITEM_Object);
  2188             }
  2189 
  2190             if (methodSignature.charAt(0) != '(') {
  2191                 throw new IllegalArgumentException("Invalid method signature");
  2192             }
  2193 
  2194             final int length = methodSignature.length();
  2195             boolean skipType = false;
  2196             int argType;
  2197             for (int i = 1; i < length; ++i) {
  2198                 switch (methodSignature.charAt(i)) {
  2199                     case 'B':
  2200                     case 'C':
  2201                     case 'S':
  2202                     case 'Z':
  2203                     case 'I':
  2204                         argType = ITEM_Integer;
  2205                         break;
  2206                     case 'J':
  2207                         argType = ITEM_Long;
  2208                         break;
  2209                     case 'F':
  2210                         argType = ITEM_Float;
  2211                         break;
  2212                     case 'D':
  2213                         argType = ITEM_Double;
  2214                         break;
  2215                     case 'L': {
  2216                         i = methodSignature.indexOf(';', i + 1);
  2217                         if (i == -1) {
  2218                             throw new IllegalArgumentException(
  2219                                 "Invalid method signature");
  2220                         }
  2221                         argType = ITEM_Object;
  2222                         break;
  2223                     }
  2224                     case ')':
  2225                         // not interested in the return value type
  2226                         return argTypes;
  2227                     case '[':
  2228                         if (!skipType) {
  2229                             argTypes.add(ITEM_Object);
  2230                             skipType = true;
  2231                         }
  2232                         continue;
  2233 
  2234                     default:
  2235                         throw new IllegalArgumentException(
  2236                             "Invalid method signature");
  2237                 }
  2238 
  2239                 if (!skipType) {
  2240                     argTypes.add(argType);
  2241                 } else {
  2242                     skipType = false;
  2243                 }
  2244             }
  2245 
  2246             return argTypes;
  2247         }
  2248     }
  2249     /* represents one entry of StackMapTable attribute
  2250      */
  2251 
  2252     private static abstract class StackMapTableData {
  2253 
  2254         final int frameType;
  2255         int offsetDelta;
  2256 
  2257         StackMapTableData(int frameType) {
  2258             this.frameType = frameType;
  2259         }
  2260 
  2261         abstract void applyTo(TypeArray localTypes, TypeArray stackTypes);
  2262 
  2263         protected static String toString(
  2264             final String frameType,
  2265             final int offset,
  2266             final int[] localTypes,
  2267             final int[] stackTypes) {
  2268             final StringBuilder sb = new StringBuilder(frameType);
  2269 
  2270             sb.append("(off: +").append(offset);
  2271             if (localTypes != null) {
  2272                 sb.append(", locals: ");
  2273                 appendTypes(sb, localTypes);
  2274             }
  2275             if (stackTypes != null) {
  2276                 sb.append(", stack: ");
  2277                 appendTypes(sb, stackTypes);
  2278             }
  2279             sb.append(')');
  2280 
  2281             return sb.toString();
  2282         }
  2283 
  2284         private static void appendTypes(final StringBuilder sb, final int[] types) {
  2285             sb.append('[');
  2286             if (types.length > 0) {
  2287                 sb.append(TypeArray.typeString(types[0]));
  2288                 for (int i = 1; i < types.length; ++i) {
  2289                     sb.append(", ");
  2290                     sb.append(TypeArray.typeString(types[i]));
  2291                 }
  2292             }
  2293             sb.append(']');
  2294         }
  2295 
  2296         private static class SameFrame extends StackMapTableData {
  2297 
  2298             SameFrame(int frameType, int offsetDelta) {
  2299                 super(frameType);
  2300                 this.offsetDelta = offsetDelta;
  2301             }
  2302 
  2303             @Override
  2304             void applyTo(TypeArray localTypes, TypeArray stackTypes) {
  2305                 stackTypes.clear();
  2306             }
  2307 
  2308             @Override
  2309             public String toString() {
  2310                 return toString("SAME" + ((frameType == SAME_FRAME_EXTENDED)
  2311                     ? "_FRAME_EXTENDED" : ""),
  2312                     offsetDelta,
  2313                     null, null);
  2314             }
  2315         }
  2316 
  2317         private static class SameLocals1StackItem extends StackMapTableData {
  2318 
  2319             final int[] stack;
  2320 
  2321             SameLocals1StackItem(int frameType, int offsetDelta, int[] stack) {
  2322                 super(frameType);
  2323                 this.offsetDelta = offsetDelta;
  2324                 this.stack = stack;
  2325             }
  2326 
  2327             @Override
  2328             void applyTo(TypeArray localTypes, TypeArray stackTypes) {
  2329                 stackTypes.setAll(stack);
  2330             }
  2331 
  2332             @Override
  2333             public String toString() {
  2334                 return toString(
  2335                     "SAME_LOCALS_1_STACK_ITEM"
  2336                     + ((frameType == SAME_LOCALS_1_STACK_ITEM_EXTENDED)
  2337                     ? "_EXTENDED" : ""),
  2338                     offsetDelta,
  2339                     null, stack);
  2340             }
  2341         }
  2342 
  2343         private static class ChopFrame extends StackMapTableData {
  2344 
  2345             ChopFrame(int frameType, int offsetDelta) {
  2346                 super(frameType);
  2347                 this.offsetDelta = offsetDelta;
  2348             }
  2349 
  2350             @Override
  2351             void applyTo(TypeArray localTypes, TypeArray stackTypes) {
  2352                 localTypes.setSize(localTypes.getSize()
  2353                     - (SAME_FRAME_EXTENDED - frameType));
  2354                 stackTypes.clear();
  2355             }
  2356 
  2357             @Override
  2358             public String toString() {
  2359                 return toString("CHOP", offsetDelta, null, null);
  2360             }
  2361         }
  2362 
  2363         private static class AppendFrame extends StackMapTableData {
  2364 
  2365             final int[] locals;
  2366 
  2367             AppendFrame(int frameType, int offsetDelta, int[] locals) {
  2368                 super(frameType);
  2369                 this.offsetDelta = offsetDelta;
  2370                 this.locals = locals;
  2371             }
  2372 
  2373             @Override
  2374             void applyTo(TypeArray localTypes, TypeArray stackTypes) {
  2375                 localTypes.addAll(locals);
  2376                 stackTypes.clear();
  2377             }
  2378 
  2379             @Override
  2380             public String toString() {
  2381                 return toString("APPEND", offsetDelta, locals, null);
  2382             }
  2383         }
  2384 
  2385         private static class FullFrame extends StackMapTableData {
  2386 
  2387             final int[] locals;
  2388             final int[] stack;
  2389 
  2390             FullFrame(int offsetDelta, int[] locals, int[] stack) {
  2391                 super(FULL_FRAME);
  2392                 this.offsetDelta = offsetDelta;
  2393                 this.locals = locals;
  2394                 this.stack = stack;
  2395             }
  2396 
  2397             @Override
  2398             void applyTo(TypeArray localTypes, TypeArray stackTypes) {
  2399                 localTypes.setAll(locals);
  2400                 stackTypes.setAll(stack);
  2401             }
  2402 
  2403             @Override
  2404             public String toString() {
  2405                 return toString("FULL", offsetDelta, locals, stack);
  2406             }
  2407         }
  2408 
  2409         static StackMapTableData getInstance(DataInputStream in, MethodData method)
  2410             throws IOException {
  2411             int frameType = in.readUnsignedByte();
  2412 
  2413             if (frameType < SAME_FRAME_BOUND) {
  2414                 // same_frame
  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) {
  2434                 // append_frame
  2435                 return new AppendFrame(frameType, in.readUnsignedShort(),
  2436                     StackMapData.readTypeArray(in, frameType - SAME_FRAME_EXTENDED, method));
  2437             } else if (frameType == FULL_FRAME) {
  2438                 // 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);
  2445             } else {
  2446                 throw new ClassFormatError("unrecognized frame_type in StackMapTable");
  2447             }
  2448         }
  2449     }
  2450 
  2451     /**
  2452      * Stores exception table data in code attribute.
  2453      *
  2454      * @author Sucheta Dambalkar (Adopted code from jdis)
  2455      */
  2456     static final class TrapData {
  2457 
  2458         public final short start_pc;
  2459         public final short end_pc;
  2460         public final short handler_pc;
  2461         public final short catch_cpx;
  2462         final int num;
  2463 
  2464         /**
  2465          * Read and store exception table data in code attribute.
  2466          */
  2467         TrapData(DataInputStream in, int num) throws IOException {
  2468             this.num = num;
  2469             start_pc = in.readShort();
  2470             end_pc = in.readShort();
  2471             handler_pc = in.readShort();
  2472             catch_cpx = in.readShort();
  2473         }
  2474 
  2475         /**
  2476          * returns recommended identifier
  2477          */
  2478         public String ident() {
  2479             return "t" + num;
  2480         }
  2481     }
  2482     /**
  2483      *
  2484      * @author Jaroslav Tulach <jtulach@netbeans.org>
  2485      */
  2486     static final class TrapDataIterator {
  2487 
  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;
  2492 
  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);
  2498             }
  2499         }
  2500 
  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);
  2504             if (v == null) {
  2505                 v = new Vector(1);
  2506                 h.put(s, v);
  2507             }
  2508             v.add(td);
  2509         }
  2510 
  2511         private boolean processAll(Hashtable h, Short key, boolean add) {
  2512             boolean change = false;
  2513             Vector v = (Vector) h.get(key);
  2514             if (v != null) {
  2515                 int s = v.size();
  2516                 for (int i = 0; i < s; i++) {
  2517                     TrapData td = (TrapData) v.elementAt(i);
  2518                     if (add) {
  2519                         add(td);
  2520                         change = true;
  2521                     } else {
  2522                         remove(td);
  2523                         change = true;
  2524                     }
  2525                 }
  2526             }
  2527             return change;
  2528         }
  2529 
  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);
  2534             return ch1 || ch2;
  2535         }
  2536 
  2537         public boolean useTry() {
  2538             return currentCount > 0;
  2539         }
  2540 
  2541         public TrapData[] current() {
  2542             TrapData[] copy = new TrapData[currentCount];
  2543             for (int i = 0; i < currentCount; i++) {
  2544                 copy[i] = current[i];
  2545             }
  2546             return copy;
  2547         }
  2548 
  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];
  2554                 }
  2555                 current = data;
  2556             }
  2557             current[currentCount++] = e;
  2558         }
  2559 
  2560         private void remove(TrapData e) {
  2561             if (currentCount == 0) {
  2562                 return;
  2563             }
  2564             int from = 0;
  2565             while (from < currentCount) {
  2566                 if (e == current[from++]) {
  2567                     break;
  2568                 }
  2569             }
  2570             while (from < currentCount) {
  2571                 current[from - 1] = current[from];
  2572                 current[from] = null;
  2573                 from++;
  2574             }
  2575             currentCount--;
  2576         }
  2577     }
  2578     static final class TypeArray {
  2579 
  2580         private static final int CAPACITY_INCREMENT = 16;
  2581         private int[] types;
  2582         private int size;
  2583 
  2584         public TypeArray() {
  2585         }
  2586 
  2587         public TypeArray(final TypeArray initialTypes) {
  2588             setAll(initialTypes);
  2589         }
  2590 
  2591         public void add(final int newType) {
  2592             ensureCapacity(size + 1);
  2593             types[size++] = newType;
  2594         }
  2595 
  2596         public void addAll(final TypeArray newTypes) {
  2597             addAll(newTypes.types, 0, newTypes.size);
  2598         }
  2599 
  2600         public void addAll(final int[] newTypes) {
  2601             addAll(newTypes, 0, newTypes.length);
  2602         }
  2603 
  2604         public void addAll(final int[] newTypes,
  2605             final int offset,
  2606             final int count) {
  2607             if (count > 0) {
  2608                 ensureCapacity(size + count);
  2609                 arraycopy(newTypes, offset, types, size, count);
  2610                 size += count;
  2611             }
  2612         }
  2613 
  2614         public void set(final int index, final int newType) {
  2615             types[index] = newType;
  2616         }
  2617 
  2618         public void setAll(final TypeArray newTypes) {
  2619             setAll(newTypes.types, 0, newTypes.size);
  2620         }
  2621 
  2622         public void setAll(final int[] newTypes) {
  2623             setAll(newTypes, 0, newTypes.length);
  2624         }
  2625 
  2626         public void setAll(final int[] newTypes,
  2627             final int offset,
  2628             final int count) {
  2629             if (count > 0) {
  2630                 ensureCapacity(count);
  2631                 arraycopy(newTypes, offset, types, 0, count);
  2632                 size = count;
  2633             } else {
  2634                 clear();
  2635             }
  2636         }
  2637 
  2638         public void setSize(final int newSize) {
  2639             if (size != newSize) {
  2640                 ensureCapacity(newSize);
  2641 
  2642                 for (int i = size; i < newSize; ++i) {
  2643                     types[i] = 0;
  2644                 }
  2645                 size = newSize;
  2646             }
  2647         }
  2648 
  2649         public void clear() {
  2650             size = 0;
  2651         }
  2652 
  2653         public int getSize() {
  2654             return size;
  2655         }
  2656 
  2657         public int get(final int index) {
  2658             return types[index];
  2659         }
  2660 
  2661         public static String typeString(final int type) {
  2662             switch (type & 0xff) {
  2663                 case ITEM_Bogus:
  2664                     return "_top_";
  2665                 case ITEM_Integer:
  2666                     return "_int_";
  2667                 case ITEM_Float:
  2668                     return "_float_";
  2669                 case ITEM_Double:
  2670                     return "_double_";
  2671                 case ITEM_Long:
  2672                     return "_long_";
  2673                 case ITEM_Null:
  2674                     return "_null_";
  2675                 case ITEM_InitObject: // UninitializedThis
  2676                     return "_init_";
  2677                 case ITEM_Object:
  2678                     return "_object_";
  2679                 case ITEM_NewObject: // Uninitialized
  2680                     return "_new_";
  2681                 default:
  2682                     throw new IllegalArgumentException("Unknown type");
  2683             }
  2684         }
  2685 
  2686         @Override
  2687         public String toString() {
  2688             final StringBuilder sb = new StringBuilder("[");
  2689             String sep = "";
  2690             for (int i = 0; i < size; ++i) {
  2691                 sb.append(sep).append(VarType.toString(types[i] & 0xff));
  2692                 sep = ", ";
  2693             }
  2694             return sb.append(']').toString();
  2695         }
  2696 
  2697         private void ensureCapacity(final int minCapacity) {
  2698             if ((minCapacity == 0)
  2699                 || (types != null) && (minCapacity <= types.length)) {
  2700                 return;
  2701             }
  2702 
  2703             final int newCapacity =
  2704                 ((minCapacity + CAPACITY_INCREMENT - 1) / CAPACITY_INCREMENT)
  2705                 * CAPACITY_INCREMENT;
  2706             final int[] newTypes = new int[newCapacity];
  2707 
  2708             if (size > 0) {
  2709                 arraycopy(types, 0, newTypes, 0, size);
  2710             }
  2711 
  2712             types = newTypes;
  2713         }
  2714 
  2715         // no System.arraycopy
  2716         private void arraycopy(final int[] src, final int srcPos,
  2717             final int[] dest, final int destPos,
  2718             final int length) {
  2719             for (int i = 0; i < length; ++i) {
  2720                 dest[destPos + i] = src[srcPos + i];
  2721             }
  2722         }
  2723     }
  2724     /**
  2725      * A JavaScript ready replacement for java.util.Vector
  2726      *
  2727      * @author Jaroslav Tulach <jtulach@netbeans.org>
  2728      */
  2729     @JavaScriptPrototype(prototype = "new Array")
  2730     private static final class Vector {
  2731 
  2732         private Object[] arr;
  2733 
  2734         Vector() {
  2735         }
  2736 
  2737         Vector(int i) {
  2738         }
  2739 
  2740         void add(Object objectType) {
  2741             addElement(objectType);
  2742         }
  2743 
  2744         @JavaScriptBody(args = {"obj"}, body =
  2745             "this.push(obj);")
  2746         void addElement(Object obj) {
  2747             final int s = size();
  2748             setSize(s + 1);
  2749             setElementAt(obj, s);
  2750         }
  2751 
  2752         @JavaScriptBody(args = {}, body =
  2753             "return this.length;")
  2754         int size() {
  2755             return arr == null ? 0 : arr.length;
  2756         }
  2757 
  2758         @JavaScriptBody(args = {"newArr"}, body =
  2759             "for (var i = 0; i < this.length; i++) {\n"
  2760             + "  newArr[i] = this[i];\n"
  2761             + "}\n")
  2762         void copyInto(Object[] newArr) {
  2763             if (arr == null) {
  2764                 return;
  2765             }
  2766             int min = Math.min(newArr.length, arr.length);
  2767             for (int i = 0; i < min; i++) {
  2768                 newArr[i] = arr[i];
  2769             }
  2770         }
  2771 
  2772         @JavaScriptBody(args = {"index"}, body =
  2773             "return this[index];")
  2774         Object elementAt(int index) {
  2775             return arr[index];
  2776         }
  2777 
  2778         private void setSize(int len) {
  2779             Object[] newArr = new Object[len];
  2780             copyInto(newArr);
  2781             arr = newArr;
  2782         }
  2783 
  2784         @JavaScriptBody(args = {"val", "index"}, body =
  2785             "this[index] = val;")
  2786         void setElementAt(Object val, int index) {
  2787             arr[index] = val;
  2788         }
  2789     }
  2790     
  2791 }