langtools/src/share/classes/sun/tools/javap/ClassData.java
author jjg
Thu, 22 May 2008 16:06:00 -0700
changeset 656 4718b910737c
parent 10 06bc494ca11e
child 735 372aa565a221
permissions -rw-r--r--
6657909: javap has unchecked compilation warnings Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2002-2004 Sun Microsystems, Inc.  All Rights Reserved.
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
package sun.tools.javap;
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
 * Central data repository of the Java Disassembler.
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
 * Stores all the information in java class file.
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 * @author  Sucheta Dambalkar (Adopted code from jdis)
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
public class ClassData implements RuntimeConstants {
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
    private int magic;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
    private int minor_version;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
    private int major_version;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
    private int cpool_count;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
    private Object cpool[];
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
    private int access;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    private int this_class = 0;;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    private int super_class;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    private int interfaces_count;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    private int[] interfaces = new int[0];;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    private int fields_count;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    private FieldData[] fields;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    private int methods_count;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    private MethodData[] methods;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    private InnerClassData[] innerClasses;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    private int attributes_count;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    private AttrData[] attrs;
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    private String classname;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    private String superclassname;
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    private int source_cpx=0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    private byte tags[];
656
4718b910737c 6657909: javap has unchecked compilation warnings
jjg
parents: 10
diff changeset
    61
    private Hashtable<Object,Integer> indexHashAscii = new Hashtable<Object,Integer>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    private String pkgPrefix="";
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    private int pkgPrefixLen=0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
     * Read classfile to disassemble.
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    public ClassData(InputStream infile){
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        try{
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
            this.read(new DataInputStream(infile));
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        }catch (FileNotFoundException ee) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
            error("cant read file");
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        }catch (Error ee) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
            ee.printStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
            error("fatal error");
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        } catch (Exception ee) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
            ee.printStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
            error("fatal exception");
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     * Reads and stores class file information.
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    public void read(DataInputStream in) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        // Read the header
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        magic = in.readInt();
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        if (magic != JAVA_MAGIC) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
            throw new ClassFormatError("wrong magic: " +
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
                                       toHex(magic) + ", expected " +
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
                                       toHex(JAVA_MAGIC));
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        minor_version = in.readShort();
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        major_version = in.readShort();
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        if (major_version != JAVA_VERSION) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        // Read the constant pool
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        readCP(in);
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        access = in.readUnsignedShort();
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        this_class = in.readUnsignedShort();
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        super_class = in.readUnsignedShort();
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        //Read interfaces.
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        interfaces_count = in.readUnsignedShort();
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        if(interfaces_count > 0){
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
            interfaces = new int[interfaces_count];
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        for (int i = 0; i < interfaces_count; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
            interfaces[i]=in.readShort();
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        // Read the fields
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        readFields(in);
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        // Read the methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        readMethods(in);
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        // Read the attributes
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        attributes_count = in.readUnsignedShort();
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        attrs=new AttrData[attributes_count];
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        for (int k = 0; k < attributes_count; k++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
            int name_cpx=in.readUnsignedShort();
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
            if (getTag(name_cpx)==CONSTANT_UTF8
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
                && getString(name_cpx).equals("SourceFile")
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
                ){      if (in.readInt()!=2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
                    throw new ClassFormatError("invalid attr length");
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
                source_cpx=in.readUnsignedShort();
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
                AttrData attr=new AttrData(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
                attr.read(name_cpx);
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
                attrs[k]=attr;
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
            } else if (getTag(name_cpx)==CONSTANT_UTF8
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
                       && getString(name_cpx).equals("InnerClasses")
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
                       ){       int length=in.readInt();
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
                       int num=in.readUnsignedShort();
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
                       if (2+num*8 != length)
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
                           throw new ClassFormatError("invalid attr length");
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
                       innerClasses=new InnerClassData[num];
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
                       for (int j = 0; j < num; j++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
                           InnerClassData innerClass=new InnerClassData(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
                           innerClass.read(in);
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
                           innerClasses[j]=innerClass;
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
                       }
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
                       AttrData attr=new AttrData(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
                       attr.read(name_cpx);
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
                       attrs[k]=attr;
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
                AttrData attr=new AttrData(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
                attr.read(name_cpx, in);
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
                attrs[k]=attr;
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        in.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    } // end ClassData.read()
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
     * Reads and stores constant pool info.
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
    void readCP(DataInputStream in) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        cpool_count = in.readUnsignedShort();
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
        tags = new byte[cpool_count];
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        cpool = new Object[cpool_count];
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        for (int i = 1; i < cpool_count; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
            byte tag = in.readByte();
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            switch(tags[i] = tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
            case CONSTANT_UTF8:
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
                String str=in.readUTF();
656
4718b910737c 6657909: javap has unchecked compilation warnings
jjg
parents: 10
diff changeset
   170
                indexHashAscii.put(cpool[i] = str, i);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
            case CONSTANT_INTEGER:
656
4718b910737c 6657909: javap has unchecked compilation warnings
jjg
parents: 10
diff changeset
   173
                cpool[i] = Integer.valueOf(in.readInt());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
            case CONSTANT_FLOAT:
656
4718b910737c 6657909: javap has unchecked compilation warnings
jjg
parents: 10
diff changeset
   176
                cpool[i] = Float.valueOf(in.readFloat());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
            case CONSTANT_LONG:
656
4718b910737c 6657909: javap has unchecked compilation warnings
jjg
parents: 10
diff changeset
   179
                cpool[i++] = Long.valueOf(in.readLong());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
            case CONSTANT_DOUBLE:
656
4718b910737c 6657909: javap has unchecked compilation warnings
jjg
parents: 10
diff changeset
   182
                cpool[i++] = Double.valueOf(in.readDouble());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
            case CONSTANT_CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
            case CONSTANT_STRING:
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
                cpool[i] = new CPX(in.readUnsignedShort());
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
            case CONSTANT_FIELD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
            case CONSTANT_METHOD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
            case CONSTANT_INTERFACEMETHOD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
            case CONSTANT_NAMEANDTYPE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
                cpool[i] = new CPX2(in.readUnsignedShort(), in.readUnsignedShort());
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
            case 0:
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
                throw new ClassFormatError("invalid constant type: " + (int)tags[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
     * Reads and strores field info.
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
    protected void readFields(DataInputStream in) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
        int fields_count = in.readUnsignedShort();
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
        fields=new FieldData[fields_count];
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
        for (int k = 0; k < fields_count; k++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
            FieldData field=new FieldData(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
            field.read(in);
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
            fields[k]=field;
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
     * Reads and strores Method info.
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
    protected void readMethods(DataInputStream in) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        int methods_count = in.readUnsignedShort();
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
        methods=new MethodData[methods_count];
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
        for (int k = 0; k < methods_count ; k++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
            MethodData method=new MethodData(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
            method.read(in);
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
            methods[k]=method;
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
     * get a string
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
    public String getString(int n) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
        return (n == 0) ? null : (String)cpool[n];
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
     * get the type of constant given an index
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
    public byte getTag(int n) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
        try{
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
            return tags[n];
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
        } catch (ArrayIndexOutOfBoundsException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
            return (byte)100;
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
    static final String hexString="0123456789ABCDEF";
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
    public static char hexTable[]=hexString.toCharArray();
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
    static String toHex(long val, int width) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
        StringBuffer s = new StringBuffer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
        for (int i=width-1; i>=0; i--)
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
            s.append(hexTable[((int)(val>>(4*i)))&0xF]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
        return "0x"+s.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
    static String toHex(long val) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        int width;
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
        for (width=16; width>0; width--) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
            if ((val>>(width-1)*4)!=0) break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
        return toHex(val, width);
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
    static String toHex(int val) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
        int width;
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
        for (width=8; width>0; width--) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
            if ((val>>(width-1)*4)!=0) break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
        return toHex(val, width);
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
    public void error(String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
        System.err.println("ERROR:" +msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
     * Returns the name of this class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
    public String getClassName() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
        String res=null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
        if (this_class==0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
            return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
        int tcpx;
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
            if (tags[this_class]!=CONSTANT_CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
                return res; //"<CP["+cpx+"] is not a Class> ";
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
            tcpx=((CPX)cpool[this_class]).cpx;
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
        } catch (ArrayIndexOutOfBoundsException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
            return res; // "#"+cpx+"// invalid constant pool index";
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
        } catch (Throwable e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
            return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
            return (String)(cpool[tcpx]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
        } catch (ArrayIndexOutOfBoundsException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
            return  res; // "class #"+scpx+"// invalid constant pool index";
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
        } catch (ClassCastException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
            return  res; // "class #"+scpx+"// invalid constant pool reference";
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
        } catch (Throwable e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
            return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
     * Returns the name of class at perticular index.
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
    public String getClassName(int cpx) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
        String res="#"+cpx;
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
        if (cpx==0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
            return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
        int scpx;
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
            if (tags[cpx]!=CONSTANT_CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
                return res; //"<CP["+cpx+"] is not a Class> ";
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
            scpx=((CPX)cpool[cpx]).cpx;
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
        } catch (ArrayIndexOutOfBoundsException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
            return res; // "#"+cpx+"// invalid constant pool index";
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
        } catch (Throwable e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
            return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
        res="#"+scpx;
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
            return (String)(cpool[scpx]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
        } catch (ArrayIndexOutOfBoundsException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
            return  res; // "class #"+scpx+"// invalid constant pool index";
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
        } catch (ClassCastException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
            return  res; // "class #"+scpx+"// invalid constant pool reference";
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
        } catch (Throwable e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
            return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
     * Returns true if it is a class
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
    public boolean isClass() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
        if((access & ACC_INTERFACE) == 0) return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
     * Returns true if it is a interface.
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
    public boolean isInterface(){
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
        if((access & ACC_INTERFACE) != 0) return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
     * Returns true if this member is public, false otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
    public boolean isPublic(){
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
        return (access & ACC_PUBLIC) != 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
     * Returns the access of this class or interface.
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
    public String[] getAccess(){
656
4718b910737c 6657909: javap has unchecked compilation warnings
jjg
parents: 10
diff changeset
   368
        Vector<String> v = new Vector<String>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
        if ((access & ACC_PUBLIC)   !=0) v.addElement("public");
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
        if ((access & ACC_FINAL)    !=0) v.addElement("final");
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
        if ((access & ACC_ABSTRACT) !=0) v.addElement("abstract");
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
        String[] accflags = new String[v.size()];
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
        v.copyInto(accflags);
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
        return accflags;
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
     * Returns list of innerclasses.
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
    public InnerClassData[] getInnerClasses(){
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
        return innerClasses;
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
     * Returns list of attributes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
    public AttrData[] getAttributes(){
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
        return attrs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
     * Returns true if superbit is set.
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
    public boolean isSuperSet(){
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
        if ((access & ACC_SUPER)   !=0) return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
     * Returns super class name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
    public String getSuperClassName(){
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
        String res=null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
        if (super_class==0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
            return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
        int scpx;
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
            if (tags[super_class]!=CONSTANT_CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
                return res; //"<CP["+cpx+"] is not a Class> ";
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
            scpx=((CPX)cpool[super_class]).cpx;
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
        } catch (ArrayIndexOutOfBoundsException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
            return res; // "#"+cpx+"// invalid constant pool index";
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
        } catch (Throwable e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
            return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
            return (String)(cpool[scpx]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
        } catch (ArrayIndexOutOfBoundsException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
            return  res; // "class #"+scpx+"// invalid constant pool index";
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
        } catch (ClassCastException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
            return  res; // "class #"+scpx+"// invalid constant pool reference";
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
        } catch (Throwable e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
            return res; // "#"+cpx+"// ERROR IN DISASSEMBLER";
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
     * Returns list of super interfaces.
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
    public String[] getSuperInterfaces(){
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
        String interfacenames[] = new String[interfaces.length];
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
        int interfacecpx = -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
        for(int i = 0; i < interfaces.length; i++){
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
            interfacecpx=((CPX)cpool[interfaces[i]]).cpx;
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
            interfacenames[i] = (String)(cpool[interfacecpx]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
        return interfacenames;
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
     * Returns string at prticular constant pool index.
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
    public String getStringValue(int cpoolx) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
            return ((String)cpool[cpoolx]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
        } catch (ArrayIndexOutOfBoundsException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
            return "//invalid constant pool index:"+cpoolx;
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
        } catch (ClassCastException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
            return "//invalid constant pool ref:"+cpoolx;
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
     * Returns list of field info.
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
    public  FieldData[] getFields(){
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
        return fields;
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
     * Returns list of method info.
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
    public  MethodData[] getMethods(){
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
        return methods;
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
     * Returns constant pool entry at that index.
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
    public CPX2 getCpoolEntry(int cpx){
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
        return ((CPX2)(cpool[cpx]));
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
    public Object getCpoolEntryobj(int cpx){
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
        return (cpool[cpx]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
     * Returns index of this class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
    public int getthis_cpx(){
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
        return this_class;
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
    public String TagString (int tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
        String res=Tables.tagName(tag);
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
        if (res==null)  return "BOGUS_TAG:"+tag;
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
     * Returns string at that index.
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
    public String StringValue(int cpx) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
        if (cpx==0) return "#0";
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
        int tag;
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
        Object x;
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
        String suffix="";
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
            tag=tags[cpx];
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
            x=cpool[cpx];
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
        } catch (IndexOutOfBoundsException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
            return "<Incorrect CP index:"+cpx+">";
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
        if (x==null) return "<NULL>";
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
        switch (tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
        case CONSTANT_UTF8: {
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
            StringBuffer sb=new StringBuffer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
            String s=(String)x;
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
            for (int k=0; k<s.length(); k++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
                char c=s.charAt(k);
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
                switch (c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
                case '\t': sb.append('\\').append('t'); break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
                case '\n': sb.append('\\').append('n'); break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
                case '\r': sb.append('\\').append('r'); break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
                case '\"': sb.append('\\').append('\"'); break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
                default: sb.append(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
            return sb.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
        case CONSTANT_DOUBLE: {
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
            Double d=(Double)x;
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
            String sd=d.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
            return sd+"d";
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
        case CONSTANT_FLOAT: {
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
            Float f=(Float)x;
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
            String sf=(f).toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
            return sf+"f";
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
        case CONSTANT_LONG: {
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
            Long ln = (Long)x;
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
            return ln.toString()+'l';
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
        case CONSTANT_INTEGER: {
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
            Integer in = (Integer)x;
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
            return in.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
        case CONSTANT_CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
            return javaName(getClassName(cpx));
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
        case CONSTANT_STRING:
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
            return StringValue(((CPX)x).cpx);
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
        case CONSTANT_FIELD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
        case CONSTANT_METHOD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
        case CONSTANT_INTERFACEMETHOD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
            //return getShortClassName(((CPX2)x).cpx1)+"."+StringValue(((CPX2)x).cpx2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
             return javaName(getClassName(((CPX2)x).cpx1))+"."+StringValue(((CPX2)x).cpx2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
        case CONSTANT_NAMEANDTYPE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
            return getName(((CPX2)x).cpx1)+":"+StringValue(((CPX2)x).cpx2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
            return "UnknownTag"; //TBD
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
     * Returns resolved java type name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
    public String javaName(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
        if( name==null) return "null";
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
        int len=name.length();
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
        if (len==0) return "\"\"";
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
        int cc='/';
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
    fullname: { // xxx/yyy/zzz
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
            int cp;
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
            for (int k=0; k<len; k += Character.charCount(cp)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
                cp=name.codePointAt(k);
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
                if (cc=='/') {
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
                    if (!Character.isJavaIdentifierStart(cp)) break fullname;
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
                } else if (cp!='/') {
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
                    if (!Character.isJavaIdentifierPart(cp)) break fullname;
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
                cc=cp;
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
            return name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
        return "\""+name+"\"";
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
    public String getName(int cpx) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
        String res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
            return javaName((String)cpool[cpx]); //.replace('/','.');
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
        } catch (ArrayIndexOutOfBoundsException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
            return "<invalid constant pool index:"+cpx+">";
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
        } catch (ClassCastException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
            return "<invalid constant pool ref:"+cpx+">";
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
     * Returns unqualified class name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
    public String getShortClassName(int cpx) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
        String classname=javaName(getClassName(cpx));
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
        pkgPrefixLen=classname.lastIndexOf("/")+1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
        if (pkgPrefixLen!=0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
            pkgPrefix=classname.substring(0,pkgPrefixLen);
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
            if (classname.startsWith(pkgPrefix)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
                return classname.substring(pkgPrefixLen);
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
        return classname;
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
     * Returns source file name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
    public String getSourceName(){
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
        return getName(source_cpx);
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
     * Returns package name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
    public String getPkgName(){
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
        String classname=getClassName(this_class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
        pkgPrefixLen=classname.lastIndexOf("/")+1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
        if (pkgPrefixLen!=0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
            pkgPrefix=classname.substring(0,pkgPrefixLen);
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
            return("package  "+pkgPrefix.substring(0,pkgPrefixLen-1)+";\n");
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
        }else return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
     * Returns total constant pool entry count.
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
    public int getCpoolCount(){
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
        return cpool_count;
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
    public String StringTag(int cpx) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
        byte tag=0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
        String str=null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
            if (cpx==0) throw new IndexOutOfBoundsException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
            tag=tags[cpx];
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
            return      TagString(tag);
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
        } catch (IndexOutOfBoundsException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
            str="Incorrect CP index:"+cpx;
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
        return str;
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
     * Returns minor version of class file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
    public int getMinor_version(){
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
        return minor_version;
06bc494ca11e Initial load
duke
parents:
diff changeset
   655
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
     * Returns major version of class file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
    public int getMajor_version(){
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
        return major_version;
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
}