KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdi > internal > LocationImpl


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdi.internal;
12
13
14 import java.io.DataInputStream JavaDoc;
15 import java.io.DataOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import com.ibm.icu.text.MessageFormat;
18
19 import com.sun.jdi.AbsentInformationException;
20 import com.sun.jdi.Location;
21 import com.sun.jdi.Method;
22 import com.sun.jdi.ReferenceType;
23
24 /**
25  * this class implements the corresponding interfaces
26  * declared by the JDI specification. See the com.sun.jdi package
27  * for more information.
28  *
29  */

30 public class LocationImpl extends MirrorImpl implements Location {
31     /** Line nr used if line numbers are not available. */
32     public static final int LINE_NR_NOT_AVAILABLE = -1;
33     
34     /** Method that holds the location. */
35     MethodImpl fMethod;
36     /** Index of location within the method, note: this value must be treated as UNSIGNED! */
37     long fIndex;
38     
39     /**
40      * Creates new instance.
41      */

42     public LocationImpl(VirtualMachineImpl vmImpl, MethodImpl method, long index) {
43         super("Location", vmImpl); //$NON-NLS-1$
44
fMethod = method;
45         fIndex = index;
46     }
47     
48     /**
49      * @return Returns the code position within this location's method.
50      */

51     public long codeIndex() {
52         return fIndex;
53     }
54     
55     /**
56      * @return Returns the type to which this Location belongs.
57      */

58     public ReferenceType declaringType() {
59         return fMethod.declaringType();
60     }
61     
62     /**
63      * @return Returns the hash code value.
64      */

65     public int hashCode() {
66         return fMethod.hashCode() + (int)fIndex;
67     }
68     
69     /**
70      * @return Returns true if two mirrors refer to the same entity in the target VM.
71      * @see java.lang.Object#equals(Object)
72      */

73     public boolean equals(Object JavaDoc object) {
74         if (object != null && object.getClass().equals(this.getClass())) {
75             LocationImpl loc = (LocationImpl)object;
76             return fMethod.equals(loc.fMethod) && fIndex == loc.fIndex;
77         }
78         return false;
79     }
80     
81     /**
82      * @return Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
83      */

84     public int compareTo(Object JavaDoc object) {
85         if (object == null || !object.getClass().equals(this.getClass()))
86             throw new ClassCastException JavaDoc(JDIMessages.LocationImpl_Can__t_compare_location_to_given_object_1);
87             
88         // See if methods are the same, if not return comparison between methods.
89
LocationImpl location2 = (LocationImpl)object;
90         if (!method().equals(location2.method()))
91             return method().compareTo(location2.method());
92
93         // Return comparison between code-indexes.
94
// Code indexes must be treated as unsigned. This matters if you have to compare them.
95
if (codeIndex() < 0 || location2.codeIndex() < 0)
96             throw new InternalError JavaDoc(JDIMessages.LocationImpl_Code_indexes_are_assumed_to_be_always_positive_2);
97
98         if (codeIndex() < location2.codeIndex())
99             return -1;
100         else if (codeIndex() > location2.codeIndex())
101             return 1;
102         else return 0;
103     }
104     
105     /**
106      * @return Returns an int specifying the line in the source, return -1 if the information is not available.
107      */

108     public int lineNumber() {
109         return lineNumber(virtualMachine().getDefaultStratum());
110     }
111     
112     /**
113      * @return Returns the Method if this location is in a method.
114      */

115     public Method method() {
116         return fMethod;
117     }
118     
119     /**
120      * @return a string specifying the source.
121      */

122     public String JavaDoc sourceName() throws AbsentInformationException {
123         return sourceName(virtualMachine().getDefaultStratum());
124     }
125     
126     /**
127      * @return Returns description of Mirror object.
128      */

129     public String JavaDoc toString() {
130         try {
131             return MessageFormat.format(JDIMessages.LocationImpl_sourcename___0___line___1__3, new String JavaDoc[]{sourceName(), Integer.toString(lineNumber())});
132         } catch (Exception JavaDoc e) {
133             return fDescription;
134         }
135     }
136
137     /**
138      * Writes JDWP representation.
139      */

140     public void write(MirrorImpl target, DataOutputStream JavaDoc out) throws IOException JavaDoc {
141         fMethod.writeWithReferenceTypeWithTag(target, out);
142         target.writeLong(fIndex, "index", out); //$NON-NLS-1$
143
}
144     
145     /**
146      * @return Reads JDWP representation and returns new instance.
147      */

148     public static LocationImpl read(MirrorImpl target, DataInputStream JavaDoc in) throws IOException JavaDoc {
149         VirtualMachineImpl vmImpl = target.virtualMachineImpl();
150         // Notice that Locations are not stored or cached because they don't 'remember' any information.
151
MethodImpl method = MethodImpl.readWithReferenceTypeWithTag(target, in);
152         long index = target.readLong("index", in); //$NON-NLS-1$
153
if (method == null) {
154             return null;
155         }
156         return new LocationImpl(vmImpl, method, index);
157     }
158     
159     /**
160      * @see Location#lineNumber(String)
161      */

162     public int lineNumber(String JavaDoc stratum) {
163         return fMethod.referenceTypeImpl().lineNumber(fIndex, fMethod, stratum);
164     }
165
166     /**
167      * @see Location#sourceName(String)
168      */

169     public String JavaDoc sourceName(String JavaDoc stratum) throws AbsentInformationException {
170         return fMethod.referenceTypeImpl().sourceName(fIndex, fMethod, stratum);
171     }
172
173     /**
174      * @see Location#sourcePath(String)
175      */

176     public String JavaDoc sourcePath(String JavaDoc stratum) throws AbsentInformationException {
177         return fMethod.referenceTypeImpl().sourcePath(fIndex, fMethod, stratum);
178     }
179
180     /**
181      * @see Location#sourcePath()
182      */

183     public String JavaDoc sourcePath() throws AbsentInformationException {
184         return sourcePath(virtualMachine().getDefaultStratum());
185     }
186
187 }
188
Popular Tags