jdk/src/share/classes/com/sun/tools/jdi/MethodImpl.java
author alanb
Fri, 02 Nov 2012 15:50:11 +0000
changeset 14342 8435a30053c1
parent 11277 e3a1c90dd439
child 24125 b85eeaae56c7
permissions -rw-r--r--
7197491: update copyright year to match last edit in jdk8 jdk repository Reviewed-by: chegar, ksrini
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public boolean isSynchronized() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return isModifierSet(VMModifiers.SYNCHRONIZED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public boolean isNative() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return isModifierSet(VMModifiers.NATIVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public boolean isVarArgs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return isModifierSet(VMModifiers.VARARGS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public boolean isBridge() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return isModifierSet(VMModifiers.BRIDGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public boolean isConstructor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return name().equals("<init>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public boolean isStaticInitializer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return name().equals("<clinit>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public boolean isObsolete() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            return JDWP.Method.IsObsolete.process(vm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                    declaringType, ref).isObsolete;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * A container class for the return value to allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * proper type-checking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    class ReturnContainer implements ValueContainer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        ReturnContainer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        public Type type() throws ClassNotLoadedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            return returnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        public String typeName(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            return returnTypeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        public String signature() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            return returnSignature(); //type().signature();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        public Type findType(String signature) throws ClassNotLoadedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            return MethodImpl.this.findType(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    ReturnContainer retValContainer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    ReturnContainer getReturnValueContainer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (retValContainer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            retValContainer = new ReturnContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        return retValContainer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * A container class for the argument to allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * proper type-checking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    class ArgumentContainer implements ValueContainer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        ArgumentContainer(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            this.index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        public Type type() throws ClassNotLoadedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            return argumentType(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        public String typeName(){
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   266
            return argumentTypeNames().get(index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        public String signature() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   269
            return argumentSignatures().get(index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        public Type findType(String signature) throws ClassNotLoadedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            return MethodImpl.this.findType(signature);
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
     * This is a var args method.  Thus, its last param is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * array. If the method has n params, then:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * 1.  If there are n args and the last is the same type as the type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *     the last param, do nothing.  IE, a String[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *     can be passed to a String...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * 2.  If there are >= n arguments and for each arg whose number is >= n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *     the arg type is 'compatible' with the component type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *     the last param, then do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *     - create an array of the type of the last param
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *     - put the n, ... args into this array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *       We might have to do conversions here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *     - put this array into arguments(n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *     - delete arguments(n+1), ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * NOTE that this might modify the input list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    void handleVarArgs(List<Value> arguments)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        throws ClassNotLoadedException, InvalidTypeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        List<Type> paramTypes = this.argumentTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        ArrayType lastParamType = (ArrayType)paramTypes.get(paramTypes.size() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        Type componentType = lastParamType.componentType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        int argCount = arguments.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        int paramCount = paramTypes.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (argCount < paramCount - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            // Error; will be caught later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (argCount == paramCount - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            // It is ok to pass 0 args to the var arg.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            // We have to gen a 0 length array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            ArrayReference argArray = lastParamType.newInstance(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            arguments.add(argArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   310
        Value nthArgValue = arguments.get(paramCount - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (nthArgValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        Type nthArgType = nthArgValue.type();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (nthArgType instanceof ArrayTypeImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if (argCount == paramCount &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                ((ArrayTypeImpl)nthArgType).isAssignableTo(lastParamType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                 * This is case 1.  A compatible array is being passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                 * var args array param.  We don't have to do anything.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
         * Case 2.  We have to verify that the n, n+1, ... args are compatible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
         * with componentType, and do conversions if necessary and create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
         * an array of componentType to hold these possibly converted values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        int count = argCount - paramCount + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        ArrayReference argArray = lastParamType.newInstance(count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
         * This will copy arguments(paramCount - 1) ... to argArray(0) ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
         * doing whatever conversions are needed!  It will throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
         * exception if an incompatible arg is encountered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        argArray.setValues(0, arguments, paramCount - 1, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        arguments.set(paramCount - 1, argArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
         * Remove the excess args
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        for (int ii = paramCount; ii < argCount; ii++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            arguments.remove(paramCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * The output list will be different than the input list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    List<Value> validateAndPrepareArgumentsForInvoke(List<? extends Value> origArguments)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                         throws ClassNotLoadedException, InvalidTypeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        List<Value> arguments = new ArrayList<Value>(origArguments);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (isVarArgs()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            handleVarArgs(arguments);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        int argSize = arguments.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        JNITypeParser parser = new JNITypeParser(signature());
11277
e3a1c90dd439 7117053: Fix build warnings in com/sun/tools/jdi/*
jjh
parents: 5506
diff changeset
   365
        List<String> signatures = parser.argumentSignatures();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if (signatures.size() != argSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            throw new IllegalArgumentException("Invalid argument count: expected " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                                               signatures.size() + ", received " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                                               arguments.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        for (int i = 0; i < argSize; i++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   374
            Value value = arguments.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            value = ValueImpl.prepareForAssignment(value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                                                   new ArgumentContainer(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            arguments.set(i, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        return arguments;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        sb.append(declaringType().name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        sb.append(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        sb.append(name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        sb.append("(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        boolean first = true;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   389
        for (String name : argumentTypeNames()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            if (!first) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                sb.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   393
            sb.append(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            first = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        sb.append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
}