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