src/share/classes/com/sun/tools/javac/util/JCDiagnostic.java

changeset 2426
829f01e7f732
parent 1759
05ec778794d0
child 2525
2eb010b6cb22
equal deleted inserted replaced
2425:76b61848c9a4 2426:829f01e7f732
353 } 353 }
354 354
355 private final DiagnosticType type; 355 private final DiagnosticType type;
356 private final DiagnosticSource source; 356 private final DiagnosticSource source;
357 private final DiagnosticPosition position; 357 private final DiagnosticPosition position;
358 private final int line;
359 private final int column;
360 private final String key; 358 private final String key;
361 protected final Object[] args; 359 protected final Object[] args;
362 private final Set<DiagnosticFlag> flags; 360 private final Set<DiagnosticFlag> flags;
363 private final LintCategory lintCategory; 361 private final LintCategory lintCategory;
362
363 /** source line position (set lazily) */
364 private SourcePosition sourcePosition;
365
366 /**
367 * This class is used to defer the line/column position fetch logic after diagnostic construction.
368 */
369 class SourcePosition {
370
371 private final int line;
372 private final int column;
373
374 SourcePosition() {
375 int n = (position == null ? Position.NOPOS : position.getPreferredPosition());
376 if (n == Position.NOPOS || source == null)
377 line = column = -1;
378 else {
379 line = source.getLineNumber(n);
380 column = source.getColumnNumber(n, true);
381 }
382 }
383
384 public int getLineNumber() {
385 return line;
386 }
387
388 public int getColumnNumber() {
389 return column;
390 }
391 }
364 392
365 /** 393 /**
366 * Create a diagnostic object. 394 * Create a diagnostic object.
367 * @param formatter the formatter to use for the diagnostic 395 * @param formatter the formatter to use for the diagnostic
368 * @param dt the type of diagnostic 396 * @param dt the type of diagnostic
389 this.flags = flags; 417 this.flags = flags;
390 this.source = source; 418 this.source = source;
391 this.position = pos; 419 this.position = pos;
392 this.key = key; 420 this.key = key;
393 this.args = args; 421 this.args = args;
394
395 int n = (pos == null ? Position.NOPOS : pos.getPreferredPosition());
396 if (n == Position.NOPOS || source == null)
397 line = column = -1;
398 else {
399 line = source.getLineNumber(n);
400 column = source.getColumnNumber(n, true);
401 }
402 } 422 }
403 423
404 /** 424 /**
405 * Get the type of this diagnostic. 425 * Get the type of this diagnostic.
406 * @return the type of this diagnostic 426 * @return the type of this diagnostic
493 /** 513 /**
494 * Get the line number within the source referred to by this diagnostic. 514 * Get the line number within the source referred to by this diagnostic.
495 * @return the line number within the source referred to by this diagnostic 515 * @return the line number within the source referred to by this diagnostic
496 */ 516 */
497 public long getLineNumber() { 517 public long getLineNumber() {
498 return line; 518 if (sourcePosition == null) {
519 sourcePosition = new SourcePosition();
520 }
521 return sourcePosition.getLineNumber();
499 } 522 }
500 523
501 /** 524 /**
502 * Get the column number within the line of source referred to by this diagnostic. 525 * Get the column number within the line of source referred to by this diagnostic.
503 * @return the column number within the line of source referred to by this diagnostic 526 * @return the column number within the line of source referred to by this diagnostic
504 */ 527 */
505 public long getColumnNumber() { 528 public long getColumnNumber() {
506 return column; 529 if (sourcePosition == null) {
530 sourcePosition = new SourcePosition();
531 }
532 return sourcePosition.getColumnNumber();
507 } 533 }
508 534
509 /** 535 /**
510 * Get the arguments to be included in the text of the diagnostic. 536 * Get the arguments to be included in the text of the diagnostic.
511 * @return the arguments to be included in the text of the diagnostic 537 * @return the arguments to be included in the text of the diagnostic

mercurial