KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > core > model > JDIVariable


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.jdt.internal.debug.core.model;
12
13
14 import org.eclipse.debug.core.DebugException;
15 import org.eclipse.debug.core.model.IValue;
16 import org.eclipse.jdt.debug.core.IJavaModifiers;
17 import org.eclipse.jdt.debug.core.IJavaType;
18 import org.eclipse.jdt.debug.core.IJavaVariable;
19
20 import com.ibm.icu.text.MessageFormat;
21 import com.sun.jdi.Type;
22 import com.sun.jdi.Value;
23
24 public abstract class JDIVariable extends JDIDebugElement implements IJavaVariable {
25     
26     /**
27      * Cache of current value - see #getValue().
28      */

29     private JDIValue fValue;
30     
31     /**
32      * Counter corresponding to this variable's debug target
33      * suspend count indicating the last time this value
34      * changed. This variable's value has changed on the
35      * last suspend event if this counter is equal to the
36      * debug target's suspend count.
37      */

38     private int fLastChangeIndex = -1;
39     
40     protected final static String JavaDoc jdiStringSignature= "Ljava/lang/String;"; //$NON-NLS-1$
41

42     public JDIVariable(JDIDebugTarget target) {
43         super(target);
44     }
45     
46     /**
47      * @see PlatformObject#getAdapter(Class)
48      */

49     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
50         if (adapter == IJavaVariable.class || adapter == IJavaModifiers.class) {
51             return this;
52         }
53         return super.getAdapter(adapter);
54     }
55
56     /**
57      * Returns this variable's current underlying jdi value.
58      * Subclasses must implement #retrieveValue() and do not
59      * need to guard against JDI exceptions, as this method
60      * handles them.
61      *
62      * @exception DebugException if unable to access the value
63      */

64     protected final Value getCurrentValue() throws DebugException {
65         try {
66             return retrieveValue();
67         } catch (RuntimeException JavaDoc e) {
68             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIVariable_exception_retrieving, new String JavaDoc[] {e.toString()}), e);
69             // execution will not reach this line, as
70
// #targetRequestFailed will throw an exception
71
return null;
72         }
73     }
74     
75     /**
76      * Returns this variable's underlying jdi value
77      */

78     protected abstract Value retrieveValue() throws DebugException;
79     
80     /**
81      * Returns the current value of this variable. The value
82      * is cached, but on each access we see if the value has changed
83      * and update if required.
84      *
85      * @see IVariable#getValue()
86      */

87     public IValue getValue() throws DebugException {
88         Value currentValue = getCurrentValue();
89         if (fValue == null) {
90             fValue = JDIValue.createValue((JDIDebugTarget)getDebugTarget(), currentValue);
91         } else {
92             Value previousValue = fValue.getUnderlyingValue();
93             if (currentValue == previousValue) {
94                 return fValue;
95             }
96             if (previousValue == null || currentValue == null) {
97                 fValue = JDIValue.createValue((JDIDebugTarget)getDebugTarget(), currentValue);
98                 setChangeCount(getJavaDebugTarget().getSuspendCount());
99             } else if (!previousValue.equals(currentValue)) {
100                 fValue = JDIValue.createValue((JDIDebugTarget)getDebugTarget(), currentValue);
101                 setChangeCount(getJavaDebugTarget().getSuspendCount());
102             }
103         }
104         return fValue;
105     }
106
107     /**
108      * @see IValueModification#supportsValueModification()
109      */

110     public boolean supportsValueModification() {
111         return false;
112     }
113
114     /**
115      * @see IValueModification#setValue(String)
116      */

117     public void setValue(String JavaDoc expression) throws DebugException {
118         notSupported(JDIDebugModelMessages.JDIVariable_does_not_support_value_modification);
119     }
120     
121     /**
122      * @see IValueModification#setValue(IValue)
123      */

124     public void setValue(IValue value) throws DebugException {
125         notSupported(JDIDebugModelMessages.JDIVariable_does_not_support_value_modification);
126     }
127
128     /**
129      * @see IValueModification#verifyValue(String)
130      */

131     public boolean verifyValue(String JavaDoc expression) throws DebugException {
132         return false;
133     }
134     
135     /**
136      * @see IValueModification#verifyValue(IValue)
137      */

138     public boolean verifyValue(IValue value) {
139         return false;
140     }
141         
142     /**
143      * @see IJavaModifiers#isSynthetic()
144      */

145     public boolean isSynthetic() {
146         return false;
147     }
148     
149     /**
150      * @see IJavaModifiers#isPublic()
151      */

152     public boolean isPublic() throws DebugException {
153         return false;
154     }
155     
156     /**
157      * @see IJavaModifiers#isPrivate()
158      */

159     public boolean isPrivate() throws DebugException {
160         return false;
161     }
162     
163     /**
164      * @see IJavaModifiers#isProtected()
165      */

166     public boolean isProtected() throws DebugException {
167         return false;
168     }
169     
170     /**
171      * @see IJavaModifiers#isPackagePrivate()
172      */

173     public boolean isPackagePrivate() {
174         return false;
175     }
176     
177     /**
178      * @see IJavaModifiers#isStatic()
179      */

180     public boolean isStatic() {
181         return false;
182     }
183     
184     /**
185      * @see IJavaModifiers#isFinal()
186      */

187     public boolean isFinal() {
188         return false;
189     }
190     
191     /**
192      * @see org.eclipse.jdt.debug.core.IJavaVariable#isLocal()
193      */

194     public boolean isLocal() {
195         return false;
196     }
197
198     /**
199      * @see IJavaVariable#getJavaType()
200      */

201     public IJavaType getJavaType() throws DebugException {
202         return JDIType.createType((JDIDebugTarget)getDebugTarget(), getUnderlyingType());
203     }
204     
205     /**
206      * Returns the underlying type of this variable
207      *
208      * @return the underlying type of this variable
209      *
210      * @exception DebugException if this method fails. Reasons include:
211      * <ul><li>Failure communicating with the VM. The DebugException's
212      * status code contains the underlying exception responsible for
213      * the failure.</li>
214      * <li>The type associated with this variable is not yet loaded</li></ul>
215      */

216     protected abstract Type getUnderlyingType() throws DebugException;
217     
218     /**
219      * Returns the last known value for this variable
220      */

221     protected Value getLastKnownValue() {
222         if (fValue == null) {
223             return null;
224         }
225         return fValue.getUnderlyingValue();
226     }
227     
228     /**
229      * Sets this variable's change counter to the specified value
230      *
231      * @param count new value
232      */

233     protected void setChangeCount(int count) {
234         fLastChangeIndex = count;
235     }
236     
237     /**
238      * Returns this variable's change counter. This corresponds to the
239      * last time this variable changed.
240      *
241      * @return this variable's change counter
242      */

243     protected int getChangeCount() {
244         return fLastChangeIndex;
245     }
246     
247     /**
248      * @see IVariable#hasValueChanged()
249      */

250     public boolean hasValueChanged() {
251         return getChangeCount() == getJavaDebugTarget().getSuspendCount();
252     }
253 }
254
Popular Tags