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 32KB

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