jdk/src/share/classes/com/sun/tools/jdi/ObjectReferenceImpl.java
author jbachorik
Tue, 29 Apr 2014 11:15:21 +0200
changeset 24125 b85eeaae56c7
parent 14342 8435a30053c1
child 24127 5d05d4c0de7f
permissions -rw-r--r--
8031195: Support default and static interface methods in JDI, JDWP and JDB Reviewed-by: sla, sspitsyn, dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 11277
diff changeset
     2
 * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
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()) {
11277
e3a1c90dd439 7117053: Fix build warnings in com/sun/tools/jdi/*
jjh
parents: 5506
diff changeset
   113
            if (cache != null && (vm.traceFlags & VirtualMachine.TRACE_OBJREFS) != 0) {
2
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);
11277
e3a1c90dd439 7117053: Fix build warnings in com/sun/tools/jdi/*
jjh
parents: 5506
diff changeset
   166
        Map<Field, Value> map = getValues(list);
e3a1c90dd439 7117053: Fix build warnings in com/sun/tools/jdi/*
jjh
parents: 5506
diff changeset
   167
        return map.get(sig);
2
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
         * Method must be in this object's class, a superclass, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
         * implemented interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        ReferenceTypeImpl declType = (ReferenceTypeImpl)method.declaringType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (!declType.isAssignableFrom(this)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            throw new IllegalArgumentException("Invalid method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
24125
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   289
        if (declType instanceof ClassTypeImpl) {
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   290
            validateClassMethodInvocation(method, options);
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   291
        } else if (declType instanceof InterfaceTypeImpl) {
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   292
            validateIfaceMethodInvocation(method, options);
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   293
        } else {
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   294
            throw new InvalidTypeException();
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   295
        }
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   296
    }
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   297
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   298
    void validateClassMethodInvocation(Method method, int options)
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   299
                                         throws InvalidTypeException,
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   300
                                         InvocationException {
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   301
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        ClassTypeImpl clazz = invokableReferenceType(method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
         * Method must be a non-constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (method.isConstructor()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            throw new IllegalArgumentException("Cannot invoke constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
         * For nonvirtual invokes, method must have a body
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if ((options & INVOKE_NONVIRTUAL) != 0) {
24125
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   315
            if (method.isAbstract()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                throw new IllegalArgumentException("Abstract method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
         * Get the class containing the method that will be invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         * This class is needed only for proper validation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         * method argument types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        ClassTypeImpl invokedClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if ((options & INVOKE_NONVIRTUAL) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            // No overrides in non-virtual invokes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            invokedClass = clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
             * For virtual invokes, find any override of the method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
             * Since we are looking for a method with a real body, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
             * don't need to bother with interfaces/abstract methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            Method invoker = clazz.concreteMethodByName(method.name(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                                        method.signature());
24125
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   337
            //  invoker is supposed to be non-null under normal circumstances
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            invokedClass = (ClassTypeImpl)invoker.declaringType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        /* The above code is left over from previous versions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
         * We haven't had time to divine the intent.  jjh, 7/31/2003
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
24125
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   345
    void validateIfaceMethodInvocation(Method method, int options)
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   346
                                         throws InvalidTypeException,
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   347
                                         InvocationException {
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   348
        /*
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   349
         * Only default methods allowed for nonvirtual invokes
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   350
         */
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   351
        if (!method.isDefault()) {
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   352
            throw new IllegalArgumentException("Not a default method");
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   353
        }
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   354
    }
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   355
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    PacketStream sendInvokeCommand(final ThreadReferenceImpl thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                   final ClassTypeImpl refType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                                   final MethodImpl method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                                   final ValueImpl[] args,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                   final int options) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        CommandSender sender =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            new CommandSender() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                public PacketStream send() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    return JDWP.ObjectReference.InvokeMethod.enqueueCommand(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                                          vm, ObjectReferenceImpl.this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                                          thread, refType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                                          method.ref(), args, options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        PacketStream stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if ((options & INVOKE_SINGLE_THREADED) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            stream = thread.sendResumingCommand(sender);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            stream = vm.sendResumingCommand(sender);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public Value invokeMethod(ThreadReference threadIntf, Method methodIntf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                              List<? extends Value> origArguments, int options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                              throws InvalidTypeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                                     IncompatibleThreadStateException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                                     InvocationException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                                     ClassNotLoadedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        validateMirror(threadIntf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        validateMirror(methodIntf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        validateMirrorsOrNulls(origArguments);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        MethodImpl method = (MethodImpl)methodIntf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        ThreadReferenceImpl thread = (ThreadReferenceImpl)threadIntf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (method.isStatic()) {
24125
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   394
            if (referenceType() instanceof InterfaceType) {
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   395
                InterfaceType type = (InterfaceType)referenceType();
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   396
                return type.invokeMethod(thread, method, origArguments, options);
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   397
            } else if (referenceType() instanceof ClassType) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                ClassType type = (ClassType)referenceType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                return type.invokeMethod(thread, method, origArguments, options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                throw new IllegalArgumentException("Invalid type for static method invocation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        validateMethodInvocation(method, options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        List<Value> arguments = method.validateAndPrepareArgumentsForInvoke(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                                                  origArguments);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   410
        ValueImpl[] args = arguments.toArray(new ValueImpl[0]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        JDWP.ObjectReference.InvokeMethod ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            PacketStream stream =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                sendInvokeCommand(thread, invokableReferenceType(method),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                                  method, args, options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            ret = JDWP.ObjectReference.InvokeMethod.waitForReply(vm, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            if (exc.errorCode() == JDWP.Error.INVALID_THREAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                throw new IncompatibleThreadStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
         * There is an implict VM-wide suspend at the conclusion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
         * of a normal (non-single-threaded) method invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        if ((options & INVOKE_SINGLE_THREADED) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            vm.notifySuspend();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (ret.exception != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            throw new InvocationException(ret.exception);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            return ret.returnValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    /* leave synchronized to keep count accurate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    public synchronized void disableCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if (gcDisableCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                JDWP.ObjectReference.DisableCollection.process(vm, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        gcDisableCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    /* leave synchronized to keep count accurate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    public synchronized void enableCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        gcDisableCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if (gcDisableCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                JDWP.ObjectReference.EnableCollection.process(vm, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                // If already collected, no harm done, no exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                if (exc.errorCode() != JDWP.Error.INVALID_OBJECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    public boolean isCollected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            return JDWP.ObjectReference.IsCollected.process(vm, this).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                                                              isCollected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            throw exc.toJDIException();
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
    public long uniqueID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        return ref();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    JDWP.ObjectReference.MonitorInfo jdwpMonitorInfo()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                             throws IncompatibleThreadStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        JDWP.ObjectReference.MonitorInfo info = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            Cache local;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            // getCache() and addlistener() must be synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            // so that no events are lost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            synchronized (vm.state()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                local = getCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                if (local != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    info = local.monitorInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    // Check if there will be something to cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    // and there is not already a listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    if (info == null && !vm.state().hasListener(this)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                        /* For other, less numerous objects, this is done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                         * in the constructor. Since there can be many
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                         * ObjectReferences, the VM listener is installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                         * and removed as needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                         * Listener must be installed before process()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                        vm.state().addListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                        addedListener = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            if (info == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                info = JDWP.ObjectReference.MonitorInfo.process(vm, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                if (local != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                    local.monitorInfo = info;
11277
e3a1c90dd439 7117053: Fix build warnings in com/sun/tools/jdi/*
jjh
parents: 5506
diff changeset
   514
                    if ((vm.traceFlags & VirtualMachine.TRACE_OBJREFS) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                        vm.printTrace("ObjectReference " + uniqueID() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                                      " temporarily caching monitor info");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
             if (exc.errorCode() == JDWP.Error.THREAD_NOT_SUSPENDED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                 throw new IncompatibleThreadStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
             } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                 throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        return info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    public List<ThreadReference> waitingThreads() throws IncompatibleThreadStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        return Arrays.asList((ThreadReference[])jdwpMonitorInfo().waiters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    public ThreadReference owningThread() throws IncompatibleThreadStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        return jdwpMonitorInfo().owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    public int entryCount() throws IncompatibleThreadStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        return jdwpMonitorInfo().entryCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    public List<ObjectReference> referringObjects(long maxReferrers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        if (!vm.canGetInstanceInfo()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                "target does not support getting referring objects");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        if (maxReferrers < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            throw new IllegalArgumentException("maxReferrers is less than zero: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                                              + maxReferrers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        int intMax = (maxReferrers > Integer.MAX_VALUE)?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            Integer.MAX_VALUE: (int)maxReferrers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        // JDWP can't currently handle more than this (in mustang)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            return Arrays.asList((ObjectReference[])JDWP.ObjectReference.ReferringObjects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                                process(vm, this, intMax).referringObjects);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    long ref() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        return ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    boolean isClassObject() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
         * Don't need to worry about subclasses since java.lang.Class is final.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        return referenceType().name().equals("java.lang.Class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    ValueImpl prepareForAssignmentTo(ValueContainer destination)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                                 throws InvalidTypeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                                        ClassNotLoadedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        validateAssignment(destination);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        return this;            // conversion never necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    void validateAssignment(ValueContainer destination)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                            throws InvalidTypeException, ClassNotLoadedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
         * Do these simpler checks before attempting a query of the destination's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
         * type which might cause a confusing ClassNotLoadedException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
         * the destination is primitive or an array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
         * TO DO: Centralize JNI signature knowledge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        if (destination.signature().length() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            throw new InvalidTypeException("Can't assign object value to primitive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        if ((destination.signature().charAt(0) == '[') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            (type().signature().charAt(0) != '[')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            throw new InvalidTypeException("Can't assign non-array value to an array");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        if ("void".equals(destination.typeName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            throw new InvalidTypeException("Can't assign object value to a void");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        // Validate assignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        ReferenceType destType = (ReferenceTypeImpl)destination.type();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        ReferenceTypeImpl myType = (ReferenceTypeImpl)referenceType();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   610
        if (!myType.isAssignableTo(destType)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            JNITypeParser parser = new JNITypeParser(destType.signature());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            String destTypeName = parser.typeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            throw new InvalidTypeException("Can't assign " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                                           type().name() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                                           " to " + destTypeName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        return "instance of " + referenceType().name() + "(id=" + uniqueID() + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    byte typeValueKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        return JDWP.Tag.OBJECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
}