jdk/src/share/classes/com/sun/tools/hat/internal/model/JavaObject.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 468 642c8c0be52e
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 468
diff changeset
     2
 * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * The Original Code is HAT. The Initial Developer of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Original Code is Bill Foote, with contributions from others
468
642c8c0be52e 6695553: Cleanup GPLv2+SPL legal notices in hat sources
ohair
parents: 2
diff changeset
    30
 * at JavaSoft/Sun.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
package com.sun.tools.hat.internal.model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.tools.hat.internal.parser.ReadBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Represents Java instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author      Bill Foote
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class JavaObject extends JavaLazyReadObject {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private Object clazz;       // Number before resolve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                                // JavaClass after resolve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * Construct a new JavaObject.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * @param classID id of the class object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * @param offset The offset of field data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public JavaObject(long classID, long offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        super(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        this.clazz = makeId(classID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public void resolve(Snapshot snapshot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (clazz instanceof JavaClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (clazz instanceof Number) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            long classID = getIdValue((Number)clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            clazz = snapshot.findThing(classID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            if (! (clazz instanceof JavaClass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                warn("Class " + Long.toHexString(classID) + " not found, " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                     "adding fake class!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                int length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                ReadBuffer buf = snapshot.getReadBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                int idSize = snapshot.getIdentifierSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                long lenOffset = getOffset() + 2*idSize + 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    length = buf.getInt(lenOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                } catch (IOException exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    throw new RuntimeException(exp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                clazz = snapshot.addFakeInstanceClass(classID, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            throw new InternalError("should not reach here");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        JavaClass cl = (JavaClass) clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        cl.resolve(snapshot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        // while resolving, parse fields in verbose mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // but, getFields calls parseFields in non-verbose mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // to avoid printing warnings repeatedly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        parseFields(getValue(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        cl.addInstance(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        super.resolve(snapshot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Are we the same type as other?  We are iff our clazz is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * same type as other's.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public boolean isSameTypeAs(JavaThing other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (!(other instanceof JavaObject)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        JavaObject oo = (JavaObject) other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return getClazz().equals(oo.getClazz());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Return our JavaClass object.  This may only be called after resolve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public JavaClass getClazz() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return (JavaClass) clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public JavaThing[] getFields() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // pass false to verbose mode so that dereference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        // warnings are not printed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return parseFields(getValue(), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    // returns the value of field of given name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public JavaThing getField(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        JavaThing[] flds = getFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        JavaField[] instFields = getClazz().getFieldsForInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        for (int i = 0; i < instFields.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (instFields[i].getName().equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                return flds[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public int compareTo(JavaThing other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (other instanceof JavaObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            JavaObject oo = (JavaObject) other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return getClazz().getName().compareTo(oo.getClazz().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return super.compareTo(other);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public void visitReferencedObjects(JavaHeapObjectVisitor v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        super.visitReferencedObjects(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        JavaThing[] flds = getFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        for (int i = 0; i < flds.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (flds[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                if (v.mightExclude()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    && v.exclude(getClazz().getClassForField(i),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                 getClazz().getFieldForInstance(i)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    // skip it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                } else if (flds[i] instanceof JavaHeapObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    v.visit((JavaHeapObject) flds[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public boolean refersOnlyWeaklyTo(Snapshot ss, JavaThing other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (ss.getWeakReferenceClass() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            final int referentFieldIndex = ss.getReferentFieldIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            if (ss.getWeakReferenceClass().isAssignableFrom(getClazz())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                // REMIND:  This introduces a dependency on the JDK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                //      implementation that is undesirable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                JavaThing[] flds = getFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                for (int i = 0; i < flds.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    if (i != referentFieldIndex && flds[i] == other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Describe the reference that this thing has to target.  This will only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * be called if target is in the array returned by getChildrenForRootset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public String describeReferenceTo(JavaThing target, Snapshot ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        JavaThing[] flds = getFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        for (int i = 0; i < flds.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (flds[i] == target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                JavaField f = getClazz().getFieldForInstance(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                return "field " + f.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return super.describeReferenceTo(target, ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (getClazz().isString()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            JavaThing value = getField("value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if (value instanceof JavaValueArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                return ((JavaValueArray)value).valueString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            return super.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    // Internals only below this point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Java instance record (HPROF_GC_INSTANCE_DUMP) looks as below:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *     object ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *     stack trace serial number (int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *     class ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *     data length (int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *     byte[length]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    protected final int readValueLength() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        JavaClass cl = getClazz();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        int idSize = cl.getIdentifierSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        long lengthOffset = getOffset() + 2*idSize + 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return cl.getReadBuffer().getInt(lengthOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    protected final byte[] readValue() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        JavaClass cl = getClazz();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        int idSize = cl.getIdentifierSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        ReadBuffer buf = cl.getReadBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        long offset = getOffset() + 2*idSize + 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        int length = buf.getInt(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            return Snapshot.EMPTY_BYTE_ARRAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            byte[] res = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            buf.get(offset + 4, res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    private JavaThing[] parseFields(byte[] data, boolean verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        JavaClass cl = getClazz();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        int target = cl.getNumFieldsForInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        JavaField[] fields = cl.getFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        JavaThing[] fieldValues = new JavaThing[target];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        Snapshot snapshot = cl.getSnapshot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        int idSize = snapshot.getIdentifierSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        int fieldNo = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // In the dump file, the fields are stored in this order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // fields of most derived class (immediate class) are stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        // first and then the super class and so on. In this object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        // fields are stored in the reverse ("natural") order. i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        // fields of most super class are stored first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        // target variable is used to compensate for the fact that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        // the dump file starts field values from the leaf working
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        // upwards in the inheritance hierarchy, whereas JavaObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        // starts with the top of the inheritance hierarchy and works down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        target -= fields.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        JavaClass currClass = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        for (int i = 0; i < fieldValues.length; i++, fieldNo++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            while (fieldNo >= fields.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                currClass = currClass.getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                fields = currClass.getFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                fieldNo = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                target -= fields.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            JavaField f = fields[fieldNo];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            char sig = f.getSignature().charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            switch (sig) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                case 'L':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                case '[': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    long id = objectIdAt(index, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    index += idSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    JavaObjectRef ref = new JavaObjectRef(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    fieldValues[target+fieldNo] = ref.dereference(snapshot, f, verbose);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                case 'Z': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    byte value = byteAt(index, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    fieldValues[target+fieldNo] = new JavaBoolean(value != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                case 'B': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    byte value = byteAt(index, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    fieldValues[target+fieldNo] = new JavaByte(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                case 'S': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    short value = shortAt(index, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    index += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    fieldValues[target+fieldNo] = new JavaShort(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                case 'C': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    char value = charAt(index, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    index += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    fieldValues[target+fieldNo] = new JavaChar(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                case 'I': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    int value = intAt(index, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    index += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    fieldValues[target+fieldNo] = new JavaInt(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                case 'J': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    long value = longAt(index, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    index += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    fieldValues[target+fieldNo] = new JavaLong(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                case 'F': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    float value = floatAt(index, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    index += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    fieldValues[target+fieldNo] = new JavaFloat(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                case 'D': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    double value = doubleAt(index, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    index += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    fieldValues[target+fieldNo] = new JavaDouble(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    throw new RuntimeException("invalid signature: " + sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return fieldValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    private void warn(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        System.out.println("WARNING: " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
}