KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.ibm.icu.text.MessageFormat;
15 import org.eclipse.debug.core.DebugEvent;
16 import org.eclipse.debug.core.DebugException;
17 import org.eclipse.debug.core.model.IValue;
18 import org.eclipse.jdt.debug.core.IJavaFieldVariable;
19 import org.eclipse.jdt.debug.core.IJavaObject;
20 import org.eclipse.jdt.debug.core.IJavaReferenceType;
21 import org.eclipse.jdt.debug.core.IJavaType;
22 import com.sun.jdi.ClassNotLoadedException;
23 import com.sun.jdi.ClassType;
24 import com.sun.jdi.Field;
25 import com.sun.jdi.InterfaceType;
26 import com.sun.jdi.InvalidTypeException;
27 import com.sun.jdi.ObjectReference;
28 import com.sun.jdi.ReferenceType;
29 import com.sun.jdi.Type;
30 import com.sun.jdi.Value;
31
32 /**
33  * A field member.
34  */

35 public class JDIFieldVariable extends JDIModificationVariable implements IJavaFieldVariable {
36     /**
37      * The wrappered field
38      */

39     private Field fField;
40     /**
41      * The object containing the field,
42      * or <code>null</code> for a static field.
43      */

44     private ObjectReference fObject;
45     /**
46      * The type containing the field.
47      */

48     private ReferenceType fType;
49     
50     /**
51      * Constructs a field wrappering the given field.
52      */

53     public JDIFieldVariable(JDIDebugTarget target, Field field, ObjectReference objectRef) {
54         super(target);
55         fField= field;
56         if (!field.isStatic()) {
57             fObject= objectRef;
58         }
59         fType= (ReferenceType)objectRef.type();
60     }
61
62     /**
63      * Constructs a field wrappering the given field.
64      */

65     public JDIFieldVariable(JDIDebugTarget target, Field field, ReferenceType refType) {
66         super(target);
67         fField= field;
68         fType= refType;
69     }
70
71     /**
72      * Returns this variable's current <code>Value</code>.
73      */

74     protected Value retrieveValue() {
75         if (getField().isStatic()) {
76             return (getField().declaringType().getValue(getField()));
77         }
78         return getObjectReference().getValue(getField());
79     }
80     
81     /**
82      * @see IJavaFieldVariable#getDeclaringType()
83      */

84     public IJavaType getDeclaringType() {
85         return JDIType.createType((JDIDebugTarget)getDebugTarget(), fField.declaringType());
86     }
87
88     /**
89      * @see IVariable#getName()
90      */

91     public String JavaDoc getName() throws DebugException {
92         try {
93             return getField().name();
94         } catch (RuntimeException JavaDoc e) {
95             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIFieldVariable_exception_retrieving_field_name, new String JavaDoc[] {e.toString()}), e);
96             // execution will not reach this line, as
97
// #targetRequestFailed will thrown an exception
98
return null;
99         }
100     }
101
102     protected void setJDIValue(Value value) throws DebugException {
103         try {
104             if (isStatic()) {
105                 ReferenceType declaringType = getField().declaringType();
106                 if (declaringType instanceof InterfaceType) {
107                     requestFailed(JDIDebugModelMessages.JDIFieldVariable_0, null);
108                 }
109                 ((ClassType)declaringType).setValue(getField(), value);
110             } else {
111                 getObjectReference().setValue(getField(), value);
112             }
113             fireChangeEvent(DebugEvent.CONTENT);
114         } catch (ClassNotLoadedException e) {
115             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIFieldVariable_exception_modifying_value, new String JavaDoc[] {e.toString()}), e);
116         } catch (InvalidTypeException e) {
117             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIFieldVariable_exception_modifying_value, new String JavaDoc[] {e.toString()}), e);
118         } catch (RuntimeException JavaDoc e) {
119             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIFieldVariable_exception_modifying_value, new String JavaDoc[] {e.toString()}), e);
120         }
121
122     }
123         
124     /**
125      * @see IJavaVariable#isVolatile()
126      */

127     public boolean isVolatile() {
128         return getField().isVolatile();
129     }
130     
131     /**
132      * @see IJavaVariable#isTransient()
133      */

134     public boolean isTransient() {
135         return getField().isTransient();
136     }
137     
138     /**
139      * @see IJavaModifiers#isSynthetic()
140      */

141     public boolean isSynthetic() {
142         return getField().isSynthetic();
143     }
144     
145     /**
146      * @see IJavaModifiers#isPublic()
147      */

148     public boolean isPublic() {
149         return getField().isPublic();
150     }
151     
152     /**
153      * @see IJavaModifiers#isPrivate()
154      */

155     public boolean isPrivate() {
156         return getField().isPrivate();
157     }
158     
159     /**
160      * @see IJavaModifiers#isProtected()
161      */

162     public boolean isProtected() {
163         return getField().isProtected();
164     }
165     
166     /**
167      * @see IJavaModifiers#isPackagePrivate()
168      */

169     public boolean isPackagePrivate() {
170         return getField().isPackagePrivate();
171     }
172     
173     /**
174      * @see IJavaModifiers#isStatic()
175      */

176     public boolean isStatic() {
177         return getField().isStatic();
178     }
179     
180     /**
181      * @see IJavaModifiers#isFinal()
182      */

183     public boolean isFinal() {
184         return getField().isFinal();
185     }
186
187     /**
188      * @see IVariable#getReferenceTypeName()
189      */

190     public String JavaDoc getReferenceTypeName() throws DebugException {
191         String JavaDoc genericSignature= getField().genericSignature();
192         if (genericSignature != null) {
193             return JDIReferenceType.getTypeName(genericSignature);
194         }
195         Type underlyingType= getUnderlyingType();
196         if (underlyingType instanceof ReferenceType) {
197             return JDIReferenceType.getGenericName((ReferenceType)underlyingType);
198         }
199         return getField().typeName();
200     }
201     
202     /**
203      * @see IJavaVariable#getSignature()
204      */

205     public String JavaDoc getSignature() throws DebugException {
206         try {
207             return getField().signature();
208         } catch (RuntimeException JavaDoc e) {
209             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIFieldVariable_exception_retrieving_field_signature, new String JavaDoc[] {e.toString()}), e);
210             // execution will not reach this line, as
211
// #targetRequestFailed will thrown an exception
212
return null;
213         }
214     }
215
216     /* (non-Javadoc)
217      * @see org.eclipse.jdt.debug.core.IJavaVariable#getGenericSignature()
218      */

219     public String JavaDoc getGenericSignature() throws DebugException {
220         try {
221             String JavaDoc genericSignature= fField.genericSignature();
222             if (genericSignature != null) {
223                 return genericSignature;
224             }
225             return fField.signature();
226         } catch (RuntimeException JavaDoc e) {
227             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIFieldVariable_exception_retrieving_field_signature, new String JavaDoc[] {e.toString()}), e);
228             // execution will not reach this line, as
229
// #targetRequestFailed will thrown an exception
230
return null;
231         }
232     }
233     
234     public Field getField() {
235         return fField;
236     }
237     
238     public ObjectReference getObjectReference() {
239         return fObject;
240     }
241     
242     public ReferenceType getReferenceType() {
243         return fType;
244     }
245     
246     public boolean supportsValueModification() {
247         if (getField().declaringType()instanceof InterfaceType) {
248             return false;
249         }
250         return super.supportsValueModification();
251     }
252     
253     /**
254      * @see java.lang.Object#toString()
255      */

256     public String JavaDoc toString() {
257         return getField().toString();
258     }
259     
260     /**
261      * @see IValueModification#setValue(IValue)
262      */

263     public void setValue(IValue v) throws DebugException {
264         if (verifyValue(v)) {
265             setJDIValue(((JDIValue)v).getUnderlyingValue());
266         }
267     }
268     
269     /**
270      * @see JDIVariable#getUnderlyingType()
271      */

272     protected Type getUnderlyingType() throws DebugException {
273         try {
274             return getField().type();
275         } catch (ClassNotLoadedException e) {
276             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIFieldVariable_exception_while_retrieving_type_of_field, new String JavaDoc[]{e.toString()}), e);
277         } catch (RuntimeException JavaDoc e) {
278             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIFieldVariable_exception_while_retrieving_type_of_field, new String JavaDoc[]{e.toString()}), e);
279         }
280         // this line will not be exceucted as an exception
281
// will be throw in type retrieval fails
282
return null;
283     }
284     
285     /**
286      * @see java.lang.Object#equals(Object)
287      */

288     public boolean equals(Object JavaDoc o) {
289         if (o instanceof JDIFieldVariable) {
290             JDIFieldVariable f = (JDIFieldVariable)o;
291             if (fObject != null) {
292                 return fObject.equals(f.fObject) && f.fField.equals(fField);
293             }
294             return f.fField.equals(fField);
295         }
296         return false;
297     }
298
299     /**
300      * @see java.lang.Object#hashCode()
301      */

302     public int hashCode() {
303         if (fObject != null) {
304             return fField.hashCode() + fObject.hashCode();
305         }
306         return fField.hashCode();
307     }
308
309     /* (non-Javadoc)
310      * @see org.eclipse.jdt.debug.core.IJavaFieldVariable#getObject()
311      */

312     public IJavaObject getReceiver() {
313         ObjectReference objectReference= getObjectReference();
314         if (objectReference == null) {
315             return null;
316         }
317         return (IJavaObject)JDIValue.createValue(getJavaDebugTarget(), objectReference);
318     }
319     
320     public IJavaReferenceType getReceivingType() {
321         return (IJavaReferenceType)JDIType.createType(getJavaDebugTarget(), getReferenceType());
322     }
323
324 }
325
326
Popular Tags