KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 com.sun.jdi.ClassNotLoadedException;
15 import com.sun.jdi.LocalVariable;
16 import com.sun.jdi.StackFrame;
17 import com.sun.jdi.Type;
18 import com.sun.jdi.VMMismatchException;
19
20 /**
21  * this class implements the corresponding interfaces
22  * declared by the JDI specification. See the com.sun.jdi package
23  * for more information.
24  *
25  */

26 public class LocalVariableImpl extends MirrorImpl implements LocalVariable {
27     /** Method that holds local variable. */
28     private MethodImpl fMethod;
29     /** First code index at which the variable is visible (unsigned). */
30     private long fCodeIndex;
31     /** The variable's name. */
32     private String JavaDoc fName;
33     /** The variable type's JNI signature. */
34     private String JavaDoc fSignature;
35     /** The variable type generic signature. */
36     private String JavaDoc fGenericSignature;
37     /** The variable's type */
38     private Type fType;
39     /** The variables type name */
40     private String JavaDoc fTypeName;
41     /**
42      * Unsigned value used in conjunction with codeIndex.
43      * The variable can be get or set only when the current
44      * codeIndex <= current frame code index < code index + length.
45      * <p>
46      * The length is set to -1 when this variable represents an
47      * inferred argument (when local var info is unavailable).
48      * We assume that such arguments are visible for the entire
49      * method.
50      * </p>
51      * */

52     private int fLength;
53     /** The local variable's index in its frame. */
54     private int fSlot;
55     /** Is the local variable an argument of its method? */
56     private boolean fIsArgument;
57     
58     public LocalVariableImpl(VirtualMachineImpl vmImpl, MethodImpl method, long codeIndex, String JavaDoc name, String JavaDoc signature, String JavaDoc genericSignature, int length, int slot, boolean isArgument) {
59         super("LocalVariable", vmImpl); //$NON-NLS-1$
60
fMethod = method;
61         fCodeIndex = codeIndex;
62         fName = name;
63         fSignature = signature;
64         fGenericSignature= genericSignature;
65         fLength = length;
66         fSlot = slot;
67         fIsArgument = isArgument;
68     }
69
70     /**
71      * @return Returns local variable's index in its frame.
72      */

73     public int slot() {
74         return fSlot;
75     }
76     
77     /**
78      * @return Returns the hash code value.
79      */

80     public int hashCode() {
81         return fMethod.hashCode() + (int)fCodeIndex + fSlot;
82     }
83     
84     /**
85      * @return Returns true if two mirrors refer to the same entity in the target VM.
86      * @see java.lang.Object#equals(Object).
87      */

88     public boolean equals(Object JavaDoc object) {
89         if (object != null && object.getClass().equals(this.getClass())) {
90             LocalVariableImpl loc = (LocalVariableImpl)object;
91             return fMethod.equals(loc.fMethod) && fCodeIndex == loc.fCodeIndex && fSlot == loc.fSlot;
92         }
93         return false;
94     }
95     
96     /**
97      * @return Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
98      */

99     public int compareTo(Object JavaDoc object) {
100         if (object == null || !object.getClass().equals(this.getClass()))
101             throw new ClassCastException JavaDoc(JDIMessages.LocalVariableImpl_Can__t_compare_local_variable_to_given_object_1);
102         
103         // See if methods are the same, if not return comparison between methods.
104
LocalVariableImpl var2 = (LocalVariableImpl)object;
105         if (!method().equals(var2.method()))
106             return method().compareTo(var2.method());
107
108         // Return comparison between the index of each local variable in its stack frame.
109
// Code indexes must be treated as unsigned. This matters if you have to compare them.
110
if (fCodeIndex < 0 || var2.fCodeIndex < 0)
111             throw new InternalError JavaDoc(JDIMessages.LocalVariableImpl_Code_indexes_are_assumed_to_be_always_positive_2);
112         
113         long index2 = var2.fCodeIndex;
114         if (fCodeIndex < index2)
115             return -1;
116         else if (fCodeIndex > index2)
117             return 1;
118         else return 0;
119     }
120     
121     /**
122      * @return Returns true if this variable is an argument to its method.
123      */

124     public boolean isArgument() {
125         return fIsArgument;
126     }
127     
128     public boolean isVisible(StackFrame frame) throws IllegalArgumentException JavaDoc, VMMismatchException {
129         checkVM(frame);
130         StackFrameImpl frameImpl = (StackFrameImpl)frame;
131         if (!fMethod.equals(frameImpl.location().method()))
132             throw new IllegalArgumentException JavaDoc(JDIMessages.LocalVariableImpl_The_stack_frame__s_method_does_not_match_this_variable__s_method_3);
133
134         if (fLength == -1) {
135             // inferred argument - assume visible for entire method
136
return true;
137         }
138         long currentIndex = frameImpl.location().codeIndex();
139         
140         // Code indexes must be treated as unsigned. This matters if you have to compare them.
141
if (currentIndex >= 0 && fCodeIndex >= 0 && fCodeIndex + fLength >= 0)
142             return fCodeIndex <= currentIndex && currentIndex < fCodeIndex + fLength;
143
144         throw new InternalError JavaDoc(JDIMessages.LocalVariableImpl_Code_indexes_are_assumed_to_be_always_positive_4);
145     }
146     
147     /**
148      * @return Returns the name of the local variable.
149      */

150     public String JavaDoc name() {
151         return fName;
152     }
153     
154     /**
155      * @return Returns the signature of the local variable.
156      */

157     public String JavaDoc signature() {
158         return fSignature;
159     }
160     
161     /**
162      * @return Returns the type of the this LocalVariable.
163      */

164     public Type type() throws ClassNotLoadedException {
165         if (fType == null) {
166             fType = TypeImpl.create(virtualMachineImpl(), fSignature, method().declaringType().classLoader());
167         }
168         return fType;
169     }
170     
171     /**
172      * @return Returns a text representation of the declared type of this variable.
173      */

174     public String JavaDoc typeName() {
175         if (fTypeName == null) {
176             fTypeName = TypeImpl.signatureToName(fSignature);
177         }
178         return fTypeName;
179     }
180     
181     /**
182      * @return Returns the tag of the declared type of this variable.
183      */

184     public byte tag() {
185         return TypeImpl.signatureToTag(fSignature);
186     }
187     
188     /**
189      * @return Returns the method that holds the local variable.
190      */

191     public MethodImpl method() {
192         return fMethod;
193     }
194     
195     /**
196      * @return Returns true if the local variable is the 'this' pointer.
197      */

198     public boolean isThis() {
199         return slot() == 0 && !method().isStatic();
200     }
201     
202     /**
203      * @return Returns description of Mirror object.
204      */

205     public String JavaDoc toString() {
206         return fName;
207     }
208     
209     public String JavaDoc genericSignature() {
210         return fGenericSignature;
211     }
212
213 }
214
Popular Tags