1 19 20 package org.netbeans.modules.debugger.importd2; 21 22 import java.util.*; 23 import javax.swing.text.StyledDocument ; 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 48 public class ImportDebugger extends SecondaryDebuggerSupport { 49 50 52 53 static final long serialVersionUID = 84466455228078708L; 54 55 private static ResourceBundle bundle; 56 57 static String getLocString (String s) { 58 if (bundle == null) 59 bundle = NbBundle.getBundle (ImportDebugger.class); 60 return bundle.getString (s); 61 } 62 63 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 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 83 transient private ValidatorImpl validator; 84 85 86 88 public ImportDebugger () { 89 validator = new ValidatorImpl (); 90 } 91 92 93 95 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 stopClassName = info.getClassName (); 109 Line l = Utils.getLine (stopClassName, 1); 110 getStack (info).push (l); 111 getIsOnStack ().add (l.getDataObject ()); 112 113 stopOnMain = true; 115 if (stopOnMain) { 116 setState (DEBUGGER_STOPPED); 117 setCurrentLine (l); 118 refreshWatches (); 119 } else 120 go (); 121 } 122 123 126 public void finishDebugger () throws DebuggerException { 127 super.finishDebugger (); 128 } 129 130 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 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 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 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 211 public void goToCalledMethod () { 212 } 213 214 217 public void goToCallingMethod () { 218 } 219 220 225 public void runToCursor (Line l) { 226 } 227 228 231 public void pause () { 232 } 233 234 public void setCurrentLine (Line l) { 235 Line old = getCurrentLine (); 236 if (old != null) { 237 currentLineAnnotation.detachLine (); 239 } 240 if (l != null) { 241 Utils.showInEditor (l); 242 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 267 public Watch createWatch () { 268 ImportWatch w = new ImportWatch (this, false); 269 addWatch (w); 270 return w; 271 } 272 273 283 public Watch createWatch (String expr, boolean hidden) { 284 ImportWatch w = new ImportWatch (this, hidden); 285 w.setVariableName (expr); 286 addWatch (w); 287 return w; 288 } 289 290 291 293 Validator getValidator () { 294 return validator; 295 } 296 297 void refreshWatches () { 298 validator.validate (); 299 } 300 301 302 304 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 String str = getText (l); 366 if (str.startsWith ("import ")) { 367 str = str.substring (7, str.length () - 1); 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 e) { 377 } 378 379 stack.pop (); 380 381 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 e) { 394 } 395 396 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 e) { 405 } 406 stack.push (ll); 407 return ll; 408 } 409 410 static String getText (Line l) throws Exception { 411 EditorCookie ec = (EditorCookie) l.getDataObject (). 412 getCookie (EditorCookie.class); 413 StyledDocument 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 |