Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

EclipseSourceContext.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.ajdt.internal.core.builder;
  13. import java.io.File;
  14. import org.aspectj.ajdt.internal.compiler.lookup.EclipseSourceLocation;
  15. import org.aspectj.bridge.ISourceLocation;
  16. import org.aspectj.bridge.SourceLocation;
  17. import org.aspectj.org.eclipse.jdt.core.compiler.IProblem;
  18. import org.aspectj.org.eclipse.jdt.internal.compiler.CompilationResult;
  19. import org.aspectj.org.eclipse.jdt.internal.compiler.CompilationResult.ProblemsForRemovalFilter;
  20. import org.aspectj.weaver.IEclipseSourceContext;
  21. import org.aspectj.weaver.IHasPosition;
  22. import org.aspectj.weaver.Member;
  23. public class EclipseSourceContext implements IEclipseSourceContext {
  24. CompilationResult result;
  25. int offset = 0;
  26. public EclipseSourceContext(CompilationResult result) {
  27. this.result = result;
  28. }
  29. public EclipseSourceContext(CompilationResult result, int offset) {
  30. this.result = result;
  31. this.offset = offset;
  32. }
  33. public int getOffset() {
  34. return offset;
  35. }
  36. private File getSourceFile() {
  37. return new File(new String(result.fileName));
  38. }
  39. public ISourceLocation makeSourceLocation(IHasPosition position) {
  40. return new EclipseSourceLocation(result, position.getStart(), position.getEnd());
  41. }
  42. public ISourceLocation makeSourceLocation(int line, int offset) {
  43. SourceLocation sl = new SourceLocation(getSourceFile(), line);
  44. if (offset > 0) {
  45. sl.setOffset(offset);
  46. } else {
  47. // compute the offset
  48. //TODO AV - should we do it lazily?
  49. int[] offsets = result.lineSeparatorPositions;
  50. int likelyOffset = 0;
  51. if (line > 0 && line < offsets.length) {
  52. //1st char of given line is next char after previous end of line
  53. likelyOffset = offsets[line-1];//FIXME may be need -2
  54. }
  55. sl.setOffset(likelyOffset);
  56. }
  57. return sl;
  58. }
  59. public void tidy() {
  60. result=null;
  61. }
  62. public void removeUnnecessaryProblems(Member member, int problemLineNumber) {
  63. if (result == null) return;
  64. IProblem[] probs = result.getProblems();
  65. if (probs!=null) {
  66. for (IProblem problem : probs) {
  67. if (problem == null) continue;
  68. if (problem.getID() == IProblem.UnusedMethodDeclaredThrownException
  69. || problem.getID() == IProblem.UnusedConstructorDeclaredThrownException) {
  70. if (problem.getSourceLineNumber() == problemLineNumber) {
  71. UnusedDeclaredThrownExceptionFilter filter =
  72. new UnusedDeclaredThrownExceptionFilter(problem);
  73. result.removeProblems(filter);
  74. }
  75. }
  76. }
  77. }
  78. }
  79. private class UnusedDeclaredThrownExceptionFilter implements ProblemsForRemovalFilter {
  80. private IProblem problemToRemove;
  81. public UnusedDeclaredThrownExceptionFilter(IProblem p) {
  82. problemToRemove = p;
  83. }
  84. public boolean accept(IProblem p) {
  85. if (p.equals(problemToRemove)) {
  86. return true;
  87. }
  88. return false;
  89. }
  90. }
  91. }