KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > models > CallStackFrameImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.debugger.jpda.models;
21
22 import com.sun.jdi.AbsentInformationException;
23 import com.sun.jdi.IncompatibleThreadStateException;
24 import com.sun.jdi.InvalidStackFrameException;
25 import com.sun.jdi.NativeMethodException;
26 import com.sun.jdi.ObjectReference;
27 import com.sun.jdi.StackFrame;
28 import com.sun.jdi.ThreadReference;
29 import com.sun.jdi.VMDisconnectedException;
30 import com.sun.jdi.Value;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.List JavaDoc;
35
36 import org.netbeans.api.debugger.jpda.CallStackFrame;
37 import org.netbeans.api.debugger.jpda.JPDAThread;
38 import org.netbeans.api.debugger.jpda.LocalVariable;
39 import org.netbeans.api.debugger.jpda.This;
40 import org.netbeans.modules.debugger.jpda.JPDADebuggerImpl;
41 import org.netbeans.spi.debugger.jpda.EditorContext.Operation;
42
43
44 /**
45 * Class representating one line of callstack.
46 */

47 public class CallStackFrameImpl implements CallStackFrame {
48     
49     private StackFrame sf;
50     private int depth;
51     private JPDADebuggerImpl debugger;
52     //private AST ast;
53
private Operation currentOperation;
54     private boolean valid;
55     
56     public CallStackFrameImpl (
57         StackFrame sf,
58         int depth,
59         JPDADebuggerImpl debugger
60     ) {
61         this.sf = sf;
62         this.depth = depth;
63         this.debugger = debugger;
64         this.valid = true; // suppose we're valid when we're new
65
}
66
67     // public interface ........................................................
68

69     /**
70     * Returns line number of this frame in this callstack.
71     *
72     * @return Returns line number of this frame in this callstack.
73     */

74     public synchronized int getLineNumber (String JavaDoc struts) {
75         if (!valid) return 0;
76         try {
77             return getStackFrame().location ().lineNumber (struts);
78         } catch (InvalidStackFrameException isfex) {
79             // this stack frame is not available or information in it is not available
80
valid = false;
81             return 0;
82         } catch (VMDisconnectedException ex) {
83             return 0;
84         }
85     }
86     
87     public synchronized Operation getCurrentOperation(String JavaDoc struts) {
88         return currentOperation;
89     }
90     
91     public synchronized void setCurrentOperation(Operation operation) {
92         this.currentOperation = operation;
93     }
94
95     /**
96     * Returns method name of this frame in this callstack.
97     *
98     * @return Returns method name of this frame in this callstack.
99     */

100     public synchronized String JavaDoc getMethodName () {
101         if (!valid) return "";
102         try {
103             return getStackFrame().location ().method ().name ();
104         } catch (InvalidStackFrameException ex) {
105             // this stack frame is not available or information in it is not available
106
valid = false;
107             return "";
108         } catch (VMDisconnectedException ex) {
109             return "";
110         }
111     }
112
113     /**
114     * Returns class name of this frame in this callstack.
115     *
116     * @return class name of this frame in this callstack
117     */

118     public synchronized String JavaDoc getClassName () {
119         if (!valid) return "";
120         try {
121             return getStackFrame().location ().declaringType ().name ();
122         } catch (InvalidStackFrameException ex) {
123             // this stack frame is not available or information in it is not available
124
valid = false;
125             return "";
126         } catch (VMDisconnectedException ex) {
127             return "";
128         }
129     }
130
131     /**
132     * Returns name of default stratumn.
133     *
134     * @return name of default stratumn
135     */

136     public synchronized String JavaDoc getDefaultStratum () {
137         if (!valid) return "";
138         try {
139             return getStackFrame().location ().declaringType ().defaultStratum ();
140         } catch (InvalidStackFrameException ex) {
141             // this stack frame is not available or information in it is not available
142
valid = false;
143             return "";
144         } catch (VMDisconnectedException ex) {
145             return "";
146         }
147     }
148
149     /**
150     * Returns name of default stratumn.
151     *
152     * @return name of default stratumn
153     */

154     public synchronized List JavaDoc<String JavaDoc> getAvailableStrata () {
155         if (!valid) return Collections.emptyList();
156         try {
157             return getStackFrame().location ().declaringType ().availableStrata ();
158         } catch (InvalidStackFrameException ex) {
159             // this stack frame is not available or information in it is not available
160
valid = false;
161             return Collections.emptyList();
162         } catch (VMDisconnectedException ex) {
163             return Collections.emptyList();
164         }
165     }
166
167     /**
168     * Returns name of file of this frame.
169     *
170     * @return name of file of this frame
171     * @throws NoInformationException if informations about source are not included or some other error
172     * occurres.
173     */

174     public synchronized String JavaDoc getSourceName (String JavaDoc stratum) throws AbsentInformationException {
175         if (!valid) return "";
176         try {
177             return getStackFrame().location ().sourceName (stratum);
178         } catch (InvalidStackFrameException ex) {
179             // this stack frame is not available or information in it is not available
180
valid = false;
181             return "";
182         } catch (VMDisconnectedException ex) {
183             return "";
184         }
185     }
186     
187     /**
188      * Returns source path of file this frame is stopped in or null.
189      *
190      * @return source path of file this frame is stopped in or null
191      */

192     public synchronized String JavaDoc getSourcePath (String JavaDoc stratum) throws AbsentInformationException {
193         if (!valid) return "";
194         try {
195             return getStackFrame().location ().sourcePath (stratum);
196         } catch (InvalidStackFrameException ex) {
197             // this stack frame is not available or information in it is not available
198
valid = false;
199             return "";
200         } catch (VMDisconnectedException ex) {
201             return "";
202         }
203     }
204     
205     /**
206      * Returns local variables.
207      *
208      * @return local variables
209      */

210     public org.netbeans.api.debugger.jpda.LocalVariable[] getLocalVariables ()
211     throws AbsentInformationException {
212         try {
213             String JavaDoc className = getStackFrame ().location ().declaringType ().name ();
214             List JavaDoc l = getStackFrame ().visibleVariables ();
215             int n = l.size();
216             LocalVariable[] locals = new LocalVariable [n];
217             for (int i = 0; i < n; i++) {
218                 com.sun.jdi.LocalVariable lv = (com.sun.jdi.LocalVariable) l.get (i);
219                 Value v = getStackFrame ().getValue (lv);
220                 Local local = (Local) debugger.getLocalVariable(lv, v);
221                 local.setFrame(this);
222                 local.setInnerValue(v);
223                 local.setClassName(className);
224                 locals[i] = local;
225             }
226             return locals;
227         } catch (NativeMethodException ex) {
228             throw new AbsentInformationException ("native method");
229         } catch (InvalidStackFrameException ex) {
230             throw new AbsentInformationException ("thread is running");
231         } catch (VMDisconnectedException ex) {
232             return new LocalVariable [0];
233         }
234     }
235     
236     /**
237      * Returns object reference this frame is associated with or null (
238      * frame is in static method).
239      *
240      * @return object reference this frame is associated with or null
241      */

242     public synchronized This getThisVariable () {
243         if (!valid) return null;
244         ObjectReference thisR;
245         try {
246             thisR = getStackFrame().thisObject ();
247         } catch (InvalidStackFrameException ex) {
248             valid = false;
249             return null;
250         }
251         if (thisR == null) return null;
252         return new ThisVariable (debugger, thisR, "");
253     }
254     
255     /**
256      * Sets this frame current.
257      *
258      * @see org.netbeans.api.debugger.jpda.JPDADebugger#getCurrentCallStackFrame
259      */

260     public void makeCurrent () {
261         debugger.setCurrentCallStackFrame (this);
262     }
263     
264     /**
265      * Returns <code>true</code> if the method in this frame is obsoleted.
266      *
267      * @return <code>true</code> if the method in this frame is obsoleted
268      * @throws InvalidStackFrameException when this stack frame becomes invalid
269      */

270     public synchronized boolean isObsolete () {
271         return getStackFrame ().location ().method ().isObsolete ();
272     }
273     
274     public boolean canPop() {
275         if (!debugger.canPopFrames()) return false;
276         ThreadReference t = getStackFrame().thread();
277         try {
278             if (t.frameCount() <= 1) { // Nowhere to pop
279
return false;
280             }
281             List JavaDoc topFrames = t.frames(0, 2);
282             if (((StackFrame) topFrames.get(0)).location().method().isNative() ||
283                 ((StackFrame) topFrames.get(1)).location().method().isNative()) {
284                 // Have native methods on the stack - can not pop
285
return