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.

WeaverMessageHandler.java 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*******************************************************************************
  2. * Copyright (c) 2004 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v 2.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. *******************************************************************************/
  11. package org.aspectj.ajdt.internal.compiler;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import org.aspectj.ajdt.internal.compiler.lookup.EclipseSourceLocation;
  15. import org.aspectj.bridge.AbortException;
  16. import org.aspectj.bridge.IMessage;
  17. import org.aspectj.bridge.IMessageHandler;
  18. import org.aspectj.bridge.ISourceLocation;
  19. import org.aspectj.bridge.SourceLocation;
  20. import org.aspectj.bridge.IMessage.Kind;
  21. import org.aspectj.org.eclipse.jdt.core.compiler.CategorizedProblem;
  22. import org.aspectj.org.eclipse.jdt.core.compiler.IProblem;
  23. import org.aspectj.org.eclipse.jdt.internal.compiler.CompilationResult;
  24. import org.aspectj.org.eclipse.jdt.internal.compiler.Compiler;
  25. import org.aspectj.org.eclipse.jdt.internal.compiler.impl.ReferenceContext;
  26. import org.aspectj.org.eclipse.jdt.internal.compiler.problem.DefaultProblem;
  27. import org.aspectj.org.eclipse.jdt.internal.compiler.problem.ProblemSeverities;
  28. import org.aspectj.weaver.LintMessage;
  29. /**
  30. * @author colyer
  31. *
  32. * To change the template for this generated type comment go to Window>Preferences>Java>Code Generation>Code and
  33. * Comments
  34. */
  35. public class WeaverMessageHandler implements IMessageHandler {
  36. private IMessageHandler sink;
  37. private CompilationResult currentlyWeaving;
  38. private Compiler compiler;
  39. public WeaverMessageHandler(IMessageHandler handler, Compiler compiler) {
  40. this.sink = handler;
  41. this.compiler = compiler;
  42. }
  43. public void resetCompiler(Compiler c) {
  44. this.compiler = c;
  45. currentlyWeaving = null;
  46. }
  47. public void setCurrentResult(CompilationResult result) {
  48. currentlyWeaving = result;
  49. }
  50. public boolean handleMessage(IMessage message) throws AbortException {
  51. if (!(message.isError() || message.isWarning()))
  52. return sink.handleMessage(message);
  53. // we only care about warnings and errors here...
  54. ISourceLocation sLoc = message.getSourceLocation();
  55. // See bug 62073. We should assert that the caller pass the correct primary source location.
  56. // But for AJ1.2 final we will simply do less processing of the locations if that is not the
  57. // case (By calling sink.handleMessage()) - this ensures we don't put out bogus source context info.
  58. if (sLoc instanceof EclipseSourceLocation) {
  59. EclipseSourceLocation esLoc = (EclipseSourceLocation) sLoc;
  60. if (currentlyWeaving != null && esLoc.getCompilationResult() != null) {
  61. if (!currentlyWeaving.equals(((EclipseSourceLocation) sLoc).getCompilationResult()))
  62. return sink.handleMessage(message);
  63. // throw new RuntimeException("Primary source location must match the file we are currently processing!");
  64. }
  65. }
  66. // bug 128618 - want to do a similar thing as in bug 62073 above, however
  67. // we're not an EclipseSourceLocation we're a SourceLocation.
  68. if (sLoc instanceof SourceLocation) {
  69. SourceLocation sl = (SourceLocation) sLoc;
  70. if (currentlyWeaving != null && sl.getSourceFile() != null) {
  71. if (!String.valueOf(currentlyWeaving.getFileName()).equals(sl.getSourceFile().getAbsolutePath())) {
  72. return sink.handleMessage(message);
  73. // throw new RuntimeException("Primary source location must match the file we are currently processing!");
  74. }
  75. }
  76. }
  77. CompilationResult problemSource = currentlyWeaving;
  78. if (problemSource == null) {
  79. // must be a problem found during completeTypeBindings phase of begin to compile
  80. if (sLoc instanceof EclipseSourceLocation) {
  81. problemSource = ((EclipseSourceLocation) sLoc).getCompilationResult();
  82. }
  83. if (problemSource == null) {
  84. // XXX this is ok for ajc, will have to do better for AJDT in time...
  85. return sink.handleMessage(message);
  86. }
  87. }
  88. int startPos = getStartPos(sLoc, problemSource);
  89. int endPos = getEndPos(sLoc, problemSource);
  90. int severity = message.isError() ? ProblemSeverities.Error : ProblemSeverities.Warning;
  91. char[] filename = problemSource.fileName;
  92. boolean usedBinarySourceFileName = false;
  93. if (problemSource.isFromBinarySource()) {
  94. if (sLoc != null) {
  95. filename = sLoc.getSourceFile().getPath().toCharArray();
  96. usedBinarySourceFileName = true;
  97. }
  98. }
  99. ReferenceContext referenceContext = findReferenceContextFor(problemSource);
  100. CategorizedProblem problem = compiler.problemReporter.createProblem(filename, IProblem.Unclassified, new String[0],
  101. new String[] { message.getMessage() }, severity, startPos, endPos, sLoc != null ? sLoc.getLine() : 0,
  102. sLoc != null ? sLoc.getColumn() : 0);
  103. IProblem[] seeAlso = buildSeeAlsoProblems(problem, message.getExtraSourceLocations(), problemSource,
  104. usedBinarySourceFileName);
  105. problem.setSeeAlsoProblems(seeAlso);
  106. StringBuilder details = new StringBuilder();
  107. // Stick more info in supplementary message info
  108. if (message.getDetails() != null) {
  109. details.append(message.getDetails());
  110. }
  111. // Remember if this message was due to a deow
  112. if (message.getDeclared()) {
  113. details.append("[deow=true]");
  114. }
  115. if (message instanceof LintMessage) {
  116. String lintMessageName = ((LintMessage) message).getLintKind();
  117. details.append("[Xlint:").append(lintMessageName).append("]");
  118. }
  119. if (details.length() != 0) {
  120. problem.setSupplementaryMessageInfo(details.toString());
  121. }
  122. compiler.problemReporter.record(problem, problemSource, referenceContext, message.isError());
  123. return true;
  124. }
  125. public boolean isIgnoring(Kind kind) {
  126. return sink.isIgnoring(kind);
  127. }
  128. /**
  129. * No-op
  130. *
  131. * @see org.aspectj.bridge.IMessageHandler#isIgnoring(org.aspectj.bridge.IMessage.Kind)
  132. * @param kind
  133. */
  134. public void dontIgnore(IMessage.Kind kind) {
  135. }
  136. /**
  137. * No-op
  138. *
  139. * @see org.aspectj.bridge.IMessageHandler#ignore(org.aspectj.bridge.IMessage.Kind)
  140. * @param kind
  141. */
  142. public void ignore(Kind kind) {
  143. }
  144. private int getStartPos(ISourceLocation sLoc, CompilationResult result) {
  145. int pos = 0;
  146. if (sLoc == null)
  147. return 0;
  148. int line = sLoc.getLine();
  149. if (sLoc instanceof EclipseSourceLocation) {
  150. pos = ((EclipseSourceLocation) sLoc).getStartPos();
  151. } else {
  152. if (line <= 1)
  153. return 0;
  154. if (result != null) {
  155. if ((result.lineSeparatorPositions != null) && (result.lineSeparatorPositions.length >= (line - 1))) {
  156. pos = result.lineSeparatorPositions[line - 2] + 1;
  157. }
  158. }
  159. }
  160. return pos;
  161. }
  162. private int getEndPos(ISourceLocation sLoc, CompilationResult result) {
  163. int pos = 0;
  164. if (sLoc == null)
  165. return 0;
  166. int line = sLoc.getLine();
  167. if (line <= 0)
  168. line = 1;
  169. if (sLoc instanceof EclipseSourceLocation) {
  170. pos = ((EclipseSourceLocation) sLoc).getEndPos();
  171. } else {
  172. if (result != null) {
  173. if ((result.lineSeparatorPositions != null) && (result.lineSeparatorPositions.length >= line)) {
  174. pos = result.lineSeparatorPositions[line - 1] - 1;
  175. }
  176. }
  177. }
  178. return pos;
  179. }
  180. private ReferenceContext findReferenceContextFor(CompilationResult result) {
  181. ReferenceContext context = null;
  182. if (compiler.unitsToProcess == null)
  183. return null;
  184. for (int i = 0; i < compiler.unitsToProcess.length; i++) {
  185. if ((compiler.unitsToProcess[i] != null) && (compiler.unitsToProcess[i].compilationResult == result)) {
  186. context = compiler.unitsToProcess[i];
  187. break;
  188. }
  189. }
  190. return context;
  191. }
  192. private IProblem[] buildSeeAlsoProblems(IProblem originalProblem, List sourceLocations, CompilationResult problemSource,
  193. boolean usedBinarySourceFileName) {
  194. List<IProblem> ret = new ArrayList<>();
  195. for (Object sourceLocation : sourceLocations) {
  196. ISourceLocation loc = (ISourceLocation) sourceLocation;
  197. if (loc != null) {
  198. DefaultProblem dp = new DefaultProblem(loc.getSourceFile().getPath().toCharArray(), "see also", 0, new String[]{},
  199. ProblemSeverities.Ignore, getStartPos(loc, null), getEndPos(loc, null), loc.getLine(), loc.getColumn());
  200. ret.add(dp);
  201. } else {
  202. System.err.println("About to abort due to null location, dumping state:");
  203. System.err.println("> Original Problem=" + problemSource.toString());
  204. throw new RuntimeException(
  205. "Internal Compiler Error: Unexpected null source location passed as 'see also' location.");
  206. }
  207. }
  208. if (usedBinarySourceFileName) {
  209. DefaultProblem dp = new DefaultProblem(problemSource.fileName, "see also", 0, new String[] {},
  210. ProblemSeverities.Ignore, 0, 0, 0, 0);
  211. ret.add(dp);
  212. }
  213. IProblem[] retValue = (IProblem[]) ret.toArray(new IProblem[] {});
  214. return retValue;
  215. }
  216. }