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