jdk/src/share/classes/com/sun/tools/example/debug/bdi/MethodBreakpointSpec.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 51 6fe31bc95bbc
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 1999-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.example.debug.bdi;
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
import com.sun.jdi.request.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class MethodBreakpointSpec extends BreakpointSpec {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    String methodId;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    37
    List<String> methodArgs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    MethodBreakpointSpec(EventRequestSpecList specs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
                         ReferenceTypeSpec refSpec,
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    41
                         String methodId, List<String> methodArgs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        super(specs, refSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        this.methodId = methodId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        this.methodArgs = methodArgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * The 'refType' is known to match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    void resolve(ReferenceType refType) throws MalformedMemberNameException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                                             AmbiguousMethodException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                                             InvalidTypeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                                             NoSuchMethodException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                                             NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (!isValidMethodName(methodId)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            throw new MalformedMemberNameException(methodId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (!(refType instanceof ClassType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            throw new InvalidTypeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        Location location = location((ClassType)refType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        setRequest(refType.virtualMachine().eventRequestManager()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                   .createBreakpointRequest(location));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private Location location(ClassType clazz) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                                               AmbiguousMethodException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                                               NoSuchMethodException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                               NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        Method method = findMatchingMethod(clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        Location location = method.location();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public String methodName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        return methodId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    79
    public List<String> methodArgs() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return methodArgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return refSpec.hashCode() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            ((methodId != null) ? methodId.hashCode() : 0) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            ((methodArgs != null) ? methodArgs.hashCode() : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (obj instanceof MethodBreakpointSpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            MethodBreakpointSpec breakpoint = (MethodBreakpointSpec)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            return methodId.equals(breakpoint.methodId) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                   methodArgs.equals(breakpoint.methodArgs) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                   refSpec.equals(breakpoint.refSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public String errorMessageFor(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (e instanceof AmbiguousMethodException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return ("Method " + methodName() + " is overloaded; specify arguments");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
             * TO DO: list the methods here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        } else if (e instanceof NoSuchMethodException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            return ("No method " + methodName() + " in " + refSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } else if (e instanceof InvalidTypeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            return ("Breakpoints can be located only in classes. " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        refSpec + " is an interface or array");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return super.errorMessageFor( e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        StringBuffer buffer = new StringBuffer("breakpoint ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        buffer.append(refSpec.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        buffer.append('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        buffer.append(methodId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (methodArgs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            boolean first = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            buffer.append('(');
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   125
            for (String name : methodArgs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                if (!first) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    buffer.append(',');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   129
                buffer.append(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                first = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            buffer.append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        buffer.append(" (");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        buffer.append(getStatusString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        buffer.append(')');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return buffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private boolean isValidMethodName(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return isJavaIdentifier(s) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
               s.equals("<init>") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
               s.equals("<clinit>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Compare a method's argument types with a Vector of type names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Return true if each argument type has a name identical to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * corresponding string in the vector (allowing for varargs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * and if the number of arguments in the method matches the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * number of names passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   153
    private boolean compareArgTypes(Method method, List<String> nameList) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   154
        List<String> argTypeNames = method.argumentTypeNames();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // If argument counts differ, we can stop here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (argTypeNames.size() != nameList.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        // Compare each argument type's name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        int nTypes = argTypeNames.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        for (int i = 0; i < nTypes; ++i) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   164
            String comp1 = argTypeNames.get(i);
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   165
            String comp2 = nameList.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            if (! comp1.equals(comp2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                 * We have to handle varargs.  EG, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                 * method's last arg type is xxx[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                 * while the nameList contains xxx...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                 * Note that the nameList can also contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                 * xxx[] in which case we don't get here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                if (i != nTypes - 1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    !method.isVarArgs()  ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    !comp2.endsWith("...")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                 * The last types differ, it is a varargs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                 * method and the nameList item is varargs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                 * We just have to compare the type names, eg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                 * make sure we don't have xxx[] for the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                 * arg type and yyy... for the nameList item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                int comp1Length = comp1.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                if (comp1Length + 1 != comp2.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    // The type names are different lengths
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                // We know the two type names are the same length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                if (!comp1.regionMatches(0, comp2, 0, comp1Length - 2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                // We do have xxx[] and xxx... as the last param type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
  private VirtualMachine vm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    return request.virtualMachine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Remove unneeded spaces and expand class names to fully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * qualified names, if necessary and possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    private String normalizeArgTypeName(String name) throws NoSessionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
         * Separate the type name from any array modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
         * stripping whitespace after the name ends.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        StringBuffer typePart = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        StringBuffer arrayPart = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        name = name.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        int nameLength = name.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
         * For varargs, there can be spaces before the ... but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         * within the ...  So, we will just ignore the ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
         * while stripping blanks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        boolean isVarArgs = name.endsWith("...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (isVarArgs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            nameLength -= 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        while (i < nameLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            char c = name.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            if (Character.isWhitespace(c) || c == '[') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                break;      // name is complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            typePart.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        while (i < nameLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            char c = name.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            if ( (c == '[') || (c == ']')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                arrayPart.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            } else if (!Character.isWhitespace(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                                "Invalid argument type name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        name = typePart.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
         * When there's no sign of a package name already,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
         * try to expand the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
         * the name to a fully qualified class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if ((name.indexOf('.') == -1) || name.startsWith("*.")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                List refs = specs.runtime.findClassesMatchingPattern(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                if (refs.size() > 0) {  //### ambiguity???
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    name = ((ReferenceType)(refs.get(0))).name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                // We'll try the name as is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        name += arrayPart.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (isVarArgs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            name += "...";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return name;
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
     * Attempt an unambiguous match of the method name and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * argument specification to a method. If no arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * are specified, the method must not be overloaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Otherwise, the argument types much match exactly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    private Method findMatchingMethod(ClassType clazz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                                        throws AmbiguousMethodException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                                               NoSuchMethodException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                                               NoSessionException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        // Normalize the argument string once before looping below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        List<String> argTypeNames = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (methodArgs() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            argTypeNames = new ArrayList<String>(methodArgs().size());
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   290
            for (String name : methodArgs()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                name = normalizeArgTypeName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                argTypeNames.add(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        // Check each method in the class for matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        Method firstMatch = null;  // first method with matching name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        Method exactMatch = null;  // (only) method with same name & sig
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        int matchCount = 0;        // > 1 implies overload
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   300
        for (Method candidate : clazz.methods()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            if (candidate.name().equals(methodName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                matchCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                // Remember the first match in case it is the only one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                if (matchCount == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    firstMatch = candidate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                // If argument types were specified, check against candidate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                if ((argTypeNames != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                        && compareArgTypes(candidate, argTypeNames) == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    exactMatch = candidate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        // Determine method for breakpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        Method method = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (exactMatch != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            // Name and signature match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            method = exactMatch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        } else if ((argTypeNames == null) && (matchCount > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            // At least one name matched and no arg types were specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            if (matchCount == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                method = firstMatch;       // Only one match; safe to use it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                throw new AmbiguousMethodException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            throw new NoSuchMethodException(methodName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
}