KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.sun.jdi.ClassNotLoadedException;
19 import com.sun.jdi.InvalidTypeException;
20 import com.sun.jdi.LocalVariable;
21 import com.sun.jdi.ReferenceType;
22 import com.sun.jdi.Type;
23 import com.sun.jdi.Value;
24
25 /**
26  * A <code>JDILocalVariable</code> represents a local variable in a stack
27  * frame.
28  */

29
30 public class JDILocalVariable extends JDIModificationVariable {
31     /**
32      * The wrappered local variable
33      */

34     private LocalVariable fLocal;
35     
36     /**
37      * The stack frame the local is contained in
38      */

39     private JDIStackFrame fStackFrame;
40     
41     /**
42      * Constructs a local variable wrappering the given local from
43      * in a stack frame.
44      */

45     public JDILocalVariable(JDIStackFrame frame, LocalVariable local) {
46         super((JDIDebugTarget)frame.getDebugTarget());
47         fStackFrame= frame;
48         fLocal= local;
49     }
50
51     /**
52      * Returns this variable's current Value.
53      */

54     protected Value retrieveValue() throws DebugException {
55         synchronized (fStackFrame.getThread()) {
56             if (getStackFrame().isSuspended()) {
57                 return getStackFrame().getUnderlyingStackFrame().getValue(fLocal);
58             }
59         }
60         // bug 6518
61
return getLastKnownValue();
62     }
63
64     /**
65      * @see IVariable#getName()
66      */

67     public String JavaDoc getName() throws DebugException {
68         try {
69             return getLocal().name();
70         } catch (RuntimeException JavaDoc e) {
71             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDILocalVariable_exception_retrieving_local_variable_name, new String JavaDoc[] {e.toString()}), e);
72             // execution will not reach this line, as
73
// #targetRequestFailed will thrown an exception
74
return null;
75         }
76     }
77
78     /**
79      * @see JDIModificationVariable#setValue(Value)
80      */

81     protected void setJDIValue(Value value) throws DebugException {
82         try {
83             synchronized (getStackFrame().getThread()) {
84                 getStackFrame().getUnderlyingStackFrame().setValue(getLocal(), value);
85             }
86             fireChangeEvent(DebugEvent.CONTENT);
87         } catch (ClassNotLoadedException e) {
88             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDILocalVariable_exception_modifying_local_variable_value, new String JavaDoc[] {e.toString()}), e);
89         } catch (InvalidTypeException e) {
90             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDILocalVariable_exception_modifying_local_variable_value, new String JavaDoc[] {e.toString()}), e);
91         } catch (RuntimeException JavaDoc e) {
92             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDILocalVariable_exception_modifying_local_variable_value, new String JavaDoc[] {e.toString()}), e);
93         }
94     }
95     
96     /**
97      * @see IVariable#getReferenceTypeName()
98      */

99     public String JavaDoc getReferenceTypeName() throws DebugException {
100         try {
101             String JavaDoc genericSignature= getLocal().genericSignature();
102             if (genericSignature != null) {
103                 return JDIReferenceType.getTypeName(genericSignature);
104             }
105             try {
106                 Type underlyingType= getUnderlyingType();
107                 if (underlyingType instanceof ReferenceType) {
108                     return JDIReferenceType.getGenericName((ReferenceType) underlyingType);
109                 }
110             } catch (DebugException e) {
111                 if (!(e.getStatus().getException() instanceof ClassNotLoadedException)) {
112                     throw e;
113                 }
114             }
115             return getLocal().typeName();
116         } catch (RuntimeException JavaDoc e) {
117             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDILocalVariable_exception_retrieving_local_variable_type_name, new String JavaDoc[] {e.toString()}), e);
118             // execution will not reach this line, as
119
// #targetRequestFailed will thrown an exception
120
return null;
121         }
122     }
123     
124     /**
125      * @see IJavaVariable#getSignature()
126      */

127     public String JavaDoc getSignature() throws DebugException {
128         try {
129             return getLocal().signature();
130         } catch (RuntimeException JavaDoc e) {
131             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDILocalVariable_exception_retrieving_local_variable_type_signature, new String JavaDoc[] {e.toString()}), e);
132             // execution will not reach this line, as
133
// #targetRequestFailed will thrown an exception
134
return null;
135         }
136     }
137     
138     /* (non-Javadoc)
139      * @see org.eclipse.jdt.debug.core.IJavaVariable#getGenericSignature()
140      */

141     public String JavaDoc getGenericSignature() throws DebugException {
142         try {
143             String JavaDoc genericSignature= fLocal.genericSignature();
144             if (genericSignature != null) {
145                 return genericSignature;
146             }
147             return fLocal.signature();
148         } catch (RuntimeException JavaDoc e) {
149             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDILocalVariable_exception_retrieving_local_variable_type_signature, new String JavaDoc[] {e.toString()}), e);
150             // execution will not reach this line, as
151
// #targetRequestFailed will thrown an exception
152
return null;
153         }
154     }
155     
156     /**
157      * Updates this local's underlying variable. Called by enclosing stack
158      * frame when doing an incremental update.
159      */

160     protected void setLocal(LocalVariable local) {
161         fLocal = local;
162     }
163     
164     protected LocalVariable getLocal() {
165         return fLocal;
166     }
167     
168     protected JDIStackFrame getStackFrame() {
169         return fStackFrame;
170     }
171
172     /**
173      * @see java.lang.Object#toString()
174      */

175     public String JavaDoc toString() {
176         return getLocal().toString();
177     }
178     
179     /**
180      * @see IValueModification#setValue(IValue)
181      */

182     public void setValue(IValue v) throws DebugException {
183         if (verifyValue(v)) {
184             JDIValue value = (JDIValue)v;
185             setJDIValue(value.getUnderlyingValue());
186         }
187     }
188
189     /**
190      * @see JDIVariable#getUnderlyingType()
191      */

192     protected Type getUnderlyingType() throws DebugException {
193         try {
194             return getLocal().type();
195         } catch (ClassNotLoadedException e) {
196             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDILocalVariable_exception_while_retrieving_type_of_local_variable, new String JavaDoc[]{e.toString()}), e);
197         } catch (RuntimeException JavaDoc e) {
198             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDILocalVariable_exception_while_retrieving_type_of_local_variable, new String JavaDoc[]{e.toString()}), e);
199         }
200         // this line will not be exceucted as an exception
201
// will be throw in type retrieval fails
202
return null;
203     }
204     
205     /**
206      *
207      * @see org.eclipse.jdt.debug.core.IJavaVariable#isLocal()
208      */

209     public boolean isLocal() {
210         return true;
211     }
212 }
213
Popular Tags