jdk/src/share/classes/com/sun/tools/example/debug/tty/BreakpointSpec.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 1998-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.tty;
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
class BreakpointSpec extends EventRequestSpec {
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
    int lineNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    BreakpointSpec(ReferenceTypeSpec refSpec, int lineNumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        super(refSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        this.methodId = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        this.methodArgs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        this.lineNumber = lineNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    BreakpointSpec(ReferenceTypeSpec refSpec, String methodId,
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    48
                   List<String> methodArgs) throws MalformedMemberNameException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        super(refSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        this.methodId = methodId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        this.methodArgs = methodArgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        this.lineNumber = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        if (!isValidMethodName(methodId)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            throw new MalformedMemberNameException(methodId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * The 'refType' is known to match, return the EventRequest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    EventRequest resolveEventRequest(ReferenceType refType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                           throws AmbiguousMethodException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                                  AbsentInformationException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                                  InvalidTypeException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                                  NoSuchMethodException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                                  LineNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        Location location = location(refType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (location == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            throw new InvalidTypeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        EventRequestManager em = refType.virtualMachine().eventRequestManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        EventRequest bp = em.createBreakpointRequest(location);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        bp.setSuspendPolicy(suspendPolicy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        bp.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return bp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    String methodName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return methodId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    int lineNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        return lineNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    86
    List<String> methodArgs() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return methodArgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    boolean isMethodBreakpoint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return (methodId != null);
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 int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return refSpec.hashCode() + lineNumber +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            ((methodId != null) ? methodId.hashCode() : 0) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            ((methodArgs != null) ? methodArgs.hashCode() : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (obj instanceof BreakpointSpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            BreakpointSpec breakpoint = (BreakpointSpec)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            return ((methodId != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                        methodId.equals(breakpoint.methodId)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                      : methodId == breakpoint.methodId) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                   ((methodArgs != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        methodArgs.equals(breakpoint.methodArgs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                      : methodArgs == breakpoint.methodArgs) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                   refSpec.equals(breakpoint.refSpec) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                   (lineNumber == breakpoint.lineNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return false;
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
    String errorMessageFor(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (e instanceof AmbiguousMethodException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return (MessageOutput.format("Method is overloaded; specify arguments",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                         methodName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
             * TO DO: list the methods here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        } else if (e instanceof NoSuchMethodException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return (MessageOutput.format("No method in",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                         new Object [] {methodName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                                        refSpec.toString()}));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        } else if (e instanceof AbsentInformationException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            return (MessageOutput.format("No linenumber information for",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                         refSpec.toString()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        } else if (e instanceof LineNotFoundException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            return (MessageOutput.format("No code at line",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                                         new Object [] {new Long (lineNumber()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                                                        refSpec.toString()}));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        } else if (e instanceof InvalidTypeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            return (MessageOutput.format("Breakpoints can be located only in classes.",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                         refSpec.toString()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            return super.errorMessageFor( e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        StringBuffer buffer = new StringBuffer(refSpec.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (isMethodBreakpoint()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            buffer.append('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            buffer.append(methodId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (methodArgs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                boolean first = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                buffer.append('(');
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   151
                for (String arg : methodArgs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    if (!first) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        buffer.append(',');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   155
                    buffer.append(arg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    first = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                buffer.append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            buffer.append(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            buffer.append(lineNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return MessageOutput.format("breakpoint", buffer.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private Location location(ReferenceType refType) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                    AmbiguousMethodException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                                    AbsentInformationException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                    NoSuchMethodException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                    LineNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        Location location = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (isMethodBreakpoint()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            Method method = findMatchingMethod(refType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            location = method.location();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            // let AbsentInformationException be thrown
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   178
            List<Location> locs = refType.locationsOfLine(lineNumber());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (locs.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                throw new LineNotFoundException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            // TO DO: handle multiple locations
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   183
            location = locs.get(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            if (location.method() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                throw new LineNotFoundException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private boolean isValidMethodName(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return isJavaIdentifier(s) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
               s.equals("<init>") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
               s.equals("<clinit>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Compare a method's argument types with a Vector of type names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Return true if each argument type has a name identical to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * corresponding string in the vector (allowing for varars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * and if the number of arguments in the method matches the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * number of names passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   204
    private boolean compareArgTypes(Method method, List<String> nameList) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   205
        List<String> argTypeNames = method.argumentTypeNames();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        // If argument counts differ, we can stop here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (argTypeNames.size() != nameList.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        // Compare each argument type's name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        int nTypes = argTypeNames.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        for (int i = 0; i < nTypes; ++i) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   215
            String comp1 = argTypeNames.get(i);
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   216
            String comp2 = nameList.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            if (! comp1.equals(comp2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                 * We have to handle varargs.  EG, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                 * method's last arg type is xxx[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                 * while the nameList contains xxx...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                 * Note that the nameList can also contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                 * xxx[] in which case we don't get here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                if (i != nTypes - 1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    !method.isVarArgs()  ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    !comp2.endsWith("...")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                 * The last types differ, it is a varargs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                 * method and the nameList item is varargs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                 * We just have to compare the type names, eg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                 * make sure we don't have xxx[] for the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                 * arg type and yyy... for the nameList item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                int comp1Length = comp1.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                if (comp1Length + 1 != comp2.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    // The type names are different lengths
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                // We know the two type names are the same length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                if (!comp1.regionMatches(0, comp2, 0, comp1Length - 2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                // We do have xxx[] and xxx... as the last param type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Remove unneeded spaces and expand class names to fully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * qualified names, if necessary and possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    private String normalizeArgTypeName(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
         * Separate the type name from any array modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
         * stripping whitespace after the name ends
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        StringBuffer typePart = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        StringBuffer arrayPart = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        name = name.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        int nameLength = name.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
         * For varargs, there can be spaces before the ... but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
         * within the ...  So, we will just ignore the ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         * while stripping blanks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        boolean isVarArgs = name.endsWith("...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (isVarArgs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            nameLength -= 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        while (i < nameLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            char c = name.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            if (Character.isWhitespace(c) || c == '[') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                break;      // name is complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            typePart.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        while (i < nameLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            char c = name.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            if ( (c == '[') || (c == ']')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                arrayPart.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            } else if (!Character.isWhitespace(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    (MessageOutput.format("Invalid argument type name"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        name = typePart.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
         * When there's no sign of a package name already, try to expand the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
         * the name to a fully qualified class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if ((name.indexOf('.') == -1) || name.startsWith("*.")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                ReferenceType argClass = Env.getReferenceTypeFromToken(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                if (argClass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    name = argClass.name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                // We'll try the name as is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        name += arrayPart.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        if (isVarArgs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            name += "...";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * Attempt an unambiguous match of the method name and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * argument specification to a method. If no arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * are specified, the method must not be overloaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Otherwise, the argument types much match exactly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    private Method findMatchingMethod(ReferenceType refType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                        throws AmbiguousMethodException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                               NoSuchMethodException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        // Normalize the argument string once before looping below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        List<String> argTypeNames = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (methodArgs() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            argTypeNames = new ArrayList<String>(methodArgs().size());
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   333
            for (String name : methodArgs()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                name = normalizeArgTypeName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                argTypeNames.add(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        // Check each method in the class for matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        Method firstMatch = null;  // first method with matching name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        Method exactMatch = null;  // (only) method with same name & sig
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        int matchCount = 0;        // > 1 implies overload
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   343
        for (Method candidate : refType.methods()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            if (candidate.name().equals(methodName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                matchCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                // Remember the first match in case it is the only one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                if (matchCount == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    firstMatch = candidate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                // If argument types were specified, check against candidate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                if ((argTypeNames != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        && compareArgTypes(candidate, argTypeNames) == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    exactMatch = candidate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        // Determine method for breakpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        Method method = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (exactMatch != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            // Name and signature match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            method = exactMatch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        } else if ((argTypeNames == null) && (matchCount > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            // At least one name matched and no arg types were specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            if (matchCount == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                method = firstMatch;       // Only one match; safe to use it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                throw new AmbiguousMethodException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            throw new NoSuchMethodException(methodName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        return method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
}