1 19 package org.netbeans.modules.debugger.jpda; 20 21 import com.sun.jdi.AbsentInformationException; 22 import com.sun.jdi.Location; 23 import com.sun.jdi.StackFrame; 24 import java.beans.PropertyChangeListener ; 25 import java.io.File ; 26 import java.util.List ; 27 import org.netbeans.spi.debugger.ContextProvider; 28 29 import org.netbeans.api.debugger.jpda.CallStackFrame; 30 import org.netbeans.api.debugger.jpda.Field; 31 import org.netbeans.api.debugger.jpda.JPDADebugger; 32 import org.netbeans.api.debugger.jpda.JPDAThread; 33 import org.netbeans.spi.debugger.jpda.SourcePathProvider; 34 import org.netbeans.spi.debugger.jpda.EditorContext; 35 import org.openide.ErrorManager; 36 37 44 public class SourcePath { 45 46 private ContextProvider lookupProvider; 47 private SourcePathProvider contextProvider; 48 private JPDADebugger debugger; 49 50 51 public SourcePath (ContextProvider lookupProvider) { 52 this.lookupProvider = lookupProvider; 53 debugger = (JPDADebugger) lookupProvider.lookupFirst 54 (null, JPDADebugger.class); 55 } 56 57 private SourcePathProvider getContext () { 58 if (contextProvider == null) { 59 List l = lookupProvider.lookup (null, SourcePathProvider.class); 60 contextProvider = (SourcePathProvider) l.get (0); 61 int i, k = l.size (); 62 for (i = 1; i < k; i++) { 63 contextProvider = new CompoundContextProvider ( 64 (SourcePathProvider) l.get (i), 65 contextProvider 66 ); 67 } 68 } 69 return contextProvider; 70 } 71 72 73 75 85 public String getRelativePath ( 86 String url, 87 char directorySeparator, 88 boolean includeExtension 89 ) { 90 return getContext ().getRelativePath 91 (url, directorySeparator, includeExtension); 92 } 93 94 101 public String getSourceRoot(String url) { 102 return getContext().getSourceRoot(url); 103 } 104 105 114 public String getURL (String relativePath, boolean global) { 115 return getContext ().getURL (relativePath, global); 116 } 117 118 public String getURL ( 119 StackFrame sf, 120 String stratumn 121 ) { 122 try { 123 return getURL ( 124 convertSlash (sf.location ().sourcePath (stratumn)), 125 true 126 ); 127 } catch (AbsentInformationException e) { 128 return getURL ( 129 convertClassNameToRelativePath ( 130 sf.location ().declaringType ().name () 131 ), 132 true 133 ); 134 } 135 } 136 137 public String getURL ( 138 Location loc, 139 String stratumn 140 ) { 141 try { 142 return getURL ( 143 convertSlash(loc.sourcePath(stratumn)), 144 true 145 ); 146 } catch (AbsentInformationException e) { 147 return getURL ( 148 convertClassNameToRelativePath ( 149 loc.declaringType().name() 150 ), 151 true 152 ); 153 } 154 } 155 156 159 public String [] getSourceRoots () { 160 return getContext ().getSourceRoots (); 161 } 162 163 168 public void setSourceRoots (String [] sourceRoots) { 169 getContext ().setSourceRoots (sourceRoots); 170 } 171 172 177 public String [] getOriginalSourceRoots () { 178 return getContext ().getOriginalSourceRoots (); 179 } 180 181 186 public void addPropertyChangeListener (PropertyChangeListener l) { 187 getContext ().addPropertyChangeListener (l); 188 } 189 190 195 public void removePropertyChangeListener ( 196 PropertyChangeListener l 197 ) { 198 getContext ().removePropertyChangeListener (l); 199 } 200 201 202 204 public boolean sourceAvailable ( 205 String relativePath 206 ) { 207 return getURL (relativePath, true) != null; 208 } 209 210 public boolean sourceAvailable ( 211 JPDAThread t, 212 String stratumn 213 ) { 214 try { 215 return sourceAvailable (convertSlash (t.getSourcePath (stratumn))); 216 } catch (AbsentInformationException e) { 217 return sourceAvailable (convertClassNameToRelativePath (t.getClassName ())); 218 } 219 } 220 221 public boolean sourceAvailable ( 222 Field f 223 ) { 224 String className = f.getClassName (); 225 return sourceAvailable (className); 226 } 227 228 public boolean sourceAvailable ( 229 CallStackFrame csf, 230 String stratumn 231 ) { 232 try { 233 return sourceAvailable (convertSlash (csf.getSourcePath (stratumn))); 234 } catch (AbsentInformationException e) { 235 return sourceAvailable (convertClassNameToRelativePath (csf.getClassName ())); 236 } 237 } 238 239 public String getURL ( 240 CallStackFrame csf, 241 String stratumn 242 ) { 243 try { 244 return getURL (convertSlash (csf.getSourcePath (stratumn)), true); 245 } catch (AbsentInformationException e) { 246 return getURL ( 247 convertClassNameToRelativePath (csf.getClassName ()), 248 true 249 ); 250 } 251 } 252 253 public boolean showSource ( 254 JPDAThread t, 255 String stratumn 256 ) { 257 int lineNumber = t.getLineNumber (stratumn); 258 if (lineNumber < 1) lineNumber = 1; 259 try { 260 return EditorContextBridge.showSource ( 261 getURL (convertSlash (t.getSourcePath (stratumn)), true), 262 lineNumber, 263 debugger 264 ); 265 } catch (AbsentInformationException e) { 266 return EditorContextBridge.showSource ( 267 getURL ( 268 convertClassNameToRelativePath (t.getClassName ()), 269 true 270 ), 271 lineNumber, 272 debugger 273 ); 274 } 275 } 276 277 public boolean showSource (Field v) { 278 String fieldName = ((Field) v).getName (); 279 String className = className = ((Field) v).getClassName (); 280 String url = getURL ( 281 EditorContextBridge.getRelativePath (className), true 282 ); 283 if (url == null) return false; 284 int lineNumber = lineNumber = EditorContextBridge.getFieldLineNumber ( 285 url, 286 className, 287 fieldName 288 ); 289 if (lineNumber < 1) lineNumber = 1; 290 return EditorContextBridge.showSource ( 291 url, 292 lineNumber, 293 debugger 294 ); 295 } 296 297 private static String convertSlash (String original) { 298 return original.replace (File.separatorChar, '/'); 299 } 300 301 public static String convertClassNameToRelativePath ( 302 String className 303 ) { 304 int i = className.indexOf ('$'); 305 if (i > 0) className = className.substring (0, i); 306 String sourceName = className.replace 307 ('.', '/') + ".java"; 308 return sourceName; 309 } 310 311 public Object annotate ( 312 JPDAThread t, 313 String stratumn 314 ) { 315 int lineNumber = t.getLineNumber (stratumn); 316 if (lineNumber < 1) return null; 317 try { 318 return EditorContextBridge.annotate ( 319 getURL (convertSlash (t.getSourcePath (stratumn)), true), 320 lineNumber, 321 EditorContext.CURRENT_LINE_ANNOTATION_TYPE, 322 debugger 323 ); 324 } catch (AbsentInformationException e) { 325 return EditorContextBridge.annotate ( 326 getURL ( 327 convertClassNameToRelativePath (t.getClassName ()), true 328 ), 329 lineNumber, 330 EditorContext.CURRENT_LINE_ANNOTATION_TYPE, 331 debugger 332 ); 333 } 334 } 335 336 public Object annotate ( 337 CallStackFrame csf, 338 String stratumn 339 ) { 340 int lineNumber = csf.getLineNumber (stratumn); 341 if (lineNumber < 1) return null; 342 try { 343 return EditorContextBridge.annotate ( 344 getURL (convertSlash (csf.getSourcePath (stratumn)), true), 345 lineNumber, 346 EditorContext.CALL_STACK_FRAME_ANNOTATION_TYPE, 347 debugger 348 ); 349 } catch (AbsentInformationException e) { 350 return EditorContextBridge.annotate ( 351 getURL ( 352 convertClassNameToRelativePath (csf.getClassName ()), true 353 ), 354 lineNumber, 355 EditorContext.CALL_STACK_FRAME_ANNOTATION_TYPE, 356 debugger 357 ); 358 } 359 } 360 361 362 364 private static class CompoundContextProvider extends SourcePathProvider { 365 366 private SourcePathProvider cp1, cp2; 367 368 CompoundContextProvider ( 369 SourcePathProvider cp1, 370 SourcePathProvider cp2 371 ) { 372 this.cp1 = cp1; 373 this.cp2 = cp2; 374 } 375 376 public String getURL (String relativePath, boolean global) { 377 String p1 = cp1.getURL (relativePath, global); 378 if (p1 != null) return p1; 379 return cp2.getURL (relativePath, global); 380 } 381 382 public String getRelativePath ( 383 String url, 384 char directorySeparator, 385 boolean includeExtension 386 ) { 387 String p1 = cp1.getRelativePath ( 388 url, 389 directorySeparator, 390 includeExtension 391 ); 392 if (p1 != null) return p1; 393 return cp2.getRelativePath ( 394 url, 395 directorySeparator, 396 includeExtension 397 ); 398 } 399 400 public String getSourceRoot(String url) { 401 String sourceRoot = cp1.getSourceRoot(url); 402 if (sourceRoot == null) { 403 sourceRoot = cp2.getSourceRoot(url); 404 } 405 return sourceRoot; 406 } 407 408 public String [] getSourceRoots () { 409 String [] fs1 = cp1.getSourceRoots (); 410 String [] fs2 = cp2.getSourceRoots (); 411 String [] fs = new String [fs1.length + fs2.length]; 412 System.arraycopy (fs1, 0, fs, 0, fs1.length); 413 System.arraycopy (fs2, 0, fs, fs1.length, fs2.length); 414 return fs; 415 } 416 417 public String [] getOriginalSourceRoots () { 418 String [] fs1 = cp1.getOriginalSourceRoots (); 419 String [] fs2 = cp2.getOriginalSourceRoots (); 420 String [] fs = new String [fs1.length + fs2.length]; 421 System.arraycopy (fs1, 0, fs, 0, fs1.length); 422 System.arraycopy (fs2, 0, fs, fs1.length, fs2.length); 423 return fs; 424 } 425 426 public void setSourceRoots (String [] sourceRoots) { 427 cp1.setSourceRoots (sourceRoots); 428 cp2.setSourceRoots (sourceRoots); 429 } 430 431 public void addPropertyChangeListener (PropertyChangeListener l) { 432 cp1.addPropertyChangeListener (l); 433 cp2.addPropertyChangeListener (l); 434 } 435 436 public void removePropertyChangeListener (PropertyChangeListener l) { 437 cp1.removePropertyChangeListener (l); 438 cp2.removePropertyChangeListener (l); 439 } 440 } 441 442 private static class CompoundAnnotation { 443 CompoundAnnotation () {} 444 445 Object annotation1; 446 Object annotation2; 447 } 448 } 449 450 | Popular Tags |