KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > importd2 > ImportDebugger


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.importd2;
21
22 import java.util.*;
23 import javax.swing.text.StyledDocument JavaDoc;
24
25 import org.openide.TopManager;
26 import org.openide.cookies.EditorCookie;
27 import org.openide.debugger.*;
28 import org.openide.text.Line;
29 import org.openide.text.NbDocument;
30 import org.openide.util.MapFormat;
31 import org.openide.util.NbBundle;
32
33 import org.netbeans.modules.debugger.*;
34 import org.netbeans.modules.debugger.support.DebuggerAnnotation;
35 import org.netbeans.modules.debugger.support.SecondaryDebuggerSupport;
36 import org.netbeans.modules.debugger.support.StateSupport;
37 import org.netbeans.modules.debugger.support.util.Utils;
38 import org.netbeans.modules.debugger.support.util.ValidatorImpl;
39
40 /**
41  * Example of Debugger Implementation. It "debugs" import statements
42  * in Java files (so it does not need compiled .class files). Step
43  * Into on "import java.awt.Button" opens .java file for Button and
44  * puts current line to the first line of it.
45  *
46  * @author Jan Jancura
47  */

48 public class ImportDebugger extends SecondaryDebuggerSupport {
49
50     // static ..................................................................
51

52     /** generated Serialized Version UID */
53     static final long serialVersionUID = 84466455228078708L;
54     /** bundle to obtain text information from */
55     private static ResourceBundle bundle;
56
57     static String JavaDoc getLocString (String JavaDoc s) {
58         if (bundle == null)
59             bundle = NbBundle.getBundle (ImportDebugger.class);
60         return bundle.getString (s);
61     }
62
63     /** debugger State constant. */
64     public static final State STATE_STOPPED =
65         new StateSupport (
66             StateSupport.PAUSE |
67             StateSupport.STEP_OVER |
68             StateSupport.STEP_INTO |
69             StateSupport.STEP_OUT
70         );
71     /** debugger State constant. */
72     public static final State STATE_RUNNING =
73         new StateSupport (
74             StateSupport.PAUSE
75         );
76     
77     static DebuggerAnnotation.CurrentPC currentLineAnnotation =
78         new DebuggerAnnotation.CurrentPC ();
79
80     
81     // variables ...............................................................
82

83     transient private ValidatorImpl validator;
84
85
86     // init ....................................................................
87

88     public ImportDebugger () {
89         validator = new ValidatorImpl ();
90     }
91
92
93     // Debugger implementation .................................................
94

95     /** Starts the debugger. The method stops the current debugging (if any)
96     * and takes information from the provided info (containing the class to start and
97     * arguments to pass it and name of class to stop debugging in) and starts
98     * new debugging session.
99     *
100     * @param info debugger info about class to start
101     * @exception DebuggerException if an error occures during the start of the debugger
102     */

103     public void startDebugger (DebuggerInfo info) throws DebuggerException {
104         boolean stopOnMain = info.getStopClassName () != null;
105
106         super.startDebugger (info);
107         setState (Debugger.DEBUGGER_RUNNING);
108         String JavaDoc stopClassName = info.getClassName ();
109         Line l = Utils.getLine (stopClassName, 1);
110         getStack (info).push (l);
111         getIsOnStack ().add (l.getDataObject ());
112
113         // stop on main
114
stopOnMain = true;
115         if (stopOnMain) {
116             setState (DEBUGGER_STOPPED);
117             setCurrentLine (l);
118             refreshWatches ();
119         } else
120             go ();
121     }
122
123     /**
124     * Finishes debugger.
125     */

126     public void finishDebugger () throws DebuggerException {
127         super.finishDebugger ();
128     }
129
130     /**
131     * Trace into.
132     */

133     synchronized public void traceInto () throws DebuggerException {
134         setState (DEBUGGER_RUNNING);
135         Line l = step ();
136         if (l == null) {
137             finishDebugger ();
138             refreshWatches ();
139             return;
140         }
141         setCurrentLine (l);
142         setState (DEBUGGER_STOPPED);
143         setLastAction (ACTION_TRACE_INTO);
144         refreshWatches ();
145     }
146
147     /**
148     * Trace over.
149     */

150     synchronized public void traceOver () throws DebuggerException {
151         setState (DEBUGGER_RUNNING);
152         Stack stack = getStack ();
153         int d = stack.size ();
154         Line l = null;
155         do {
156             l = step ();
157             if (l == null) {
158                 finishDebugger ();
159                 refreshWatches ();
160                 return;
161             }
162         } while (stack.size () > d);
163         setCurrentLine (l);
164         setState (DEBUGGER_STOPPED);
165         setLastAction (ACTION_TRACE_OVER);
166         refreshWatches ();
167     }
168
169     /**
170     * Go.
171     */

172     synchronized public void go () throws DebuggerException {
173         setLastAction (ACTION_CONTINUE);
174         setState (DEBUGGER_RUNNING);
175         
176         Line l = null;
177         do {
178             l = step ();
179         } while (l != null);
180         finishDebugger ();
181         refreshWatches ();
182     }
183
184     /**
185     * Step out.
186     */

187     synchronized public void stepOut () throws DebuggerException {
188         setLastAction (ACTION_STEP_OUT);
189         setState (DEBUGGER_RUNNING);
190         
191         Stack stack = getStack ();
192         int d = stack.size () - 1;
193         Line l = null;
194         do {
195             l = step ();
196             if (l == null) {
197                 finishDebugger ();
198                 refreshWatches ();
199                 return;
200             }
201         } while (stack.size () > d);
202         setCurrentLine (l);
203         setState (DEBUGGER_STOPPED);
204         setLastAction (ACTION_TRACE_INTO);
205         refreshWatches ();
206     }
207
208     /**
209      * Jumps to the next call site (towards the top of the call-stack).
210      */

211     public void goToCalledMethod () {
212     }
213     
214     /**
215      * Jumps to the previous call site (towards the bottom of the call-stack).
216      */

217     public void goToCallingMethod () {
218     }
219
220     /**
221      * Jumps to a given line.
222      *
223      * @param l a line jump to
224      */

225     public void runToCursor (Line l) {
226     }
227
228     /**
229      * Pauses debugging.
230      */

231     public void pause () {
232     }
233
234     public void setCurrentLine (Line l) {
235         Line old = getCurrentLine ();
236         if (old != null) {
237            // old.unmarkCurrentLine ();
238
currentLineAnnotation.detachLine ();
239         }
240         if (l != null) {
241             Utils.showInEditor (l);
242             //l.markCurrentLine ();
243
currentLineAnnotation.attachLine (l);
244         }
245         super.setCurrentLine (l);
246     }
247
248     protected void setState (int state) {
249         super.setState (state);
250         switch (state) {
251             case DEBUGGER_NOT_RUNNING:
252                 setDebuggerState (STATE_NOT_RUNNING);
253                 break;
254             case DEBUGGER_RUNNING:
255                 setDebuggerState (STATE_RUNNING);
256                 break;
257             case DEBUGGER_STOPPED:
258                 setDebuggerState (STATE_STOPPED);
259                 break;
260         }
261     }
262     
263     /** Creates new uninitialized watch. The watch is visible (not hidden).
264     *
265     * @return new uninitialized watch
266     */

267     public Watch createWatch () {
268         ImportWatch w = new ImportWatch (this, false);
269         addWatch (w);
270         return w;
271     }
272
273     /** Creates a watch its expression is set to initial value. Also
274     * allows to create a watch not presented to the user, for example
275     * for internal usage in the editor to obtain values of variables
276     * under the mouse pointer.
277     *
278     * @param expr expresion to watch for
279     * @param hidden allows to specify whether the watch should be presented
280     * to the user or not (be only of internal usage of the IDE).
281     * @return new watch
282     */

283     public Watch createWatch (String JavaDoc expr, boolean hidden) {
284         ImportWatch w = new ImportWatch (this, hidden);
285         w.setVariableName (expr);
286         addWatch (w);
287         return w;
288     }
289     
290     
291     // inner debugger implementation ...........................................
292

293     Validator getValidator () {
294         return validator;
295     }
296     
297     void refreshWatches () {
298         validator.validate ();
299     }
300     
301     
302     // inner debugger implementation ...........................................
303

304     //private HashSet isOnStack = new HashSet ();
305

306     private HashMap diToStack = new HashMap ();
307     private HashMap diToIsOnStack = new HashMap ();
308     private CoreDebugger coreDebugger;
309     
310     Stack getStack (DebuggerInfo debuggerInfo) {
311         Stack s = (Stack) diToStack.get (debuggerInfo);
312         if (s == null) {
313             s = new Stack ();
314             diToStack.put (debuggerInfo, s);
315         }
316         return s;
317     }
318     
319     HashSet getIsOnStack (DebuggerInfo debuggerInfo) {
320         HashSet s = (HashSet) diToIsOnStack.get (debuggerInfo);
321         if (s == null) {
322             s = new HashSet ();
323             diToIsOnStack.put (debuggerInfo, s);
324         }
325         return s;
326     }
327     
328     CoreDebugger getCoreDebugger () {
329         if (coreDebugger == null)
330             try {
331                 coreDebugger = (CoreDebugger) TopManager.getDefault ().
332                     getDebugger ();
333             } catch (DebuggerException e) {
334             }
335         return coreDebugger;
336     }
337     
338     Stack getStack () {
339         return getStack (
340             getInfo ()
341         );
342     }
343     
344     HashSet getIsOnStack () {
345         return getIsOnStack (
346             getInfo ()
347         );
348     }
349     
350     DebuggerInfo getInfo () {
351         DebuggerInfo i = null;
352         if (getCoreDebugger ().getCurrentDebugger () != null)
353             i = getCoreDebugger ().getCurrentDebugger ().getDebuggerInfo ();
354         if (i == null) i = getDebuggerInfo ();
355         Thread.dumpStack();
356         return i;
357     }
358     
359     Line step () {
360         Stack stack = getStack ();
361         HashSet isOnStack = getIsOnStack ();
362         Line l = (Line) stack.lastElement ();
363         try {
364             // Trace into
365
String JavaDoc str = getText (l);
366             if (str.startsWith ("import ")) {
367                 str = str.substring (7, str.length () - 1); //.replace ('.', File.separatorChar);
368
Line ll = Utils.getLine (str, 1);
369                 if (ll != null)
370                     if (!isOnStack.contains (ll.getDataObject ())) {
371                         stack.push (ll);
372                         isOnStack.add (ll.getDataObject ());
373                         return ll;
374                     }
375             }
376         } catch (Exception JavaDoc e) {
377         }
378             
379         stack.pop ();
380
381         // trace over
382
try {
383             if (l.getLineNumber () < 50) {
384                 Line ll = Utils.getLine (
385                     l.getDataObject ().getPrimaryFile ().getPackageName ('.'),
386                     l.getLineNumber () + 2
387                 );
388                 if (ll != null) {
389                     stack.push (ll);
390                     return ll;
391                 }
392             }
393         } catch (Exception JavaDoc e) {
394         }
395
396         // Step out
397
if (stack.empty ()) return null;
398         Line ll = (Line) stack.pop ();
399         if (ll.getLineNumber () < 50)
400             try {
401                 ll = Utils.getLine (ll.getDataObject ().getPrimaryFile ().
402                     getPackageName ('.'), ll.getLineNumber () + 2
403                 );
404             } catch (Exception JavaDoc e) {
405             }
406         stack.push (ll);
407         return ll;
408     }
409     
410     static String JavaDoc getText (Line l) throws Exception JavaDoc {
411         EditorCookie ec = (EditorCookie) l.getDataObject ().
412             getCookie (EditorCookie.class);
413         StyledDocument JavaDoc doc = ec.openDocument ();
414         if (doc == null) return "";
415         int off = NbDocument.findLineOffset (doc, l.getLineNumber ());
416         int len = NbDocument.findLineOffset (doc, l.getLineNumber () + 1) -
417             off - 1;
418         return doc.getText (off, len);
419     }
420 }
421
Popular Tags