1 19 20 package org.netbeans.modules.debugger.importd; 21 22 import java.util.*; 23 import javax.swing.text.StyledDocument ; 24 25 import org.openide.cookies.EditorCookie; 26 import org.openide.debugger.*; 27 import org.openide.text.Line; 28 import org.openide.text.NbDocument; 29 import org.openide.util.MapFormat; 30 import org.openide.util.NbBundle; 31 32 import org.netbeans.modules.debugger.*; 33 import org.netbeans.modules.debugger.support.DebuggerAnnotation; 34 import org.netbeans.modules.debugger.support.SecondaryDebuggerSupport; 35 import org.netbeans.modules.debugger.support.StateSupport; 36 import org.netbeans.modules.debugger.support.util.Utils; 37 import org.netbeans.modules.debugger.support.util.ValidatorImpl; 38 39 47 public class ImportDebugger extends SecondaryDebuggerSupport { 48 49 51 52 static final long serialVersionUID = 84466455228078708L; 53 54 private static ResourceBundle bundle; 55 56 static String getLocString (String s) { 57 if (bundle == null) 58 bundle = NbBundle.getBundle (ImportDebugger.class); 59 return bundle.getString (s); 60 } 61 62 63 public static final State STATE_STOPPED = 64 new StateSupport ( 65 StateSupport.PAUSE | 66 StateSupport.STEP_OVER | 67 StateSupport.STEP_INTO | 68 StateSupport.STEP_OUT 69 ); 70 71 public static final State STATE_RUNNING = 72 new StateSupport ( 73 StateSupport.PAUSE 74 ); 75 76 static DebuggerAnnotation.CurrentPC currentLineAnnotation = 77 new DebuggerAnnotation.CurrentPC (); 78 79 80 82 transient private ValidatorImpl validator; 83 84 85 87 public ImportDebugger () { 88 validator = new ValidatorImpl (); 89 } 90 91 92 94 102 public void startDebugger (DebuggerInfo info) throws DebuggerException { 103 boolean stopOnMain = info.getStopClassName () != null; 104 105 super.startDebugger (info); 106 setState (Debugger.DEBUGGER_RUNNING); 107 String stopClassName = info.getClassName (); 108 Line l = Utils.getLine (stopClassName, 1); 109 stack = new Stack (); 110 stack.push (l); 111 isOnStack = new HashSet (); 112 isOnStack.add (l.getDataObject ()); 113 114 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 int d = stack.size (); 153 Line l = null; 154 do { 155 l = step (); 156 if (l == null) { 157 finishDebugger (); 158 refreshWatches (); 159 return; 160 } 161 } while (stack.size () > d); 162 setCurrentLine (l); 163 setState (DEBUGGER_STOPPED); 164 setLastAction (ACTION_TRACE_OVER); 165 refreshWatches (); 166 } 167 168 171 synchronized public void go () throws DebuggerException { 172 setLastAction (ACTION_CONTINUE); 173 setState (DEBUGGER_RUNNING); 174 175 Line l = null; 176 do { 177 l = step (); 178 } while (l != null); 179 finishDebugger (); 180 refreshWatches (); 181 } 182 183 186 synchronized public void stepOut () throws DebuggerException { 187 setLastAction (ACTION_STEP_OUT); 188 setState (DEBUGGER_RUNNING); 189 190 int d = stack.size () - 1; 191 Line l = null; 192 do { 193 l = step (); 194 if (l == null) { 195 finishDebugger (); 196 refreshWatches (); 197 return; 198 } 199 } while (stack.size () > d); 200 setCurrentLine (l); 201 setState (DEBUGGER_STOPPED); 202 setLastAction (ACTION_TRACE_INTO); 203 refreshWatches (); 204 } 205 206 209 public void goToCalledMethod () { 210 } 211 212 215 public void goToCallingMethod () { 216 } 217 218 223 public void runToCursor (Line l) { 224 } 225 226 229 public void pause () { 230 } 231 232 public void setCurrentLine (Line l) { 233 Line old = getCurrentLine (); 234 if (old != null) { 235 currentLineAnnotation.detachLine (); 237 } 238 if (l != null) { 239 Utils.showInEditor (l); 240 currentLineAnnotation.attachLine (l); 242 } 243 super.setCurrentLine (l); 244 } 245 246 protected void setState (int state) { 247 super.setState (state); 248 switch (state) { 249 case DEBUGGER_NOT_RUNNING: 250 setDebuggerState (STATE_NOT_RUNNING); 251 break; 252 case DEBUGGER_RUNNING: 253 setDebuggerState (STATE_RUNNING); 254 break; 255 case DEBUGGER_STOPPED: 256 setDebuggerState (STATE_STOPPED); 257 break; 258 } 259 } 260 261 265 public Watch createWatch () { 266 ImportWatch w = new ImportWatch (this, false); 267 addWatch (w); 268 return w; 269 } 270 271 281 public Watch createWatch (String expr, boolean hidden) { 282 ImportWatch w = new ImportWatch (this, hidden); 283 w.setVariableName (expr); 284 addWatch (w); 285 return w; 286 } 287 288 289 291 Validator getValidator () { 292 return validator; 293 } 294 295 void refreshWatches () { 296 validator.validate (); 297 } 298 299 300 302 private Stack stack = new Stack (); 303 private HashSet isOnStack = new HashSet (); 304 HashMap lineBreakpoints = new HashMap (); 305 306 Stack getStack () { 307 return stack; 308 } 309 310 Line step () { 311 Line l = (Line) stack.lastElement (); 312 try { 313 String str = getText (l); 315 if (str.startsWith ("import ")) { 316 str = str.substring (7, str.length () - 1); Line ll = Utils.getLine (str, 1); 318 if (ll != null) 319 if (!isOnStack.contains (ll.getDataObject ())) { 320 stack.push (ll); 321 isOnStack.add (ll.getDataObject ()); 322 return ll; 323 } 324 } 325 } catch (Exception e) { 326 } 327 328 stack.pop (); 329 330 try { 332 if (l.getLineNumber () < 50) { 333 Line ll = Utils.getLine ( 334 l.getDataObject ().getPrimaryFile ().getPackageName ('.'), 335 l.getLineNumber () + 2 336 ); 337 if (ll != null) { 338 stack.push (ll); 339 return ll; 340 } 341 } 342 } catch (Exception e) { 343 } 344 345 if (stack.empty ()) return null; 347 Line ll = (Line) stack.pop (); 348 if (ll.getLineNumber () < 50) 349 try { 350 ll = Utils.getLine (ll.getDataObject ().getPrimaryFile (). 351 getPackageName ('.'), ll.getLineNumber () + 2 352 ); 353 } catch (Exception e) { 354 } 355 stack.push (ll); 356 return ll; 357 } 358 359 static String getText (Line l) throws Exception { 360 EditorCookie ec = (EditorCookie) l.getDataObject (). 361 getCookie (EditorCookie.class); 362 StyledDocument doc = ec.openDocument (); 363 if (doc == null) return ""; 364 int off = NbDocument.findLineOffset (doc, l.getLineNumber ()); 365 int len = NbDocument.findLineOffset (doc, l.getLineNumber () + 1) - 366 off - 1; 367 return doc.getText (off, len); 368 } 369 } 370 | Popular Tags |