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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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 (ConcreteTypeMunger m : onTypeX.getInterTypeMungersIncludingSupers()) {
  211. ResolvedMember sig = m.getSignature();
  212. if (!Modifier.isAbstract(sig.getModifiers())) {
  213. if (ResolvedType.matches(
  214. AjcMemberMaker.interMethod(sig, m.getAspectType(), sig.getDeclaringType().resolve(factory.getWorld())
  215. .isInterface()), factory.makeResolvedMember(concreteMethod))) {
  216. return;
  217. }
  218. }
  219. }
  220. super.inheritedMethodReducesVisibility(type, concreteMethod, abstractMethods);
  221. }
  222. // if either of the MethodBinding is an ITD, we have already reported it.
  223. public void staticAndInstanceConflict(MethodBinding currentMethod, MethodBinding inheritedMethod) {
  224. if (currentMethod instanceof InterTypeMethodBinding)
  225. return;
  226. if (inheritedMethod instanceof InterTypeMethodBinding)
  227. return;
  228. super.staticAndInstanceConflict(currentMethod, inheritedMethod);
  229. }
  230. public void abstractMethodMustBeImplemented(SourceTypeBinding type, MethodBinding abstractMethod) {
  231. // if this is a PointcutDeclaration then there is no error
  232. if (isPointcutDeclaration(abstractMethod))
  233. return;
  234. if (isIntertypeDeclaration(abstractMethod))
  235. return; // when there is a problem with an ITD not being implemented, it will be reported elsewhere
  236. if (CharOperation.prefixEquals("ajc$interField".toCharArray(), abstractMethod.selector)) {
  237. // ??? think through how this could go wrong
  238. return;
  239. }
  240. // if we implemented this method by an inter-type declaration, then there is no error
  241. // ??? be sure this is always right
  242. ResolvedType onTypeX = null;
  243. // If the type is anonymous, look at its supertype
  244. if (!type.isAnonymousType()) {
  245. onTypeX = factory.fromEclipse(type);
  246. } else {
  247. // Hmmm. If the ITD is on an interface that is being 'instantiated' using an anonymous type,
  248. // we sort it out elsewhere and don't come into this method -
  249. // so we don't have to worry about interfaces, just the superclass.
  250. onTypeX = factory.fromEclipse(type.superclass()); // abstractMethod.declaringClass);
  251. }
  252. if (onTypeX.isRawType())
  253. onTypeX = onTypeX.getGenericType();
  254. List<ConcreteTypeMunger> mungers = onTypeX.getInterTypeMungersIncludingSupers();
  255. for (ConcreteTypeMunger m : mungers) {
  256. ResolvedMember sig = m.getSignature();
  257. if (sig != null && !Modifier.isAbstract(sig.getModifiers())) {
  258. ResolvedMember abstractMember = factory.makeResolvedMember(abstractMethod);
  259. if (abstractMember.getName().startsWith("ajc$interMethodDispatch")) {
  260. ResolvedType dType = factory.getWorld().resolve(sig.getDeclaringType(), false);
  261. if (ResolvedType.matches(AjcMemberMaker.interMethod(sig, m.getAspectType(), dType.isInterface()),
  262. abstractMember)) {
  263. return;
  264. }
  265. } else {
  266. // In this case we have something like:
  267. // interface I {}
  268. // abstract class C implements I { abstract void foo();}
  269. // class D extends C {}
  270. // ITD: public void I.foo() {...}
  271. // The ITD is providing the implementation of foo in the class D but when checking for whether the abstract
  272. // method is overridden, we won't be looking at whether the ITD overrides ajc$interMethodDispath$...foo but
  273. // whether it overrides the foo method from class C
  274. if (ResolvedType.matches(sig, factory.makeResolvedMember(abstractMethod)))
  275. return;
  276. }
  277. }
  278. }
  279. super.abstractMethodMustBeImplemented(type, abstractMethod);
  280. }
  281. /*
  282. * (non-Javadoc)
  283. *
  284. * @see
  285. * org.aspectj.org.eclipse.jdt.internal.compiler.problem.ProblemReporter#disallowedTargetForAnnotation(org.aspectj.org.eclipse
  286. * .jdt.internal.compiler.ast.Annotation)
  287. */
  288. public void disallowedTargetForAnnotation(Annotation annotation) {
  289. // if the annotation's recipient is an ITD, it might be allowed after all...
  290. if (annotation.recipient instanceof MethodBinding) {
  291. MethodBinding binding = (MethodBinding) annotation.recipient;
  292. String name = new String(binding.selector);
  293. if (name.startsWith("ajc$")) {
  294. long metaTagBits = annotation.resolvedType.getAnnotationTagBits(); // could be forward reference
  295. if (name.contains("interField")) {
  296. if ((metaTagBits & TagBits.AnnotationForField) != 0)
  297. return;
  298. } else if (name.contains("interConstructor")) {
  299. if ((metaTagBits & TagBits.AnnotationForConstructor) != 0)
  300. return;
  301. } else if (name.contains("interMethod")) {
  302. if ((metaTagBits & TagBits.AnnotationForMethod) != 0)
  303. return;
  304. } else if (name.contains("declare_" + DeclareAnnotation.AT_TYPE + "_")) {
  305. if ((metaTagBits & TagBits.AnnotationForAnnotationType) != 0 || (metaTagBits & TagBits.AnnotationForType) != 0)
  306. return;
  307. } else if (name.contains("declare_" + DeclareAnnotation.AT_FIELD + "_")) {
  308. if ((metaTagBits & TagBits.AnnotationForField) != 0)
  309. return;
  310. } else if (name.contains("declare_" + DeclareAnnotation.AT_CONSTRUCTOR + "_")) {
  311. if ((metaTagBits & TagBits.AnnotationForConstructor) != 0)
  312. return;
  313. } else if (name.contains("declare_eow")) {
  314. if ((metaTagBits & TagBits.AnnotationForField) != 0)
  315. return;
  316. }
  317. }
  318. }
  319. // not our special case, report the problem...
  320. super.disallowedTargetForAnnotation(annotation);
  321. }
  322. public void overridesPackageDefaultMethod(MethodBinding localMethod, MethodBinding inheritedMethod) {
  323. if (new String(localMethod.selector).startsWith("ajc$"))
  324. return;
  325. super.overridesPackageDefaultMethod(localMethod, inheritedMethod);
  326. }
  327. public void handle(int problemId, String[] problemArguments, String[] messageArguments, int severity, int problemStartPosition,
  328. int problemEndPosition, ReferenceContext referenceContext, CompilationResult unitResult) {
  329. if (severity != ProblemSeverities.Ignore && DUMP_STACK) {
  330. Thread.dumpStack();
  331. }
  332. super.handle(problemId, problemArguments,
  333. 0, // no message elaboration
  334. messageArguments, severity, problemStartPosition, problemEndPosition,
  335. referenceContext, unitResult);
  336. }
  337. // PR71076
  338. public void javadocMissingParamTag(char[] name, int sourceStart, int sourceEnd, int modifiers) {
  339. boolean reportIt = true;
  340. String sName = new String(name);
  341. if (sName.startsWith("ajc$"))
  342. reportIt = false;
  343. if (sName.equals("thisJoinPoint"))
  344. reportIt = false;
  345. if (sName.equals("thisJoinPointStaticPart"))
  346. reportIt = false;
  347. if (sName.equals("thisEnclosingJoinPointStaticPart"))
  348. reportIt = false;
  349. if (sName.equals("ajc_aroundClosure"))
  350. reportIt = false;
  351. if (reportIt)
  352. super.javadocMissingParamTag(name, sourceStart, sourceEnd, modifiers);
  353. }
  354. public void abstractMethodInAbstractClass(SourceTypeBinding type, AbstractMethodDeclaration methodDecl) {
  355. String abstractMethodName = new String(methodDecl.selector);
  356. if (abstractMethodName.startsWith("ajc$pointcut")) {
  357. // This will already have been reported, see: PointcutDeclaration.postParse()
  358. return;
  359. }
  360. String[] arguments = new String[] { new String(type.sourceName()), abstractMethodName };
  361. super.handle(IProblem.AbstractMethodInAbstractClass, arguments, arguments, methodDecl.sourceStart, methodDecl.sourceEnd,
  362. this.referenceContext, this.referenceContext == null ? null : this.referenceContext.compilationResult());
  363. }
  364. /**
  365. * Called when there is an ITD marked @override that doesn't override a supertypes method. The method and the binding are passed
  366. * - some information is useful from each. The 'method' knows about source offsets for the message, the 'binding' has the
  367. * signature of what the ITD is trying to be in the target class.
  368. */
  369. public void itdMethodMustOverride(AbstractMethodDeclaration method, MethodBinding binding) {
  370. this.handle(IProblem.MethodMustOverride,
  371. new String[] { new String(binding.selector), typesAsString(binding.isVarargs(), binding.parameters, false),
  372. new String(binding.declaringClass.readableName()), },
  373. new String[] { new String(binding.selector), typesAsString(binding.isVarargs(), binding.parameters, true),
  374. new String(binding.declaringClass.shortReadableName()), }, method.sourceStart, method.sourceEnd,
  375. this.referenceContext, this.referenceContext == null ? null : this.referenceContext.compilationResult());
  376. }
  377. /**
  378. * Overrides the implementation in ProblemReporter and is ITD aware. To report a *real* problem with an ITD marked @override,
  379. * the other methodMustOverride() method is used.
  380. */
  381. public void methodMustOverride(AbstractMethodDeclaration method, long complianceLevel) {
  382. // ignore ajc$ methods
  383. if (new String(method.selector).startsWith("ajc$"))
  384. return;
  385. ResolvedMember possiblyErroneousRm = factory.makeResolvedMember(method.binding);
  386. ResolvedType onTypeX = factory.fromEclipse(method.binding.declaringClass);
  387. // Can't use 'getInterTypeMungersIncludingSupers()' since that will exclude abstract ITDs
  388. // on any super classes - so we have to trawl up ourselves.. I wonder if this problem
  389. // affects other code in the problem reporter that looks through ITDs...
  390. ResolvedType supertypeToLookAt = onTypeX.getSuperclass();
  391. while (supertypeToLookAt != null) {
  392. List<ConcreteTypeMunger> itMungers = supertypeToLookAt.getInterTypeMungers();
  393. for (ConcreteTypeMunger m : itMungers) {
  394. if (m.getMunger() != null && m.getMunger().getKind() == ResolvedTypeMunger.PrivilegedAccess) {
  395. continue;
  396. }
  397. ResolvedMember sig = m.getSignature();
  398. if (sig == null)
  399. continue; // we aren't interested in other kinds of munger
  400. UnresolvedType dType = sig.getDeclaringType();
  401. if (dType == null)
  402. continue;
  403. ResolvedType resolvedDeclaringType = dType.resolve(factory.getWorld());
  404. ResolvedMember rm = AjcMemberMaker.interMethod(sig, m.getAspectType(), resolvedDeclaringType.isInterface());
  405. if (ResolvedType.matches(rm, possiblyErroneousRm)) {
  406. // match, so dont need to report a problem!
  407. return;
  408. }
  409. }
  410. supertypeToLookAt = supertypeToLookAt.getSuperclass();
  411. }
  412. // report the error...
  413. super.methodMustOverride(method,complianceLevel);
  414. }
  415. private String typesAsString(boolean isVarargs, TypeBinding[] types, boolean makeShort) {
  416. StringBuffer buffer = new StringBuffer(10);
  417. for (int i = 0, length = types.length; i < length; i++) {
  418. if (i != 0)
  419. buffer.append(", "); //$NON-NLS-1$
  420. TypeBinding type = types[i];
  421. boolean isVarargType = isVarargs && i == length - 1;
  422. if (isVarargType)
  423. type = ((ArrayBinding) type).elementsType();
  424. buffer.append(new String(makeShort ? type.shortReadableName() : type.readableName()));
  425. if (isVarargType)
  426. buffer.append("..."); //$NON-NLS-1$
  427. }
  428. return buffer.toString();
  429. }
  430. public void visibilityConflict(MethodBinding currentMethod, MethodBinding inheritedMethod) {
  431. // Not quite sure if the conditions on this test are right - basically I'm saying
  432. // DONT WORRY if its ITDs since the error will be reported another way...
  433. if (isIntertypeDeclaration(currentMethod) && isIntertypeDeclaration(inheritedMethod)
  434. && Modifier.isPrivate(currentMethod.modifiers) && Modifier.isPrivate(inheritedMethod.modifiers)) {
  435. return;
  436. }
  437. super.visibilityConflict(currentMethod, inheritedMethod);
  438. }
  439. public void unusedPrivateType(TypeDeclaration typeDecl) {
  440. // don't output unused type warnings for aspects!
  441. if (typeDecl instanceof AspectDeclaration)
  442. return;
  443. if (typeDecl.enclosingType != null && (typeDecl.enclosingType instanceof AspectDeclaration)) {
  444. AspectDeclaration ad = (AspectDeclaration) typeDecl.enclosingType;
  445. if (ad.concreteName != null) {
  446. List<Declare> declares = ad.concreteName.declares;
  447. for (Object dec : declares) {
  448. if (dec instanceof DeclareParents) {
  449. DeclareParents decp = (DeclareParents) dec;
  450. TypePattern[] newparents = decp.getParents().getTypePatterns();
  451. for (TypePattern pattern : newparents) {
  452. UnresolvedType ut = pattern.getExactType();
  453. if (ut == null)
  454. continue;
  455. if (CharOperation.compareWith(typeDecl.binding.signature(), ut.getSignature().toCharArray()) == 0)
  456. return;
  457. }
  458. }
  459. }
  460. }
  461. }
  462. super.unusedPrivateType(typeDecl);
  463. }
  464. private final static char[] thisJoinPointName = "thisJoinPoint".toCharArray();
  465. private final static char[] thisJoinPointStaticPartName = "thisJoinPointStaticPart".toCharArray();
  466. private final static char[] thisEnclosingJoinPointStaticPartName = "thisEnclosingJoinPointStaticPart".toCharArray();
  467. private final static char[] thisAspectInstanceName = "thisAspectInstance".toCharArray();
  468. @Override
  469. public void uninitializedLocalVariable(LocalVariableBinding binding, ASTNode location, Scope scope) {
  470. if (CharOperation.equals(binding.name, thisJoinPointName) ||
  471. CharOperation.equals(binding.name, thisJoinPointStaticPartName) ||
  472. CharOperation.equals(binding.name, thisAspectInstanceName) ||
  473. CharOperation.equals(binding.name, thisEnclosingJoinPointStaticPartName)) {
  474. // If in advice, this is not a problem
  475. if (binding.declaringScope!=null && (binding.declaringScope.referenceContext() instanceof AdviceDeclaration ||
  476. binding.declaringScope.referenceContext() instanceof IfMethodDeclaration)) {
  477. return;
  478. }
  479. }
  480. super.uninitializedLocalVariable(binding, location, scope);
  481. }
  482. public void abstractMethodInConcreteClass(SourceTypeBinding type) {
  483. if (type.scope!=null && type.scope.referenceContext instanceof AspectDeclaration) {
  484. // TODO could put out an Aspect specific message here
  485. return;
  486. }
  487. super.abstractMethodInConcreteClass(type);
  488. }
  489. // Don't warn if there is an ITD method/ctor from a privileged aspect
  490. public void unusedPrivateField(FieldDeclaration fieldDecl) {
  491. if (fieldDecl!=null && fieldDecl.binding != null && fieldDecl.binding.declaringClass != null) {
  492. ReferenceBinding type = fieldDecl.binding.declaringClass;
  493. ResolvedType weaverType = null;
  494. if (!type.isAnonymousType()) {
  495. weaverType = factory.fromEclipse(type);
  496. } else {
  497. weaverType = factory.fromEclipse(type.superclass());
  498. }
  499. Set checked = new HashSet();
  500. for (ConcreteTypeMunger m : weaverType.getInterTypeMungersIncludingSupers()) {
  501. ResolvedType theAspect = m.getAspectType();
  502. if (!checked.contains(theAspect)) {
  503. TypeBinding tb = factory.makeTypeBinding(m.getAspectType());
  504. // Let's check the privilegedHandler from that aspect
  505. if (tb instanceof SourceTypeBinding) { // BinaryTypeBinding is also a SourceTypeBinding ;)
  506. IPrivilegedHandler privilegedHandler = ((SourceTypeBinding) tb).privilegedHandler;
  507. if (privilegedHandler != null) {
  508. if (privilegedHandler.definesPrivilegedAccessToField(fieldDecl.binding)) {
  509. return;
  510. }
  511. } else if (theAspect instanceof ReferenceType) {
  512. // ResolvedMember rm = factory.makeResolvedMember(fieldDecl.binding);
  513. String fname = new String(fieldDecl.name);
  514. Collection/* ResolvedMember */privvies = ((ReferenceType) theAspect).getPrivilegedAccesses();
  515. // On an incremental compile the information is in the bcel delegate
  516. if (privvies != null) {
  517. for (Object privvy : privvies) {
  518. ResolvedMember priv = (ResolvedMember) privvy;
  519. if (priv.getName().equals(fname)) {
  520. return;
  521. }
  522. }
  523. }
  524. }
  525. }
  526. checked.add(theAspect);
  527. }
  528. }
  529. }
  530. super.unusedPrivateField(fieldDecl);
  531. }
  532. public void unusedPrivateMethod(AbstractMethodDeclaration methodDecl) {
  533. // don't output unused warnings for pointcuts...
  534. if (!(methodDecl instanceof PointcutDeclaration))
  535. super.unusedPrivateMethod(methodDecl);
  536. }
  537. public void caseExpressionMustBeConstant(Expression expression) {
  538. if (expression instanceof QualifiedNameReference) {
  539. QualifiedNameReference qnr = (QualifiedNameReference) expression;
  540. if (qnr.otherBindings != null && qnr.otherBindings.length > 0 && qnr.otherBindings[0] instanceof PrivilegedFieldBinding) {
  541. super.signalError(expression.sourceStart, expression.sourceEnd,
  542. "Fields accessible due to an aspect being privileged can not be used in switch statements");
  543. referenceContext.tagAsHavingErrors();
  544. return;
  545. }
  546. }
  547. super.caseExpressionMustBeConstant(expression);
  548. }
  549. public void unusedArgument(LocalDeclaration localDecl) {
  550. // don't warn if this is an aj synthetic arg
  551. String argType = new String(localDecl.type.resolvedType.signature());
  552. if (argType.startsWith("Lorg/aspectj/runtime/internal"))
  553. return;
  554. // If the unused argument is in a pointcut, don't report the problem (for now... pr148219)
  555. if (localDecl instanceof Argument) {
  556. Argument arg = (Argument) localDecl;
  557. if (arg.binding != null && arg.binding.declaringScope != null) {
  558. ReferenceContext context = arg.binding.declaringScope.referenceContext();
  559. if (context != null && context instanceof PointcutDeclaration)
  560. return;
  561. }
  562. }
  563. if (new String(localDecl.name).startsWith("ajc$")) {
  564. // Do not report problems for infrastructure variables beyond the users control - pr195090
  565. return;
  566. }
  567. super.unusedArgument(localDecl);
  568. }
  569. /**
  570. * A side-effect of the way that we handle itds on default methods on top-most implementors of interfaces is that a class
  571. * acquiring a final default ITD will erroneously report that it can't override its own member. This method detects that
  572. * situation.
  573. */
  574. public void finalMethodCannotBeOverridden(MethodBinding currentMethod, MethodBinding inheritedMethod) {
  575. if (currentMethod == inheritedMethod)
  576. return;
  577. super.finalMethodCannotBeOverridden(currentMethod, inheritedMethod);
  578. }
  579. /**
  580. * The method verifier is a bit 'keen' and doesn't cope well with ITDMs which are of course to be considered a 'default'
  581. * implementation if the target type doesn't supply one. This test may not be complete - it is possible that it should read if
  582. * *either* is an ITD...but I dont have a testcase that shows that is required. yet. (pr115788)
  583. */
  584. public void duplicateInheritedMethods(SourceTypeBinding type, MethodBinding inheritedMethod1, MethodBinding inheritedMethod2, boolean isJava8) {
  585. if (inheritedMethod1 instanceof InterTypeMethodBinding || inheritedMethod2 instanceof InterTypeMethodBinding)
  586. return;
  587. if ((inheritedMethod1 instanceof ParameterizedMethodBinding)
  588. && ((ParameterizedMethodBinding) inheritedMethod1).original() instanceof InterTypeMethodBinding)
  589. return;
  590. if ((inheritedMethod2 instanceof ParameterizedMethodBinding)
  591. && ((ParameterizedMethodBinding) inheritedMethod2).original() instanceof InterTypeMethodBinding)
  592. return;
  593. super.duplicateInheritedMethods(type, inheritedMethod1, inheritedMethod2, isJava8);
  594. }
  595. /**
  596. * All problems end up routed through here at some point...
  597. */
  598. public IProblem createProblem(char[] fileName, int problemId, String[] problemArguments, String[] messageArguments,
  599. int severity, int problemStartPosition, int problemEndPosition, int lineNumber) {
  600. IProblem problem = super.createProblem(fileName, problemId, problemArguments, messageArguments, severity,
  601. problemStartPosition, problemEndPosition, lineNumber, 0);
  602. if (factory.getWorld().isInPinpointMode()) {
  603. MessageIssued ex = new MessageIssued();
  604. ex.fillInStackTrace();
  605. StringWriter sw = new StringWriter();
  606. ex.printStackTrace(new PrintWriter(sw));
  607. StringBuffer sb = new StringBuffer();
  608. sb.append(CompilationAndWeavingContext.getCurrentContext());
  609. sb.append(sw.toString());
  610. problem = new PinpointedProblem(problem, sb.toString());
  611. }
  612. return problem;
  613. }
  614. private static class MessageIssued extends RuntimeException {
  615. public String getMessage() {
  616. return "message issued...";
  617. }
  618. }
  619. private static class PinpointedProblem implements IProblem {
  620. private IProblem delegate;
  621. private String message;
  622. public PinpointedProblem(IProblem aProblem, String pinpoint) {
  623. this.delegate = aProblem;
  624. // if this was a problem that came via the weaver, it will already have
  625. // pinpoint info, don't do it twice...
  626. if (!delegate.getMessage().contains("message issued...")) {
  627. this.message = delegate.getMessage() + "\n" + pinpoint;
  628. } else {
  629. this.message = delegate.getMessage();
  630. }
  631. }
  632. public String[] getArguments() {
  633. return delegate.getArguments();
  634. }
  635. public int getID() {
  636. return delegate.getID();
  637. }
  638. public String getMessage() {
  639. return message;
  640. }
  641. public char[] getOriginatingFileName() {
  642. return delegate.getOriginatingFileName();
  643. }
  644. public int getSourceEnd() {
  645. return delegate.getSourceEnd();
  646. }
  647. public int getSourceLineNumber() {
  648. return delegate.getSourceLineNumber();
  649. }
  650. public int getSourceStart() {
  651. return delegate.getSourceStart();
  652. }
  653. public boolean isError() {
  654. return delegate.isError();
  655. }
  656. public boolean isWarning() {
  657. return delegate.isWarning();
  658. }
  659. public void setSourceEnd(int sourceEnd) {
  660. delegate.setSourceEnd(sourceEnd);
  661. }
  662. public void setSourceLineNumber(int lineNumber) {
  663. delegate.setSourceLineNumber(lineNumber);
  664. }
  665. public void setSourceStart(int sourceStart) {
  666. delegate.setSourceStart(sourceStart);
  667. }
  668. public void setSeeAlsoProblems(IProblem[] problems) {
  669. delegate.setSeeAlsoProblems(problems);
  670. }
  671. public IProblem[] seeAlso() {
  672. return delegate.seeAlso();
  673. }
  674. public void setSupplementaryMessageInfo(String msg) {
  675. delegate.setSupplementaryMessageInfo(msg);
  676. }
  677. public String getSupplementaryMessageInfo() {
  678. return delegate.getSupplementaryMessageInfo();
  679. }
  680. @Override
  681. public boolean isInfo() {
  682. return delegate.isInfo();
  683. }
  684. }
  685. public void duplicateMethodInType(AbstractMethodDeclaration methodDecl, boolean equalParameters, int severity) {
  686. if (new String(methodDecl.selector).startsWith("ajc$interMethod")) {
  687. // this is an ITD clash and will be reported in another way by AspectJ (173602)
  688. return;
  689. }
  690. super.duplicateMethodInType(methodDecl, equalParameters, severity);
  691. }
  692. // pr246393 - if we are going to complain about privileged, we clearly don't know what is going on, so don't
  693. // confuse the user
  694. public void parseErrorInsertAfterToken(int start, int end, int currentKind, char[] errorTokenSource, String errorTokenName,
  695. String expectedToken) {
  696. if (expectedToken.equals("privileged") || expectedToken.equals("around")) {
  697. super.parseErrorNoSuggestion(start, end, currentKind, errorTokenSource, errorTokenName);
  698. } else {
  699. super.parseErrorInsertAfterToken(start, end, currentKind, errorTokenSource, errorTokenName, expectedToken);
  700. }
  701. }
  702. public void missingValueForAnnotationMember(Annotation annotation, char[] memberName) {
  703. if (referenceContext instanceof DeclareAnnotationDeclaration) {
  704. // If a remover then the values are not necessary
  705. if (((DeclareAnnotationDeclaration) referenceContext).isRemover()) {
  706. return;
  707. }
  708. }
  709. super.missingValueForAnnotationMember(annotation, memberName);
  710. }
  711. }