Path: blob/master/src/jdk.jdi/share/classes/com/sun/tools/jdi/LocationImpl.java
41161 views
/*1* Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.jdi;2627import com.sun.jdi.AbsentInformationException;28import com.sun.jdi.Location;29import com.sun.jdi.Method;30import com.sun.jdi.ReferenceType;31import com.sun.jdi.VirtualMachine;3233public class LocationImpl extends MirrorImpl implements Location {34private final ReferenceTypeImpl declaringType;35private Method method;36private long methodRef;37private long codeIndex;38private LineInfo baseLineInfo = null;39private LineInfo otherLineInfo = null;4041LocationImpl(VirtualMachine vm, Method method, long codeIndex) {42super(vm);43this.method = method;44this.codeIndex = method.isNative()? -1 : codeIndex;45this.declaringType = (ReferenceTypeImpl)method.declaringType();46}4748/*49* This constructor allows lazy creation of the method mirror. This50* can be a performance savings if the method mirror does not yet51* exist.52*/53LocationImpl(VirtualMachine vm, ReferenceTypeImpl declaringType,54long methodRef, long codeIndex) {55super(vm);5657this.method = null;58this.codeIndex = codeIndex;59this.declaringType = declaringType;60this.methodRef = methodRef;61}6263public boolean equals(Object obj) {64if ((obj != null) && (obj instanceof Location)) {65Location other = (Location)obj;66return (method().equals(other.method())) &&67(codeIndex() == other.codeIndex()) &&68super.equals(obj);69} else {70return false;71}72}7374public int hashCode() {75/*76* TO DO: better hash code?77*/78return method().hashCode() + (int)codeIndex();79}8081public int compareTo(Location other) {82int rc = method().compareTo(other.method());83if (rc == 0) {84long diff = codeIndex() - other.codeIndex();85if (diff < 0)86return -1;87else if (diff > 0)88return 1;89else90return 0;91}92return rc;93}9495public ReferenceType declaringType() {96return declaringType;97}9899public Method method() {100if (method == null) {101method = declaringType.getMethodMirror(methodRef);102if (method.isNative()) {103codeIndex = -1;104}105}106return method;107}108109public long codeIndex() {110method(); // be sure information is up-to-date111return codeIndex;112}113114LineInfo getBaseLineInfo(SDE.Stratum stratum) {115LineInfo lineInfo;116117/* check if there is cached info to use */118if (baseLineInfo != null) {119return baseLineInfo;120}121122/* compute the line info */123MethodImpl methodImpl = (MethodImpl)method();124lineInfo = methodImpl.codeIndexToLineInfo(stratum, codeIndex());125126/* cache it */127addBaseLineInfo(lineInfo);128129return lineInfo;130}131132LineInfo getLineInfo(SDE.Stratum stratum) {133LineInfo lineInfo;134135/* base stratum is done slighly differently */136if (stratum.isJava()) {137return getBaseLineInfo(stratum);138}139140/* check if there is cached info to use */141lineInfo = otherLineInfo; // copy because of concurrency142if (lineInfo != null && stratum.id().equals(lineInfo.liStratum())) {143return lineInfo;144}145146int baseLineNumber = lineNumber(SDE.BASE_STRATUM_NAME);147SDE.LineStratum lineStratum =148stratum.lineStratum(declaringType, baseLineNumber);149150if (lineStratum != null && lineStratum.lineNumber() != -1) {151lineInfo = new StratumLineInfo(stratum.id(),152lineStratum.lineNumber(),153lineStratum.sourceName(),154lineStratum.sourcePath());155} else {156/* find best match */157MethodImpl methodImpl = (MethodImpl)method();158lineInfo = methodImpl.codeIndexToLineInfo(stratum, codeIndex());159}160161/* cache it */162addStratumLineInfo(lineInfo);163164return lineInfo;165}166167void addStratumLineInfo(LineInfo lineInfo) {168otherLineInfo = lineInfo;169}170171void addBaseLineInfo(LineInfo lineInfo) {172baseLineInfo = lineInfo;173}174175public String sourceName() throws AbsentInformationException {176return sourceName(vm.getDefaultStratum());177}178179public String sourceName(String stratumID)180throws AbsentInformationException {181return sourceName(declaringType.stratum(stratumID));182}183184String sourceName(SDE.Stratum stratum)185throws AbsentInformationException {186return getLineInfo(stratum).liSourceName();187}188189public String sourcePath() throws AbsentInformationException {190return sourcePath(vm.getDefaultStratum());191}192193public String sourcePath(String stratumID)194throws AbsentInformationException {195return sourcePath(declaringType.stratum(stratumID));196}197198String sourcePath(SDE.Stratum stratum)199throws AbsentInformationException {200return getLineInfo(stratum).liSourcePath();201}202203public int lineNumber() {204return lineNumber(vm.getDefaultStratum());205}206207public int lineNumber(String stratumID) {208return lineNumber(declaringType.stratum(stratumID));209}210211int lineNumber(SDE.Stratum stratum) {212return getLineInfo(stratum).liLineNumber();213}214215public String toString() {216if (lineNumber() == -1) {217return method().toString() + "+" + codeIndex();218} else {219return declaringType().name() + ":" + lineNumber();220}221}222}223224225