jdk/src/share/classes/com/sun/tools/jdi/MethodImpl.java
author jbachorik
Tue, 29 Apr 2014 11:15:21 +0200
changeset 24125 b85eeaae56c7
parent 14342 8435a30053c1
child 24127 5d05d4c0de7f
permissions -rw-r--r--
8031195: Support default and static interface methods in JDI, JDWP and JDB Reviewed-by: sla, sspitsyn, dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 11277
diff changeset
     2
 * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.jdi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public abstract class MethodImpl extends TypeComponentImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    implements Method {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private JNITypeParser signatureParser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    abstract int argSlotCount() throws AbsentInformationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    abstract List<Location> allLineLocations(SDE.Stratum stratum,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
                                   String sourceName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                           throws AbsentInformationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    abstract List<Location> locationsOfLine(SDE.Stratum stratum,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
                                  String sourceName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                                  int lineNumber)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                           throws AbsentInformationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    MethodImpl(VirtualMachine vm, ReferenceTypeImpl declaringType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
               long ref,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
               String name, String signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
               String genericSignature, int modifiers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        super(vm, declaringType, ref, name, signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
              genericSignature, modifiers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        signatureParser = new JNITypeParser(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static MethodImpl createMethodImpl(VirtualMachine vm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                                       ReferenceTypeImpl declaringType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                                       long ref,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                                       String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                                       String signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                                       String genericSignature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                                       int modifiers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if ((modifiers &
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
             (VMModifiers.NATIVE | VMModifiers.ABSTRACT)) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            return new NonConcreteMethodImpl(vm, declaringType, ref,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                                             name, signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                             genericSignature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                                             modifiers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            return new ConcreteMethodImpl(vm, declaringType, ref,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                          name, signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                          genericSignature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                          modifiers);
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
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if ((obj != null) && (obj instanceof MethodImpl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            MethodImpl other = (MethodImpl)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            return (declaringType().equals(other.declaringType())) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                   (ref() == other.ref()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                   super.equals(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return (int)ref();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public final List<Location> allLineLocations()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                           throws AbsentInformationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return allLineLocations(vm.getDefaultStratum(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public List<Location> allLineLocations(String stratumID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                 String sourceName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                           throws AbsentInformationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return allLineLocations(declaringType.stratum(stratumID),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                sourceName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public final List<Location> locationsOfLine(int lineNumber)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                           throws AbsentInformationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return locationsOfLine(vm.getDefaultStratum(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                               null, lineNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public List<Location> locationsOfLine(String stratumID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                String sourceName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                int lineNumber)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                           throws AbsentInformationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return locationsOfLine(declaringType.stratum(stratumID),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                               sourceName, lineNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    LineInfo codeIndexToLineInfo(SDE.Stratum stratum,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                 long codeIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (stratum.isJava()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            return new BaseLineInfo(-1, declaringType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return new StratumLineInfo(stratum.id(), -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                       null, null);
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
     * @return a text representation of the declared return type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public String returnTypeName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return signatureParser.typeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private String returnSignature() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return signatureParser.signature();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public Type returnType() throws ClassNotLoadedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return findType(returnSignature());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public Type findType(String signature) throws ClassNotLoadedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        ReferenceTypeImpl enclosing = (ReferenceTypeImpl)declaringType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return enclosing.findType(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public List<String> argumentTypeNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return signatureParser.argumentTypeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public List<String> argumentSignatures() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return signatureParser.argumentSignatures();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    Type argumentType(int index) throws ClassNotLoadedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        ReferenceTypeImpl enclosing = (ReferenceTypeImpl)declaringType();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   161
        String signature = argumentSignatures().get(index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return enclosing.findType(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public List<Type> argumentTypes() throws ClassNotLoadedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        int size = argumentSignatures().size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        ArrayList<Type> types = new ArrayList<Type>(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            Type type = argumentType(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            types.add(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return types;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public int compareTo(Method method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        ReferenceTypeImpl declaringType = (ReferenceTypeImpl)declaringType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        int rc = declaringType.compareTo(method.declaringType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (rc == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            rc = declaringType.indexOf(this) -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    declaringType.indexOf(method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public boolean isAbstract() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        return isModifierSet(VMModifiers.ABSTRACT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
24125
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   190
    public boolean isDefault() {
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   191
        return !isModifierSet(VMModifiers.ABSTRACT) &&
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   192
               !isModifierSet(VMModifiers.STATIC) &&
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   193
               !isModifierSet(VMModifiers.PRIVATE) &&
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   194
               declaringType() instanceof InterfaceType;
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   195
    }
b85eeaae56c7 8031195: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents: 14342
diff changeset
   196
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public boolean isSynchronized() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return isModifierSet(VMModifiers.SYNCHRONIZED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public boolean isNative() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return isModifierSet(VMModifiers.NATIVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public boolean isVarArgs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return isModifierSet(VMModifiers.VARARGS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public boolean isBridge() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return isModifierSet(VMModifiers.BRIDGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public boolean isConstructor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        return name().equals("<init>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public boolean isStaticInitializer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return name().equals("<clinit>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public boolean isObsolete() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            return JDWP.Method.IsObsolete.process(vm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                    declaringType, ref).isObsolete;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * A container class for the return value to allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * proper type-checking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    class ReturnContainer implements ValueContainer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        ReturnContainer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        public Type type() throws ClassNotLoadedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return returnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        public String typeName(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            return returnTypeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        public String signature() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            return returnSignature(); //type().signature();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        public Type findType(String signature) throws ClassNotLoadedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            return MethodImpl.this.findType(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    ReturnContainer retValContainer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    ReturnContainer getReturnValueContainer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (retValContainer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            retValContainer = new ReturnContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return retValContainer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * A container class for the argument to allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * proper type-checking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    class ArgumentContainer implements ValueContainer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        ArgumentContainer(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            this.index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        public Type type() throws ClassNotLoadedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            return argumentType(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        public String typeName(){
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   273
            return argumentTypeNames().get(index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        public String signature() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   276
            return argumentSignatures().get(index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        public Type findType(String signature) throws ClassNotLoadedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            return MethodImpl.this.findType(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * This is a var args method.  Thus, its last param is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * array. If the method has n params, then:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * 1.  If there are n args and the last is the same type as the type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *     the last param, do nothing.  IE, a String[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *     can be passed to a String...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * 2.  If there are >= n arguments and for each arg whose number is >= n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *     the arg type is 'compatible' with the component type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *     the last param, then do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *     - create an array of the type of the last param
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *     - put the n, ... args into this array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *       We might have to do conversions here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *     - put this array into arguments(n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *     - delete arguments(n+1), ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * NOTE that this might modify the input list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    void handleVarArgs(List<Value> arguments)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        throws ClassNotLoadedException, InvalidTypeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        List<Type> paramTypes = this.argumentTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        ArrayType lastParamType = (ArrayType)paramTypes.get(paramTypes.size() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        Type componentType = lastParamType.componentType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        int argCount = arguments.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        int paramCount = paramTypes.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (argCount < paramCount - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            // Error; will be caught later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if (argCount == paramCount - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            // It is ok to pass 0 args to the var arg.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            // We have to gen a 0 length array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            ArrayReference argArray = lastParamType.newInstance(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            arguments.add(argArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   317
        Value nthArgValue = arguments.get(paramCount - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (nthArgValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        Type nthArgType = nthArgValue.type();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (nthArgType instanceof ArrayTypeImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            if (argCount == paramCount &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                ((ArrayTypeImpl)nthArgType).isAssignableTo(lastParamType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                 * This is case 1.  A compatible array is being passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                 * var args array param.  We don't have to do anything.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
         * Case 2.  We have to verify that the n, n+1, ... args are compatible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
         * with componentType, and do conversions if necessary and create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
         * an array of componentType to hold these possibly converted values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        int count = argCount - paramCount + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        ArrayReference argArray = lastParamType.newInstance(count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
         * This will copy arguments(paramCount - 1) ... to argArray(0) ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
         * doing whatever conversions are needed!  It will throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
         * exception if an incompatible arg is encountered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        argArray.setValues(0, arguments, paramCount - 1, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        arguments.set(paramCount - 1, argArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
         * Remove the excess args
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        for (int ii = paramCount; ii < argCount; ii++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            arguments.remove(paramCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * The output list will be different than the input list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    List<Value> validateAndPrepareArgumentsForInvoke(List<? extends Value> origArguments)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                         throws ClassNotLoadedException, InvalidTypeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        List<Value> arguments = new ArrayList<Value>(origArguments);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (isVarArgs()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            handleVarArgs(arguments);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        int argSize = arguments.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        JNITypeParser parser = new JNITypeParser(signature());
11277
e3a1c90dd439 7117053: Fix build warnings in com/sun/tools/jdi/*
jjh
parents: 5506
diff changeset
   372
        List<String> signatures = parser.argumentSignatures();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (signatures.size() != argSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            throw new IllegalArgumentException("Invalid argument count: expected " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                                               signatures.size() + ", received " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                                               arguments.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        for (int i = 0; i < argSize; i++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   381
            Value value = arguments.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            value = ValueImpl.prepareForAssignment(value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                                                   new ArgumentContainer(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            arguments.set(i, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        return arguments;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        sb.append(declaringType().name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        sb.append(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        sb.append(name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        sb.append("(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        boolean first = true;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   396
        for (String name : argumentTypeNames()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            if (!first) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                sb.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   400
            sb.append(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            first = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        sb.append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
}