jdk/src/share/classes/com/sun/tools/jdi/ObjectReferenceImpl.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 51 6fe31bc95bbc
permissions -rw-r--r--
Initial load
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-2006 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class ObjectReferenceImpl extends ValueImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
             implements ObjectReference, VMListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    protected long ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private ReferenceType type = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private int gcDisableCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    boolean addedListener = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    // This is cached only while the VM is suspended
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    protected static class Cache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        JDWP.ObjectReference.MonitorInfo monitorInfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final Cache noInitCache = new Cache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final Cache markerCache = new Cache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private Cache cache = noInitCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private void disableCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        synchronized (vm.state()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            cache = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private void enableCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        synchronized (vm.state()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            cache = markerCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // Override in subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    protected Cache newCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return new Cache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    protected Cache getCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        synchronized (vm.state()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            if (cache == noInitCache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                if (vm.state().isSuspended()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    // Set cache now, otherwise newly created objects are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    // not cached until resuspend
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    enableCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    disableCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            if (cache == markerCache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                cache = newCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            return cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // Return the ClassTypeImpl upon which to invoke a method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // By default it is our very own referenceType() but subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // can override.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    protected ClassTypeImpl invokableReferenceType(Method method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return (ClassTypeImpl)referenceType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    ObjectReferenceImpl(VirtualMachine aVm,long aRef) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        super(aVm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        ref = aRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    protected String description() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return "ObjectReference " + uniqueID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * VMListener implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public boolean vmSuspended(VMAction action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        enableCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public boolean vmNotSuspended(VMAction action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        // make sure that cache and listener management are synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        synchronized (vm.state()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (cache != null && (vm.traceFlags & vm.TRACE_OBJREFS) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                vm.printTrace("Clearing temporary cache for " + description());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            disableCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (addedListener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                 * If a listener was added (i.e. this is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                 * ObjectReference that adds a listener on startup),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                 * remove it here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                addedListener = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                return false;  // false says remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if ((obj != null) && (obj instanceof ObjectReferenceImpl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            ObjectReferenceImpl other = (ObjectReferenceImpl)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            return (ref() == other.ref()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                   super.equals(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            return false;
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
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return(int)ref();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public Type type() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return referenceType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public ReferenceType referenceType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (type == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                JDWP.ObjectReference.ReferenceType rtinfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    JDWP.ObjectReference.ReferenceType.process(vm, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                type = vm.referenceType(rtinfo.typeID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                                        rtinfo.refTypeTag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public Value getValue(Field sig) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        List<Field> list = new ArrayList<Field>(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        list.add(sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        Map map = getValues(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return(Value)map.get(sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public Map<Field,Value> getValues(List<? extends Field> theFields) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        validateMirrors(theFields);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        List<Field> staticFields = new ArrayList<Field>(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        int size = theFields.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        List<Field> instanceFields = new ArrayList<Field>(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        for (int i=0; i<size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            Field field = (Field)theFields.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            // Make sure the field is valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            ((ReferenceTypeImpl)referenceType()).validateFieldAccess(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            // FIX ME! We need to do some sanity checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            // here; make sure the field belongs to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            // object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            if (field.isStatic())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                staticFields.add(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                instanceFields.add(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        Map<Field, Value> map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (staticFields.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            map = referenceType().getValues(staticFields);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            map = new HashMap<Field, Value>(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        size = instanceFields.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        JDWP.ObjectReference.GetValues.Field[] queryFields =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                         new JDWP.ObjectReference.GetValues.Field[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        for (int i=0; i<size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            FieldImpl field = (FieldImpl)instanceFields.get(i);/* thanks OTI */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            queryFields[i] = new JDWP.ObjectReference.GetValues.Field(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                                         field.ref());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        ValueImpl[] values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            values = JDWP.ObjectReference.GetValues.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                     process(vm, this, queryFields).values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (size != values.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            throw new InternalException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                         "Wrong number of values returned from target VM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        for (int i=0; i<size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            FieldImpl field = (FieldImpl)instanceFields.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            map.put(field, values[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public void setValue(Field field, Value value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                   throws InvalidTypeException, ClassNotLoadedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        validateMirror(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        validateMirrorOrNull(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        // Make sure the field is valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        ((ReferenceTypeImpl)referenceType()).validateFieldSet(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (field.isStatic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            ReferenceType type = referenceType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            if (type instanceof ClassType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                ((ClassType)type).setValue(field, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                    "Invalid type for static field set");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            JDWP.ObjectReference.SetValues.FieldValue[] fvals =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                      new JDWP.ObjectReference.SetValues.FieldValue[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            fvals[0] = new JDWP.ObjectReference.SetValues.FieldValue(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                           ((FieldImpl)field).ref(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                           // Validate and convert if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                           ValueImpl.prepareForAssignment(value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                                                          (FieldImpl)field));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                JDWP.ObjectReference.SetValues.process(vm, this, fvals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        } catch (ClassNotLoadedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
             * Since we got this exception,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
             * the field type must be a reference type. The value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
             * we're trying to set is null, but if the field's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
             * class has not yet been loaded through the enclosing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
             * class loader, then setting to null is essentially a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
             * no-op, and we should allow it without an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    void validateMethodInvocation(Method method, int options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                                         throws InvalidTypeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                         InvocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
         * Method must be in this object's class, a superclass, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
         * implemented interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        ReferenceTypeImpl declType = (ReferenceTypeImpl)method.declaringType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if (!declType.isAssignableFrom(this)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            throw new IllegalArgumentException("Invalid method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        ClassTypeImpl clazz = invokableReferenceType(method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
         * Method must be a non-constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (method.isConstructor()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            throw new IllegalArgumentException("Cannot invoke constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
         * For nonvirtual invokes, method must have a body
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if ((options & INVOKE_NONVIRTUAL) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if (method.declaringType() instanceof InterfaceType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                throw new IllegalArgumentException("Interface method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            } else if (method.isAbstract()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                throw new IllegalArgumentException("Abstract method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            }
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
         * Get the class containing the method that will be invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
         * This class is needed only for proper validation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
         * method argument types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        ClassTypeImpl invokedClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if ((options & INVOKE_NONVIRTUAL) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            // No overrides in non-virtual invokes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            invokedClass = clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
             * For virtual invokes, find any override of the method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
             * Since we are looking for a method with a real body, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
             * don't need to bother with interfaces/abstract methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            Method invoker = clazz.concreteMethodByName(method.name(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                                        method.signature());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            //  isAssignableFrom check above guarantees non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            invokedClass = (ClassTypeImpl)invoker.declaringType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        /* The above code is left over from previous versions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
         * We haven't had time to divine the intent.  jjh, 7/31/2003
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
    PacketStream sendInvokeCommand(final ThreadReferenceImpl thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                   final ClassTypeImpl refType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                                   final MethodImpl method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                                   final ValueImpl[] args,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                                   final int options) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        CommandSender sender =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            new CommandSender() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                public PacketStream send() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    return JDWP.ObjectReference.InvokeMethod.enqueueCommand(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                                          vm, ObjectReferenceImpl.this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                                          thread, refType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                                          method.ref(), args, options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        PacketStream stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if ((options & INVOKE_SINGLE_THREADED) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            stream = thread.sendResumingCommand(sender);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            stream = vm.sendResumingCommand(sender);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        return stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public Value invokeMethod(ThreadReference threadIntf, Method methodIntf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                              List<? extends Value> origArguments, int options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                              throws InvalidTypeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                                     IncompatibleThreadStateException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                                     InvocationException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                                     ClassNotLoadedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        validateMirror(threadIntf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        validateMirror(methodIntf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        validateMirrorsOrNulls(origArguments);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        MethodImpl method = (MethodImpl)methodIntf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        ThreadReferenceImpl thread = (ThreadReferenceImpl)threadIntf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (method.isStatic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            if (referenceType() instanceof ClassType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                ClassType type = (ClassType)referenceType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                return type.invokeMethod(thread, method, origArguments, options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                throw new IllegalArgumentException("Invalid type for static method invocation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        validateMethodInvocation(method, options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        List<Value> arguments = method.validateAndPrepareArgumentsForInvoke(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                                                  origArguments);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        ValueImpl[] args = (ValueImpl[])arguments.toArray(new ValueImpl[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        JDWP.ObjectReference.InvokeMethod ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            PacketStream stream =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                sendInvokeCommand(thread, invokableReferenceType(method),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                  method, args, options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            ret = JDWP.ObjectReference.InvokeMethod.waitForReply(vm, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            if (exc.errorCode() == JDWP.Error.INVALID_THREAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                throw new IncompatibleThreadStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
         * There is an implict VM-wide suspend at the conclusion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
         * of a normal (non-single-threaded) method invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if ((options & INVOKE_SINGLE_THREADED) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            vm.notifySuspend();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (ret.exception != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            throw new InvocationException(ret.exception);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            return ret.returnValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /* leave synchronized to keep count accurate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    public synchronized void disableCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        if (gcDisableCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                JDWP.ObjectReference.DisableCollection.process(vm, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        gcDisableCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    /* leave synchronized to keep count accurate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    public synchronized void enableCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        gcDisableCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        if (gcDisableCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                JDWP.ObjectReference.EnableCollection.process(vm, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                // If already collected, no harm done, no exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                if (exc.errorCode() != JDWP.Error.INVALID_OBJECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    public boolean isCollected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            return JDWP.ObjectReference.IsCollected.process(vm, this).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                                                              isCollected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    public long uniqueID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        return ref();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    JDWP.ObjectReference.MonitorInfo jdwpMonitorInfo()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                             throws IncompatibleThreadStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        JDWP.ObjectReference.MonitorInfo info = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            Cache local;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            // getCache() and addlistener() must be synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            // so that no events are lost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            synchronized (vm.state()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                local = getCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                if (local != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    info = local.monitorInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    // Check if there will be something to cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    // and there is not already a listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    if (info == null && !vm.state().hasListener(this)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                        /* For other, less numerous objects, this is done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                         * in the constructor. Since there can be many
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                         * ObjectReferences, the VM listener is installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                         * and removed as needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                         * Listener must be installed before process()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                        vm.state().addListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                        addedListener = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            if (info == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                info = JDWP.ObjectReference.MonitorInfo.process(vm, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                if (local != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    local.monitorInfo = info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                    if ((vm.traceFlags & vm.TRACE_OBJREFS) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                        vm.printTrace("ObjectReference " + uniqueID() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                                      " temporarily caching monitor info");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
             if (exc.errorCode() == JDWP.Error.THREAD_NOT_SUSPENDED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                 throw new IncompatibleThreadStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
             } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                 throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        return info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    public List<ThreadReference> waitingThreads() throws IncompatibleThreadStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        return Arrays.asList((ThreadReference[])jdwpMonitorInfo().waiters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    public ThreadReference owningThread() throws IncompatibleThreadStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        return jdwpMonitorInfo().owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    public int entryCount() throws IncompatibleThreadStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        return jdwpMonitorInfo().entryCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    public List<ObjectReference> referringObjects(long maxReferrers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        if (!vm.canGetInstanceInfo()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                "target does not support getting referring objects");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        if (maxReferrers < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            throw new IllegalArgumentException("maxReferrers is less than zero: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                                              + maxReferrers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        int intMax = (maxReferrers > Integer.MAX_VALUE)?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            Integer.MAX_VALUE: (int)maxReferrers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        // JDWP can't currently handle more than this (in mustang)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            return Arrays.asList((ObjectReference[])JDWP.ObjectReference.ReferringObjects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                                process(vm, this, intMax).referringObjects);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    long ref() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        return ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    boolean isClassObject() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
         * Don't need to worry about subclasses since java.lang.Class is final.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        return referenceType().name().equals("java.lang.Class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    ValueImpl prepareForAssignmentTo(ValueContainer destination)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                                 throws InvalidTypeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                                        ClassNotLoadedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        validateAssignment(destination);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        return this;            // conversion never necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    void validateAssignment(ValueContainer destination)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                            throws InvalidTypeException, ClassNotLoadedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
         * Do these simpler checks before attempting a query of the destination's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
         * type which might cause a confusing ClassNotLoadedException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
         * the destination is primitive or an array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
         * TO DO: Centralize JNI signature knowledge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        if (destination.signature().length() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            throw new InvalidTypeException("Can't assign object value to primitive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        if ((destination.signature().charAt(0) == '[') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            (type().signature().charAt(0) != '[')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            throw new InvalidTypeException("Can't assign non-array value to an array");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        if ("void".equals(destination.typeName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            throw new InvalidTypeException("Can't assign object value to a void");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        // Validate assignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        ReferenceType destType = (ReferenceTypeImpl)destination.type();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        ReferenceTypeImpl myType = (ReferenceTypeImpl)referenceType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        if (!myType.isAssignableTo((ReferenceType)destType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            JNITypeParser parser = new JNITypeParser(destType.signature());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            String destTypeName = parser.typeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            throw new InvalidTypeException("Can't assign " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                                           type().name() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                                           " to " + destTypeName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        return "instance of " + referenceType().name() + "(id=" + uniqueID() + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    byte typeValueKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        return JDWP.Tag.OBJECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
}