You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SourceContextImpl.java 2.4KB

15 anni fa
15 anni fa
15 anni fa
15 anni fa
15 anni fa
15 anni fa
15 anni fa
15 anni fa
15 anni fa
15 anni fa
15 anni fa
15 anni fa
15 anni fa
15 anni fa
15 anni fa
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver;
  13. import java.io.File;
  14. import java.util.Arrays;
  15. import org.aspectj.bridge.ISourceLocation;
  16. import org.aspectj.bridge.SourceLocation;
  17. public class SourceContextImpl implements ISourceContext {
  18. private int[] lineBreaks;
  19. String sourceFilename;
  20. public SourceContextImpl(AbstractReferenceTypeDelegate delegate) {
  21. sourceFilename = delegate.getSourcefilename();
  22. }
  23. public void configureFromAttribute(String name, int[] linebreaks) {
  24. this.sourceFilename = name;
  25. this.lineBreaks = linebreaks;
  26. }
  27. public void setSourceFileName(String name) {
  28. sourceFilename = name;
  29. }
  30. private File getSourceFile() {
  31. return new File(sourceFilename);
  32. }
  33. public void tidy() {
  34. }
  35. public int getOffset() {
  36. return 0;
  37. }
  38. public ISourceLocation makeSourceLocation(IHasPosition position) {
  39. if (lineBreaks != null) {
  40. int line = Arrays.binarySearch(lineBreaks, position.getStart());
  41. if (line < 0) {
  42. line = -line;
  43. }
  44. return new SourceLocation(getSourceFile(), line); // ??? have more info
  45. } else {
  46. return new SourceLocation(getSourceFile(), 0);
  47. }
  48. }
  49. public ISourceLocation makeSourceLocation(int line, int offset) {
  50. if (line < 0) {
  51. line = 0;
  52. }
  53. SourceLocation sl = new SourceLocation(getSourceFile(), line);
  54. if (offset > 0) {
  55. sl.setOffset(offset);
  56. } else {
  57. if (lineBreaks != null) {
  58. int likelyOffset = 0;
  59. if (line > 0 && line < lineBreaks.length) {
  60. // 1st char of given line is next char after previous end of line
  61. likelyOffset = lineBreaks[line - 1] + 1;
  62. }
  63. sl.setOffset(likelyOffset);
  64. }
  65. }
  66. return sl;
  67. }
  68. public final static ISourceContext UNKNOWN_SOURCE_CONTEXT = new ISourceContext() {
  69. public ISourceLocation makeSourceLocation(IHasPosition position) {
  70. return null;
  71. }
  72. public ISourceLocation makeSourceLocation(int line, int offset) {
  73. return null;
  74. }
  75. public int getOffset() {
  76. return 0;
  77. }
  78. public void tidy() {
  79. }
  80. };
  81. }