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.

AjProblemReporter.java 33KB

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