001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdi.internal;
011:
012: import java.io.DataInputStream;
013: import java.io.DataOutputStream;
014: import java.io.IOException;
015: import com.ibm.icu.text.MessageFormat;
016:
017: import com.sun.jdi.AbsentInformationException;
018: import com.sun.jdi.Location;
019: import com.sun.jdi.Method;
020: import com.sun.jdi.ReferenceType;
021:
022: /**
023: * this class implements the corresponding interfaces
024: * declared by the JDI specification. See the com.sun.jdi package
025: * for more information.
026: *
027: */
028: public class LocationImpl extends MirrorImpl implements Location {
029: /** Line nr used if line numbers are not available. */
030: public static final int LINE_NR_NOT_AVAILABLE = -1;
031:
032: /** Method that holds the location. */
033: MethodImpl fMethod;
034: /** Index of location within the method, note: this value must be treated as UNSIGNED! */
035: long fIndex;
036:
037: /**
038: * Creates new instance.
039: */
040: public LocationImpl(VirtualMachineImpl vmImpl, MethodImpl method,
041: long index) {
042: super ("Location", vmImpl); //$NON-NLS-1$
043: fMethod = method;
044: fIndex = index;
045: }
046:
047: /**
048: * @return Returns the code position within this location's method.
049: */
050: public long codeIndex() {
051: return fIndex;
052: }
053:
054: /**
055: * @return Returns the type to which this Location belongs.
056: */
057: public ReferenceType declaringType() {
058: return fMethod.declaringType();
059: }
060:
061: /**
062: * @return Returns the hash code value.
063: */
064: public int hashCode() {
065: return fMethod.hashCode() + (int) fIndex;
066: }
067:
068: /**
069: * @return Returns true if two mirrors refer to the same entity in the target VM.
070: * @see java.lang.Object#equals(Object)
071: */
072: public boolean equals(Object object) {
073: if (object != null && object.getClass().equals(this .getClass())) {
074: LocationImpl loc = (LocationImpl) object;
075: return fMethod.equals(loc.fMethod) && fIndex == loc.fIndex;
076: }
077: return false;
078: }
079:
080: /**
081: * @return Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
082: */
083: public int compareTo(Object object) {
084: if (object == null
085: || !object.getClass().equals(this .getClass()))
086: throw new ClassCastException(
087: JDIMessages.LocationImpl_Can__t_compare_location_to_given_object_1);
088:
089: // See if methods are the same, if not return comparison between methods.
090: LocationImpl location2 = (LocationImpl) object;
091: if (!method().equals(location2.method()))
092: return method().compareTo(location2.method());
093:
094: // Return comparison between code-indexes.
095: // Code indexes must be treated as unsigned. This matters if you have to compare them.
096: if (codeIndex() < 0 || location2.codeIndex() < 0)
097: throw new InternalError(
098: JDIMessages.LocationImpl_Code_indexes_are_assumed_to_be_always_positive_2);
099:
100: if (codeIndex() < location2.codeIndex())
101: return -1;
102: else if (codeIndex() > location2.codeIndex())
103: return 1;
104: else
105: return 0;
106: }
107:
108: /**
109: * @return Returns an int specifying the line in the source, return -1 if the information is not available.
110: */
111: public int lineNumber() {
112: return lineNumber(virtualMachine().getDefaultStratum());
113: }
114:
115: /**
116: * @return Returns the Method if this location is in a method.
117: */
118: public Method method() {
119: return fMethod;
120: }
121:
122: /**
123: * @return a string specifying the source.
124: */
125: public String sourceName() throws AbsentInformationException {
126: return sourceName(virtualMachine().getDefaultStratum());
127: }
128:
129: /**
130: * @return Returns description of Mirror object.
131: */
132: public String toString() {
133: try {
134: return MessageFormat
135: .format(
136: JDIMessages.LocationImpl_sourcename___0___line___1__3,
137: new String[] { sourceName(),
138: Integer.toString(lineNumber()) });
139: } catch (Exception e) {
140: return fDescription;
141: }
142: }
143:
144: /**
145: * Writes JDWP representation.
146: */
147: public void write(MirrorImpl target, DataOutputStream out)
148: throws IOException {
149: fMethod.writeWithReferenceTypeWithTag(target, out);
150: target.writeLong(fIndex, "index", out); //$NON-NLS-1$
151: }
152:
153: /**
154: * @return Reads JDWP representation and returns new instance.
155: */
156: public static LocationImpl read(MirrorImpl target,
157: DataInputStream in) throws IOException {
158: VirtualMachineImpl vmImpl = target.virtualMachineImpl();
159: // Notice that Locations are not stored or cached because they don't 'remember' any information.
160: MethodImpl method = MethodImpl.readWithReferenceTypeWithTag(
161: target, in);
162: long index = target.readLong("index", in); //$NON-NLS-1$
163: if (method == null) {
164: return null;
165: }
166: return new LocationImpl(vmImpl, method, index);
167: }
168:
169: /**
170: * @see Location#lineNumber(String)
171: */
172: public int lineNumber(String stratum) {
173: return fMethod.referenceTypeImpl().lineNumber(fIndex, fMethod,
174: stratum);
175: }
176:
177: /**
178: * @see Location#sourceName(String)
179: */
180: public String sourceName(String stratum)
181: throws AbsentInformationException {
182: return fMethod.referenceTypeImpl().sourceName(fIndex, fMethod,
183: stratum);
184: }
185:
186: /**
187: * @see Location#sourcePath(String)
188: */
189: public String sourcePath(String stratum)
190: throws AbsentInformationException {
191: return fMethod.referenceTypeImpl().sourcePath(fIndex, fMethod,
192: stratum);
193: }
194:
195: /**
196: * @see Location#sourcePath()
197: */
198: public String sourcePath() throws AbsentInformationException {
199: return sourcePath(virtualMachine().getDefaultStratum());
200: }
201:
202: }
|