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.

AjLookupEnvironment.java 67KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  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 v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.ajdt.internal.compiler.lookup;
  13. import java.lang.reflect.Modifier;
  14. import java.util.*;
  15. import org.aspectj.ajdt.internal.compiler.CommonPrinter;
  16. import org.aspectj.ajdt.internal.compiler.ast.AspectDeclaration;
  17. import org.aspectj.ajdt.internal.compiler.ast.PointcutDeclaration;
  18. import org.aspectj.asm.AsmManager;
  19. import org.aspectj.bridge.IMessage;
  20. import org.aspectj.bridge.context.CompilationAndWeavingContext;
  21. import org.aspectj.bridge.context.ContextToken;
  22. import org.aspectj.org.eclipse.jdt.core.compiler.CharOperation;
  23. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
  24. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.Annotation;
  25. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
  26. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.NormalAnnotation;
  27. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference;
  28. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.SingleTypeReference;
  29. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
  30. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeReference;
  31. import org.aspectj.org.eclipse.jdt.internal.compiler.env.AccessRestriction;
  32. import org.aspectj.org.eclipse.jdt.internal.compiler.env.IBinaryType;
  33. import org.aspectj.org.eclipse.jdt.internal.compiler.env.INameEnvironment;
  34. import org.aspectj.org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
  35. import org.aspectj.org.eclipse.jdt.internal.compiler.impl.ITypeRequestor;
  36. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding;
  37. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ClassScope;
  38. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope;
  39. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.LocalTypeBinding;
  40. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment;
  41. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
  42. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.MissingTypeBinding;
  43. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ModuleBinding;
  44. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.PackageBinding;
  45. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding;
  46. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.RawTypeBinding;
  47. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
  48. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding;
  49. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.SourceTypeCollisionException;
  50. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TagBits;
  51. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
  52. import org.aspectj.org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
  53. import org.aspectj.weaver.AnnotationAJ;
  54. import org.aspectj.weaver.ConcreteTypeMunger;
  55. import org.aspectj.weaver.ReferenceType;
  56. import org.aspectj.weaver.ReferenceTypeDelegate;
  57. import org.aspectj.weaver.ResolvedMember;
  58. import org.aspectj.weaver.ResolvedType;
  59. import org.aspectj.weaver.ResolvedTypeMunger;
  60. import org.aspectj.weaver.UnresolvedType;
  61. import org.aspectj.weaver.WeaverMessages;
  62. import org.aspectj.weaver.WeaverStateInfo;
  63. import org.aspectj.weaver.World;
  64. import org.aspectj.weaver.bcel.BcelAnnotation;
  65. import org.aspectj.weaver.bcel.BcelObjectType;
  66. import org.aspectj.weaver.bcel.FakeAnnotation;
  67. import org.aspectj.weaver.bcel.LazyClassGen;
  68. import org.aspectj.weaver.patterns.DeclareAnnotation;
  69. import org.aspectj.weaver.patterns.DeclareParents;
  70. /**
  71. * Overrides the default eclipse LookupEnvironment for two purposes.
  72. *
  73. * 1. To provide some additional phases to <code>completeTypeBindings</code> that weave declare parents and inter-type declarations
  74. * at the correct time.
  75. *
  76. * 2. To intercept the loading of new binary types to ensure the they will have declare parents and inter-type declarations woven
  77. * when appropriate.
  78. *
  79. * @author Jim Hugunin
  80. */
  81. public class AjLookupEnvironment extends LookupEnvironment implements AnonymousClassCreationListener {
  82. public EclipseFactory factory = null;
  83. // private boolean builtInterTypesAndPerClauses = false;
  84. private final List<SourceTypeBinding> pendingTypesToWeave = new ArrayList<>();
  85. // Q: What are dangerousInterfaces?
  86. // A: An interface is considered dangerous if an ITD has been made upon it
  87. // and that ITD
  88. // requires the top most implementors of the interface to be woven *and yet*
  89. // the aspect
  90. // responsible for the ITD is not in the 'world'.
  91. // Q: Err, how can that happen?
  92. // A: When a type is on the inpath, it is 'processed' when completing type
  93. // bindings. At this
  94. // point we look at any type mungers it was affected by previously (stored
  95. // in the weaver
  96. // state info attribute). Effectively we are working with a type munger and
  97. // yet may not have its
  98. // originating aspect in the world. This is a problem if, for example, the
  99. // aspect supplied
  100. // a 'body' for a method targetting an interface - since the top most
  101. // implementors should
  102. // be woven by the munger from the aspect. When this happens we store the
  103. // interface name here
  104. // in the map - if we later process a type that is the topMostImplementor of
  105. // a dangerous
  106. // interface then we put out an error message.
  107. /**
  108. * interfaces targetted by ITDs that have to be implemented by accessing the topMostImplementor of the interface, yet the aspect
  109. * where the ITD originated is not in the world
  110. */
  111. private final Map<ResolvedType, String> dangerousInterfaces = new HashMap<>();
  112. public AjLookupEnvironment(ITypeRequestor typeRequestor, CompilerOptions options, ProblemReporter problemReporter,
  113. INameEnvironment nameEnvironment) {
  114. super(typeRequestor, options, problemReporter, nameEnvironment);
  115. }
  116. public AjLookupEnvironment(LookupEnvironment env, ModuleBinding moduleBinding) {
  117. super(env, moduleBinding);
  118. }
  119. // ??? duplicates some of super's code
  120. @Override
  121. public void completeTypeBindings() {
  122. AsmManager.setCompletingTypeBindings(true);
  123. ContextToken completeTypeBindingsToken = CompilationAndWeavingContext.enteringPhase(
  124. CompilationAndWeavingContext.COMPLETING_TYPE_BINDINGS, "");
  125. // builtInterTypesAndPerClauses = false;
  126. // pendingTypesToWeave = new ArrayList();
  127. stepCompleted = BUILD_TYPE_HIERARCHY;
  128. for (int i = lastCompletedUnitIndex + 1; i <= lastUnitIndex; i++) {
  129. ContextToken tok = CompilationAndWeavingContext.enteringPhase(CompilationAndWeavingContext.CHECK_AND_SET_IMPORTS,
  130. units[i].compilationResult.fileName);
  131. units[i].scope.checkAndSetImports();
  132. CompilationAndWeavingContext.leavingPhase(tok);
  133. }
  134. stepCompleted = CHECK_AND_SET_IMPORTS;
  135. for (int i = lastCompletedUnitIndex + 1; i <= lastUnitIndex; i++) {
  136. ContextToken tok = CompilationAndWeavingContext.enteringPhase(CompilationAndWeavingContext.CONNECTING_TYPE_HIERARCHY,
  137. units[i].compilationResult.fileName);
  138. units[i].scope.connectTypeHierarchy();
  139. CompilationAndWeavingContext.leavingPhase(tok);
  140. }
  141. stepCompleted = CONNECT_TYPE_HIERARCHY;
  142. for (int i = lastCompletedUnitIndex + 1; i <= lastUnitIndex; i++) {
  143. ContextToken tok = CompilationAndWeavingContext.enteringPhase(CompilationAndWeavingContext.BUILDING_FIELDS_AND_METHODS,
  144. units[i].compilationResult.fileName);
  145. // units[i].scope.checkParameterizedTypes(); do this check a little
  146. // later, after ITDs applied to stbs
  147. units[i].scope.buildFieldsAndMethods();
  148. CompilationAndWeavingContext.leavingPhase(tok);
  149. }
  150. // would like to gather up all TypeDeclarations at this point and put
  151. // them in the factory
  152. for (int i = lastCompletedUnitIndex + 1; i <= lastUnitIndex; i++) {
  153. SourceTypeBinding[] b = units[i].scope.topLevelTypes;
  154. for (SourceTypeBinding sourceTypeBinding : b) {
  155. factory.addSourceTypeBinding(sourceTypeBinding, units[i]);
  156. if (sourceTypeBinding.superclass instanceof MissingTypeBinding) {
  157. // e37: Undoing the work in ClassScope.connectSuperClass() as it will lead to cascade errors
  158. // TODO allow MissingTypeBinding through here and cope with it in all situations later?
  159. sourceTypeBinding.superclass = units[i].scope.getJavaLangObject();
  160. }
  161. }
  162. }
  163. // We won't find out about anonymous types until later though, so
  164. // register to be
  165. // told about them when they turn up.
  166. AnonymousClassPublisher.aspectOf().setAnonymousClassCreationListener(this);
  167. // need to build inter-type declarations for all AspectDeclarations at
  168. // this point
  169. // this MUST be done in order from super-types to subtypes
  170. List<SourceTypeBinding> typesToProcess = new ArrayList<>();
  171. List<SourceTypeBinding> aspectsToProcess = new ArrayList<>();
  172. for (int i = lastCompletedUnitIndex + 1; i <= lastUnitIndex; i++) {
  173. CompilationUnitScope cus = units[i].scope;
  174. SourceTypeBinding[] stbs = cus.topLevelTypes;
  175. for (SourceTypeBinding stb : stbs) {
  176. typesToProcess.add(stb);
  177. TypeDeclaration typeDeclaration = stb.scope.referenceContext;
  178. if (typeDeclaration instanceof AspectDeclaration) {
  179. aspectsToProcess.add(stb);
  180. }
  181. }
  182. }
  183. factory.getWorld().getCrosscuttingMembersSet().reset();
  184. // Need to do these before the other ITDs
  185. for (SourceTypeBinding aspectToProcess : aspectsToProcess) {
  186. processInterTypeMemberTypes(aspectToProcess.scope);
  187. }
  188. while (typesToProcess.size() > 0) {
  189. // removes types from the list as they are processed...
  190. collectAllITDsAndDeclares(typesToProcess.get(0), typesToProcess);
  191. }
  192. factory.finishTypeMungers();
  193. // now do weaving
  194. final List<ConcreteTypeMunger> typeMungers = factory.getTypeMungers();
  195. final List<DeclareParents> declareParents = factory.getDeclareParents();
  196. final List<DeclareAnnotation> declareAnnotationOnTypes = factory.getDeclareAnnotationOnTypes();
  197. doPendingWeaves();
  198. // We now have some list of types to process, and we are about to apply
  199. // the type mungers.
  200. // There can be situations where the order of types passed to the
  201. // compiler causes the
  202. // output from the compiler to vary - THIS IS BAD. For example, if we
  203. // have class A
  204. // and class B extends A. Also, an aspect that 'declare parents: A+
  205. // implements Serializable'
  206. // then depending on whether we see A first, we may or may not make B
  207. // serializable.
  208. // The fix is to process them in the right order, ensuring that for a
  209. // type we process its
  210. // supertypes and superinterfaces first. This algorithm may have
  211. // problems with:
  212. // - partial hierarchies (e.g. suppose types A,B,C are in a hierarchy
  213. // and A and C are to be woven but not B)
  214. // - weaving that brings new types in for processing (see
  215. // pendingTypesToWeave.add() calls) after we thought
  216. // we had the full list.
  217. //
  218. // but these aren't common cases (he bravely said...)
  219. boolean typeProcessingOrderIsImportant = declareParents.size() > 0 || declareAnnotationOnTypes.size() > 0; // DECAT
  220. if (typeProcessingOrderIsImportant) {
  221. typesToProcess = new ArrayList<>();
  222. for (int i = lastCompletedUnitIndex + 1; i <= lastUnitIndex; i++) {
  223. CompilationUnitScope cus = units[i].scope;
  224. SourceTypeBinding[] stbs = cus.topLevelTypes;
  225. Collections.addAll(typesToProcess, stbs);
  226. }
  227. List<SourceTypeBinding> stb2 = new ArrayList<>(typesToProcess);
  228. while (typesToProcess.size() > 0) {
  229. // A side effect of weaveIntertypes() is that the processed type is removed from the collection
  230. weaveIntertypes(typesToProcess, typesToProcess.get(0), typeMungers, declareParents, declareAnnotationOnTypes, 1);
  231. }
  232. while (stb2.size() > 0) {
  233. // A side effect of weaveIntertypes() is that the processed type is removed from the collection
  234. weaveIntertypes(stb2, stb2.get(0), typeMungers, declareParents, declareAnnotationOnTypes, 2);
  235. }
  236. } else {
  237. // Order isn't important
  238. for (int i = lastCompletedUnitIndex + 1; i <= lastUnitIndex; i++) {
  239. weaveInterTypeDeclarations(units[i].scope, typeMungers, declareParents, declareAnnotationOnTypes);
  240. }
  241. }
  242. for (int i = lastCompletedUnitIndex + 1; i <= lastUnitIndex; i++) {
  243. units[i].scope.checkParameterizedTypes();
  244. }
  245. for (int i = lastCompletedUnitIndex + 1; i <= lastUnitIndex; i++) {
  246. SourceTypeBinding[] b = units[i].scope.topLevelTypes;
  247. for (SourceTypeBinding sourceTypeBinding : b) {
  248. ContextToken tok = CompilationAndWeavingContext.enteringPhase(
  249. CompilationAndWeavingContext.RESOLVING_POINTCUT_DECLARATIONS, sourceTypeBinding.sourceName);
  250. resolvePointcutDeclarations(sourceTypeBinding.scope);
  251. CompilationAndWeavingContext.leavingPhase(tok);
  252. }
  253. }
  254. for (int i = lastCompletedUnitIndex + 1; i <= lastUnitIndex; i++) {
  255. SourceTypeBinding[] b = units[i].scope.topLevelTypes;
  256. for (SourceTypeBinding sourceTypeBinding : b) {
  257. ContextToken tok = CompilationAndWeavingContext.enteringPhase(
  258. CompilationAndWeavingContext.ADDING_DECLARE_WARNINGS_AND_ERRORS, sourceTypeBinding.sourceName);
  259. addAdviceLikeDeclares(sourceTypeBinding.scope);
  260. CompilationAndWeavingContext.leavingPhase(tok);
  261. }
  262. }
  263. for (int i = lastCompletedUnitIndex + 1; i <= lastUnitIndex; i++) {
  264. units[i] = null; // release unnecessary reference to the parsed unit
  265. }
  266. stepCompleted = BUILD_FIELDS_AND_METHODS;
  267. lastCompletedUnitIndex = lastUnitIndex;
  268. AsmManager.setCompletingTypeBindings(false);
  269. factory.getWorld().getCrosscuttingMembersSet().verify();
  270. CompilationAndWeavingContext.leavingPhase(completeTypeBindingsToken);
  271. if (isProcessingAnnotations) {
  272. throw new SourceTypeCollisionException(); // TODO(yushkovskiy): temporary solution; forcing to recompile units to insert mungers into types
  273. }
  274. }
  275. // /**
  276. // * For any given sourcetypebinding, this method checks that if it is a
  277. // parameterized aspect that
  278. // * the type parameters specified for any supertypes meet the bounds for
  279. // the generic type
  280. // * variables.
  281. // */
  282. // private void verifyAnyTypeParametersMeetBounds(SourceTypeBinding
  283. // sourceType) {
  284. // ResolvedType onType = factory.fromEclipse(sourceType);
  285. // if (onType.isAspect()) {
  286. // ResolvedType superType = factory.fromEclipse(sourceType.superclass);
  287. // // Don't need to check if it was used in its RAW form or isnt generic
  288. // if (superType.isGenericType() || superType.isParameterizedType()) {
  289. // TypeVariable[] typeVariables = superType.getTypeVariables();
  290. // UnresolvedType[] typeParams = superType.getTypeParameters();
  291. // if (typeVariables!=null && typeParams!=null) {
  292. // for (int i = 0; i < typeVariables.length; i++) {
  293. // boolean ok =
  294. // typeVariables[i].canBeBoundTo(typeParams[i].resolve(factory.getWorld()));
  295. // if (!ok) { // the supplied parameter violates the bounds
  296. // // Type {0} does not meet the specification for type parameter {1} ({2})
  297. // in generic type {3}
  298. // String msg =
  299. // WeaverMessages.format(
  300. // WeaverMessages.VIOLATES_TYPE_VARIABLE_BOUNDS,
  301. // typeParams[i],
  302. // new Integer(i+1),
  303. // typeVariables[i].getDisplayName(),
  304. // superType.getGenericType().getName());
  305. // factory.getWorld().getMessageHandler().handleMessage(MessageUtil.error(msg
  306. // ,onType.getSourceLocation()));
  307. // }
  308. // }
  309. // }
  310. // }
  311. // }
  312. // }
  313. public void doSupertypesFirst(ReferenceBinding rb, Collection<? extends ReferenceBinding> yetToProcess) {
  314. if (rb instanceof SourceTypeBinding) {
  315. if (yetToProcess.contains(rb)) {
  316. collectAllITDsAndDeclares((SourceTypeBinding) rb, yetToProcess);
  317. }
  318. } else if (rb instanceof ParameterizedTypeBinding) {
  319. // If its a PTB we need to pull the SourceTypeBinding out of it.
  320. ParameterizedTypeBinding ptb = (ParameterizedTypeBinding) rb;
  321. if (ptb.type instanceof SourceTypeBinding && yetToProcess.contains(ptb.type)) {
  322. collectAllITDsAndDeclares((SourceTypeBinding) ptb.type, yetToProcess);
  323. }
  324. }
  325. }
  326. /**
  327. * Find all the ITDs and Declares, but it is important we do this from the supertypes down to the subtypes.
  328. *
  329. * @param sourceType
  330. * @param yetToProcess
  331. */
  332. private void collectAllITDsAndDeclares(SourceTypeBinding sourceType, Collection<? extends ReferenceBinding> yetToProcess) {
  333. // Look at the supertype first
  334. ContextToken tok = CompilationAndWeavingContext.enteringPhase(CompilationAndWeavingContext.COLLECTING_ITDS_AND_DECLARES,
  335. sourceType.sourceName);
  336. yetToProcess.remove(sourceType);
  337. // look out our direct supertype
  338. doSupertypesFirst(sourceType.superclass(), yetToProcess);
  339. // now check our membertypes (pr119570)
  340. ReferenceBinding[] memberTypes = sourceType.memberTypes;
  341. for (ReferenceBinding memberType : memberTypes) {
  342. SourceTypeBinding rb = (SourceTypeBinding) memberType;
  343. if (!rb.superclass().equals(sourceType)) {
  344. doSupertypesFirst(rb.superclass(), yetToProcess);
  345. }
  346. }
  347. buildInterTypeAndPerClause(sourceType.scope);
  348. addCrosscuttingStructures(sourceType.scope);
  349. CompilationAndWeavingContext.leavingPhase(tok);
  350. }
  351. /**
  352. * Weave the parents and intertype decls into a given type. This method looks at the supertype and superinterfaces for the
  353. * specified type and recurses to weave those first if they are in the full list of types we are going to process during this
  354. * compile... it stops recursing the first time it hits a type we aren't going to process during this compile. This could cause
  355. * problems if you supply 'pieces' of a hierarchy, i.e. the bottom and the top, but not the middle - but what the hell are you
  356. * doing if you do that?
  357. *
  358. * @param mode 0=do everything, 1=do declare parents, 2=do ITDs
  359. */
  360. private void weaveIntertypes(List<SourceTypeBinding> typesToProcess, SourceTypeBinding typeToWeave,
  361. List<ConcreteTypeMunger> typeMungers, List<DeclareParents> declareParents,
  362. List<DeclareAnnotation> declareAnnotationOnTypes, int mode) {
  363. // Look at the supertype first
  364. ReferenceBinding superType = typeToWeave.superclass();
  365. if (typesToProcess.contains(superType) && superType instanceof SourceTypeBinding) {
  366. // System.err.println("Recursing to supertype "+new
  367. // String(superType.getFileName()));
  368. weaveIntertypes(typesToProcess, (SourceTypeBinding) superType, typeMungers, declareParents, declareAnnotationOnTypes,
  369. mode);
  370. }
  371. // Then look at the superinterface list
  372. ReferenceBinding[] interfaceTypes = typeToWeave.superInterfaces();
  373. for (ReferenceBinding binding : interfaceTypes) {
  374. if (typesToProcess.contains(binding) && binding instanceof SourceTypeBinding) {
  375. // System.err.println("Recursing to superinterface "+new
  376. // String(binding.getFileName()));
  377. weaveIntertypes(typesToProcess, (SourceTypeBinding) binding, typeMungers, declareParents, declareAnnotationOnTypes,
  378. mode);
  379. } else if (binding instanceof ParameterizedTypeBinding && (((ParameterizedTypeBinding) binding).type instanceof SourceTypeBinding) && typesToProcess.contains(((ParameterizedTypeBinding) binding).type)) {
  380. weaveIntertypes(typesToProcess, (SourceTypeBinding) ((ParameterizedTypeBinding) binding).type, typeMungers, declareParents, declareAnnotationOnTypes, mode);
  381. }
  382. }
  383. weaveInterTypeDeclarations(typeToWeave, typeMungers, declareParents, declareAnnotationOnTypes, false, mode);
  384. typesToProcess.remove(typeToWeave);
  385. }
  386. private void doPendingWeaves() {
  387. for (SourceTypeBinding t: pendingTypesToWeave) {
  388. ContextToken tok = CompilationAndWeavingContext.enteringPhase(
  389. CompilationAndWeavingContext.WEAVING_INTERTYPE_DECLARATIONS, t.sourceName);
  390. weaveInterTypeDeclarations(t);
  391. CompilationAndWeavingContext.leavingPhase(tok);
  392. }
  393. pendingTypesToWeave.clear();
  394. }
  395. private void addAdviceLikeDeclares(ClassScope s) {
  396. TypeDeclaration dec = s.referenceContext;
  397. if (dec instanceof AspectDeclaration) {
  398. ResolvedType typeX = factory.fromEclipse(dec.binding);
  399. factory.getWorld().getCrosscuttingMembersSet().addAdviceLikeDeclares(typeX);
  400. }
  401. SourceTypeBinding sourceType = s.referenceContext.binding;
  402. ReferenceBinding[] memberTypes = sourceType.memberTypes;
  403. for (ReferenceBinding memberType : memberTypes) {
  404. addAdviceLikeDeclares(((SourceTypeBinding) memberType).scope);
  405. }
  406. }
  407. private void addCrosscuttingStructures(ClassScope s) {
  408. TypeDeclaration dec = s.referenceContext;
  409. if (dec instanceof AspectDeclaration) {
  410. ResolvedType typeX = factory.fromEclipse(dec.binding);
  411. factory.getWorld().getCrosscuttingMembersSet().addOrReplaceAspect(typeX, false);
  412. if (typeX.getSuperclass().isAspect() && !typeX.getSuperclass().isExposedToWeaver()) {
  413. factory.getWorld().getCrosscuttingMembersSet().addOrReplaceAspect(typeX.getSuperclass(), false);
  414. }
  415. }
  416. SourceTypeBinding sourceType = s.referenceContext.binding;
  417. ReferenceBinding[] memberTypes = sourceType.memberTypes;
  418. for (ReferenceBinding memberType : memberTypes) {
  419. addCrosscuttingStructures(((SourceTypeBinding) memberType).scope);
  420. }
  421. }
  422. private void resolvePointcutDeclarations(ClassScope s) {
  423. TypeDeclaration dec = s.referenceContext;
  424. SourceTypeBinding sourceType = s.referenceContext.binding;
  425. boolean hasPointcuts = false;
  426. AbstractMethodDeclaration[] methods = dec.methods;
  427. boolean initializedMethods = false;
  428. if (methods != null) {
  429. for (AbstractMethodDeclaration method : methods) {
  430. if (method instanceof PointcutDeclaration) {
  431. hasPointcuts = true;
  432. if (!initializedMethods) {
  433. sourceType.methods(); // force initialization
  434. initializedMethods = true;
  435. }
  436. ((PointcutDeclaration) method).resolvePointcut(s);
  437. }
  438. }
  439. }
  440. if (hasPointcuts || dec instanceof AspectDeclaration || couldBeAnnotationStyleAspectDeclaration(dec)) {
  441. ReferenceType name = (ReferenceType) factory.fromEclipse(sourceType);
  442. EclipseSourceType eclipseSourceType = (EclipseSourceType) name.getDelegate();
  443. eclipseSourceType.checkPointcutDeclarations();
  444. }
  445. ReferenceBinding[] memberTypes = sourceType.memberTypes;
  446. for (ReferenceBinding memberType : memberTypes) {
  447. resolvePointcutDeclarations(((SourceTypeBinding) memberType).scope);
  448. }
  449. }
  450. /**
  451. * Return true if the declaration has @Aspect annotation. Called 'couldBe' rather than 'is' because someone else may have
  452. * defined an annotation called Aspect - we can't verify the full name (including package name) because it may not have been
  453. * resolved just yet and rather going through expensive resolution when we dont have to, this gives us a cheap check that tells
  454. * us whether to bother.
  455. */
  456. private boolean couldBeAnnotationStyleAspectDeclaration(TypeDeclaration dec) {
  457. Annotation[] annotations = dec.annotations;
  458. boolean couldBeAtAspect = false;
  459. if (annotations != null) {
  460. for (int i = 0; i < annotations.length && !couldBeAtAspect; i++) {
  461. if (annotations[i].toString().equals("@Aspect")) {
  462. couldBeAtAspect = true;
  463. }
  464. }
  465. }
  466. return couldBeAtAspect;
  467. }
  468. /**
  469. * Applies any intertype member type declarations up front.
  470. */
  471. private void processInterTypeMemberTypes(ClassScope classScope) {
  472. TypeDeclaration dec = classScope.referenceContext;
  473. if (dec instanceof AspectDeclaration) {
  474. ((AspectDeclaration) dec).processIntertypeMemberTypes(classScope);
  475. }
  476. // if we are going to support nested aspects making itd member types, copy the logic from the end of
  477. // buildInterTypeAndPerClause() which walks members
  478. }
  479. private void buildInterTypeAndPerClause(ClassScope s) {
  480. TypeDeclaration dec = s.referenceContext;
  481. if (dec instanceof AspectDeclaration) {
  482. ((AspectDeclaration) dec).buildInterTypeAndPerClause(s);
  483. }
  484. SourceTypeBinding sourceType = s.referenceContext.binding;
  485. // test classes don't extend aspects
  486. if (sourceType.superclass != null) {
  487. ResolvedType parent = factory.fromEclipse(sourceType.superclass);
  488. if (parent.isAspect() && !isAspect(dec)) {
  489. factory.showMessage(IMessage.ERROR, "class \'" + new String(sourceType.sourceName) + "\' can not extend aspect \'"
  490. + parent.getName() + "\'", factory.fromEclipse(sourceType).getSourceLocation(), null);
  491. }
  492. }
  493. ReferenceBinding[] memberTypes = sourceType.memberTypes;
  494. if (memberTypes == null) {
  495. System.err.println("Unexpectedly found null for memberTypes of " + sourceType.debugName());
  496. }
  497. if (memberTypes != null) {
  498. for (ReferenceBinding memberType : memberTypes) {
  499. buildInterTypeAndPerClause(((SourceTypeBinding) memberType).scope);
  500. }
  501. }
  502. }
  503. private boolean isAspect(TypeDeclaration decl) {
  504. if ((decl instanceof AspectDeclaration)) {
  505. return true;
  506. } else if (decl.annotations == null) {
  507. return false;
  508. } else {
  509. for (int i = 0; i < decl.annotations.length; i++) {
  510. Annotation ann = decl.annotations[i];
  511. if (ann.type instanceof SingleTypeReference) {
  512. if (CharOperation.equals("Aspect".toCharArray(), ((SingleTypeReference) ann.type).token)) {
  513. return true;
  514. }
  515. } else if (ann.type instanceof QualifiedTypeReference) {
  516. QualifiedTypeReference qtr = (QualifiedTypeReference) ann.type;
  517. if (qtr.tokens.length != 5) {
  518. return false;
  519. }
  520. if (!CharOperation.equals("org".toCharArray(), qtr.tokens[0])) {
  521. return false;
  522. }
  523. if (!CharOperation.equals("aspectj".toCharArray(), qtr.tokens[1])) {
  524. return false;
  525. }
  526. if (!CharOperation.equals("lang".toCharArray(), qtr.tokens[2])) {
  527. return false;
  528. }
  529. if (!CharOperation.equals("annotation".toCharArray(), qtr.tokens[3])) {
  530. return false;
  531. }
  532. if (!CharOperation.equals("Aspect".toCharArray(), qtr.tokens[4])) {
  533. return false;
  534. }
  535. return true;
  536. }
  537. }
  538. }
  539. return false;
  540. }
  541. private void weaveInterTypeDeclarations(CompilationUnitScope unit, List<ConcreteTypeMunger> typeMungers,
  542. List<DeclareParents> declareParents, List<DeclareAnnotation> declareAnnotationOnTypes) {
  543. for (int i = 0, length = unit.topLevelTypes.length; i < length; i++) {
  544. weaveInterTypeDeclarations(unit.topLevelTypes[i], typeMungers, declareParents, declareAnnotationOnTypes, false, 0);
  545. }
  546. }
  547. private void weaveInterTypeDeclarations(SourceTypeBinding sourceType) {
  548. if (!factory.areTypeMungersFinished()) {
  549. if (!pendingTypesToWeave.contains(sourceType)) {
  550. pendingTypesToWeave.add(sourceType);
  551. // inner type ITD support - may need this for some incremental cases...
  552. // List<ConcreteTypeMunger> ctms = factory.getWorld().getCrosscuttingMembersSet().getTypeMungersOfKind(
  553. // ResolvedTypeMunger.InnerClass);
  554. // // List<ConcreteTypeMunger> innerTypeMungers = new ArrayList<ConcreteTypeMunger>();
  555. // // for (ConcreteTypeMunger ctm : ctms) {
  556. // // if (ctm.getMunger() != null && ctm.getMunger().getKind() == ResolvedTypeMunger.InnerClass) {
  557. // // innerTypeMungers.add(ctm);
  558. // // }
  559. // // }
  560. // // that includes the innertype one...
  561. // // doPendingWeaves at this level is about applying inner class
  562. // BinaryTypeBinding t = (BinaryTypeBinding) sourceType;
  563. // for (ConcreteTypeMunger ctm : innerTypeMungers) {
  564. // NewMemberClassTypeMunger nmctm = (NewMemberClassTypeMunger) ctm.getMunger();
  565. // ReferenceBinding[] rbs = t.memberTypes;
  566. // UnresolvedType ut = factory.fromBinding(t);
  567. // if (ut.equals(nmctm.getTargetType())) {
  568. // // got a match here
  569. // SourceTypeBinding aspectTypeBinding = (SourceTypeBinding) factory.makeTypeBinding(ctm.getAspectType());
  570. //
  571. // char[] mungerMemberTypeName = ("$" + nmctm.getMemberTypeName()).toCharArray();
  572. // ReferenceBinding innerTypeBinding = null;
  573. // for (ReferenceBinding innerType : aspectTypeBinding.memberTypes) {
  574. // char[] compounded = CharOperation.concatWith(innerType.compoundName, '.');
  575. // if (org.aspectj.org.eclipse.jdt.core.compiler.CharOperation.endsWith(compounded, mungerMemberTypeName)) {
  576. // innerTypeBinding = innerType;
  577. // break;
  578. // }
  579. // }
  580. // // may be unresolved if the aspect type binding was a BinaryTypeBinding
  581. // if (innerTypeBinding instanceof UnresolvedReferenceBinding) {
  582. // innerTypeBinding = BinaryTypeBinding
  583. // .resolveType(innerTypeBinding, factory.getLookupEnvironment(), true);
  584. // }
  585. // t.memberTypes(); // cause initialization
  586. // t.memberTypes = new ReferenceBinding[] { innerTypeBinding };
  587. //
  588. // int stop = 1;
  589. // // The inner type from the aspect should be put into the membertypebindings for this
  590. //
  591. // }
  592. // }
  593. }
  594. } else {
  595. weaveInterTypeDeclarations(sourceType, factory.getTypeMungers(), factory.getDeclareParents(),
  596. factory.getDeclareAnnotationOnTypes(), true, 0);
  597. }
  598. }
  599. /**
  600. * @param mode 0=do everything, 1=do declare parents, 2=do ITDs
  601. */
  602. private void weaveInterTypeDeclarations(SourceTypeBinding sourceType, List<ConcreteTypeMunger> typeMungers,
  603. List<DeclareParents> declareParents, List<DeclareAnnotation> declareAnnotationOnTypes, boolean skipInners, int mode) {
  604. ContextToken tok = CompilationAndWeavingContext.enteringPhase(CompilationAndWeavingContext.WEAVING_INTERTYPE_DECLARATIONS,
  605. sourceType.sourceName);
  606. ResolvedType onType = factory.fromEclipse(sourceType);
  607. // AMC we shouldn't need this when generic sigs are fixed??
  608. if (onType.isRawType()) {
  609. onType = onType.getGenericType();
  610. }
  611. WeaverStateInfo info = onType.getWeaverState();
  612. if (mode < 2) {
  613. // this test isnt quite right - there will be a case where we fail to
  614. // flag a problem
  615. // with a 'dangerous interface' because the type is reweavable when we
  616. // should have
  617. // because the type wasn't going to be rewoven... if that happens, we
  618. // should perhaps
  619. // move this test and dangerous interface processing to the end of this
  620. // method and
  621. // make it conditional on whether any of the typeMungers passed into
  622. // here actually
  623. // matched this type.
  624. if (info != null && !info.isOldStyle() && !info.isReweavable()) {
  625. processTypeMungersFromExistingWeaverState(sourceType, onType);
  626. CompilationAndWeavingContext.leavingPhase(tok);
  627. return;
  628. }
  629. // Check if the type we are looking at is the topMostImplementor of a
  630. // dangerous interface -
  631. // report a problem if it is.
  632. for (Map.Entry<ResolvedType, String> entry : dangerousInterfaces.entrySet()) {
  633. ResolvedType interfaceType = entry.getKey();
  634. if (onType.isTopmostImplementor(interfaceType)) {
  635. factory.showMessage(IMessage.ERROR, onType + ": " + entry.getValue(), onType.getSourceLocation(), null);
  636. }
  637. }
  638. boolean needOldStyleWarning = (info != null && info.isOldStyle());
  639. onType.clearInterTypeMungers();
  640. onType.ensureConsistent();
  641. // FIXME asc perf Could optimize here, after processing the expected set
  642. // of types we may bring
  643. // binary types that are not exposed to the weaver, there is no need to
  644. // attempt declare parents
  645. // or declare annotation really - unless we want to report the
  646. // not-exposed to weaver
  647. // messages...
  648. List<DeclareParents> decpToRepeat = new ArrayList<>();
  649. List<DeclareAnnotation> decaToRepeat = new ArrayList<>();
  650. boolean anyNewParents = false;
  651. boolean anyNewAnnotations = false;
  652. // first pass
  653. // try and apply all decps - if they match, then great. If they don't
  654. // then
  655. // check if they are starred-annotation patterns. If they are not
  656. // starred
  657. // annotation patterns then they might match later...remember that...
  658. for (DeclareParents decp : declareParents) {
  659. if (!decp.isMixin()) {
  660. boolean didSomething = doDeclareParents(decp, sourceType);
  661. if (didSomething) {
  662. if (factory.pushinCollector != null) {
  663. factory.pushinCollector.tagAsMunged(sourceType, decp.getParents().get(0));
  664. }
  665. anyNewParents = true;
  666. } else {
  667. if (!decp.getChild().isStarAnnotation()) {
  668. decpToRepeat.add(decp);
  669. }
  670. }
  671. }
  672. }
  673. for (DeclareAnnotation deca : declareAnnotationOnTypes) {
  674. boolean didSomething = doDeclareAnnotations(deca, sourceType, true);
  675. if (didSomething) {
  676. anyNewAnnotations = true;
  677. } else {
  678. if (!deca.getTypePattern().isStar()) {
  679. decaToRepeat.add(deca);
  680. }
  681. }
  682. }
  683. List<Object> forRemoval = new ArrayList<>();
  684. // now lets loop over and over until we have done all we can
  685. while ((anyNewAnnotations || anyNewParents) && (!decpToRepeat.isEmpty() || !decaToRepeat.isEmpty())) {
  686. anyNewParents = anyNewAnnotations = false;
  687. forRemoval.clear();
  688. for (DeclareParents decp : decpToRepeat) {
  689. boolean didSomething = doDeclareParents(decp, sourceType);
  690. if (didSomething) {
  691. if (factory.pushinCollector != null) {
  692. factory.pushinCollector.tagAsMunged(sourceType, decp.getParents().get(0));
  693. }
  694. anyNewParents = true;
  695. forRemoval.add(decp);
  696. }
  697. }
  698. decpToRepeat.removeAll(forRemoval);
  699. forRemoval.clear();
  700. for (DeclareAnnotation deca : decaToRepeat) {
  701. boolean didSomething = doDeclareAnnotations(deca, sourceType, false);
  702. if (didSomething) {
  703. if (factory.pushinCollector != null) {
  704. factory.pushinCollector.tagAsMunged(sourceType, deca.getAnnotationString());
  705. }
  706. anyNewAnnotations = true;
  707. forRemoval.add(deca);
  708. }
  709. }
  710. decaToRepeat.removeAll(forRemoval);
  711. }
  712. }
  713. if (mode == 0 || mode == 2) {
  714. for (ConcreteTypeMunger typeMunger : typeMungers) {
  715. EclipseTypeMunger munger = (EclipseTypeMunger) typeMunger;
  716. if (munger.matches(onType)) {
  717. // if (needOldStyleWarning) {
  718. // factory.showMessage(IMessage.WARNING, "The class for " + onType
  719. // + " should be recompiled with ajc-1.1.1 for best results", onType.getSourceLocation(), null);
  720. // needOldStyleWarning = false;
  721. // }
  722. onType.addInterTypeMunger(munger, true);
  723. if (munger.getMunger() != null && munger.getMunger().getKind() == ResolvedTypeMunger.InnerClass) {
  724. // Must do these right now, because if we do an ITD member afterwards it may attempt to reference the
  725. // type being applied (the call above 'addInterTypeMunger' will fail for these ITDs if it needed
  726. // it to be in place)
  727. if (munger.munge(sourceType, onType)) {
  728. if (factory.pushinCollector != null) {
  729. factory.pushinCollector.tagAsMunged(sourceType, munger.getSourceMethod());
  730. }
  731. }
  732. }
  733. }
  734. }
  735. onType.checkInterTypeMungers();
  736. for (ConcreteTypeMunger concreteTypeMunger : onType.getInterTypeMungers()) {
  737. EclipseTypeMunger munger = (EclipseTypeMunger) concreteTypeMunger;
  738. if (munger.getMunger() == null || munger.getMunger().getKind() != ResolvedTypeMunger.InnerClass) {
  739. if (munger.munge(sourceType, onType)) {
  740. if (factory.pushinCollector != null) {
  741. factory.pushinCollector.tagAsMunged(sourceType, munger.getSourceMethod());
  742. }
  743. }
  744. }
  745. }
  746. }
  747. // Call if you would like to do source weaving of declare
  748. // @method/@constructor
  749. // at source time... no need to do this as it can't impact anything, but
  750. // left here for
  751. // future generations to enjoy. Method source is commented out at the
  752. // end of this module
  753. // doDeclareAnnotationOnMethods();
  754. // Call if you would like to do source weaving of declare @field
  755. // at source time... no need to do this as it can't impact anything, but
  756. // left here for
  757. // future generations to enjoy. Method source is commented out at the
  758. // end of this module
  759. // doDeclareAnnotationOnFields();
  760. if (skipInners) {
  761. CompilationAndWeavingContext.leavingPhase(tok);
  762. return;
  763. }
  764. ReferenceBinding[] memberTypes = sourceType.memberTypes;
  765. for (ReferenceBinding memberType : memberTypes) {
  766. if (memberType instanceof SourceTypeBinding) {
  767. weaveInterTypeDeclarations((SourceTypeBinding) memberType, typeMungers, declareParents,
  768. declareAnnotationOnTypes, false, mode);
  769. }
  770. }
  771. CompilationAndWeavingContext.leavingPhase(tok);
  772. }
  773. /**
  774. * Called when we discover we are weaving intertype declarations on some type that has an existing 'WeaverStateInfo' object -
  775. * this is typically some previously woven type that has been passed on the inpath.
  776. *
  777. * sourceType and onType are the 'same type' - the former is the 'Eclipse' version and the latter is the 'Weaver' version.
  778. */
  779. private void processTypeMungersFromExistingWeaverState(SourceTypeBinding sourceType, ResolvedType onType) {
  780. List<ConcreteTypeMunger> previouslyAppliedMungers = onType.getWeaverState().getTypeMungers(onType);
  781. for (ConcreteTypeMunger m : previouslyAppliedMungers) {
  782. EclipseTypeMunger munger = factory.makeEclipseTypeMunger(m);
  783. if (munger.munge(sourceType, onType)) {
  784. if (onType.isInterface() && munger.getMunger().needsAccessToTopmostImplementor()) {
  785. if (!onType.getWorld().getCrosscuttingMembersSet().containsAspect(munger.getAspectType())) {
  786. dangerousInterfaces
  787. .put(onType, "implementors of " + onType + " must be woven by " + munger.getAspectType());
  788. }
  789. }
  790. }
  791. }
  792. }
  793. private boolean doDeclareParents(DeclareParents declareParents, SourceTypeBinding sourceType) {
  794. ContextToken tok = CompilationAndWeavingContext.enteringPhase(CompilationAndWeavingContext.PROCESSING_DECLARE_PARENTS,
  795. sourceType.sourceName);
  796. ResolvedType resolvedSourceType = factory.fromEclipse(sourceType);
  797. List<ResolvedType> newParents = declareParents.findMatchingNewParents(resolvedSourceType, false);
  798. if (!newParents.isEmpty()) {
  799. for (ResolvedType parent : newParents) {
  800. if (dangerousInterfaces.containsKey(parent)) {
  801. ResolvedType onType = factory.fromEclipse(sourceType);
  802. factory.showMessage(IMessage.ERROR, onType + ": " + dangerousInterfaces.get(parent),
  803. onType.getSourceLocation(), null);
  804. }
  805. if (Modifier.isFinal(parent.getModifiers())) {
  806. factory.showMessage(IMessage.ERROR, "cannot extend final class " + parent.getClassName(),
  807. declareParents.getSourceLocation(), null);
  808. } else {
  809. // do not actually do it if the type isn't exposed - this
  810. // will correctly reported as a problem elsewhere
  811. if (!resolvedSourceType.isExposedToWeaver()) {
  812. return false;
  813. }
  814. // AsmRelationshipProvider.getDefault().
  815. // addDeclareParentsRelationship
  816. // (declareParents.getSourceLocation(),
  817. // factory.fromEclipse(sourceType), newParents);
  818. addParent(sourceType, parent);
  819. }
  820. }
  821. CompilationAndWeavingContext.leavingPhase(tok);
  822. return true;
  823. }
  824. CompilationAndWeavingContext.leavingPhase(tok);
  825. return false;
  826. }
  827. private String stringifyTargets(long bits) {
  828. if ((bits & TagBits.AnnotationTargetMASK) == 0) {
  829. return "";
  830. }
  831. Set<String> s = new HashSet<>();
  832. if ((bits & TagBits.AnnotationForAnnotationType) != 0) {
  833. s.add("ANNOTATION_TYPE");
  834. }
  835. if ((bits & TagBits.AnnotationForConstructor) != 0) {
  836. s.add("CONSTRUCTOR");
  837. }
  838. if ((bits & TagBits.AnnotationForField) != 0) {
  839. s.add("FIELD");
  840. }
  841. if ((bits & TagBits.AnnotationForLocalVariable) != 0) {
  842. s.add("LOCAL_VARIABLE");
  843. }
  844. if ((bits & TagBits.AnnotationForMethod) != 0) {
  845. s.add("METHOD");
  846. }
  847. if ((bits & TagBits.AnnotationForPackage) != 0) {
  848. s.add("PACKAGE");
  849. }
  850. if ((bits & TagBits.AnnotationForParameter) != 0) {
  851. s.add("PARAMETER");
  852. }
  853. if ((bits & TagBits.AnnotationForType) != 0) {
  854. s.add("TYPE");
  855. }
  856. StringBuilder sb = new StringBuilder();
  857. sb.append("{");
  858. for (Iterator<String> iter = s.iterator(); iter.hasNext();) {
  859. String element = iter.next();
  860. sb.append(element);
  861. if (iter.hasNext()) {
  862. sb.append(",");
  863. }
  864. }
  865. sb.append("}");
  866. return sb.toString();
  867. }
  868. private boolean doDeclareAnnotations(DeclareAnnotation decA, SourceTypeBinding sourceType, boolean reportProblems) {
  869. ResolvedType rtx = factory.fromEclipse(sourceType);
  870. if (!decA.matches(rtx)) {
  871. return false;
  872. }
  873. if (!rtx.isExposedToWeaver()) {
  874. return false;
  875. }
  876. ContextToken tok = CompilationAndWeavingContext.enteringPhase(CompilationAndWeavingContext.PROCESSING_DECLARE_ANNOTATIONS,
  877. sourceType.sourceName);
  878. // Get the annotation specified in the declare
  879. UnresolvedType aspectType = decA.getAspect();
  880. if (aspectType instanceof ReferenceType) {
  881. ReferenceType rt = (ReferenceType) aspectType;
  882. if (rt.isParameterizedType() || rt.isRawType()) {
  883. aspectType = rt.getGenericType();
  884. }
  885. }
  886. TypeBinding tb = factory.makeTypeBinding(aspectType);
  887. // Hideousness follows:
  888. // There are multiple situations to consider here and they relate to the
  889. // combinations of
  890. // where the annotation is coming from and where the annotation is going
  891. // to be put:
  892. //
  893. // 1. Straight full build, all from source - the annotation is from a
  894. // dec@type and
  895. // is being put on some type. Both types are real SourceTypeBindings.
  896. // WORKS
  897. // 2. Incremental build, changing the affected type - the annotation is
  898. // from a
  899. // dec@type in a BinaryTypeBinding (so has to be accessed via bcel) and
  900. // the
  901. // affected type is a real SourceTypeBinding. Mostly works (pr128665)
  902. // 3. ?
  903. SourceTypeBinding stb = (SourceTypeBinding) tb;
  904. Annotation[] toAdd = null;
  905. long abits = 0;
  906. AbstractMethodDeclaration methodDecl = null;
  907. // Might have to retrieve the annotation through BCEL and construct an
  908. // eclipse one for it.
  909. if (stb instanceof BinaryTypeBinding) {
  910. toAdd = retrieveAnnotationFromBinaryTypeBinding(decA, stb);
  911. if (toAdd != null && toAdd.length > 0 && toAdd[0].resolvedType != null) {
  912. abits = toAdd[0].resolvedType.getAnnotationTagBits();
  913. }
  914. } else if (stb != null) {
  915. // much nicer, its a real SourceTypeBinding so we can stay in
  916. // eclipse land
  917. // if (decA.getAnnotationMethod() != null) {
  918. char[] declareSelector = decA.getAnnotationMethod().toCharArray();
  919. ReferenceBinding rb = stb;
  920. String declaringAspectName = decA.getDeclaringType().getRawName();
  921. while (rb != null && !new String(CharOperation.concatWith(rb.compoundName, '.')).equals(declaringAspectName)) {
  922. rb = rb.superclass();
  923. }
  924. MethodBinding[] mbs = rb.getMethods(declareSelector);
  925. ReferenceBinding declaringBinding = mbs[0].declaringClass;
  926. if (declaringBinding instanceof ParameterizedTypeBinding) {
  927. // Unwrap - this means we don't allow the type of the annotation to be parameterized, may need to revisit that
  928. declaringBinding = ((ParameterizedTypeBinding) declaringBinding).type;
  929. }
  930. if (declaringBinding instanceof BinaryTypeBinding) {
  931. toAdd = retrieveAnnotationFromBinaryTypeBinding(decA, declaringBinding);
  932. if (toAdd != null && toAdd.length > 0 && toAdd[0].resolvedType != null) {
  933. abits = toAdd[0].resolvedType.getAnnotationTagBits();
  934. }
  935. } else {
  936. abits = mbs[0].getAnnotationTagBits(); // ensure resolved
  937. TypeDeclaration typeDecl = ((SourceTypeBinding) declaringBinding).scope.referenceContext;
  938. methodDecl = typeDecl.declarationOf(mbs[0]);
  939. toAdd = methodDecl.annotations; // this is what to add
  940. toAdd[0] = createAnnotationCopy(toAdd[0]);
  941. if (toAdd[0].resolvedType != null) {
  942. abits = toAdd[0].resolvedType.getAnnotationTagBits();
  943. // }
  944. }
  945. }
  946. }
  947. // This happens if there is another error in the code - that should be reported separately
  948. if (toAdd == null || toAdd[0] == null || toAdd[0].type == null) {
  949. CompilationAndWeavingContext.leavingPhase(tok);
  950. return false;
  951. }
  952. if (sourceType instanceof BinaryTypeBinding) {
  953. // In this case we can't access the source type binding to add a new
  954. // annotation, so let's put something
  955. // on the weaver type temporarily
  956. ResolvedType theTargetType = factory.fromEclipse(sourceType);
  957. TypeBinding theAnnotationType = toAdd[0].resolvedType;
  958. // The annotation type may be null if it could not be resolved (eg. the relevant import has not been added yet)
  959. // In this case an error will be put out about the annotation but not if we crash here
  960. if (theAnnotationType == null) {
  961. return false;
  962. }
  963. String sig = new String(theAnnotationType.signature());
  964. UnresolvedType bcelAnnotationType = UnresolvedType.forSignature(sig);
  965. String name = bcelAnnotationType.getName();
  966. if (theTargetType.hasAnnotation(bcelAnnotationType)) {
  967. CompilationAndWeavingContext.leavingPhase(tok);
  968. return false;
  969. }
  970. // FIXME asc tidy up this code that duplicates whats below!
  971. // Simple checks on the bits
  972. boolean giveupnow = false;
  973. if (((abits & TagBits.AnnotationTargetMASK) != 0)) {
  974. if (isAnnotationTargettingSomethingOtherThanAnnotationOrNormal(abits)) {
  975. // error will have been already reported
  976. giveupnow = true;
  977. } else if ((sourceType.isAnnotationType() && (abits & TagBits.AnnotationForAnnotationType) == 0)
  978. || (!sourceType.isAnnotationType() && (abits & TagBits.AnnotationForType) == 0)) {
  979. if (reportProblems) {
  980. if (decA.isExactPattern()) {
  981. factory.showMessage(IMessage.ERROR, WeaverMessages.format(
  982. WeaverMessages.INCORRECT_TARGET_FOR_DECLARE_ANNOTATION, rtx.getName(), toAdd[0].type,
  983. stringifyTargets(abits)), decA.getSourceLocation(), null);
  984. }
  985. // dont put out the lint - the weaving process will do
  986. // that
  987. // else {
  988. // if (factory.getWorld().getLint().
  989. // invalidTargetForAnnotation.isEnabled()) {
  990. // factory.getWorld().getLint().invalidTargetForAnnotation
  991. // .signal(new
  992. // String[]{rtx.getName(),toAdd[0].type.toString(),
  993. // stringifyTargets
  994. // (abits)},decA.getSourceLocation(),null);
  995. // }
  996. // }
  997. }
  998. giveupnow = true;
  999. }
  1000. }
  1001. if (giveupnow) {
  1002. CompilationAndWeavingContext.leavingPhase(tok);
  1003. return false;
  1004. }
  1005. theTargetType.addAnnotation(new BcelAnnotation(new FakeAnnotation(name, sig,
  1006. (abits & TagBits.AnnotationRuntimeRetention) != 0), factory.getWorld()));
  1007. CompilationAndWeavingContext.leavingPhase(tok);
  1008. return true;
  1009. }
  1010. Annotation currentAnnotations[] = sourceType.scope.referenceContext.annotations;
  1011. if (currentAnnotations != null) {
  1012. for (Annotation annotation : currentAnnotations) {
  1013. String a = CharOperation.toString(annotation.type.getTypeName());
  1014. String b = CharOperation.toString(toAdd[0].type.getTypeName());
  1015. // FIXME asc we have a lint for attempting to add an annotation
  1016. // twice to a method,
  1017. // we could put it out here *if* we can resolve the problem of
  1018. // errors coming out
  1019. // multiple times if we have cause to loop through here
  1020. if (a.equals(b)) {
  1021. CompilationAndWeavingContext.leavingPhase(tok);
  1022. return false;
  1023. }
  1024. }
  1025. }
  1026. if (((abits & TagBits.AnnotationTargetMASK) != 0)) {
  1027. if ((abits & (TagBits.AnnotationForAnnotationType | TagBits.AnnotationForType)) == 0) {
  1028. // this means it specifies something other than annotation or
  1029. // normal type - error will have been already reported,
  1030. // just resolution process above
  1031. CompilationAndWeavingContext.leavingPhase(tok);
  1032. return false;
  1033. }
  1034. if ((sourceType.isAnnotationType() && (abits & TagBits.AnnotationForAnnotationType) == 0)
  1035. || (!sourceType.isAnnotationType() && (abits & TagBits.AnnotationForType) == 0)) {
  1036. if (reportProblems) {
  1037. if (decA.isExactPattern()) {
  1038. factory.showMessage(IMessage.ERROR, WeaverMessages.format(
  1039. WeaverMessages.INCORRECT_TARGET_FOR_DECLARE_ANNOTATION, rtx.getName(), toAdd[0].type,
  1040. stringifyTargets(abits)), decA.getSourceLocation(), null);
  1041. }
  1042. // dont put out the lint - the weaving process will do that
  1043. // else {
  1044. // if
  1045. // (factory.getWorld().getLint().invalidTargetForAnnotation
  1046. // .isEnabled()) {
  1047. // factory.getWorld().getLint().invalidTargetForAnnotation.
  1048. // signal(new
  1049. // String[]{rtx.getName(),toAdd[0].type.toString(),
  1050. // stringifyTargets(abits)},decA.getSourceLocation(),null);
  1051. // }
  1052. // }
  1053. }
  1054. CompilationAndWeavingContext.leavingPhase(tok);
  1055. return false;
  1056. }
  1057. }
  1058. // Build a new array of annotations
  1059. // remember the current set (rememberAnnotations only does something the
  1060. // first time it is called for a type)
  1061. sourceType.scope.referenceContext.rememberAnnotations();
  1062. // AsmRelationshipProvider.getDefault().addDeclareAnnotationRelationship(
  1063. // decA.getSourceLocation(), rtx.getSourceLocation());
  1064. final Annotation[] abefore = sourceType.scope.referenceContext.annotations;
  1065. final Annotation[] newset = new Annotation[toAdd.length + (abefore == null ? 0 : abefore.length)];
  1066. System.arraycopy(toAdd, 0, newset, 0, toAdd.length);
  1067. if (abefore != null) {
  1068. System.arraycopy(abefore, 0, newset, toAdd.length, abefore.length);
  1069. }
  1070. sourceType.scope.referenceContext.annotations = newset;
  1071. if ((sourceType.tagBits & TagBits.AnnotationResolved)!=0) {
  1072. sourceType.tagBits = sourceType.tagBits - TagBits.AnnotationResolved;
  1073. }
  1074. CompilationAndWeavingContext.leavingPhase(tok);
  1075. if (factory.pushinCollector != null) {
  1076. factory.pushinCollector.tagAsMunged(sourceType, new CommonPrinter((methodDecl == null ? null : methodDecl.scope))
  1077. .printAnnotation(toAdd[0]).toString());
  1078. }
  1079. return true;
  1080. }
  1081. private Annotation[] retrieveAnnotationFromBinaryTypeBinding(DeclareAnnotation decA, ReferenceBinding declaringBinding) {
  1082. ReferenceType rt = (ReferenceType) factory.fromEclipse(declaringBinding);
  1083. ResolvedMember[] methods = rt.getDeclaredMethods();
  1084. ResolvedMember decaMethod = null;
  1085. String nameToLookFor = decA.getAnnotationMethod();
  1086. for (ResolvedMember method : methods) {
  1087. if (method.getName().equals(nameToLookFor)) {
  1088. decaMethod = method;
  1089. break;
  1090. }
  1091. }
  1092. if (decaMethod != null) { // could assert this ...
  1093. AnnotationAJ[] axs = decaMethod.getAnnotations();
  1094. if (axs != null) { // another error has occurred, dont crash here because of it
  1095. Annotation[] toAdd = new Annotation[1];
  1096. toAdd[0] = createAnnotationFromBcelAnnotation(axs[0], decaMethod.getSourceLocation().getOffset(), factory);
  1097. // BUG BUG BUG - We dont test these abits are correct, in fact
  1098. // we'll be very lucky if they are.
  1099. // What does that mean? It means on an incremental compile you
  1100. // might get away with an
  1101. // annotation that isn't allowed on a type being put on a type.
  1102. // if (toAdd[0].resolvedType != null) {
  1103. // abits = toAdd[0].resolvedType.getAnnotationTagBits();
  1104. // }
  1105. return toAdd;
  1106. }
  1107. }
  1108. return null;
  1109. }
  1110. /**
  1111. * Transform an annotation from its AJ form to an eclipse form. We *DONT* care about the values of the annotation. that is
  1112. * because it is only being stuck on a type during type completion to allow for other constructs (decps, decas) that might be
  1113. * looking for it - when the class actually gets to disk it wont have this new annotation on it and during weave time we will do
  1114. * the right thing copying across values too.
  1115. */
  1116. private static Annotation createAnnotationFromBcelAnnotation(AnnotationAJ annX, int pos, EclipseFactory factory) {
  1117. String name = annX.getTypeName();
  1118. TypeBinding tb = factory.makeTypeBinding(annX.getType());
  1119. // String theName = annX.getSignature().getBaseName();
  1120. char[][] typeName = CharOperation.splitOn('.', name.replace('$', '.').toCharArray()); // pr149293 - not bulletproof...
  1121. long[] positions = new long[typeName.length];
  1122. for (int i = 0; i < positions.length; i++) {
  1123. positions[i] = pos;
  1124. }
  1125. TypeReference annType = new QualifiedTypeReference(typeName, positions);
  1126. NormalAnnotation ann = new NormalAnnotation(annType, pos);
  1127. ann.resolvedType = tb; // yuck - is this OK in all cases?
  1128. // We don't need membervalues...
  1129. // Expression pcExpr = new
  1130. // StringLiteral(pointcutExpression.toCharArray(),pos,pos);
  1131. // MemberValuePair[] mvps = new MemberValuePair[2];
  1132. // mvps[0] = new MemberValuePair("value".toCharArray(),pos,pos,pcExpr);
  1133. // Expression argNamesExpr = new
  1134. // StringLiteral(argNames.toCharArray(),pos,pos);
  1135. // mvps[1] = new
  1136. // MemberValuePair("argNames".toCharArray(),pos,pos,argNamesExpr);
  1137. // ann.memberValuePairs = mvps;
  1138. return ann;
  1139. }
  1140. /**
  1141. * Create a copy of an annotation, not deep but deep enough so we don't copy across fields that will get us into trouble like
  1142. * 'recipient'
  1143. */
  1144. private static Annotation createAnnotationCopy(Annotation ann) {
  1145. NormalAnnotation ann2 = new NormalAnnotation(ann.type, ann.sourceStart);
  1146. ann2.memberValuePairs = ann.memberValuePairs();
  1147. ann2.resolvedType = ann.resolvedType;
  1148. ann2.bits = ann.bits;
  1149. return ann2;
  1150. // String name = annX.getTypeName();
  1151. // TypeBinding tb = factory.makeTypeBinding(annX.getSignature());
  1152. // String theName = annX.getSignature().getBaseName();
  1153. // char[][] typeName =
  1154. // CharOperation.splitOn('.',name.replace('$','.').toCharArray());
  1155. // //pr149293 - not bulletproof...
  1156. // long[] positions = new long[typeName.length];
  1157. // for (int i = 0; i < positions.length; i++) positions[i]=pos;
  1158. // TypeReference annType = new
  1159. // QualifiedTypeReference(typeName,positions);
  1160. // NormalAnnotation ann = new NormalAnnotation(annType,pos);
  1161. // ann.resolvedType=tb; // yuck - is this OK in all cases?
  1162. // // We don't need membervalues...
  1163. // // Expression pcExpr = new
  1164. // StringLiteral(pointcutExpression.toCharArray(),pos,pos);
  1165. // // MemberValuePair[] mvps = new MemberValuePair[2];
  1166. // // mvps[0] = new
  1167. // MemberValuePair("value".toCharArray(),pos,pos,pcExpr);
  1168. // // Expression argNamesExpr = new
  1169. // StringLiteral(argNames.toCharArray(),pos,pos);
  1170. // // mvps[1] = new
  1171. // MemberValuePair("argNames".toCharArray(),pos,pos,argNamesExpr);
  1172. // // ann.memberValuePairs = mvps;
  1173. // return ann;
  1174. }
  1175. private boolean isAnnotationTargettingSomethingOtherThanAnnotationOrNormal(long abits) {
  1176. return (abits & (TagBits.AnnotationForAnnotationType | TagBits.AnnotationForType)) == 0;
  1177. }
  1178. // private void reportDeclareParentsMessage(WeaveMessage.WeaveMessageKind wmk, SourceTypeBinding sourceType, ResolvedType parent) {
  1179. // if (!factory.getWorld().getMessageHandler().isIgnoring(IMessage.WEAVEINFO)) {
  1180. // String filename = new String(sourceType.getFileName());
  1181. //
  1182. // int takefrom = filename.lastIndexOf('/');
  1183. // if (takefrom == -1) {
  1184. // takefrom = filename.lastIndexOf('\\');
  1185. // }
  1186. // filename = filename.substring(takefrom + 1);
  1187. //
  1188. // factory.getWorld()
  1189. // .getMessageHandler()
  1190. // .handleMessage(
  1191. // WeaveMessage.constructWeavingMessage(wmk,
  1192. // new String[] { CharOperation.toString(sourceType.compoundName), filename,
  1193. // parent.getClassName(),
  1194. // getShortname(parent.getSourceLocation().getSourceFile().getPath()) }));
  1195. // }
  1196. // }
  1197. // private String getShortname(String path) {
  1198. // int takefrom = path.lastIndexOf('/');
  1199. // if (takefrom == -1) {
  1200. // takefrom = path.lastIndexOf('\\');
  1201. // }
  1202. // return path.substring(takefrom + 1);
  1203. // }
  1204. private void addParent(SourceTypeBinding sourceType, ResolvedType parent) {
  1205. ReferenceBinding parentBinding = (ReferenceBinding) factory.makeTypeBinding(parent);
  1206. if (parentBinding == null) {
  1207. return; // The parent is missing, it will be reported elsewhere.
  1208. }
  1209. // Due to e37 switching to MethodVerifier15 for everything, it is important added types are correctly
  1210. // raw or not. For example, if Comparable is used in generic form compareTo(T) will be used to check
  1211. // methods against in the verifier rather than compareTo(Object)
  1212. if (!factory.getWorld().isInJava5Mode()) {
  1213. parentBinding = (ReferenceBinding)convertToRawType(parentBinding, false /*do not force conversion of enclosing types*/);
  1214. } else if (sourceType.isGenericType()) {
  1215. RawTypeBinding rawTargetType = (RawTypeBinding)convertToRawType(sourceType, false);
  1216. if (rawTargetType != null) {
  1217. // assert: don't need to 'rememberTypeHierarchy' because the class file is constructed based on the generic type
  1218. if (parentBinding.isClass()) {
  1219. rawTargetType.superclass = parentBinding;
  1220. } else {
  1221. ReferenceBinding[] oldI = rawTargetType.superInterfaces;
  1222. ReferenceBinding[] newI;
  1223. if (oldI == null) {
  1224. newI = new ReferenceBinding[1];
  1225. newI[0] = parentBinding;
  1226. } else {
  1227. int n = oldI.length;
  1228. newI = new ReferenceBinding[n + 1];
  1229. System.arraycopy(oldI, 0, newI, 0, n);
  1230. newI[n] = parentBinding;
  1231. }
  1232. rawTargetType.superInterfaces = newI;
  1233. }
  1234. }
  1235. // TODO what about parameterized types?
  1236. }
  1237. sourceType.rememberTypeHierarchy();
  1238. if (parentBinding.isClass()) {
  1239. sourceType.superclass = parentBinding;
  1240. // this used to be true, but I think I've fixed it now, decp is done
  1241. // at weave time!
  1242. // TAG: WeavingMessage DECLARE PARENTS: EXTENDS
  1243. // Compiler restriction: Can't do EXTENDS at weave time
  1244. // So, only see this message if doing a source compilation
  1245. // reportDeclareParentsMessage(WeaveMessage.
  1246. // WEAVEMESSAGE_DECLAREPARENTSEXTENDS,sourceType,parent);
  1247. } else {
  1248. ReferenceBinding[] oldI = sourceType.superInterfaces;
  1249. ReferenceBinding[] newI;
  1250. if (oldI == null) {
  1251. newI = new ReferenceBinding[1];
  1252. newI[0] = parentBinding;
  1253. } else {
  1254. int n = oldI.length;
  1255. newI = new ReferenceBinding[n + 1];
  1256. System.arraycopy(oldI, 0, newI, 0, n);
  1257. newI[n] = parentBinding;
  1258. }
  1259. sourceType.superInterfaces = newI;
  1260. // warnOnAddedInterface(factory.fromEclipse(sourceType),parent); //
  1261. // now reported at weave time...
  1262. // this used to be true, but I think I've fixed it now, decp is done
  1263. // at weave time!
  1264. // TAG: WeavingMessage DECLARE PARENTS: IMPLEMENTS
  1265. // This message will come out of BcelTypeMunger.munge if doing a
  1266. // binary weave
  1267. // reportDeclareParentsMessage(WeaveMessage.
  1268. // WEAVEMESSAGE_DECLAREPARENTSIMPLEMENTS,sourceType,parent);
  1269. }
  1270. // also add it to the bcel delegate if there is one
  1271. if (sourceType instanceof BinaryTypeBinding) {
  1272. ResolvedType onType = factory.fromEclipse(sourceType);
  1273. ReferenceType rt = (ReferenceType) onType;
  1274. ReferenceTypeDelegate rtd = rt.getDelegate();
  1275. if (rtd instanceof BcelObjectType) {
  1276. if (rt.isRawType()) {
  1277. rt = rt.getGenericType();
  1278. }
  1279. rt.addParent(parent);
  1280. // ((BcelObjectType) rtd).addParent(parent);
  1281. }
  1282. }
  1283. }
  1284. public void warnOnAddedInterface(ResolvedType type, ResolvedType parent) {
  1285. World world = factory.getWorld();
  1286. ResolvedType serializable = world.getCoreType(UnresolvedType.SERIALIZABLE);
  1287. if (serializable.isAssignableFrom(type) && !serializable.isAssignableFrom(parent)
  1288. && !LazyClassGen.hasSerialVersionUIDField(type)) {
  1289. world.getLint().needsSerialVersionUIDField.signal(new String[] { type.getName().toString(),
  1290. "added interface " + parent.getName().toString() }, null, null);
  1291. }
  1292. }
  1293. private final List<BinaryTypeBinding> pendingTypesToFinish = new ArrayList<>();
  1294. boolean inBinaryTypeCreationAndWeaving = false;
  1295. boolean processingTheQueue = false;
  1296. @Override
  1297. public BinaryTypeBinding createBinaryTypeFrom(IBinaryType binaryType, PackageBinding packageBinding,
  1298. boolean needFieldsAndMethods, AccessRestriction accessRestriction) {
  1299. if (inBinaryTypeCreationAndWeaving) {
  1300. BinaryTypeBinding ret = super.createBinaryTypeFrom(binaryType, packageBinding, needFieldsAndMethods, accessRestriction);
  1301. pendingTypesToFinish.add(ret);
  1302. return ret;
  1303. }
  1304. inBinaryTypeCreationAndWeaving = true;
  1305. try {
  1306. BinaryTypeBinding ret = super.createBinaryTypeFrom(binaryType, packageBinding, needFieldsAndMethods, accessRestriction);
  1307. factory.getWorld().validateType(factory.fromBinding(ret));
  1308. // if you need the bytes to pass to validate, here they
  1309. // are:((ClassFileReader)binaryType).getReferenceBytes()
  1310. weaveInterTypeDeclarations(ret);
  1311. return ret;
  1312. } finally {
  1313. inBinaryTypeCreationAndWeaving = false;
  1314. // Start processing the list...
  1315. if (pendingTypesToFinish.size() > 0) {
  1316. processingTheQueue = true;
  1317. while (!pendingTypesToFinish.isEmpty()) {
  1318. BinaryTypeBinding nextVictim = pendingTypesToFinish.remove(0);
  1319. // During this call we may recurse into this method and add
  1320. // more entries to the pendingTypesToFinish list.
  1321. weaveInterTypeDeclarations(nextVictim);
  1322. }
  1323. processingTheQueue = false;
  1324. }
  1325. }
  1326. }
  1327. /**
  1328. * Callback driven when the compiler detects an anonymous type during block resolution. We need to add it to the weaver so that
  1329. * we don't trip up later.
  1330. *
  1331. * @param aBinding
  1332. */
  1333. @Override
  1334. public void anonymousTypeBindingCreated(LocalTypeBinding aBinding) {
  1335. factory.addSourceTypeBinding(aBinding, null);
  1336. }
  1337. @Override
  1338. public void buildTypeBindings(CompilationUnitDeclaration unit, AccessRestriction accessRestriction) {
  1339. if (this.isProcessingAnnotations && hasAspectDeclarations(unit)) {
  1340. throw new SourceTypeCollisionException();
  1341. }
  1342. super.buildTypeBindings(unit, accessRestriction);
  1343. }
  1344. private static boolean hasAspectDeclarations(CompilationUnitDeclaration unit) {
  1345. for (int j = 0; j < unit.types.length; j++) {
  1346. if (unit.types[j] instanceof AspectDeclaration) {
  1347. return true;
  1348. }
  1349. }
  1350. return false;
  1351. }
  1352. @Override
  1353. public void reset() {
  1354. this.factory.cleanup();
  1355. super.reset();
  1356. }
  1357. @Override
  1358. public LookupEnvironment wrapInModuleEnvironment(ModuleBinding moduleBinding) {
  1359. AjLookupEnvironment newAjLookupEnvironment = new AjLookupEnvironment(this, moduleBinding);
  1360. newAjLookupEnvironment.factory = this.factory;
  1361. return newAjLookupEnvironment;
  1362. }
  1363. }
  1364. // commented out, supplied as info on how to manipulate annotations in an
  1365. // eclipse world
  1366. //
  1367. // public void doDeclareAnnotationOnMethods() {
  1368. // Do the declare annotation on fields/methods/ctors
  1369. // Collection daoms = factory.getDeclareAnnotationOnMethods();
  1370. // if (daoms!=null && daoms.size()>0 && !(sourceType instanceof
  1371. // BinaryTypeBinding)) {
  1372. // System.err.println("Going through the methods on "+sourceType.debugName()+
  1373. // " looking for DECA matches");
  1374. // // We better take a look through them...
  1375. // for (Iterator iter = daoms.iterator(); iter.hasNext();) {
  1376. // DeclareAnnotation element = (DeclareAnnotation) iter.next();
  1377. // System.err.println("Looking for anything that might match "+element+" on "+
  1378. // sourceType.debugName()+" "+getType(sourceType.
  1379. // compoundName).debugName()+" "+(sourceType instanceof BinaryTypeBinding));
  1380. //
  1381. // ReferenceBinding rbb = getType(sourceType.compoundName);
  1382. // // fix me if we ever uncomment this code... should iterate the other way
  1383. // round, over the methods then over the decas
  1384. // sourceType.methods();
  1385. // MethodBinding sourceMbs[] = sourceType.methods;
  1386. // for (int i = 0; i < sourceMbs.length; i++) {
  1387. // MethodBinding sourceMb = sourceMbs[i];
  1388. // MethodBinding mbbbb =
  1389. // ((SourceTypeBinding)rbb).getExactMethod(sourceMb.selector
  1390. // ,sourceMb.parameters);
  1391. // boolean isCtor = sourceMb.selector[0]=='<';
  1392. //
  1393. // if ((element.isDeclareAtConstuctor() ^ !isCtor)) {
  1394. // System.err.println("Checking "+sourceMb+" ... declaringclass="+sourceMb.
  1395. // declaringClass.debugName()+" rbb="+rbb.debugName()+" "+
  1396. // sourceMb.declaringClass.equals(rbb));
  1397. //
  1398. // ResolvedMember rm = null;
  1399. // rm = EclipseFactory.makeResolvedMember(mbbbb);
  1400. // if (element.matches(rm,factory.getWorld())) {
  1401. // System.err.println("MATCH");
  1402. //
  1403. // // Determine the set of annotations that are currently on the method
  1404. // ReferenceBinding rb = getType(sourceType.compoundName);
  1405. // // TypeBinding tb = factory.makeTypeBinding(decA.getAspect());
  1406. // MethodBinding mb =
  1407. // ((SourceTypeBinding)rb).getExactMethod(sourceMb.selector,sourceMb
  1408. // .parameters);
  1409. // //long abits = mbs[0].getAnnotationTagBits(); // ensure resolved
  1410. // TypeDeclaration typeDecl =
  1411. // ((SourceTypeBinding)sourceMb.declaringClass).scope.referenceContext;
  1412. // AbstractMethodDeclaration methodDecl = typeDecl.declarationOf(sourceMb);
  1413. // Annotation[] currentlyHas = methodDecl.annotations; // this is what to add
  1414. // //abits = toAdd[0].resolvedType.getAnnotationTagBits();
  1415. //
  1416. // // Determine the annotations to add to that method
  1417. // TypeBinding tb = factory.makeTypeBinding(element.getAspect());
  1418. // MethodBinding[] aspectMbs =
  1419. // ((SourceTypeBinding)tb).getMethods(element.getAnnotationMethod
  1420. // ().toCharArray());
  1421. // long abits = aspectMbs[0].getAnnotationTagBits(); // ensure resolved
  1422. // TypeDeclaration typeDecl2 =
  1423. // ((SourceTypeBinding)aspectMbs[0].declaringClass).scope.referenceContext;
  1424. // AbstractMethodDeclaration methodDecl2 =
  1425. // typeDecl2.declarationOf(aspectMbs[0]);
  1426. // Annotation[] toAdd = methodDecl2.annotations; // this is what to add
  1427. // // abits = toAdd[0].resolvedType.getAnnotationTagBits();
  1428. // System.err.println("Has: "+currentlyHas+" toAdd: "+toAdd);
  1429. //
  1430. // // fix me? should check if it already has the annotation
  1431. // //Annotation abefore[] = sourceType.scope.referenceContext.annotations;
  1432. // Annotation[] newset = new
  1433. // Annotation[(currentlyHas==null?0:currentlyHas.length)+1];
  1434. // System.arraycopy(toAdd,0,newset,0,toAdd.length);
  1435. // if (currentlyHas!=null) {
  1436. // System.arraycopy(currentlyHas,0,newset,1,currentlyHas.length);
  1437. // }
  1438. // methodDecl.annotations = newset;
  1439. // System.err.println("New set on "+CharOperation.charToString(sourceMb.selector)
  1440. // +" is "+newset);
  1441. // } else
  1442. // System.err.println("NO MATCH");
  1443. // }
  1444. // }
  1445. // }
  1446. // }
  1447. // }
  1448. // commented out, supplied as info on how to manipulate annotations in an
  1449. // eclipse world
  1450. //
  1451. // public void doDeclareAnnotationOnFields() {
  1452. // Collection daofs = factory.getDeclareAnnotationOnFields();
  1453. // if (daofs!=null && daofs.size()>0 && !(sourceType instanceof
  1454. // BinaryTypeBinding)) {
  1455. // System.err.println("Going through the fields on "+sourceType.debugName()+
  1456. // " looking for DECA matches");
  1457. // // We better take a look through them...
  1458. // for (Iterator iter = daofs.iterator(); iter.hasNext();) {
  1459. // DeclareAnnotation element = (DeclareAnnotation) iter.next();
  1460. // System.err.println("Processing deca "+element+" on "+sourceType.debugName()+
  1461. // " "+getType(sourceType.compoundName).debugName()+" "
  1462. // +(sourceType instanceof BinaryTypeBinding));
  1463. //
  1464. // ReferenceBinding rbb = getType(sourceType.compoundName);
  1465. // // fix me? should iterate the other way round, over the methods then over the
  1466. // decas
  1467. // sourceType.fields(); // resolve the bloody things
  1468. // FieldBinding sourceFbs[] = sourceType.fields;
  1469. // for (int i = 0; i < sourceFbs.length; i++) {
  1470. // FieldBinding sourceFb = sourceFbs[i];
  1471. // //FieldBinding fbbbb =
  1472. // ((SourceTypeBinding)rbb).getgetExactMethod(sourceMb.selector
  1473. // ,sourceMb.parameters);
  1474. //
  1475. // System.err.println("Checking "+sourceFb+" ... declaringclass="+sourceFb.
  1476. // declaringClass.debugName()+" rbb="+rbb.debugName());
  1477. //
  1478. // ResolvedMember rm = null;
  1479. // rm = EclipseFactory.makeResolvedMember(sourceFb);
  1480. // if (element.matches(rm,factory.getWorld())) {
  1481. // System.err.println("MATCH");
  1482. //
  1483. // // Determine the set of annotations that are currently on the field
  1484. // ReferenceBinding rb = getType(sourceType.compoundName);
  1485. // // TypeBinding tb = factory.makeTypeBinding(decA.getAspect());
  1486. // FieldBinding fb = ((SourceTypeBinding)rb).getField(sourceFb.name,true);
  1487. // //long abits = mbs[0].getAnnotationTagBits(); // ensure resolved
  1488. // TypeDeclaration typeDecl =
  1489. // ((SourceTypeBinding)sourceFb.declaringClass).scope.referenceContext;
  1490. // FieldDeclaration fd = typeDecl.declarationOf(sourceFb);
  1491. // //AbstractMethodDeclaration methodDecl = typeDecl.declarationOf(sourceMb);
  1492. // Annotation[] currentlyHas = fd.annotations; // this is what to add
  1493. // //abits = toAdd[0].resolvedType.getAnnotationTagBits();
  1494. //
  1495. // // Determine the annotations to add to that method
  1496. // TypeBinding tb = factory.makeTypeBinding(element.getAspect());
  1497. // MethodBinding[] aspectMbs =
  1498. // ((SourceTypeBinding)tb).getMethods(element.getAnnotationMethod
  1499. // ().toCharArray());
  1500. // long abits = aspectMbs[0].getAnnotationTagBits(); // ensure resolved
  1501. // TypeDeclaration typeDecl2 =
  1502. // ((SourceTypeBinding)aspectMbs[0].declaringClass).scope.referenceContext;
  1503. // AbstractMethodDeclaration methodDecl2 =
  1504. // typeDecl2.declarationOf(aspectMbs[0]);
  1505. // Annotation[] toAdd = methodDecl2.annotations; // this is what to add
  1506. // // abits = toAdd[0].resolvedType.getAnnotationTagBits();
  1507. // System.err.println("Has: "+currentlyHas+" toAdd: "+toAdd);
  1508. //
  1509. // // fix me? check if it already has the annotation
  1510. //
  1511. //
  1512. // //Annotation abefore[] = sourceType.scope.referenceContext.annotations;
  1513. // Annotation[] newset = new
  1514. // Annotation[(currentlyHas==null?0:currentlyHas.length)+1];
  1515. // System.arraycopy(toAdd,0,newset,0,toAdd.length);
  1516. // if (currentlyHas!=null) {
  1517. // System.arraycopy(currentlyHas,0,newset,1,currentlyHas.length);
  1518. // }
  1519. // fd.annotations = newset;
  1520. // System.err.println("New set on "+CharOperation.charToString(sourceFb.name)+
  1521. // " is "+newset);
  1522. // } else
  1523. // System.err.println("NO MATCH");
  1524. // }
  1525. //
  1526. // }
  1527. // }