jdk/src/share/classes/com/sun/tools/jdi/LocationImpl.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1998, 2004, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.jdi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
public class LocationImpl extends MirrorImpl implements Location {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    private final ReferenceTypeImpl declaringType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private Method method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private long methodRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private long codeIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private LineInfo baseLineInfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private LineInfo otherLineInfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    LocationImpl(VirtualMachine vm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
                 Method method, long codeIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        super(vm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        this.method = method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        this.codeIndex = method.isNative()? -1 : codeIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        this.declaringType = (ReferenceTypeImpl)method.declaringType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * This constructor allows lazy creation of the method mirror. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * can be a performance savings if the method mirror does not yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    LocationImpl(VirtualMachine vm, ReferenceTypeImpl declaringType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                 long methodRef, long codeIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        super(vm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.method = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        this.codeIndex = codeIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        this.declaringType = declaringType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        this.methodRef = methodRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if ((obj != null) && (obj instanceof Location)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            Location other = (Location)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            return (method().equals(other.method())) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                   (codeIndex() == other.codeIndex()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                   super.equals(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
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 int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
         * TO DO: better hash code?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return method().hashCode() + (int)codeIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public int compareTo(Location object) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        LocationImpl other = (LocationImpl)object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        int rc = method().compareTo(other.method());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (rc == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            long diff = codeIndex() - other.codeIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            if (diff < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            else if (diff > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public ReferenceType declaringType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return declaringType;
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 Method method() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (method == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            method = declaringType.getMethodMirror(methodRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (method.isNative()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                codeIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public long codeIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        method();  // be sure information is up-to-date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return codeIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    LineInfo getBaseLineInfo(SDE.Stratum stratum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        LineInfo lineInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        /* check if there is cached info to use */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (baseLineInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            return baseLineInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        /* compute the line info */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        MethodImpl methodImpl = (MethodImpl)method();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        lineInfo = methodImpl.codeIndexToLineInfo(stratum,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                                  codeIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        /* cache it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        addBaseLineInfo(lineInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return lineInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    LineInfo getLineInfo(SDE.Stratum stratum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        LineInfo lineInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        /* base stratum is done slighly differently */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (stratum.isJava()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return getBaseLineInfo(stratum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        /* check if there is cached info to use */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        lineInfo = otherLineInfo; // copy because of concurrency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (lineInfo != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                           stratum.id().equals(lineInfo.liStratum())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            return lineInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        int baseLineNumber = lineNumber(SDE.BASE_STRATUM_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        SDE.LineStratum lineStratum =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                  stratum.lineStratum(declaringType, baseLineNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (lineStratum != null && lineStratum.lineNumber() != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            lineInfo = new StratumLineInfo(stratum.id(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                                           lineStratum.lineNumber(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                           lineStratum.sourceName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                           lineStratum.sourcePath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            /* find best match */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            MethodImpl methodImpl = (MethodImpl)method();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            lineInfo = methodImpl.codeIndexToLineInfo(stratum,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                                      codeIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        /* cache it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        addStratumLineInfo(lineInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return lineInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    void addStratumLineInfo(LineInfo lineInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        otherLineInfo = lineInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    void addBaseLineInfo(LineInfo lineInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        baseLineInfo = lineInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public String sourceName() throws AbsentInformationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return sourceName(vm.getDefaultStratum());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public String sourceName(String stratumID)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                               throws AbsentInformationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        return sourceName(declaringType.stratum(stratumID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    String sourceName(SDE.Stratum stratum)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                               throws AbsentInformationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return getLineInfo(stratum).liSourceName();
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 String sourcePath() throws AbsentInformationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return sourcePath(vm.getDefaultStratum());
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 String sourcePath(String stratumID)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                               throws AbsentInformationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return sourcePath(declaringType.stratum(stratumID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    String sourcePath(SDE.Stratum stratum)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                               throws AbsentInformationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return getLineInfo(stratum).liSourcePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public int lineNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return lineNumber(vm.getDefaultStratum());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public int lineNumber(String stratumID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return lineNumber(declaringType.stratum(stratumID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    int lineNumber(SDE.Stratum stratum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return getLineInfo(stratum).liLineNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (lineNumber() == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            return method().toString() + "+" + codeIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            return declaringType().name() + ":" + lineNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
}