jdk/src/share/classes/com/sun/tools/jdi/PacketStream.java
author martin
Mon, 10 Mar 2008 15:07:09 -0700
changeset 51 6fe31bc95bbc
parent 2 90ce3da70b43
child 715 f16baef3a20e
permissions -rw-r--r--
6600143: Remove another 450 unnecessary casts Reviewed-by: alanb, iris, lmalvent, bristor, peterjones, darcy, wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2005 Sun Microsystems, Inc.  All Rights Reserved.
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
package com.sun.tools.jdi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
class PacketStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    final VirtualMachineImpl vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private int inCursor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    final Packet pkt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private boolean isCommitted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    PacketStream(VirtualMachineImpl vm, int cmdSet, int cmd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        this.vm = vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        this.pkt = new Packet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        pkt.cmdSet = (short)cmdSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        pkt.cmd = (short)cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    PacketStream(VirtualMachineImpl vm, Packet pkt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        this.vm = vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        this.pkt = pkt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        this.isCommitted = true; /* read only stream */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    int id() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        return pkt.id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    void send() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if (!isCommitted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            pkt.data = dataStream.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            vm.sendToTarget(pkt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            isCommitted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    void waitForReply() throws JDWPException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (!isCommitted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            throw new InternalException("waitForReply without send");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        vm.waitForTargetReply(pkt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (pkt.errorCode != Packet.ReplyNoError) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            throw new JDWPException(pkt.errorCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    void writeBoolean(boolean data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if(data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            dataStream.write( 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            dataStream.write( 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    void writeByte(byte data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        dataStream.write( data );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    void writeChar(char data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        dataStream.write( (byte)((data >>> 8) & 0xFF) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        dataStream.write( (byte)((data >>> 0) & 0xFF) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    void writeShort(short data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        dataStream.write( (byte)((data >>> 8) & 0xFF) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        dataStream.write( (byte)((data >>> 0) & 0xFF) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    void writeInt(int data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        dataStream.write( (byte)((data >>> 24) & 0xFF) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        dataStream.write( (byte)((data >>> 16) & 0xFF) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        dataStream.write( (byte)((data >>> 8) & 0xFF) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        dataStream.write( (byte)((data >>> 0) & 0xFF) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    void writeLong(long data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        dataStream.write( (byte)((data >>> 56) & 0xFF) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        dataStream.write( (byte)((data >>> 48) & 0xFF) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        dataStream.write( (byte)((data >>> 40) & 0xFF) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        dataStream.write( (byte)((data >>> 32) & 0xFF) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        dataStream.write( (byte)((data >>> 24) & 0xFF) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        dataStream.write( (byte)((data >>> 16) & 0xFF) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        dataStream.write( (byte)((data >>> 8) & 0xFF) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        dataStream.write( (byte)((data >>> 0) & 0xFF) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    void writeFloat(float data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        writeInt(Float.floatToIntBits(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    void writeDouble(double data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        writeLong(Double.doubleToLongBits(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    void writeID(int size, long data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        switch (size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            case 8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                writeLong(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                writeInt((int)data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                writeShort((short)data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                throw new UnsupportedOperationException("JDWP: ID size not supported: " + size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    void writeNullObjectRef() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        writeObjectRef(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    void writeObjectRef(long data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        writeID(vm.sizeofObjectRef, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    void writeClassRef(long data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        writeID(vm.sizeofClassRef, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    void writeMethodRef(long data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        writeID(vm.sizeofMethodRef, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    void writeFieldRef(long data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        writeID(vm.sizeofFieldRef, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    void writeFrameRef(long data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        writeID(vm.sizeofFrameRef, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    void writeByteArray(byte[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        dataStream.write(data, 0, data.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    void writeString(String string) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            byte[] stringBytes = string.getBytes("UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            writeInt(stringBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            writeByteArray(stringBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } catch (java.io.UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            throw new InternalException("Cannot convert string to UTF8 bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    void writeLocation(Location location) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        ReferenceTypeImpl refType = (ReferenceTypeImpl)location.declaringType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        byte tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (refType instanceof ClassType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            tag = JDWP.TypeTag.CLASS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } else if (refType instanceof InterfaceType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            // It's possible to have executable code in an interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            tag = JDWP.TypeTag.INTERFACE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            throw new InternalException("Invalid Location");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        writeByte(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        writeClassRef(refType.ref());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        writeMethodRef(((MethodImpl)location.method()).ref());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        writeLong(location.codeIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    void writeValue(Value val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            writeValueChecked(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        } catch (InvalidTypeException exc) {  // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                "Internal error: Invalid Tag/Type pair");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    void writeValueChecked(Value val) throws InvalidTypeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        writeByte(ValueImpl.typeValueKey(val));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        writeUntaggedValue(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    void writeUntaggedValue(Value val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            writeUntaggedValueChecked(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        } catch (InvalidTypeException exc) {  // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                "Internal error: Invalid Tag/Type pair");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    void writeUntaggedValueChecked(Value val) throws InvalidTypeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        byte tag = ValueImpl.typeValueKey(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (isObjectTag(tag)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                 writeObjectRef(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                if (!(val instanceof ObjectReference)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    throw new InvalidTypeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                writeObjectRef(((ObjectReferenceImpl)val).ref());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            switch (tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                case JDWP.Tag.BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    if(!(val instanceof ByteValue))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                        throw new InvalidTypeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    writeByte(((PrimitiveValue)val).byteValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                case JDWP.Tag.CHAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    if(!(val instanceof CharValue))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                        throw new InvalidTypeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    writeChar(((PrimitiveValue)val).charValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                case JDWP.Tag.FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    if(!(val instanceof FloatValue))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        throw new InvalidTypeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    writeFloat(((PrimitiveValue)val).floatValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                case JDWP.Tag.DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    if(!(val instanceof DoubleValue))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                        throw new InvalidTypeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    writeDouble(((PrimitiveValue)val).doubleValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                case JDWP.Tag.INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    if(!(val instanceof IntegerValue))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                        throw new InvalidTypeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    writeInt(((PrimitiveValue)val).intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                case JDWP.Tag.LONG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    if(!(val instanceof LongValue))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                        throw new InvalidTypeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    writeLong(((PrimitiveValue)val).longValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                case JDWP.Tag.SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    if(!(val instanceof ShortValue))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                        throw new InvalidTypeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    writeShort(((PrimitiveValue)val).shortValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                case JDWP.Tag.BOOLEAN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    if(!(val instanceof BooleanValue))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        throw new InvalidTypeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    writeBoolean(((PrimitiveValue)val).booleanValue());
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
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Read byte represented as one bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    byte readByte() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        byte ret = pkt.data[inCursor];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        inCursor += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Read boolean represented as one byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    boolean readBoolean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        byte ret = readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return (ret != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Read char represented as two bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    char readChar() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        int b1, b2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        b1 = pkt.data[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        b2 = pkt.data[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return (char)((b1 << 8) + b2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Read short represented as two bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    short readShort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        int b1, b2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        b1 = pkt.data[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        b2 = pkt.data[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return (short)((b1 << 8) + b2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Read int represented as four bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    int readInt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        int b1,b2,b3,b4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        b1 = pkt.data[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        b2 = pkt.data[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        b3 = pkt.data[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        b4 = pkt.data[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return ((b1 << 24) + (b2 << 16) + (b3 << 8) + b4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * Read long represented as eight bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    long readLong() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        long b1,b2,b3,b4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        long b5,b6,b7,b8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        b1 = pkt.data[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        b2 = pkt.data[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        b3 = pkt.data[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        b4 = pkt.data[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        b5 = pkt.data[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        b6 = pkt.data[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        b7 = pkt.data[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        b8 = pkt.data[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return ((b1 << 56) + (b2 << 48) + (b3 << 40) + (b4 << 32)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                + (b5 << 24) + (b6 << 16) + (b7 << 8) + b8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * Read float represented as four bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    float readFloat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        return Float.intBitsToFloat(readInt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * Read double represented as eight bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    double readDouble() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        return Double.longBitsToDouble(readLong());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * Read string represented as four byte length followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * characters of the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    String readString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        String ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        int len = readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            ret = new String(pkt.data, inCursor, len, "UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        } catch(java.io.UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            System.err.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            ret = "Conversion error!";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        inCursor += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    private long readID(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        switch (size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
          case 8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
              return readLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
          case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
              return (long)readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
          case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
              return (long)readShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
          default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
              throw new UnsupportedOperationException("JDWP: ID size not supported: " + size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * Read object represented as vm specific byte sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    long readObjectRef() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        return readID(vm.sizeofObjectRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    long readClassRef() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        return readID(vm.sizeofClassRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    ObjectReferenceImpl readTaggedObjectReference() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        byte typeKey = readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        return vm.objectMirror(readObjectRef(), typeKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    ObjectReferenceImpl readObjectReference() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        return vm.objectMirror(readObjectRef());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    StringReferenceImpl readStringReference() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        long ref = readObjectRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        return vm.stringMirror(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    ArrayReferenceImpl readArrayReference() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        long ref = readObjectRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        return vm.arrayMirror(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    ThreadReferenceImpl readThreadReference() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        long ref = readObjectRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        return vm.threadMirror(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    ThreadGroupReferenceImpl readThreadGroupReference() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        long ref = readObjectRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        return vm.threadGroupMirror(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    ClassLoaderReferenceImpl readClassLoaderReference() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        long ref = readObjectRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        return vm.classLoaderMirror(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    ClassObjectReferenceImpl readClassObjectReference() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        long ref = readObjectRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        return vm.classObjectMirror(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    ReferenceTypeImpl readReferenceType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        byte tag = readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        long ref = readObjectRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        return vm.referenceType(ref, tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * Read method reference represented as vm specific byte sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    long readMethodRef() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        return readID(vm.sizeofMethodRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * Read field reference represented as vm specific byte sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    long readFieldRef() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        return readID(vm.sizeofFieldRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * Read field represented as vm specific byte sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    Field readField() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   488
        ReferenceTypeImpl refType = readReferenceType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        long fieldRef = readFieldRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        return refType.getFieldMirror(fieldRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * Read frame represented as vm specific byte sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    long readFrameRef() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        return readID(vm.sizeofFrameRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * Read a value, first byte describes type of value to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    ValueImpl readValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        byte typeKey = readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        return readUntaggedValue(typeKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    ValueImpl readUntaggedValue(byte typeKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        ValueImpl val = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        if (isObjectTag(typeKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            val = vm.objectMirror(readObjectRef(), typeKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            switch(typeKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                case JDWP.Tag.BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    val = new ByteValueImpl(vm, readByte());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                case JDWP.Tag.CHAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                    val = new CharValueImpl(vm, readChar());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                case JDWP.Tag.FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    val = new FloatValueImpl(vm, readFloat());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                case JDWP.Tag.DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    val = new DoubleValueImpl(vm, readDouble());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                case JDWP.Tag.INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    val = new IntegerValueImpl(vm, readInt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                case JDWP.Tag.LONG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    val = new LongValueImpl(vm, readLong());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                case JDWP.Tag.SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    val = new ShortValueImpl(vm, readShort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                case JDWP.Tag.BOOLEAN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    val = new BooleanValueImpl(vm, readBoolean());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                case JDWP.Tag.VOID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    val = new VoidValueImpl(vm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * Read location represented as vm specific byte sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    Location readLocation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        byte tag = readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        long classRef = readObjectRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        long methodRef = readMethodRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        long codeIndex = readLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        if (classRef != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            /* Valid location */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            ReferenceTypeImpl refType = vm.referenceType(classRef, tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            return new LocationImpl(vm, refType, methodRef, codeIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            /* Null location (example: uncaught exception) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
           return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    byte[] readByteArray(int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        byte[] array = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        System.arraycopy(pkt.data, inCursor, array, 0, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        inCursor += length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        return array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    List<Value> readArrayRegion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        byte typeKey = readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        int length = readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        List<Value> list = new ArrayList<Value>(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        boolean gettingObjects = isObjectTag(typeKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
             * Each object comes back with a type key which might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
             * identify a more specific type than the type key we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
             * passed in, so we use it in the decodeValue call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
             * (For primitives, we just use the original one)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            if (gettingObjects) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                typeKey = readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            Value value = readUntaggedValue(typeKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            list.add(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    void writeArrayRegion(List<Value> srcValues) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        writeInt(srcValues.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        for (int i = 0; i < srcValues.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            Value value = srcValues.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            writeUntaggedValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    int skipBytes(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        inCursor += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    byte command() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        return (byte)pkt.cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    static boolean isObjectTag(byte tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        return (tag == JDWP.Tag.OBJECT) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
               (tag == JDWP.Tag.ARRAY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
               (tag == JDWP.Tag.STRING) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
               (tag == JDWP.Tag.THREAD) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
               (tag == JDWP.Tag.THREAD_GROUP) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
               (tag == JDWP.Tag.CLASS_LOADER) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
               (tag == JDWP.Tag.CLASS_OBJECT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
}