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

AjProblemReporter.java 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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.ajdt.internal.compiler.problem;
  13. import java.io.PrintWriter;
  14. import java.io.StringWriter;
  15. import java.lang.reflect.Modifier;
  16. import java.util.Collection;
  17. import java.util.HashSet;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import java.util.Set;
  21. import org.aspectj.ajdt.internal.compiler.ast.AspectDeclaration;
  22. import org.aspectj.ajdt.internal.compiler.ast.DeclareAnnotationDeclaration;
  23. import org.aspectj.ajdt.internal.compiler.ast.PointcutDeclaration;
  24. import org.aspectj.ajdt.internal.compiler.ast.Proceed;
  25. import org.aspectj.ajdt.internal.compiler.lookup.EclipseFactory;
  26. import org.aspectj.ajdt.internal.compiler.lookup.InterTypeMethodBinding;
  27. import org.aspectj.ajdt.internal.compiler.lookup.PrivilegedFieldBinding;
  28. import org.aspectj.bridge.context.CompilationAndWeavingContext;
  29. import org.aspectj.org.eclipse.jdt.core.compiler.CharOperation;
  30. import org.aspectj.org.eclipse.jdt.core.compiler.IProblem;
  31. import org.aspectj.org.eclipse.jdt.internal.compiler.CompilationResult;
  32. import org.aspectj.org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy;
  33. import org.aspectj.org.eclipse.jdt.internal.compiler.IProblemFactory;
  34. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.ASTNode;
  35. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
  36. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.Annotation;
  37. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.Argument;
  38. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall;
  39. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.Expression;
  40. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
  41. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
  42. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference;
  43. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
  44. import org.aspectj.org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
  45. import org.aspectj.org.eclipse.jdt.internal.compiler.impl.ReferenceContext;
  46. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ArrayBinding;
  47. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.IPrivilegedHandler;
  48. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
  49. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ParameterizedMethodBinding;
  50. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
  51. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding;
  52. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TagBits;
  53. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
  54. import org.aspectj.org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
  55. import org.aspectj.org.eclipse.jdt.internal.compiler.problem.ProblemSeverities;
  56. import org.aspectj.util.FuzzyBoolean;
  57. import org.aspectj.weaver.AjcMemberMaker;
  58. import org.aspectj.weaver.ConcreteTypeMunger;
  59. import org.aspectj.weaver.ReferenceType;
  60. import org.aspectj.weaver.ResolvedMember;
  61. import org.aspectj.weaver.ResolvedType;
  62. import org.aspectj.weaver.ResolvedTypeMunger;
  63. import org.aspectj.weaver.Shadow;
  64. import org.aspectj.weaver.UnresolvedType;
  65. import org.aspectj.weaver.patterns.DeclareAnnotation;
  66. import org.aspectj.weaver.patterns.DeclareParents;
  67. import org.aspectj.weaver.patterns.DeclareSoft;
  68. import org.aspectj.weaver.patterns.TypePattern;
  69. /**
  70. * Extends problem reporter to support compiler-side implementation of declare soft. Also overrides error reporting for the need to
  71. * implement abstract methods to account for inter-type declarations and pointcut declarations. This second job might be better done
  72. * directly in the SourceTypeBinding/ClassScope classes.
  73. *
  74. * @author Jim Hugunin
  75. */
  76. public class AjProblemReporter extends ProblemReporter {
  77. private static final boolean DUMP_STACK = false;
  78. public EclipseFactory factory;
  79. public AjProblemReporter(IErrorHandlingPolicy policy, CompilerOptions options, IProblemFactory problemFactory) {
  80. super(policy, options, problemFactory);
  81. }
  82. public void unhandledException(TypeBinding exceptionType, ASTNode location) {
  83. if (!factory.getWorld().getDeclareSoft().isEmpty()) {
  84. Shadow callSite = factory.makeShadow(location, referenceContext);
  85. Shadow enclosingExec = factory.makeShadow(referenceContext);
  86. // PR 72157 - calls to super / this within a constructor are not part of the cons join point.
  87. if ((callSite == null) && (enclosingExec.getKind() == Shadow.ConstructorExecution)
  88. && (location instanceof ExplicitConstructorCall)) {
  89. super.unhandledException(exceptionType, location);
  90. return;
  91. }
  92. // System.err.println("about to show error for unhandled exception: " + new String(exceptionType.sourceName()) +
  93. // " at " + location + " in " + referenceContext);
  94. for (DeclareSoft d: factory.getWorld().getDeclareSoft()) {
  95. // for (Iterator<DeclareSoft> i = factory.getWorld().getDeclareSoft().iterator(); i.hasNext();) {
  96. // DeclareSoft d = (DeclareSoft) i.next();
  97. // We need the exceptionType to match the type in the declare soft statement
  98. // This means it must either be the same type or a subtype
  99. ResolvedType throwException = factory.fromEclipse((ReferenceBinding) exceptionType);
  100. FuzzyBoolean isExceptionTypeOrSubtype = d.getException().matchesInstanceof(throwException);
  101. if (!isExceptionTypeOrSubtype.alwaysTrue())
  102. continue;
  103. if (callSite != null) {
  104. FuzzyBoolean match = d.getPointcut().match(callSite);
  105. if (match.alwaysTrue()) {
  106. // System.err.println("matched callSite: " + callSite + " with " + d);
  107. return;
  108. } else if (!match.alwaysFalse()) {
  109. // !!! need this check to happen much sooner
  110. // throw new RuntimeException("unimplemented, shouldn't have fuzzy match here");
  111. }
  112. }
  113. if (enclosingExec != null) {
  114. FuzzyBoolean match = d.getPointcut().match(enclosingExec);
  115. if (match.alwaysTrue()) {
  116. // System.err.println("matched enclosingExec: " + enclosingExec + " with " + d);
  117. return;
  118. } else if (!match.alwaysFalse()) {
  119. // !!! need this check to happen much sooner
  120. // throw new RuntimeException("unimplemented, shouldn't have fuzzy match here");
  121. }
  122. }
  123. }
  124. }
  125. // ??? is this always correct
  126. if (location instanceof Proceed) {
  127. return;
  128. }
  129. super.unhandledException(exceptionType, location);
  130. }
  131. public void unhandledExceptionFromAutoClose(TypeBinding exceptionType, ASTNode location) {
  132. if (!factory.getWorld().getDeclareSoft().isEmpty()) {
  133. Shadow callSite = factory.makeShadow(location, referenceContext);
  134. Shadow enclosingExec = factory.makeShadow(referenceContext);
  135. // PR 72157 - calls to super / this within a constructor are not part of the cons join point.
  136. if ((callSite == null) && (enclosingExec.getKind() == Shadow.ConstructorExecution)
  137. && (location instanceof ExplicitConstructorCall)) {
  138. super.unhandledException(exceptionType, location);
  139. return;
  140. }
  141. // System.err.println("about to show error for unhandled exception: " + new String(exceptionType.sourceName()) +
  142. // " at " + location + " in " + referenceContext);
  143. for (DeclareSoft d: factory.getWorld().getDeclareSoft()) {
  144. // for (Iterator<DeclareSoft> i = factory.getWorld().getDeclareSoft().iterator(); i.hasNext();) {
  145. // DeclareSoft d = (DeclareSoft) i.next();
  146. // We need the exceptionType to match the type in the declare soft statement
  147. // This means it must either be the same type or a subtype
  148. ResolvedType throwException = factory.fromEclipse((ReferenceBinding) exceptionType);
  149. FuzzyBoolean isExceptionTypeOrSubtype = d.getException().matchesInstanceof(throwException);
  150. if (!isExceptionTypeOrSubtype.alwaysTrue())
  151. continue;
  152. if (callSite != null) {
  153. FuzzyBoolean match = d.getPointcut().match(callSite);
  154. if (match.alwaysTrue()) {
  155. // System.err.println("matched callSite: " + callSite + " with " + d);
  156. return;
  157. } else if (!match.alwaysFalse()) {
  158. // !!! need this check to happen much sooner
  159. // throw new RuntimeException("unimplemented, shouldn't have fuzzy match here");
  160. }
  161. }
  162. if (enclosingExec != null) {
  163. FuzzyBoolean match = d.getPointcut().match(enclosingExec);
  164. if (match.alwaysTrue()) {
  165. // System.err.println("matched enclosingExec: " + enclosingExec + " with " + d);
  166. return;
  167. } else if (!match.alwaysFalse()) {
  168. // !!! need this check to happen much sooner
  169. // throw new RuntimeException("unimplemented, shouldn't have fuzzy match here");
  170. }
  171. }
  172. }
  173. }
  174. // ??? is this always correct
  175. if (location instanceof Proceed) {
  176. return;
  177. }
  178. super.unhandledExceptionFromAutoClose(exceptionType, location);
  179. }
  180. private boolean isPointcutDeclaration(MethodBinding binding) {
  181. return CharOperation.prefixEquals(PointcutDeclaration.mangledPrefix, binding.selector);
  182. }
  183. private boolean isIntertypeDeclaration(MethodBinding binding) {
  184. return (binding instanceof InterTypeMethodBinding);
  185. }
  186. public void abstractMethodCannotBeOverridden(SourceTypeBinding type, MethodBinding concreteMethod) {
  187. if (isPointcutDeclaration(concreteMethod)) {
  188. return;
  189. }
  190. super.abstractMethodCannotBeOverridden(type, concreteMethod);
  191. }
  192. public void inheritedMethodReducesVisibility(SourceTypeBinding type, MethodBinding concreteMethod,
  193. MethodBinding[] abstractMethods) {
  194. // if we implemented this method by a public inter-type declaration, then there is no error
  195. ResolvedType onTypeX = null;
  196. // If the type is anonymous, look at its supertype
  197. if (!type.isAnonymousType()) {
  198. onTypeX = factory.fromEclipse(type);
  199. } else {
  200. // Hmmm. If the ITD is on an interface that is being 'instantiated' using an anonymous type,
  201. // we sort it out elsewhere and don't come into this method -
  202. // so we don't have to worry about interfaces, just the superclass.
  203. onTypeX = factory.fromEclipse(type.superclass()); // abstractMethod.declaringClass);
  204. }
  205. for (Iterator i = onTypeX.getInterTypeMungersIncludingSupers().iterator(); i.hasNext();) {
  206. ConcreteTypeMunger m = (ConcreteTypeMunger) i.next();
  207. ResolvedMember sig = m.getSignature();
  208. if (!Modifier.isAbstract(sig.getModifiers())) {
  209. if (ResolvedType.matches(
  210. AjcMemberMaker.interMethod(sig, m.getAspectType(), sig.getDeclaringType().resolve(factory.getWorld())
  211. .isInterface()), factory.makeResolvedMember(concreteMethod))) {
  212. return;
  213. }
  214. }
  215. }
  216. super.inheritedMethodReducesVisibility(type, concreteMethod, abstractMethods);
  217. }
  218. // if either of the MethodBinding is an ITD, we have already reported it.
  219. public void staticAndInstanceConflict(MethodBinding currentMethod, MethodBinding inheritedMethod) {
  220. if (currentMethod instanceof InterTypeMethodBinding)
  221. return;
  222. if (inheritedMethod instanceof InterTypeMethodBinding)
  223. return;
  224. super.staticAndInstanceConflict(currentMethod, inheritedMethod);
  225. }
  226. public void abstractMethodMustBeImplemented(SourceTypeBinding type, MethodBinding abstractMethod) {
  227. // if this is a PointcutDeclaration then there is no error
  228. if (isPointcutDeclaration(abstractMethod))
  229. return;
  230. if (isIntertypeDeclaration(abstractMethod))
  231. return; // when there is a problem with an ITD not being implemented, it will be reported elsewhere
  232. if (CharOperation.prefixEquals("ajc$interField".toCharArray(), abstractMethod.selector)) {
  233. // ??? think through how this could go wrong
  234. return;
  235. }
  236. // if we implemented this method by an inter-type declaration, then there is no error
  237. // ??? be sure this is always right
  238. ResolvedType onTypeX = null;
  239. // If the type is anonymous, look at its supertype
  240. if (!type.isAnonymousType()) {
  241. onTypeX = factory.fromEclipse(type);
  242. } else {
  243. // Hmmm. If the ITD is on an interface that is being 'instantiated' using an anonymous type,
  244. // we sort it out elsewhere and don't come into this method -
  245. // so we don't have to worry about interfaces, just the superclass.
  246. onTypeX = factory.fromEclipse(type.superclass()); // abstractMethod.declaringClass);
  247. }
  248. if (onTypeX.isRawType())
  249. onTypeX = onTypeX.getGenericType();
  250. List<ConcreteTypeMunger> mungers = onTypeX.getInterTypeMungersIncludingSupers();
  251. for (ConcreteTypeMunger m : mungers) {
  252. ResolvedMember sig = m.getSignature();
  253. if (sig != null && !Modifier.isAbstract(sig.getModifiers())) {
  254. ResolvedMember abstractMember = factory.makeResolvedMember(abstractMethod);
  255. if (abstractMember.getName().startsWith("ajc$interMethodDispatch")) {
  256. ResolvedType dType = factory.getWorld().resolve(sig.getDeclaringType(), false);
  257. if (ResolvedType.matches(AjcMemberMaker.interMethod(sig, m.getAspectType(), dType.isInterface()),
  258. abstractMember)) {
  259. return;
  260. }
  261. } else {
  262. // In this case we have something like:
  263. // interface I {}
  264. // abstract class C implements I { abstract void foo();}
  265. // class D extends C {}
  266. // ITD: public void I.foo() {...}
  267. // The ITD is providing the implementation of foo in the class D but when checking for whether the abstract
  268. // method is overridden, we won't be looking at whether the ITD overrides ajc$interMethodDispath$...foo but
  269. // whether it overrides the foo method from class C
  270. if (ResolvedType.matches(sig, factory.makeResolvedMember(abstractMethod)))
  271. return;
  272. }
  273. }
  274. }
  275. super.abstractMethodMustBeImplemented(type, abstractMethod);
  276. }
  277. /*
  278. * (non-Javadoc)
  279. *
  280. * @see
  281. * org.aspectj.org.eclipse.jdt.internal.compiler.problem.ProblemReporter#disallowedTargetForAnnotation(org.aspectj.org.eclipse
  282. * .jdt.internal.compiler.ast.Annotation)
  283. */
  284. public void disallowedTargetForAnnotation(Annotation annotation) {
  285. // if the annotation's recipient is an ITD, it might be allowed after all...
  286. if (annotation.recipient instanceof MethodBinding) {
  287. MethodBinding binding = (MethodBinding) annotation.recipient;
  288. String name = new String(binding.selector);
  289. if (name.startsWith("ajc$")) {
  290. long metaTagBits = annotation.resolvedType.getAnnotationTagBits(); // could be forward reference
  291. if (name.indexOf("interField") != -1) {
  292. if ((metaTagBits & TagBits.AnnotationForField) != 0)
  293. return;
  294. } else if (name.indexOf("interConstructor") != -1) {
  295. if ((metaTagBits & TagBits.AnnotationForConstructor) != 0)
  296. return;
  297. } else if (name.indexOf("interMethod") != -1) {
  298. if ((metaTagBits & TagBits.AnnotationForMethod) != 0)
  299. return;
  300. } else if (name.indexOf("declare_" + DeclareAnnotation.AT_TYPE + "_") != -1) {
  301. if ((metaTagBits & TagBits.AnnotationForAnnotationType) != 0 || (metaTagBits & TagBits.AnnotationForType) != 0)
  302. return;
  303. } else if (name.indexOf("declare_" + DeclareAnnotation.AT_FIELD + "_") != -1) {
  304. if ((metaTagBits & TagBits.AnnotationForField) != 0)
  305. return;
  306. } else if (name.indexOf("declare_" + DeclareAnnotation.AT_CONSTRUCTOR + "_") != -1) {
  307. if ((metaTagBits & TagBits.AnnotationForConstructor) != 0)
  308. return;
  309. } else if (name.indexOf("declare_eow") != -1) {
  310. if ((metaTagBits & TagBits.AnnotationForField) != 0)
  311. return;
  312. }
  313. }
  314. }
  315. // not our special case, report the problem...
  316. super.disallowedTargetForAnnotation(annotation);
  317. }
  318. public void overridesPackageDefaultMethod(MethodBinding localMethod, MethodBinding inheritedMethod) {
  319. if (new String(localMethod.selector).startsWith("ajc$"))
  320. return;
  321. super.overridesPackageDefaultMethod(localMethod, inheritedMethod);
  322. }
  323. public void handle(int problemId, String[] problemArguments, String[] messageArguments, int severity, int problemStartPosition,
  324. int problemEndPosition, ReferenceContext referenceContext, CompilationResult unitResult) {
  325. if (severity != ProblemSeverities.Ignore && DUMP_STACK) {
  326. Thread.dumpStack();
  327. }
  328. super.handle(problemId, problemArguments,
  329. 0, // no message elaboration
  330. messageArguments, severity, problemStartPosition, problemEndPosition,
  331. referenceContext, unitResult);
  332. }
  333. // PR71076
  334. public void javadocMissingParamTag(char[] name, int sourceStart, int sourceEnd, int modifiers) {
  335. boolean reportIt = true;
  336. String sName = new String(name);
  337. if (sName.startsWith("ajc$"))
  338. reportIt = false;
  339. if (sName.equals("thisJoinPoint"))
  340. reportIt = false;
  341. if (sName.equals("thisJoinPointStaticPart"))
  342. reportIt = false;
  343. if (sName.equals("thisEnclosingJoinPointStaticPart"))
  344. reportIt = false;
  345. if (sName.equals("ajc_aroundClosure"))
  346. reportIt = false;
  347. if (reportIt)
  348. super.javadocMissingParamTag(name, sourceStart, sourceEnd, modifiers);
  349. }
  350. public void abstractMethodInAbstractClass(SourceTypeBinding type, AbstractMethodDeclaration methodDecl) {
  351. String abstractMethodName = new String(methodDecl.selector);
  352. if (abstractMethodName.startsWith("ajc$pointcut")) {
  353. // This will already have been reported, see: PointcutDeclaration.postParse()
  354. return;
  355. }
  356. String[] arguments = new String[] { new String(type.sourceName()), abstractMethodName };
  357. super.handle(IProblem.AbstractMethodInAbstractClass, arguments, arguments, methodDecl.sourceStart, methodDecl.sourceEnd,
  358. this.referenceContext, this.referenceContext == null ? null : this.referenceContext.compilationResult());
  359. }
  360. /**
  361. * Called when there is an ITD marked @override that doesn't override a supertypes method. The method and the binding are passed
  362. * - some information is useful from each. The 'method' knows about source offsets for the message, the 'binding' has the
  363. * signature of what the ITD is trying to be in the target class.
  364. */
  365. public void itdMethodMustOverride(AbstractMethodDeclaration method, MethodBinding binding) {
  366. this.handle(IProblem.MethodMustOverride,
  367. new String[] { new String(binding.selector), typesAsString(binding.isVarargs(), binding.parameters, false),
  368. new String(binding.declaringClass.readableName()), },
  369. new String[] { new String(binding.selector), typesAsString(binding.isVarargs(), binding.parameters, true),
  370. new String(binding.declaringClass.shortReadableName()), }, method.sourceStart, method.sourceEnd,
  371. this.referenceContext, this.referenceContext == null ? null : this.referenceContext.compilationResult());
  372. }
  373. /**
  374. * Overrides the implementation in ProblemReporter and is ITD aware. To report a *real* problem with an ITD marked @override,
  375. * the other methodMustOverride() method is used.
  376. */
  377. public void methodMustOverride(AbstractMethodDeclaration method, long complianceLevel) {
  378. // ignore ajc$ methods
  379. if (new String(method.selector).startsWith("ajc$"))
  380. return;
  381. ResolvedMember possiblyErroneousRm = factory.makeResolvedMember(method.binding);
  382. ResolvedType onTypeX = factory.fromEclipse(method.binding.declaringClass);
  383. // Can't use 'getInterTypeMungersIncludingSupers()' since that will exclude abstract ITDs
  384. // on any super classes - so we have to trawl up ourselves.. I wonder if this problem
  385. // affects other code in the problem reporter that looks through ITDs...
  386. ResolvedType supertypeToLookAt = onTypeX.getSuperclass();
  387. while (supertypeToLookAt != null) {
  388. List<ConcreteTypeMunger> itMungers = supertypeToLookAt.getInterTypeMungers();
  389. for (Iterator<ConcreteTypeMunger> i = itMungers.iterator(); i.hasNext();) {
  390. ConcreteTypeMunger m = (ConcreteTypeMunger) i.next();
  391. if (m.getMunger()!=null && m.getMunger().getKind()== ResolvedTypeMunger.PrivilegedAccess) {
  392. continue;
  393. }
  394. ResolvedMember sig = m.getSignature();
  395. if (sig == null)
  396. continue; // we aren't interested in other kinds of munger
  397. UnresolvedType dType = sig.getDeclaringType();
  398. if (dType == null)
  399. continue;
  400. ResolvedType resolvedDeclaringType = dType.resolve(factory.getWorld());
  401. ResolvedMember rm = AjcMemberMaker.interMethod(sig, m.getAspectType(), resolvedDeclaringType.isInterface());
  402. if (ResolvedType.matches(rm, possiblyErroneousRm)) {
  403. // match, so dont need to report a problem!
  404. return;
  405. }
  406. }
  407. supertypeToLookAt = supertypeToLookAt.getSuperclass();
  408. }
  409. // report the error...
  410. super.methodMustOverride(method,complianceLevel);
  411. }
  412. private String typesAsString(boolean isVarargs, TypeBinding[] types, boolean makeShort) {
  413. StringBuffer buffer = new StringBuffer(10);
  414. for (int i = 0, length = types.length; i < length; i++) {
  415. if (i != 0)
  416. buffer.append(", "); //$NON-NLS-1$
  417. TypeBinding type = types[i];
  418. boolean isVarargType = isVarargs && i == length - 1;
  419. if (isVarargType)
  420. type = ((ArrayBinding) type).elementsType();
  421. buffer.append(new String(makeShort ? type.shortReadableName() : type.readableName()));
  422. if (isVarargType)
  423. buffer.append("..."); //$NON-NLS-1$
  424. }
  425. return buffer.toString();
  426. }
  427. public void visibilityConflict(MethodBinding currentMethod, MethodBinding inheritedMethod) {
  428. // Not quite sure if the conditions on this test are right - basically I'm saying
  429. // DONT WORRY if its ITDs since the error will be reported another way...
  430. if (isIntertypeDeclaration(currentMethod) && isIntertypeDeclaration(inheritedMethod)
  431. && Modifier.isPrivate(currentMethod.modifiers) && Modifier.isPrivate(inheritedMethod.modifiers)) {
  432. return;
  433. }
  434. super.visibilityConflict(currentMethod, inheritedMethod);
  435. }
  436. public void unusedPrivateType(TypeDeclaration typeDecl) {
  437. // don't output unused type warnings for aspects!
  438. if (typeDecl instanceof AspectDeclaration)
  439. return;
  440. if (typeDecl.enclosingType != null && (typeDecl.enclosingType instanceof AspectDeclaration)) {
  441. AspectDeclaration ad = (AspectDeclaration) typeDecl.enclosingType;
  442. if (ad.concreteName != null) {
  443. List declares = ad.concreteName.declares;
  444. for (Iterator iter = declares.iterator(); iter.hasNext();) {
  445. Object dec = iter.next();
  446. if (dec instanceof DeclareParents) {
  447. DeclareParents decp = (DeclareParents) dec;
  448. TypePattern[] newparents = decp.getParents().getTypePatterns();
  449. for (int i = 0; i < newparents.length; i++) {
  450. TypePattern pattern = newparents[i];
  451. UnresolvedType ut = pattern.getExactType();
  452. if (ut == null)
  453. continue;
  454. if (CharOperation.compareWith(typeDecl.binding.signature(), ut.getSignature().toCharArray()) == 0)
  455. return;
  456. }
  457. }
  458. }
  459. }
  460. }
  461. super.unusedPrivateType(typeDecl);
  462. }
  463. public void abstractMethodInConcreteClass(SourceTypeBinding type) {
  464. if (type.scope!=null && type.scope.referenceContext instanceof AspectDeclaration) {
  465. // TODO could put out an Aspect specific message here
  466. return;
  467. }
  468. super.abstractMethodInConcreteClass(type);
  469. }
  470. // Don't warn if there is an ITD method/ctor from a privileged aspect
  471. public void unusedPrivateField(FieldDeclaration fieldDecl) {
  472. if (fieldDecl!=null && fieldDecl.binding != null && fieldDecl.binding.declaringClass != null) {
  473. ReferenceBinding type = fieldDecl.binding.declaringClass;
  474. ResolvedType weaverType = null;
  475. if (!type.isAnonymousType()) {
  476. weaverType = factory.fromEclipse(type);
  477. } else {
  478. weaverType = factory.fromEclipse(type.superclass());
  479. }
  480. Set checked = new HashSet();
  481. for (Iterator i = weaverType.getInterTypeMungersIncludingSupers().iterator(); i.hasNext();) {
  482. ConcreteTypeMunger m = (ConcreteTypeMunger) i.next();
  483. ResolvedType theAspect = m.getAspectType();
  484. if (!checked.contains(theAspect)) {
  485. TypeBinding tb = factory.makeTypeBinding(m.getAspectType());
  486. // Let's check the privilegedHandler from that aspect
  487. if (tb instanceof SourceTypeBinding) { // BinaryTypeBinding is also a SourceTypeBinding ;)
  488. IPrivilegedHandler privilegedHandler = ((SourceTypeBinding) tb).privilegedHandler;
  489. if (privilegedHandler != null) {
  490. if (privilegedHandler.definesPrivilegedAccessToField(fieldDecl.binding)) {
  491. return;
  492. }
  493. } else if (theAspect instanceof ReferenceType) {
  494. // ResolvedMember rm = factory.makeResolvedMember(fieldDecl.binding);
  495. String fname = new String(fieldDecl.name);
  496. Collection/* ResolvedMember */privvies = ((ReferenceType) theAspect).getPrivilegedAccesses();
  497. // On an incremental compile the information is in the bcel delegate
  498. if (privvies != null) {
  499. for (Iterator iterator = privvies.iterator(); iterator.hasNext();) {
  500. ResolvedMember priv = (ResolvedMember) iterator.next();
  501. if (priv.getName().equals(fname)) {
  502. return;
  503. }
  504. }
  505. }
  506. }
  507. }
  508. checked.add(theAspect);
  509. }
  510. }
  511. }
  512. super.unusedPrivateField(fieldDecl);
  513. }
  514. public void unusedPrivateMethod(AbstractMethodDeclaration methodDecl) {
  515. // don't output unused warnings for pointcuts...
  516. if (!(methodDecl instanceof PointcutDeclaration))
  517. super.unusedPrivateMethod(methodDecl);
  518. }
  519. public void caseExpressionMustBeConstant(Expression expression) {
  520. if (expression instanceof QualifiedNameReference) {
  521. QualifiedNameReference qnr = (QualifiedNameReference) expression;
  522. if (qnr.otherBindings != null && qnr.otherBindings.length > 0 && qnr.otherBindings[0] instanceof PrivilegedFieldBinding) {
  523. super.signalError(expression.sourceStart, expression.sourceEnd,
  524. "Fields accessible due to an aspect being privileged can not be used in switch statements");
  525. referenceContext.tagAsHavingErrors();
  526. return;
  527. }
  528. }
  529. super.caseExpressionMustBeConstant(expression);
  530. }
  531. public void unusedArgument(LocalDeclaration localDecl) {
  532. // don't warn if this is an aj synthetic arg
  533. String argType = new String(localDecl.type.resolvedType.signature());
  534. if (argType.startsWith("Lorg/aspectj/runtime/internal"))
  535. return;
  536. // If the unused argument is in a pointcut, don't report the problem (for now... pr148219)
  537. if (localDecl instanceof Argument) {
  538. Argument arg = (Argument) localDecl;
  539. if (arg.binding != null && arg.binding.declaringScope != null) {
  540. ReferenceContext context = arg.binding.declaringScope.referenceContext();
  541. if (context != null && context instanceof PointcutDeclaration)
  542. return;
  543. }
  544. }
  545. if (new String(localDecl.name).startsWith("ajc$")) {
  546. // Do not report problems for infrastructure variables beyond the users control - pr195090
  547. return;
  548. }
  549. super.unusedArgument(localDecl);
  550. }
  551. /**
  552. * A side-effect of the way that we handle itds on default methods on top-most implementors of interfaces is that a class
  553. * acquiring a final default ITD will erroneously report that it can't override its own member. This method detects that
  554. * situation.
  555. */
  556. public void finalMethodCannotBeOverridden(MethodBinding currentMethod, MethodBinding inheritedMethod) {
  557. if (currentMethod == inheritedMethod)
  558. return;
  559. super.finalMethodCannotBeOverridden(currentMethod, inheritedMethod);
  560. }
  561. /**
  562. * The method verifier is a bit 'keen' and doesn't cope well with ITDMs which are of course to be considered a 'default'
  563. * implementation if the target type doesn't supply one. This test may not be complete - it is possible that it should read if
  564. * *either* is an ITD...but I dont have a testcase that shows that is required. yet. (pr115788)
  565. */
  566. public void duplicateInheritedMethods(SourceTypeBinding type, MethodBinding inheritedMethod1, MethodBinding inheritedMethod2) {
  567. if (inheritedMethod1 instanceof InterTypeMethodBinding || inheritedMethod2 instanceof InterTypeMethodBinding)
  568. return;
  569. if ((inheritedMethod1 instanceof ParameterizedMethodBinding)
  570. && ((ParameterizedMethodBinding) inheritedMethod1).original() instanceof InterTypeMethodBinding)
  571. return;
  572. if ((inheritedMethod2 instanceof ParameterizedMethodBinding)
  573. && ((ParameterizedMethodBinding) inheritedMethod2).original() instanceof InterTypeMethodBinding)
  574. return;
  575. super.duplicateInheritedMethods(type, inheritedMethod1, inheritedMethod2);
  576. }
  577. /**
  578. * All problems end up routed through here at some point...
  579. */
  580. public IProblem createProblem(char[] fileName, int problemId, String[] problemArguments, String[] messageArguments,
  581. int severity, int problemStartPosition, int problemEndPosition, int lineNumber) {
  582. IProblem problem = super.createProblem(fileName, problemId, problemArguments, messageArguments, severity,
  583. problemStartPosition, problemEndPosition, lineNumber, 0);
  584. if (factory.getWorld().isInPinpointMode()) {
  585. MessageIssued ex = new MessageIssued();
  586. ex.fillInStackTrace();
  587. StringWriter sw = new StringWriter();
  588. ex.printStackTrace(new PrintWriter(sw));
  589. StringBuffer sb = new StringBuffer();
  590. sb.append(CompilationAndWeavingContext.getCurrentContext());
  591. sb.append(sw.toString());
  592. problem = new PinpointedProblem(problem, sb.toString());
  593. }
  594. return problem;
  595. }
  596. private static class MessageIssued extends RuntimeException {
  597. public String getMessage() {
  598. return "message issued...";
  599. }
  600. }
  601. private static class PinpointedProblem implements IProblem {
  602. private IProblem delegate;
  603. private String message;
  604. public PinpointedProblem(IProblem aProblem, String pinpoint) {
  605. this.delegate = aProblem;
  606. // if this was a problem that came via the weaver, it will already have
  607. // pinpoint info, don't do it twice...
  608. if (delegate.getMessage().indexOf("message issued...") == -1) {
  609. this.message = delegate.getMessage() + "\n" + pinpoint;
  610. } else {
  611. this.message = delegate.getMessage();
  612. }
  613. }
  614. public String[] getArguments() {
  615. return delegate.getArguments();
  616. }
  617. public int getID() {
  618. return delegate.getID();
  619. }
  620. public String getMessage() {
  621. return message;
  622. }
  623. public char[] getOriginatingFileName() {
  624. return delegate.getOriginatingFileName();
  625. }
  626. public int getSourceEnd() {
  627. return delegate.getSourceEnd();
  628. }
  629. public int getSourceLineNumber() {
  630. return delegate.getSourceLineNumber();
  631. }
  632. public int getSourceStart() {
  633. return delegate.getSourceStart();
  634. }
  635. public boolean isError() {
  636. return delegate.isError();
  637. }
  638. public boolean isWarning() {
  639. return delegate.isWarning();
  640. }
  641. public void setSourceEnd(int sourceEnd) {
  642. delegate.setSourceEnd(sourceEnd);
  643. }
  644. public void setSourceLineNumber(int lineNumber) {
  645. delegate.setSourceLineNumber(lineNumber);
  646. }
  647. public void setSourceStart(int sourceStart) {
  648. delegate.setSourceStart(sourceStart);
  649. }
  650. public void setSeeAlsoProblems(IProblem[] problems) {
  651. delegate.setSeeAlsoProblems(problems);
  652. }
  653. public IProblem[] seeAlso() {
  654. return delegate.seeAlso();
  655. }
  656. public void setSupplementaryMessageInfo(String msg) {
  657. delegate.setSupplementaryMessageInfo(msg);
  658. }
  659. public String getSupplementaryMessageInfo() {
  660. return delegate.getSupplementaryMessageInfo();
  661. }
  662. }
  663. public void duplicateMethodInType(SourceTypeBinding type, AbstractMethodDeclaration methodDecl, boolean equalParameters, int severity) {
  664. if (new String(methodDecl.selector).startsWith("ajc$interMethod")) {
  665. // this is an ITD clash and will be reported in another way by AspectJ (173602)
  666. return;
  667. }
  668. super.duplicateMethodInType(type, methodDecl, equalParameters, severity);
  669. }
  670. // pr246393 - if we are going to complain about privileged, we clearly don't know what is going on, so don't
  671. // confuse the user
  672. public void parseErrorInsertAfterToken(int start, int end, int currentKind, char[] errorTokenSource, String errorTokenName,
  673. String expectedToken) {
  674. if (expectedToken.equals("privileged") || expectedToken.equals("around")) {
  675. super.parseErrorNoSuggestion(start, end, currentKind, errorTokenSource, errorTokenName);
  676. } else {
  677. super.parseErrorInsertAfterToken(start, end, currentKind, errorTokenSource, errorTokenName, expectedToken);
  678. }
  679. }
  680. public void missingValueForAnnotationMember(Annotation annotation, char[] memberName) {
  681. if (referenceContext instanceof DeclareAnnotationDeclaration) {
  682. // If a remover then the values are not necessary
  683. if (((DeclareAnnotationDeclaration) referenceContext).isRemover()) {
  684. return;
  685. }
  686. }
  687. super.missingValueForAnnotationMember(annotation, memberName);
  688. }
  689. }