1 19 20 package org.netbeans.modules.debugger.jpda.ui; 21 22 import java.beans.PropertyChangeListener ; 23 import java.util.List ; 24 import org.netbeans.api.debugger.*; 25 import org.netbeans.api.debugger.DebuggerManager; 26 import org.netbeans.api.debugger.Session; 27 import org.netbeans.api.debugger.jpda.*; 28 import org.netbeans.api.debugger.jpda.event.JPDABreakpointEvent; 29 import org.netbeans.api.debugger.jpda.event.JPDABreakpointListener; 30 import org.netbeans.modules.debugger.jpda.ui.models.BreakpointsNodeModel; 31 import org.netbeans.spi.debugger.ContextProvider; 32 import java.beans.PropertyChangeEvent ; 33 import java.util.*; 34 import java.util.regex.Pattern ; 35 import java.util.regex.Matcher ; 36 import org.netbeans.spi.viewmodel.NodeModel; 37 import org.openide.util.NbBundle; 38 39 45 public class BreakpointOutput extends LazyActionsManagerListener 46 implements DebuggerManagerListener, JPDABreakpointListener, 47 PropertyChangeListener { 48 49 private static final Pattern dollarEscapePattern = Pattern.compile 50 ("\\$"); 51 private static final Pattern backslashEscapePattern = Pattern.compile 52 ("\\\\"); 53 private static final Pattern threadNamePattern = Pattern.compile 54 ("\\{threadName\\}"); 55 private static final Pattern classNamePattern = Pattern.compile 56 ("\\{className\\}"); 57 private static final Pattern methodNamePattern = Pattern.compile 58 ("\\{methodName\\}"); 59 private static final Pattern lineNumberPattern = Pattern.compile 60 ("\\{lineNumber\\}"); 61 private static final Pattern expressionPattern = Pattern.compile 62 ("\\{=(.*?)\\}"); 63 64 private IOManager ioManager; 65 private JPDADebugger debugger; 66 private ContextProvider contextProvider; 67 private Object lock = new Object (); 68 69 70 public BreakpointOutput (ContextProvider contextProvider) { 71 this.contextProvider = contextProvider; 72 this.debugger = (JPDADebugger) contextProvider.lookupFirst 73 (null, JPDADebugger.class); 74 debugger.addPropertyChangeListener ( 75 debugger.PROP_STATE, 76 this 77 ); 78 hookBreakpoints (); 79 DebuggerManager.getDebuggerManager ().addDebuggerListener 80 (DebuggerManager.PROP_BREAKPOINTS, this); 81 } 82 83 84 86 protected void destroy () { 87 DebuggerManager.getDebuggerManager ().removeDebuggerListener 88 (DebuggerManager.PROP_BREAKPOINTS, this); 89 unhookBreakpoints (); 90 synchronized (lock) { 91 ioManager = null; 92 debugger = null; 93 } 94 } 95 96 public String [] getProperties () { 97 return new String [] { ActionsManagerListener.PROP_ACTION_PERFORMED }; 98 } 99 100 101 103 public void breakpointReached (JPDABreakpointEvent event) { 104 synchronized (lock) { 105 if (event.getDebugger () != debugger) return; 106 } 107 if (event.getConditionResult () == event.CONDITION_FALSE) return; 108 JPDABreakpoint breakpoint = (JPDABreakpoint) event.getSource (); 109 getBreakpointsNodeModel ().setCurrentBreakpoint (breakpoint); 110 synchronized (lock) { 111 if (ioManager == null) { 112 lookupIOManager (); 113 if (ioManager == null) return; 114 } 115 } 116 String printText = breakpoint.getPrintText (); 117 if (printText == null || printText.length () == 0) return; 118 printText = substitute(printText, event); 119 synchronized (lock) { 120 if (ioManager != null) { 121 ioManager.println (printText, null); 122 } 123 } 124 } 125 126 127 129 public void breakpointAdded (Breakpoint breakpoint) { 130 hookBreakpoint (breakpoint); 131 } 132 133 public void breakpointRemoved (Breakpoint breakpoint) { 134 unhookBreakpoint (breakpoint); 135 } 136 137 public Breakpoint[] initBreakpoints () {return new Breakpoint[0];} 138 public void initWatches () {} 139 public void watchAdded (Watch watch) {} 140 public void watchRemoved (Watch watch) {} 141 public void sessionAdded (Session session) {} 142 public void sessionRemoved (Session session) {} 143 public void engineAdded (DebuggerEngine engine) {} 144 public void engineRemoved (DebuggerEngine engine) {} 145 146 147 149 public void propertyChange (PropertyChangeEvent evt) { 150 if (JPDABreakpoint.PROP_VALIDITY.equals(evt.getPropertyName())) { 151 if (JPDABreakpoint.VALIDITY.INVALID.equals(evt.getNewValue())) { 152 JPDABreakpoint bp = (JPDABreakpoint) evt.getSource(); 153 String msg = bp.getValidityMessage(); 154 synchronized (lock) { 155 if (ioManager == null) { 156 lookupIOManager (); 157 if (ioManager == null) return; 158 } 159 String printText = (msg != null) ? 160 NbBundle.getMessage(BreakpointOutput.class, "MSG_InvalidBreakpointWithReason", bp.toString(), msg) : 161 NbBundle.getMessage(BreakpointOutput.class, "MSG_InvalidBreakpoint", bp.toString()); 162 IOManager.Line line = null; 163 if (bp instanceof LineBreakpoint) { 164 line = new IOManager.Line ( 165 ((LineBreakpoint) bp).getURL(), 166 ((LineBreakpoint) bp).getLineNumber(), 167 debugger 168 ); 169 } 170 ioManager.println (printText, null, true); 171 if (line != null) { 172 ioManager.println( 173 NbBundle.getMessage(BreakpointOutput.class, "Link_InvalidBreakpoint", bp.toString()), 174 line, true); 175 } 176 } 177 } else if (JPDABreakpoint.VALIDITY.VALID.equals(evt.getNewValue())) { 178 JPDABreakpoint bp = (JPDABreakpoint) evt.getSource(); 179 synchronized (lock) { 180 if (ioManager == null) { 181 lookupIOManager (); 182 if (ioManager == null) return; 183 } 184 String printText = NbBundle.getMessage(BreakpointOutput.class, "MSG_ValidBreakpoint", bp.toString()); 185 IOManager.Line line = null; 186 if (bp instanceof LineBreakpoint) { 187 line = new IOManager.Line ( 188 ((LineBreakpoint) bp).getURL(), 189 ((LineBreakpoint) bp).getLineNumber(), 190 debugger 191 ); 192 } 193 ioManager.println (printText, line, false); 194 } 195 } 196 return ; 197 } 198 synchronized (lock) { 199 if (debugger == null || 200 evt.getPropertyName () != debugger.PROP_STATE || 201 debugger.getState () == debugger.STATE_STOPPED) { 202 203 return ; 204 } 205 } 206 getBreakpointsNodeModel ().setCurrentBreakpoint (null); 207 208 } 209 210 211 213 222 private String substitute (String printText, JPDABreakpointEvent event) { 223 224 JPDAThread t = event.getThread (); 226 if (t != null) { 227 String name = backslashEscapePattern.matcher (t.getName ()). 229 replaceAll ("\\\\\\\\"); 230 name = dollarEscapePattern.matcher (name).replaceAll ("\\\\\\$"); 232 printText = threadNamePattern.matcher (printText).replaceAll (name); 233 } 234 else 235 printText = threadNamePattern.matcher (printText).replaceAll ("?"); 236 237 if (event.getReferenceType () != null) { 239 String name = dollarEscapePattern.matcher 241 (event.getReferenceType ().name ()).replaceAll ("\\\\\\$"); 242 printText = classNamePattern.matcher (printText).replaceAll (name); 243 } else 244 printText = classNamePattern.matcher (printText).replaceAll ("?"); 245 246 Session session = null; 248 Session[] sessions = DebuggerManager.getDebuggerManager().getSessions(); 249 for (int i = 0; i < sessions.length; i++) { 250 if (sessions[i].lookupFirst(null, JPDADebugger.class) == debugger) { 251 session = sessions[i]; 252 break; 253 } 254 } 255 String language = (session != null) ? session.getCurrentLanguage() : null; 256 String methodName = t.getMethodName (); 257 if ("".equals (methodName)) methodName = "?"; 258 methodName = dollarEscapePattern.matcher (methodName).replaceAll 260 ("\\\\\\$"); 261 printText = methodNamePattern.matcher (printText).replaceAll 262 (methodName); 263 264 int lineNumber = t.getLineNumber (language); 266 if (lineNumber < 0) 267 printText = lineNumberPattern.matcher (printText).replaceAll 268 ("?"); 269 else 270 printText = lineNumberPattern.matcher (printText).replaceAll 271 (String.valueOf (lineNumber)); 272 273 for (;;) { 275 Matcher m = expressionPattern.matcher (printText); 276 if (!m.find ()) break; 277 String expression = m.group (1); 278 String value = ""; 279 try { 280 JPDADebugger theDebugger; 281 synchronized (lock) { 282 if (debugger == null) { 283 return value; } 285 theDebugger = debugger; 286 } 287 value = theDebugger.evaluate (expression).getValue (); 288 value = backslashEscapePattern.matcher (value). 289 replaceAll ("\\\\\\\\"); 290 value = dollarEscapePattern.matcher (value). 291 replaceAll ("\\\\\\$"); 292 } catch (InvalidExpressionException e) { 293 String msg = e.getCause () != null ? 295 e.getCause ().getMessage () : e.getMessage (); 296 synchronized (lock) { 297 if (ioManager != null) { 298 ioManager.println ( 299 "Cannot evaluate expression '" + expression + "' : " + msg, 300 null 301 ); 302 } 303 } 304 } 305 printText = m.replaceFirst (value); 306 } 307 Throwable thr = event.getConditionException(); 308 if (thr != null) { 309 printText = printText + "\n***\n"+ thr.getLocalizedMessage()+"\n***\n"; 310 } 311 return printText; 312 } 313 314 private void lookupIOManager () { 315 List lamls = contextProvider.lookup 316 (null, LazyActionsManagerListener.class); 317 for (Iterator i = lamls.iterator (); i.hasNext ();) { 318 Object o = i.next(); 319 if (o instanceof DebuggerOutput) { 320 ioManager = ((DebuggerOutput) o).getIOManager (); 321 break; 322 } 323 } 324 } 325 326 private void hookBreakpoints () { 327 Breakpoint [] bpts = DebuggerManager.getDebuggerManager (). 328 getBreakpoints (); 329 for (int i = 0; i < bpts.length; i++) { 330 Breakpoint bpt = bpts [i]; 331 hookBreakpoint (bpt); 332 } 333 } 334 335 private void unhookBreakpoints () { 336 Breakpoint [] bpts = DebuggerManager.getDebuggerManager (). 337 getBreakpoints (); 338 for (int i = 0; i < bpts.length; i++) { 339 Breakpoint bpt = bpts [i]; 340 unhookBreakpoint (bpt); 341 } 342 } 343 344 private void hookBreakpoint (Breakpoint breakpoint) { 345 if (breakpoint instanceof JPDABreakpoint) { 346 JPDABreakpoint jpdaBreakpoint = (JPDABreakpoint) breakpoint; 347 jpdaBreakpoint.addJPDABreakpointListener (this); 348 jpdaBreakpoint.addPropertyChangeListener(JPDABreakpoint.PROP_VALIDITY, this); 349 } 350 } 351 352 private void unhookBreakpoint (Breakpoint breakpoint) { 353 if (breakpoint instanceof JPDABreakpoint) { 354 JPDABreakpoint jpdaBreakpoint = (JPDABreakpoint) breakpoint; 355 jpdaBreakpoint.removeJPDABreakpointListener (this); 356 jpdaBreakpoint.removePropertyChangeListener(JPDABreakpoint.PROP_VALIDITY, this); 357 } 358 } 359 360 private BreakpointsNodeModel breakpointsNodeModel; 361 private BreakpointsNodeModel getBreakpointsNodeModel () { 362 if (breakpointsNodeModel == null) { 363 List l = DebuggerManager.getDebuggerManager ().lookup 364 ("BreakpointsView", NodeModel.class); 365 Iterator it = l.iterator (); 366 while (it.hasNext ()) { 367 NodeModel nm = (NodeModel) it.next (); 368 if (nm instanceof BreakpointsNodeModel) { 369 breakpointsNodeModel = (BreakpointsNodeModel) nm; 370 break; 371 } 372 } 373 } 374 return breakpointsNodeModel; 375 } 376 } 377 | Popular Tags |