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.

BcelClassWeaver.java 124KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232
  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.weaver.bcel;
  13. import java.lang.reflect.Modifier;
  14. import java.util.ArrayList;
  15. import java.util.Collections;
  16. import java.util.Comparator;
  17. import java.util.HashMap;
  18. import java.util.HashSet;
  19. import java.util.Iterator;
  20. import java.util.LinkedHashSet;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Properties;
  24. import java.util.Set;
  25. import org.aspectj.apache.bcel.Constants;
  26. import org.aspectj.apache.bcel.classfile.ConstantPool;
  27. import org.aspectj.apache.bcel.classfile.Method;
  28. import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen;
  29. import org.aspectj.apache.bcel.generic.FieldGen;
  30. import org.aspectj.apache.bcel.generic.FieldInstruction;
  31. import org.aspectj.apache.bcel.generic.Instruction;
  32. import org.aspectj.apache.bcel.generic.InstructionBranch;
  33. import org.aspectj.apache.bcel.generic.InstructionCP;
  34. import org.aspectj.apache.bcel.generic.InstructionConstants;
  35. import org.aspectj.apache.bcel.generic.InstructionFactory;
  36. import org.aspectj.apache.bcel.generic.InstructionHandle;
  37. import org.aspectj.apache.bcel.generic.InstructionLV;
  38. import org.aspectj.apache.bcel.generic.InstructionList;
  39. import org.aspectj.apache.bcel.generic.InstructionSelect;
  40. import org.aspectj.apache.bcel.generic.InstructionTargeter;
  41. import org.aspectj.apache.bcel.generic.InvokeInstruction;
  42. import org.aspectj.apache.bcel.generic.LineNumberTag;
  43. import org.aspectj.apache.bcel.generic.LocalVariableTag;
  44. import org.aspectj.apache.bcel.generic.MULTIANEWARRAY;
  45. import org.aspectj.apache.bcel.generic.MethodGen;
  46. import org.aspectj.apache.bcel.generic.ObjectType;
  47. import org.aspectj.apache.bcel.generic.RET;
  48. import org.aspectj.apache.bcel.generic.Tag;
  49. import org.aspectj.apache.bcel.generic.Type;
  50. import org.aspectj.asm.AsmManager;
  51. import org.aspectj.bridge.IMessage;
  52. import org.aspectj.bridge.ISourceLocation;
  53. import org.aspectj.bridge.Message;
  54. import org.aspectj.bridge.MessageUtil;
  55. import org.aspectj.bridge.WeaveMessage;
  56. import org.aspectj.bridge.context.CompilationAndWeavingContext;
  57. import org.aspectj.bridge.context.ContextToken;
  58. import org.aspectj.util.PartialOrder;
  59. import org.aspectj.weaver.AjAttribute;
  60. import org.aspectj.weaver.AjcMemberMaker;
  61. import org.aspectj.weaver.AnnotationAJ;
  62. import org.aspectj.weaver.BCException;
  63. import org.aspectj.weaver.ConcreteTypeMunger;
  64. import org.aspectj.weaver.IClassWeaver;
  65. import org.aspectj.weaver.IntMap;
  66. import org.aspectj.weaver.Member;
  67. import org.aspectj.weaver.MissingResolvedTypeWithKnownSignature;
  68. import org.aspectj.weaver.NameMangler;
  69. import org.aspectj.weaver.NewConstructorTypeMunger;
  70. import org.aspectj.weaver.NewFieldTypeMunger;
  71. import org.aspectj.weaver.NewMethodTypeMunger;
  72. import org.aspectj.weaver.ResolvedMember;
  73. import org.aspectj.weaver.ResolvedMemberImpl;
  74. import org.aspectj.weaver.ResolvedType;
  75. import org.aspectj.weaver.ResolvedTypeMunger;
  76. import org.aspectj.weaver.Shadow;
  77. import org.aspectj.weaver.ShadowMunger;
  78. import org.aspectj.weaver.UnresolvedType;
  79. import org.aspectj.weaver.UnresolvedTypeVariableReferenceType;
  80. import org.aspectj.weaver.WeaverStateInfo;
  81. import org.aspectj.weaver.World;
  82. import org.aspectj.weaver.model.AsmRelationshipProvider;
  83. import org.aspectj.weaver.patterns.DeclareAnnotation;
  84. import org.aspectj.weaver.patterns.ExactTypePattern;
  85. import org.aspectj.weaver.tools.Trace;
  86. import org.aspectj.weaver.tools.TraceFactory;
  87. class BcelClassWeaver implements IClassWeaver {
  88. private static Trace trace = TraceFactory.getTraceFactory().getTrace(BcelClassWeaver.class);
  89. public static boolean weave(BcelWorld world, LazyClassGen clazz, List<ShadowMunger> shadowMungers,
  90. List<ConcreteTypeMunger> typeMungers, List<ConcreteTypeMunger> lateTypeMungers, boolean inReweavableMode) {
  91. BcelClassWeaver classWeaver = new BcelClassWeaver(world, clazz, shadowMungers, typeMungers, lateTypeMungers);
  92. classWeaver.setReweavableMode(inReweavableMode);
  93. boolean b = classWeaver.weave();
  94. return b;
  95. }
  96. // --------------------------------------------
  97. private final LazyClassGen clazz;
  98. private final List<ShadowMunger> shadowMungers;
  99. private final List<ConcreteTypeMunger> typeMungers;
  100. private final List<ConcreteTypeMunger> lateTypeMungers;
  101. private List<ShadowMunger>[] indexedShadowMungers;
  102. private boolean canMatchBodyShadows = false;
  103. private final BcelObjectType ty; // alias of clazz.getType()
  104. private final BcelWorld world; // alias of ty.getWorld()
  105. private final ConstantPool cpg; // alias of clazz.getConstantPoolGen()
  106. private final InstructionFactory fact; // alias of clazz.getFactory();
  107. private final List<LazyMethodGen> addedLazyMethodGens = new ArrayList<LazyMethodGen>();
  108. private final Set<ResolvedMember> addedDispatchTargets = new HashSet<ResolvedMember>();
  109. private boolean inReweavableMode = false;
  110. private List<IfaceInitList> addedSuperInitializersAsList = null;
  111. private final Map<ResolvedType, IfaceInitList> addedSuperInitializers = new HashMap<ResolvedType, IfaceInitList>();
  112. private final List<ConcreteTypeMunger> addedThisInitializers = new ArrayList<ConcreteTypeMunger>();
  113. private final List<ConcreteTypeMunger> addedClassInitializers = new ArrayList<ConcreteTypeMunger>();
  114. private final Map<ResolvedMember, ResolvedType[]> mapToAnnotations = new HashMap<ResolvedMember, ResolvedType[]>();
  115. // private BcelShadow clinitShadow = null;
  116. /**
  117. * This holds the initialization and pre-initialization shadows for this class that were actually matched by mungers (if no
  118. * match, then we don't even create the shadows really).
  119. */
  120. private final List<BcelShadow> initializationShadows = new ArrayList<BcelShadow>();
  121. private BcelClassWeaver(BcelWorld world, LazyClassGen clazz, List<ShadowMunger> shadowMungers,
  122. List<ConcreteTypeMunger> typeMungers, List<ConcreteTypeMunger> lateTypeMungers) {
  123. super();
  124. this.world = world;
  125. this.clazz = clazz;
  126. this.shadowMungers = shadowMungers;
  127. this.typeMungers = typeMungers;
  128. this.lateTypeMungers = lateTypeMungers;
  129. this.ty = clazz.getBcelObjectType();
  130. this.cpg = clazz.getConstantPool();
  131. this.fact = clazz.getFactory();
  132. indexShadowMungers();
  133. initializeSuperInitializerMap(ty.getResolvedTypeX());
  134. if (!checkedXsetForLowLevelContextCapturing) {
  135. Properties p = world.getExtraConfiguration();
  136. if (p != null) {
  137. String s = p.getProperty(World.xsetCAPTURE_ALL_CONTEXT, "false");
  138. captureLowLevelContext = s.equalsIgnoreCase("true");
  139. if (captureLowLevelContext) {
  140. world.getMessageHandler().handleMessage(
  141. MessageUtil.info("[" + World.xsetCAPTURE_ALL_CONTEXT
  142. + "=true] Enabling collection of low level context for debug/crash messages"));
  143. }
  144. }
  145. checkedXsetForLowLevelContextCapturing = true;
  146. }
  147. }
  148. private boolean canMatch(Shadow.Kind kind) {
  149. return indexedShadowMungers[kind.getKey()] != null;
  150. }
  151. // private void fastMatchShadowMungers(List shadowMungers, ArrayList
  152. // mungers, Kind kind) {
  153. // FastMatchInfo info = new FastMatchInfo(clazz.getType(), kind);
  154. // for (Iterator i = shadowMungers.iterator(); i.hasNext();) {
  155. // ShadowMunger munger = (ShadowMunger) i.next();
  156. // FuzzyBoolean fb = munger.getPointcut().fastMatch(info);
  157. // WeaverMetrics.recordFastMatchResult(fb);// Could pass:
  158. // munger.getPointcut().toString()
  159. // if (fb.maybeTrue()) mungers.add(munger);
  160. // }
  161. // }
  162. private void initializeSuperInitializerMap(ResolvedType child) {
  163. ResolvedType[] superInterfaces = child.getDeclaredInterfaces();
  164. for (int i = 0, len = superInterfaces.length; i < len; i++) {
  165. if (ty.getResolvedTypeX().isTopmostImplementor(superInterfaces[i])) {
  166. if (addSuperInitializer(superInterfaces[i])) {
  167. initializeSuperInitializerMap(superInterfaces[i]);
  168. }
  169. }
  170. }
  171. }
  172. /**
  173. * Process the shadow mungers into array 'buckets', each bucket represents a shadow kind and contains a list of shadowmungers
  174. * that could potentially apply at that shadow kind.
  175. */
  176. private void indexShadowMungers() {
  177. // beware the annoying property that SHADOW_KINDS[i].getKey == (i+1) !
  178. indexedShadowMungers = new List[Shadow.MAX_SHADOW_KIND + 1];
  179. for (ShadowMunger shadowMunger : shadowMungers) {
  180. int couldMatchKinds = shadowMunger.getPointcut().couldMatchKinds();
  181. for (Shadow.Kind kind : Shadow.SHADOW_KINDS) {
  182. if (kind.isSet(couldMatchKinds)) {
  183. byte k = kind.getKey();
  184. if (indexedShadowMungers[k] == null) {
  185. indexedShadowMungers[k] = new ArrayList<ShadowMunger>();
  186. if (!kind.isEnclosingKind()) {
  187. canMatchBodyShadows = true;
  188. }
  189. }
  190. indexedShadowMungers[k].add(shadowMunger);
  191. }
  192. }
  193. }
  194. }
  195. private boolean addSuperInitializer(ResolvedType onType) {
  196. if (onType.isRawType() || onType.isParameterizedType()) {
  197. onType = onType.getGenericType();
  198. }
  199. IfaceInitList l = addedSuperInitializers.get(onType);
  200. if (l != null) {
  201. return false;
  202. }
  203. l = new IfaceInitList(onType);
  204. addedSuperInitializers.put(onType, l);
  205. return true;
  206. }
  207. public void addInitializer(ConcreteTypeMunger cm) {
  208. NewFieldTypeMunger m = (NewFieldTypeMunger) cm.getMunger();
  209. ResolvedType onType = m.getSignature().getDeclaringType().resolve(world);
  210. if (onType.isRawType()) {
  211. onType = onType.getGenericType();
  212. }
  213. if (Modifier.isStatic(m.getSignature().getModifiers())) {
  214. addedClassInitializers.add(cm);
  215. } else {
  216. if (onType == ty.getResolvedTypeX()) {
  217. addedThisInitializers.add(cm);
  218. } else {
  219. IfaceInitList l = addedSuperInitializers.get(onType);
  220. l.list.add(cm);
  221. }
  222. }
  223. }
  224. private static class IfaceInitList implements PartialOrder.PartialComparable {
  225. final ResolvedType onType;
  226. List<ConcreteTypeMunger> list = new ArrayList<ConcreteTypeMunger>();
  227. IfaceInitList(ResolvedType onType) {
  228. this.onType = onType;
  229. }
  230. public int compareTo(Object other) {
  231. IfaceInitList o = (IfaceInitList) other;
  232. if (onType.isAssignableFrom(o.onType)) {
  233. return +1;
  234. } else if (o.onType.isAssignableFrom(onType)) {
  235. return -1;
  236. } else {
  237. return 0;
  238. }
  239. }
  240. public int fallbackCompareTo(Object other) {
  241. return 0;
  242. }
  243. }
  244. // XXX this is being called, but the result doesn't seem to be being used
  245. public boolean addDispatchTarget(ResolvedMember m) {
  246. return addedDispatchTargets.add(m);
  247. }
  248. public void addLazyMethodGen(LazyMethodGen gen) {
  249. addedLazyMethodGens.add(gen);
  250. }
  251. public void addOrReplaceLazyMethodGen(LazyMethodGen mg) {
  252. if (alreadyDefined(clazz, mg)) {
  253. return;
  254. }
  255. for (Iterator<LazyMethodGen> i = addedLazyMethodGens.iterator(); i.hasNext();) {
  256. LazyMethodGen existing = i.next();
  257. if (signaturesMatch(mg, existing)) {
  258. if (existing.definingType == null) {
  259. // this means existing was introduced on the class itself
  260. return;
  261. } else if (mg.definingType.isAssignableFrom(existing.definingType)) {
  262. // existing is mg's subtype and dominates mg
  263. return;
  264. } else if (existing.definingType.isAssignableFrom(mg.definingType)) {
  265. // mg is existing's subtype and dominates existing
  266. i.remove();
  267. addedLazyMethodGens.add(mg);
  268. return;
  269. } else {
  270. throw new BCException("conflict between: " + mg + " and " + existing);
  271. }
  272. }
  273. }
  274. addedLazyMethodGens.add(mg);
  275. }
  276. private boolean alreadyDefined(LazyClassGen clazz, LazyMethodGen mg) {
  277. for (Iterator<LazyMethodGen> i = clazz.getMethodGens().iterator(); i.hasNext();) {
  278. LazyMethodGen existing = i.next();
  279. if (signaturesMatch(mg, existing)) {
  280. if (!mg.isAbstract() && existing.isAbstract()) {
  281. i.remove();
  282. return false;
  283. }
  284. return true;
  285. }
  286. }
  287. return false;
  288. }
  289. private boolean signaturesMatch(LazyMethodGen mg, LazyMethodGen existing) {
  290. return mg.getName().equals(existing.getName()) && mg.getSignature().equals(existing.getSignature());
  291. }
  292. protected static LazyMethodGen makeBridgeMethod(LazyClassGen gen, ResolvedMember member) {
  293. // remove abstract modifier
  294. int mods = member.getModifiers();
  295. if (Modifier.isAbstract(mods)) {
  296. mods = mods - Modifier.ABSTRACT;
  297. }
  298. LazyMethodGen ret = new LazyMethodGen(mods, BcelWorld.makeBcelType(member.getReturnType()), member.getName(),
  299. BcelWorld.makeBcelTypes(member.getParameterTypes()), UnresolvedType.getNames(member.getExceptions()), gen);
  300. // 43972 : Static crosscutting makes interfaces unusable for javac
  301. // ret.makeSynthetic();
  302. return ret;
  303. }
  304. /**
  305. * Create a single bridge method called 'theBridgeMethod' that bridges to 'whatToBridgeTo'
  306. */
  307. private static void createBridgeMethod(BcelWorld world, LazyMethodGen whatToBridgeToMethodGen, LazyClassGen clazz,
  308. ResolvedMember theBridgeMethod) {
  309. InstructionList body;
  310. InstructionFactory fact;
  311. int pos = 0;
  312. ResolvedMember whatToBridgeTo = whatToBridgeToMethodGen.getMemberView();
  313. if (whatToBridgeTo == null) {
  314. whatToBridgeTo = new ResolvedMemberImpl(Member.METHOD, whatToBridgeToMethodGen.getEnclosingClass().getType(),
  315. whatToBridgeToMethodGen.getAccessFlags(), whatToBridgeToMethodGen.getName(),
  316. whatToBridgeToMethodGen.getSignature());
  317. }
  318. // The bridge method in this type will have the same signature as the one in the supertype
  319. LazyMethodGen bridgeMethod = makeBridgeMethod(clazz, theBridgeMethod);
  320. int newflags = bridgeMethod.getAccessFlags() | 0x00000040;// BRIDGE = 0x00000040
  321. if ((newflags & 0x00000100) != 0) {
  322. newflags = newflags - 0x100;// NATIVE = 0x00000100 - need to clear it
  323. }
  324. bridgeMethod.setAccessFlags(newflags);
  325. Type returnType = BcelWorld.makeBcelType(theBridgeMethod.getReturnType());
  326. Type[] paramTypes = BcelWorld.makeBcelTypes(theBridgeMethod.getParameterTypes());
  327. Type[] newParamTypes = whatToBridgeToMethodGen.getArgumentTypes();
  328. body = bridgeMethod.getBody();
  329. fact = clazz.getFactory();
  330. if (!whatToBridgeToMethodGen.isStatic()) {
  331. body.append(InstructionFactory.createThis());
  332. pos++;
  333. }
  334. for (int i = 0, len = paramTypes.length; i < len; i++) {
  335. Type paramType = paramTypes[i];
  336. body.append(InstructionFactory.createLoad(paramType, pos));
  337. if (!newParamTypes[i].equals(paramTypes[i])) {
  338. if (world.forDEBUG_bridgingCode) {
  339. System.err.println("Bridging: Cast " + newParamTypes[i] + " from " + paramTypes[i]);
  340. }
  341. body.append(fact.createCast(paramTypes[i], newParamTypes[i]));
  342. }
  343. pos += paramType.getSize();
  344. }
  345. body.append(Utility.createInvoke(fact, world, whatToBridgeTo));
  346. body.append(InstructionFactory.createReturn(returnType));
  347. clazz.addMethodGen(bridgeMethod);
  348. }
  349. /**
  350. * Weave a class and indicate through the return value whether the class was modified.
  351. *
  352. * @return true if the class was modified
  353. */
  354. public boolean weave() {
  355. if (clazz.isWoven() && !clazz.isReweavable()) {
  356. if (world.getLint().nonReweavableTypeEncountered.isEnabled()) {
  357. world.getLint().nonReweavableTypeEncountered.signal(clazz.getType().getName(), ty.getSourceLocation());
  358. }
  359. // Integer uniqueID = new Integer(rm.hashCode() * deca.hashCode());
  360. // if (!reportedProblems.contains(uniqueID)) {
  361. // reportedProblems.add(uniqueID);
  362. // world.getLint().elementAlreadyAnnotated.signal(new String[] { rm.toString(),
  363. // world.showMessage(IMessage.ERROR, WeaverMessages.format(WeaverMessages.ALREADY_WOVEN, clazz.getType().getName()),
  364. // ty.getSourceLocation(), null);
  365. return false;
  366. }
  367. Set<String> aspectsAffectingType = null;
  368. if (inReweavableMode || clazz.getType().isAspect()) {
  369. aspectsAffectingType = new HashSet<String>();
  370. }
  371. boolean isChanged = false;
  372. // we want to "touch" all aspects
  373. if (clazz.getType().isAspect()) {
  374. isChanged = true;
  375. }
  376. WeaverStateInfo typeWeaverState = (world.isOverWeaving() ? getLazyClassGen().getType().getWeaverState() : null);
  377. // start by munging all typeMungers
  378. for (ConcreteTypeMunger o : typeMungers) {
  379. if (!(o instanceof BcelTypeMunger)) {
  380. // ???System.err.println("surprising: " + o);
  381. continue;
  382. }
  383. BcelTypeMunger munger = (BcelTypeMunger) o;
  384. if (typeWeaverState != null && typeWeaverState.isAspectAlreadyApplied(munger.getAspectType())) {
  385. continue;
  386. }
  387. boolean typeMungerAffectedType = munger.munge(this);
  388. if (typeMungerAffectedType) {
  389. isChanged = true;
  390. if (inReweavableMode || clazz.getType().isAspect()) {
  391. aspectsAffectingType.add(munger.getAspectType().getSignature());
  392. }
  393. }
  394. }
  395. // Weave special half type/half shadow mungers...
  396. isChanged = weaveDeclareAtMethodCtor(clazz) || isChanged;
  397. isChanged = weaveDeclareAtField(clazz) || isChanged;
  398. // XXX do major sort of stuff
  399. // sort according to: Major: type hierarchy
  400. // within each list: dominates
  401. // don't forget to sort addedThisInitialiers according to dominates
  402. addedSuperInitializersAsList = new ArrayList<IfaceInitList>(addedSuperInitializers.values());
  403. addedSuperInitializersAsList = PartialOrder.sort(addedSuperInitializersAsList);
  404. if (addedSuperInitializersAsList == null) {
  405. throw new BCException("circularity in inter-types");
  406. }
  407. // this will create a static initializer if there isn't one
  408. // this is in just as bad taste as NOPs
  409. LazyMethodGen staticInit = clazz.getStaticInitializer();
  410. staticInit.getBody().insert(genInitInstructions(addedClassInitializers, true));
  411. // now go through each method, and match against each method. This
  412. // sets up each method's {@link LazyMethodGen#matchedShadows} field,
  413. // and it also possibly adds to {@link #initializationShadows}.
  414. List<LazyMethodGen> methodGens = new ArrayList<LazyMethodGen>(clazz.getMethodGens());
  415. for (LazyMethodGen member : methodGens) {
  416. if (!member.hasBody()) {
  417. continue;
  418. }
  419. if (world.isJoinpointSynchronizationEnabled() && world.areSynchronizationPointcutsInUse()
  420. && member.getMethod().isSynchronized()) {
  421. transformSynchronizedMethod(member);
  422. }
  423. boolean shadowMungerMatched = match(member);
  424. if (shadowMungerMatched) {
  425. // For matching mungers, add their declaring aspects to the list
  426. // that affected this type
  427. if (inReweavableMode || clazz.getType().isAspect()) {
  428. aspectsAffectingType.addAll(findAspectsForMungers(member));
  429. }
  430. isChanged = true;
  431. }
  432. }
  433. // now we weave all but the initialization shadows
  434. for (LazyMethodGen methodGen : methodGens) {
  435. if (!methodGen.hasBody()) {
  436. continue;
  437. }
  438. implement(methodGen);
  439. }
  440. // if we matched any initialization shadows, we inline and weave
  441. if (!initializationShadows.isEmpty()) {
  442. // Repeat next step until nothing left to inline...cant go on
  443. // infinetly as compiler will have detected and reported
  444. // "Recursive constructor invocation"
  445. List<LazyMethodGen> recursiveCtors = new ArrayList<LazyMethodGen>();
  446. while (inlineSelfConstructors(methodGens, recursiveCtors)) {
  447. }
  448. positionAndImplement(initializationShadows);
  449. }
  450. // now proceed with late type mungers
  451. if (lateTypeMungers != null) {
  452. for (Iterator<ConcreteTypeMunger> i = lateTypeMungers.iterator(); i.hasNext();) {
  453. BcelTypeMunger munger = (BcelTypeMunger) i.next();
  454. if (munger.matches(clazz.getType())) {
  455. boolean typeMungerAffectedType = munger.munge(this);
  456. if (typeMungerAffectedType) {
  457. isChanged = true;
  458. if (inReweavableMode || clazz.getType().isAspect()) {
  459. aspectsAffectingType.add(munger.getAspectType().getSignature());
  460. }
  461. }
  462. }
  463. }
  464. }
  465. // FIXME AV - see #75442, for now this is not enough to fix the bug,
  466. // comment that out until we really fix it
  467. // // flush to save some memory
  468. // PerObjectInterfaceTypeMunger.unregisterFromAsAdvisedBy(clazz.getType()
  469. // );
  470. // finally, if we changed, we add in the introduced methods.
  471. if (isChanged) {
  472. clazz.getOrCreateWeaverStateInfo(inReweavableMode);
  473. weaveInAddedMethods(); // FIXME asc are these potentially affected
  474. // by declare annotation?
  475. }
  476. if (inReweavableMode) {
  477. WeaverStateInfo wsi = clazz.getOrCreateWeaverStateInfo(true);
  478. wsi.addAspectsAffectingType(aspectsAffectingType);
  479. wsi.setUnwovenClassFileData(ty.getJavaClass().getBytes());
  480. wsi.setReweavable(true);
  481. } else {
  482. clazz.getOrCreateWeaverStateInfo(false).setReweavable(false);
  483. }
  484. // tidyup, reduce ongoing memory usage of BcelMethods that hang around
  485. for (LazyMethodGen mg : methodGens) {
  486. BcelMethod method = mg.getMemberView();
  487. if (method != null) {
  488. method.wipeJoinpointSignatures();
  489. }
  490. }
  491. return isChanged;
  492. }
  493. // **************************** start of bridge method creation code
  494. // *****************
  495. // FIXASC tidy this lot up !!
  496. // FIXASC refactor into ResolvedType or even ResolvedMember?
  497. /**
  498. * Check if a particular method is overriding another - refactored into this helper so it can be used from multiple places.
  499. */
  500. private static ResolvedMember isOverriding(ResolvedType typeToCheck, ResolvedMember methodThatMightBeGettingOverridden,
  501. String mname, String mrettype, int mmods, boolean inSamePackage, UnresolvedType[] methodParamsArray) {
  502. // Check if we can be an override...
  503. if (Modifier.isStatic(methodThatMightBeGettingOverridden.getModifiers())) {
  504. // we can't be overriding a static method
  505. return null;
  506. }
  507. if (Modifier.isPrivate(methodThatMightBeGettingOverridden.getModifiers())) {
  508. // we can't be overriding a private method
  509. return null;
  510. }
  511. if (!methodThatMightBeGettingOverridden.getName().equals(mname)) {
  512. // names do not match (this will also skip <init> and <clinit>)
  513. return null;
  514. }
  515. if (methodThatMightBeGettingOverridden.getParameterTypes().length != methodParamsArray.length) {
  516. // not the same number of parameters
  517. return null;
  518. }
  519. if (!isVisibilityOverride(mmods, methodThatMightBeGettingOverridden, inSamePackage)) {
  520. // not override from visibility point of view
  521. return null;
  522. }
  523. if (typeToCheck.getWorld().forDEBUG_bridgingCode) {
  524. System.err.println(" Bridging:seriously considering this might be getting overridden '"
  525. + methodThatMightBeGettingOverridden + "'");
  526. }
  527. World w = typeToCheck.getWorld();
  528. // Look at erasures of parameters (List<String> erased is List)
  529. boolean sameParams = true;
  530. for (int p = 0, max = methodThatMightBeGettingOverridden.getParameterTypes().length; p < max; p++) {
  531. UnresolvedType mtmbgoParameter = methodThatMightBeGettingOverridden.getParameterTypes()[p];
  532. UnresolvedType ptype = methodParamsArray[p];
  533. if (mtmbgoParameter.isTypeVariableReference()) {
  534. if (!mtmbgoParameter.resolve(w).isAssignableFrom(ptype.resolve(w))) {
  535. sameParams = false;
  536. }
  537. } else {
  538. // old condition:
  539. boolean b = !methodThatMightBeGettingOverridden.getParameterTypes()[p].getErasureSignature().equals(
  540. methodParamsArray[p].getErasureSignature());
  541. UnresolvedType parameterType = methodThatMightBeGettingOverridden.getParameterTypes()[p];
  542. // Collapse to first bound (isn't that the same as erasure!
  543. if (parameterType instanceof UnresolvedTypeVariableReferenceType) {
  544. parameterType = ((UnresolvedTypeVariableReferenceType) parameterType).getTypeVariable().getFirstBound();
  545. }
  546. if (b) { // !parameterType.resolve(w).equals(parameterType2.resolve(w))) {
  547. sameParams = false;
  548. }
  549. }
  550. //
  551. // if (!ut.getErasureSignature().equals(ut2.getErasureSignature()))
  552. // sameParams = false;
  553. }
  554. // If the 'typeToCheck' represents a parameterized type then the method
  555. // will be the parameterized form of the
  556. // generic method in the generic type. So if the method was 'void
  557. // m(List<T> lt, T t)' and the parameterized type here
  558. // is I<String> then the method we are looking at will be 'void
  559. // m(List<String> lt, String t)' which when erased
  560. // is 'void m(List lt,String t)' - so if the parameters *do* match then
  561. // there is a generic method we are
  562. // overriding
  563. // FIXASC Why bother with the return type? If it is incompatible then the code has other problems!
  564. if (sameParams) {
  565. if (typeToCheck.isParameterizedType()) {
  566. return methodThatMightBeGettingOverridden.getBackingGenericMember();
  567. } else if (!methodThatMightBeGettingOverridden.getReturnType().getErasureSignature().equals(mrettype)) {
  568. // addressing the wierd situation from bug 147801
  569. // just check whether these things are in the right relationship
  570. // for covariance...
  571. ResolvedType superReturn = typeToCheck.getWorld().resolve(
  572. UnresolvedType.forSignature(methodThatMightBeGettingOverridden.getReturnType().getErasureSignature()));
  573. ResolvedType subReturn = typeToCheck.getWorld().resolve(UnresolvedType.forSignature(mrettype));
  574. if (superReturn.isAssignableFrom(subReturn)) {
  575. return methodThatMightBeGettingOverridden;
  576. }
  577. // } else if (typeToCheck.isParameterizedType()) {
  578. // return methodThatMightBeGettingOverridden.getBackingGenericMember();
  579. } else {
  580. return methodThatMightBeGettingOverridden;
  581. }
  582. }
  583. return null;
  584. }
  585. /**
  586. * Looks at the visibility modifiers between two methods, and knows whether they are from classes in the same package, and
  587. * decides whether one overrides the other.
  588. *
  589. * @return true if there is an overrides rather than a 'hides' relationship
  590. */
  591. static boolean isVisibilityOverride(int methodMods, ResolvedMember inheritedMethod, boolean inSamePackage) {
  592. int inheritedModifiers = inheritedMethod.getModifiers();
  593. if (Modifier.isStatic(inheritedModifiers)) {
  594. return false;
  595. }
  596. if (methodMods == inheritedModifiers) {
  597. return true;
  598. }
  599. if (Modifier.isPrivate(inheritedModifiers)) {
  600. return false;
  601. }
  602. boolean isPackageVisible = !Modifier.isPrivate(inheritedModifiers) && !Modifier.isProtected(inheritedModifiers)
  603. && !Modifier.isPublic(inheritedModifiers);
  604. if (isPackageVisible && !inSamePackage) {
  605. return false;
  606. }
  607. return true;
  608. }
  609. /**
  610. * This method recurses up a specified type looking for a method that overrides the one passed in.
  611. *
  612. * @return the method being overridden or null if none is found
  613. */
  614. public static ResolvedMember checkForOverride(ResolvedType typeToCheck, String mname, String mparams, String mrettype,
  615. int mmods, String mpkg, UnresolvedType[] methodParamsArray) {
  616. if (typeToCheck == null) {
  617. return null;
  618. }
  619. if (typeToCheck instanceof MissingResolvedTypeWithKnownSignature) {
  620. return null; // we just can't tell !
  621. }
  622. if (typeToCheck.getWorld().forDEBUG_bridgingCode) {
  623. System.err.println(" Bridging:checking for override of " + mname + " in " + typeToCheck);
  624. }
  625. String packageName = typeToCheck.getPackageName();
  626. if (packageName == null) {
  627. packageName = "";
  628. }
  629. boolean inSamePackage = packageName.equals(mpkg); // used when looking
  630. // at visibility
  631. // rules
  632. ResolvedMember[] methods = typeToCheck.getDeclaredMethods();
  633. for (int ii = 0; ii < methods.length; ii++) {
  634. ResolvedMember methodThatMightBeGettingOverridden = methods[ii]; // the
  635. // method
  636. // we
  637. // are
  638. // going
  639. // to
  640. // check
  641. ResolvedMember isOverriding = isOverriding(typeToCheck, methodThatMightBeGettingOverridden, mname, mrettype, mmods,
  642. inSamePackage, methodParamsArray);
  643. if (isOverriding != null) {
  644. return isOverriding;
  645. }
  646. }
  647. // was: List l = typeToCheck.getInterTypeMungers();
  648. List<ConcreteTypeMunger> l = (typeToCheck.isRawType() ? typeToCheck.getGenericType().getInterTypeMungers() : typeToCheck
  649. .getInterTypeMungers());
  650. for (Iterator<ConcreteTypeMunger> iterator = l.iterator(); iterator.hasNext();) {
  651. ConcreteTypeMunger o = iterator.next();
  652. // FIXME asc if its not a BcelTypeMunger then its an
  653. // EclipseTypeMunger ... do I need to worry about that?
  654. if (o instanceof BcelTypeMunger) {
  655. BcelTypeMunger element = (BcelTypeMunger) o;
  656. if (element.getMunger() instanceof NewMethodTypeMunger) {
  657. if (typeToCheck.getWorld().forDEBUG_bridgingCode) {
  658. System.err.println("Possible ITD candidate " + element);
  659. }
  660. ResolvedMember aMethod = element.getSignature();
  661. ResolvedMember isOverriding = isOverriding(typeToCheck, aMethod, mname, mrettype, mmods, inSamePackage,
  662. methodParamsArray);
  663. if (isOverriding != null) {
  664. return isOverriding;
  665. }
  666. }
  667. }
  668. }
  669. if (typeToCheck.equals(UnresolvedType.OBJECT)) {
  670. return null;
  671. }
  672. ResolvedType superclass = typeToCheck.getSuperclass();
  673. ResolvedMember overriddenMethod = checkForOverride(superclass, mname, mparams, mrettype, mmods, mpkg, methodParamsArray);
  674. if (overriddenMethod != null) {
  675. return overriddenMethod;
  676. }
  677. ResolvedType[] interfaces = typeToCheck.getDeclaredInterfaces();
  678. for (int i = 0; i < interfaces.length; i++) {
  679. ResolvedType anInterface = interfaces[i];
  680. overriddenMethod = checkForOverride(anInterface, mname, mparams, mrettype, mmods, mpkg, methodParamsArray);
  681. if (overriddenMethod != null) {
  682. return overriddenMethod;
  683. }
  684. }
  685. return null;
  686. }
  687. /**
  688. * We need to determine if any methods in this type require bridge methods - this method should only be called if necessary to
  689. * do this calculation, i.e. we are on a 1.5 VM (where covariance/generics exist) and the type hierarchy for the specified class
  690. * has changed (via decp/itd).
  691. *
  692. * See pr108101
  693. */
  694. public static boolean calculateAnyRequiredBridgeMethods(BcelWorld world, LazyClassGen clazz) {
  695. world.ensureAdvancedConfigurationProcessed();
  696. if (!world.isInJava5Mode()) {
  697. return false; // just double check... the caller should have already
  698. }
  699. // verified this
  700. if (clazz.isInterface()) {
  701. return false; // dont bother if we're an interface
  702. }
  703. boolean didSomething = false; // set if we build any bridge methods
  704. // So what methods do we have right now in this class?
  705. List<LazyMethodGen> methods = clazz.getMethodGens();
  706. // Keep a set of all methods from this type - it'll help us to check if
  707. // bridge methods
  708. // have already been created, we don't want to do it twice!
  709. Set<String> methodsSet = new HashSet<String>();
  710. for (int i = 0; i < methods.size(); i++) {
  711. LazyMethodGen aMethod = methods.get(i);
  712. methodsSet.add(aMethod.getName() + aMethod.getSignature()); // e.g.
  713. // "foo(Ljava/lang/String;)V"
  714. }
  715. // Now go through all the methods in this type
  716. for (int i = 0; i < methods.size(); i++) {
  717. // This is the local method that we *might* have to bridge to
  718. LazyMethodGen bridgeToCandidate = methods.get(i);
  719. if (bridgeToCandidate.isBridgeMethod()) {
  720. continue; // Doh!
  721. }
  722. String name = bridgeToCandidate.getName();
  723. String psig = bridgeToCandidate.getParameterSignature();
  724. String rsig = bridgeToCandidate.getReturnType().getSignature();
  725. // if (bridgeToCandidate.isAbstract()) continue;
  726. if (bridgeToCandidate.isStatic()) {
  727. continue; // ignore static methods
  728. }
  729. if (name.endsWith("init>")) {
  730. continue; // Skip constructors and static initializers
  731. }
  732. if (world.forDEBUG_bridgingCode) {
  733. System.err.println("Bridging: Determining if we have to bridge to " + clazz.getName() + "." + name + ""
  734. + bridgeToCandidate.getSignature());
  735. }
  736. // Let's take a look at the superclass
  737. ResolvedType theSuperclass = clazz.getSuperClass();
  738. if (world.forDEBUG_bridgingCode) {
  739. System.err.println("Bridging: Checking supertype " + theSuperclass);
  740. }
  741. String pkgName = clazz.getPackageName();
  742. UnresolvedType[] bm = BcelWorld.fromBcel(bridgeToCandidate.getArgumentTypes());
  743. ResolvedMember overriddenMethod = checkForOverride(theSuperclass, name, psig, rsig, bridgeToCandidate.getAccessFlags(),
  744. pkgName, bm);
  745. if (overriddenMethod != null) {
  746. String key = new StringBuffer().append(overriddenMethod.getName()).append(overriddenMethod.getSignatureErased())
  747. .toString(); // pr237419
  748. boolean alreadyHaveABridgeMethod = methodsSet.contains(key);
  749. if (!alreadyHaveABridgeMethod) {
  750. if (world.forDEBUG_bridgingCode) {
  751. System.err.println("Bridging:bridging to '" + overriddenMethod + "'");
  752. }
  753. createBridgeMethod(world, bridgeToCandidate, clazz, overriddenMethod);
  754. methodsSet.add(key);
  755. didSomething = true;
  756. continue; // look at the next method
  757. }
  758. }
  759. // Check superinterfaces
  760. String[] interfaces = clazz.getInterfaceNames();
  761. for (int j = 0; j < interfaces.length; j++) {
  762. if (world.forDEBUG_bridgingCode) {
  763. System.err.println("Bridging:checking superinterface " + interfaces[j]);
  764. }
  765. ResolvedType interfaceType = world.resolve(interfaces[j]);
  766. overriddenMethod = checkForOverride(interfaceType, name, psig, rsig, bridgeToCandidate.getAccessFlags(),
  767. clazz.getPackageName(), bm);
  768. if (overriddenMethod != null) {
  769. String key = new StringBuffer().append(overriddenMethod.getName())
  770. .append(overriddenMethod.getSignatureErased()).toString(); // pr
  771. // 237419
  772. boolean alreadyHaveABridgeMethod = methodsSet.contains(key);
  773. if (!alreadyHaveABridgeMethod) {
  774. createBridgeMethod(world, bridgeToCandidate, clazz, overriddenMethod);
  775. methodsSet.add(key);
  776. didSomething = true;
  777. if (world.forDEBUG_bridgingCode) {
  778. System.err.println("Bridging:bridging to " + overriddenMethod);
  779. }
  780. continue; // look at the next method
  781. }
  782. }
  783. }
  784. }
  785. return didSomething;
  786. }
  787. // **************************** end of bridge method creation code
  788. // *****************
  789. /**
  790. * Weave any declare @method/@ctor statements into the members of the supplied class
  791. */
  792. private boolean weaveDeclareAtMethodCtor(LazyClassGen clazz) {
  793. List<Integer> reportedProblems = new ArrayList<Integer>();
  794. List<DeclareAnnotation> allDecams = world.getDeclareAnnotationOnMethods();
  795. if (allDecams.isEmpty()) {
  796. return false;
  797. }
  798. boolean isChanged = false;
  799. // deal with ITDs
  800. List<ConcreteTypeMunger> itdMethodsCtors = getITDSubset(clazz, ResolvedTypeMunger.Method);
  801. itdMethodsCtors.addAll(getITDSubset(clazz, ResolvedTypeMunger.Constructor));
  802. if (!itdMethodsCtors.isEmpty()) {
  803. // Can't use the subset called 'decaMs' as it won't be right for
  804. // ITDs...
  805. isChanged = weaveAtMethodOnITDSRepeatedly(allDecams, itdMethodsCtors, reportedProblems);
  806. }
  807. // deal with all the other methods...
  808. List<LazyMethodGen> members = clazz.getMethodGens();
  809. List<DeclareAnnotation> decaMs = getMatchingSubset(allDecams, clazz.getType());
  810. if (decaMs.isEmpty()) {
  811. return false; // nothing to do
  812. }
  813. if (!members.isEmpty()) {
  814. Set<DeclareAnnotation> unusedDecams = new HashSet<DeclareAnnotation>();
  815. unusedDecams.addAll(decaMs);
  816. for (int memberCounter = 0; memberCounter < members.size(); memberCounter++) {
  817. LazyMethodGen mg = members.get(memberCounter);
  818. if (!mg.getName().startsWith(NameMangler.PREFIX)) {
  819. // Single first pass
  820. List<DeclareAnnotation> worthRetrying = new ArrayList<DeclareAnnotation>();
  821. boolean modificationOccured = false;
  822. List<AnnotationGen> annotationsToAdd = null;
  823. for (DeclareAnnotation decaM : decaMs) {
  824. if (decaM.matches(mg.getMemberView(), world)) {
  825. if (doesAlreadyHaveAnnotation(mg.getMemberView(), decaM, reportedProblems)) {
  826. // remove the declare @method since don't want
  827. // an error when the annotation is already there
  828. unusedDecams.remove(decaM);
  829. continue; // skip this one...
  830. }
  831. if (annotationsToAdd == null) {
  832. annotationsToAdd = new ArrayList<AnnotationGen>();
  833. }
  834. AnnotationGen a = ((BcelAnnotation) decaM.getAnnotation()).getBcelAnnotation();
  835. AnnotationGen ag = new AnnotationGen(a, clazz.getConstantPool(), true);
  836. annotationsToAdd.add(ag);
  837. mg.addAnnotation(decaM.getAnnotation());
  838. AsmRelationshipProvider.addDeclareAnnotationMethodRelationship(decaM.getSourceLocation(),
  839. clazz.getName(), mg.getMemberView(), world.getModelAsAsmManager());// getMethod());
  840. reportMethodCtorWeavingMessage(clazz, mg.getMemberView(), decaM, mg.getDeclarationLineNumber());
  841. isChanged = true;
  842. modificationOccured = true;
  843. // remove the declare @method since have matched
  844. // against it
  845. unusedDecams.remove(decaM);
  846. } else {
  847. if (!decaM.isStarredAnnotationPattern()) {
  848. worthRetrying.add(decaM); // an annotation is
  849. // specified that
  850. // might be put on
  851. // by a subsequent
  852. // decaf
  853. }
  854. }
  855. }
  856. // Multiple secondary passes
  857. while (!worthRetrying.isEmpty() && modificationOccured) {
  858. modificationOccured = false;
  859. // lets have another go
  860. List<DeclareAnnotation> forRemoval = new ArrayList<DeclareAnnotation>();
  861. for (DeclareAnnotation decaM : worthRetrying) {
  862. if (decaM.matches(mg.getMemberView(), world)) {
  863. if (doesAlreadyHaveAnnotation(mg.getMemberView(), decaM, reportedProblems)) {
  864. // remove the declare @method since don't
  865. // want an error when
  866. // the annotation is already there
  867. unusedDecams.remove(decaM);
  868. continue; // skip this one...
  869. }
  870. if (annotationsToAdd == null) {
  871. annotationsToAdd = new ArrayList<AnnotationGen>();
  872. }
  873. AnnotationGen a = ((BcelAnnotation) decaM.getAnnotation()).getBcelAnnotation();
  874. // create copy to get the annotation type into the right constant pool
  875. AnnotationGen ag = new AnnotationGen(a, clazz.getConstantPool(), true);
  876. annotationsToAdd.add(ag);
  877. mg.addAnnotation(decaM.getAnnotation());
  878. AsmRelationshipProvider.addDeclareAnnotationMethodRelationship(decaM.getSourceLocation(),
  879. clazz.getName(), mg.getMemberView(), world.getModelAsAsmManager());// getMethod());
  880. isChanged = true;
  881. modificationOccured = true;
  882. forRemoval.add(decaM);
  883. // remove the declare @method since have matched
  884. // against it
  885. unusedDecams.remove(decaM);
  886. }
  887. }
  888. worthRetrying.removeAll(forRemoval);
  889. }
  890. if (annotationsToAdd != null) {
  891. Method oldMethod = mg.getMethod();
  892. MethodGen myGen = new MethodGen(oldMethod, clazz.getClassName(), clazz.getConstantPool(), false);
  893. for (AnnotationGen a : annotationsToAdd) {
  894. myGen.addAnnotation(a);
  895. }
  896. Method newMethod = myGen.getMethod();
  897. members.set(memberCounter, new LazyMethodGen(newMethod, clazz));
  898. }
  899. }
  900. }
  901. checkUnusedDeclareAts(unusedDecams, false);
  902. }
  903. return isChanged;
  904. }
  905. // TAG: WeavingMessage
  906. private void reportMethodCtorWeavingMessage(LazyClassGen clazz, ResolvedMember member, DeclareAnnotation decaM,
  907. int memberLineNumber) {
  908. if (!getWorld().getMessageHandler().isIgnoring(IMessage.WEAVEINFO)) {
  909. StringBuffer parmString = new StringBuffer("(");
  910. UnresolvedType[] paramTypes = member.getParameterTypes();
  911. for (int i = 0; i < paramTypes.length; i++) {
  912. UnresolvedType type = paramTypes[i];
  913. String s = org.aspectj.apache.bcel.classfile.Utility.signatureToString(type.getSignature());
  914. if (s.lastIndexOf('.') != -1) {
  915. s = s.substring(s.lastIndexOf('.') + 1);
  916. }
  917. parmString.append(s);
  918. if ((i + 1) < paramTypes.length) {
  919. parmString.append(",");
  920. }
  921. }
  922. parmString.append(")");
  923. String methodName = member.getName();
  924. StringBuffer sig = new StringBuffer();
  925. sig.append(org.aspectj.apache.bcel.classfile.Utility.accessToString(member.getModifiers()));
  926. sig.append(" ");
  927. sig.append(member.getReturnType().toString());
  928. sig.append(" ");
  929. sig.append(member.getDeclaringType().toString());
  930. sig.append(".");
  931. sig.append(methodName.equals("<init>") ? "new" : methodName);
  932. sig.append(parmString);
  933. StringBuffer loc = new StringBuffer();
  934. if (clazz.getFileName() == null) {
  935. loc.append("no debug info available");
  936. } else {
  937. loc.append(clazz.getFileName());
  938. if (memberLineNumber != -1) {
  939. loc.append(":" + memberLineNumber);
  940. }
  941. }
  942. getWorld().getMessageHandler().handleMessage(
  943. WeaveMessage.constructWeavingMessage(
  944. WeaveMessage.WEAVEMESSAGE_ANNOTATES,
  945. new String[] { sig.toString(), loc.toString(), decaM.getAnnotationString(),
  946. methodName.startsWith("<init>") ? "constructor" : "method", decaM.getAspect().toString(),
  947. Utility.beautifyLocation(decaM.getSourceLocation()) }));
  948. }
  949. }
  950. /**
  951. * Looks through a list of declare annotation statements and only returns those that could possibly match on a field/method/ctor
  952. * in type.
  953. */
  954. private List<DeclareAnnotation> getMatchingSubset(List<DeclareAnnotation> declareAnnotations, ResolvedType type) {
  955. List<DeclareAnnotation> subset = new ArrayList<DeclareAnnotation>();
  956. for (DeclareAnnotation da : declareAnnotations) {
  957. if (da.couldEverMatch(type)) {
  958. subset.add(da);
  959. }
  960. }
  961. return subset;
  962. }
  963. /**
  964. * Get a subset of all the type mungers defined on this aspect
  965. */
  966. private List<ConcreteTypeMunger> getITDSubset(LazyClassGen clazz, ResolvedTypeMunger.Kind wantedKind) {
  967. List<ConcreteTypeMunger> subset = new ArrayList<ConcreteTypeMunger>();
  968. for (ConcreteTypeMunger typeMunger : clazz.getBcelObjectType().getTypeMungers()) {
  969. if (typeMunger.getMunger().getKind() == wantedKind) {
  970. subset.add(typeMunger);
  971. }
  972. }
  973. return subset;
  974. }
  975. public LazyMethodGen locateAnnotationHolderForFieldMunger(LazyClassGen clazz, ConcreteTypeMunger fieldMunger) {
  976. NewFieldTypeMunger newFieldMunger = (NewFieldTypeMunger) fieldMunger.getMunger();
  977. ResolvedMember lookingFor = AjcMemberMaker.interFieldInitializer(newFieldMunger.getSignature(), clazz.getType());
  978. for (LazyMethodGen method : clazz.getMethodGens()) {
  979. if (method.getName().equals(lookingFor.getName())) {
  980. return method;
  981. }
  982. }
  983. return null;
  984. }
  985. // FIXME asc refactor this to neaten it up
  986. public LazyMethodGen locateAnnotationHolderForMethodCtorMunger(LazyClassGen clazz, ConcreteTypeMunger methodCtorMunger) {
  987. ResolvedTypeMunger rtMunger = methodCtorMunger.getMunger();
  988. ResolvedMember lookingFor = null;
  989. if (rtMunger instanceof NewMethodTypeMunger) {
  990. NewMethodTypeMunger nftm = (NewMethodTypeMunger) rtMunger;
  991. lookingFor = AjcMemberMaker.interMethodDispatcher(nftm.getSignature(), methodCtorMunger.getAspectType());
  992. } else if (rtMunger instanceof NewConstructorTypeMunger) {
  993. NewConstructorTypeMunger nftm = (NewConstructorTypeMunger) rtMunger;
  994. lookingFor = AjcMemberMaker.postIntroducedConstructor(methodCtorMunger.getAspectType(), nftm.getSignature()
  995. .getDeclaringType(), nftm.getSignature().getParameterTypes());
  996. } else {
  997. throw new BCException("Not sure what this is: " + methodCtorMunger);
  998. }
  999. String name = lookingFor.getName();
  1000. String paramSignature = lookingFor.getParameterSignature();
  1001. for (LazyMethodGen member : clazz.getMethodGens()) {
  1002. if (member.getName().equals(name) && member.getParameterSignature().equals(paramSignature)) {
  1003. return member;
  1004. }
  1005. }
  1006. return null;
  1007. }
  1008. /**
  1009. * Applies some set of declare @field constructs (List<DeclareAnnotation>) to some bunch of ITDfields (List<BcelTypeMunger>. It
  1010. * will iterate over the fields repeatedly until everything has been applied.
  1011. *
  1012. */
  1013. private boolean weaveAtFieldRepeatedly(List<DeclareAnnotation> decaFs, List<ConcreteTypeMunger> itdFields,
  1014. List<Integer> reportedErrors) {
  1015. boolean isChanged = false;
  1016. for (Iterator<ConcreteTypeMunger> iter = itdFields.iterator(); iter.hasNext();) {
  1017. BcelTypeMunger fieldMunger = (BcelTypeMunger) iter.next();
  1018. ResolvedMember itdIsActually = fieldMunger.getSignature();
  1019. Set<DeclareAnnotation> worthRetrying = new LinkedHashSet<DeclareAnnotation>();
  1020. boolean modificationOccured = false;
  1021. for (Iterator<DeclareAnnotation> iter2 = decaFs.iterator(); iter2.hasNext();) {
  1022. DeclareAnnotation decaF = iter2.next();
  1023. if (decaF.matches(itdIsActually, world)) {
  1024. if (decaF.isRemover()) {
  1025. LazyMethodGen annotationHolder = locateAnnotationHolderForFieldMunger(clazz, fieldMunger);
  1026. if (annotationHolder.hasAnnotation(decaF.getAnnotationType())) {
  1027. isChanged = true;
  1028. // something to remove
  1029. annotationHolder.removeAnnotation(decaF.getAnnotationType());
  1030. AsmRelationshipProvider.addDeclareAnnotationRelationship(world.getModelAsAsmManager(),
  1031. decaF.getSourceLocation(), itdIsActually.getSourceLocation(), true);
  1032. } else {
  1033. worthRetrying.add(decaF);
  1034. }
  1035. } else {
  1036. LazyMethodGen annotationHolder = locateAnnotationHolderForFieldMunger(clazz, fieldMunger);
  1037. if (doesAlreadyHaveAnnotation(annotationHolder, itdIsActually, decaF, reportedErrors)) {
  1038. continue; // skip this one...
  1039. }
  1040. annotationHolder.addAnnotation(decaF.getAnnotation());
  1041. AsmRelationshipProvider.addDeclareAnnotationRelationship(world.getModelAsAsmManager(),
  1042. decaF.getSourceLocation(), itdIsActually.getSourceLocation(), false);
  1043. isChanged = true;
  1044. modificationOccured = true;
  1045. }
  1046. } else {
  1047. if (!decaF.isStarredAnnotationPattern()) {
  1048. worthRetrying.add(decaF); // an annotation is specified
  1049. // that might be put on by a
  1050. // subsequent decaf
  1051. }
  1052. }
  1053. }
  1054. while (!worthRetrying.isEmpty() && modificationOccured) {
  1055. modificationOccured = false;
  1056. List<DeclareAnnotation> forRemoval = new ArrayList<DeclareAnnotation>();
  1057. for (Iterator<DeclareAnnotation> iter2 = worthRetrying.iterator(); iter2.hasNext();) {
  1058. DeclareAnnotation decaF = iter2.next();
  1059. if (decaF.matches(itdIsActually, world)) {
  1060. if (decaF.isRemover()) {
  1061. LazyMethodGen annotationHolder = locateAnnotationHolderForFieldMunger(clazz, fieldMunger);
  1062. if (annotationHolder.hasAnnotation(decaF.getAnnotationType())) {
  1063. isChanged = true;
  1064. // something to remove
  1065. annotationHolder.removeAnnotation(decaF.getAnnotationType());
  1066. AsmRelationshipProvider.addDeclareAnnotationRelationship(world.getModelAsAsmManager(),
  1067. decaF.getSourceLocation(), itdIsActually.getSourceLocation(), true);
  1068. forRemoval.add(decaF);
  1069. }
  1070. } else {
  1071. LazyMethodGen annotationHolder = locateAnnotationHolderForFieldMunger(clazz, fieldMunger);
  1072. if (doesAlreadyHaveAnnotation(annotationHolder, itdIsActually, decaF, reportedErrors)) {
  1073. continue; // skip this one...
  1074. }
  1075. annotationHolder.addAnnotation(decaF.getAnnotation());
  1076. AsmRelationshipProvider.addDeclareAnnotationRelationship(world.getModelAsAsmManager(),
  1077. decaF.getSourceLocation(), itdIsActually.getSourceLocation(), false);
  1078. isChanged = true;
  1079. modificationOccured = true;
  1080. forRemoval.add(decaF);
  1081. }
  1082. }
  1083. }
  1084. worthRetrying.removeAll(forRemoval);
  1085. }
  1086. }
  1087. return isChanged;
  1088. }
  1089. /**
  1090. * Applies some set of declare @method/@ctor constructs (List<DeclareAnnotation>) to some bunch of ITDmembers
  1091. * (List<BcelTypeMunger>. It will iterate over the fields repeatedly until everything has been applied.
  1092. */
  1093. private boolean weaveAtMethodOnITDSRepeatedly(List<DeclareAnnotation> decaMCs,
  1094. List<ConcreteTypeMunger> itdsForMethodAndConstructor, List<Integer> reportedErrors) {
  1095. boolean isChanged = false;
  1096. AsmManager asmManager = world.getModelAsAsmManager();
  1097. for (ConcreteTypeMunger methodctorMunger : itdsForMethodAndConstructor) {
  1098. // for (Iterator iter = itdsForMethodAndConstructor.iterator(); iter.hasNext();) {
  1099. // BcelTypeMunger methodctorMunger = (BcelTypeMunger) iter.next();
  1100. ResolvedMember unMangledInterMethod = methodctorMunger.getSignature();
  1101. List<DeclareAnnotation> worthRetrying = new ArrayList<DeclareAnnotation>();
  1102. boolean modificationOccured = false;
  1103. for (Iterator<DeclareAnnotation> iter2 = decaMCs.iterator(); iter2.hasNext();) {
  1104. DeclareAnnotation decaMC = iter2.next();
  1105. if (decaMC.matches(unMangledInterMethod, world)) {
  1106. LazyMethodGen annotationHolder = locateAnnotationHolderForMethodCtorMunger(clazz, methodctorMunger);
  1107. if (annotationHolder == null
  1108. || doesAlreadyHaveAnnotation(annotationHolder, unMangledInterMethod, decaMC, reportedErrors)) {
  1109. continue; // skip this one...
  1110. }
  1111. annotationHolder.addAnnotation(decaMC.getAnnotation());
  1112. isChanged = true;
  1113. AsmRelationshipProvider.addDeclareAnnotationRelationship(asmManager, decaMC.getSourceLocation(),
  1114. unMangledInterMethod.getSourceLocation(), false);
  1115. reportMethodCtorWeavingMessage(clazz, unMangledInterMethod, decaMC, -1);
  1116. modificationOccured = true;
  1117. } else {
  1118. // If an annotation is specified, it might be added by one of the other declare annotation statements
  1119. if (!decaMC.isStarredAnnotationPattern()) {
  1120. worthRetrying.add(decaMC);
  1121. }
  1122. }
  1123. }
  1124. while (!worthRetrying.isEmpty() && modificationOccured) {
  1125. modificationOccured = false;
  1126. List<DeclareAnnotation> forRemoval = new ArrayList<DeclareAnnotation>();
  1127. for (Iterator<DeclareAnnotation> iter2 = worthRetrying.iterator(); iter2.hasNext();) {
  1128. DeclareAnnotation decaMC = iter2.next();
  1129. if (decaMC.matches(unMangledInterMethod, world)) {
  1130. LazyMethodGen annotationHolder = locateAnnotationHolderForFieldMunger(clazz, methodctorMunger);
  1131. if (doesAlreadyHaveAnnotation(annotationHolder, unMangledInterMethod, decaMC, reportedErrors)) {
  1132. continue; // skip this one...
  1133. }
  1134. annotationHolder.addAnnotation(decaMC.getAnnotation());
  1135. unMangledInterMethod.addAnnotation(decaMC.getAnnotation());
  1136. AsmRelationshipProvider.addDeclareAnnotationRelationship(asmManager, decaMC.getSourceLocation(),
  1137. unMangledInterMethod.getSourceLocation(), false);
  1138. isChanged = true;
  1139. modificationOccured = true;
  1140. forRemoval.add(decaMC);
  1141. }
  1142. worthRetrying.removeAll(forRemoval);
  1143. }
  1144. }
  1145. }
  1146. return isChanged;
  1147. }
  1148. private boolean dontAddTwice(DeclareAnnotation decaF, AnnotationAJ[] dontAddMeTwice) {
  1149. for (AnnotationAJ ann : dontAddMeTwice) {
  1150. if (ann != null && decaF.getAnnotation().getTypeName().equals(ann.getTypeName())) {
  1151. return true;
  1152. }
  1153. }
  1154. return false;
  1155. }
  1156. // BUGWARNING not getting enough warnings out on declare @field ? There is a potential problem here with warnings not
  1157. // coming out - this will occur if they are created on the second iteration round this loop.
  1158. // We currently deactivate error reporting for the second time round. A possible solution is to record what annotations
  1159. // were added by what decafs and check that to see if an error needs to be reported - this would be expensive so lets
  1160. // skip it for now
  1161. /**
  1162. * Weave any declare @field statements into the fields of the supplied class. This will attempt to apply them to the ITDs too.
  1163. *
  1164. * Interesting case relating to public ITDd fields. The annotations are really stored against the interfieldinit method in the
  1165. * aspect, but the public field is placed in the target type and then is processed in the 2nd pass over fields that occurs. I
  1166. * think it would be more expensive to avoid putting the annotation on that inserted public field than just to have it put there
  1167. * as well as on the interfieldinit method.
  1168. */
  1169. private boolean weaveDeclareAtField(LazyClassGen clazz) {
  1170. List<Integer> reportedProblems = new ArrayList<Integer>();
  1171. List<DeclareAnnotation> allDecafs = world.getDeclareAnnotationOnFields();
  1172. if (allDecafs.isEmpty()) {
  1173. return false;
  1174. }
  1175. boolean typeIsChanged = false;
  1176. List<ConcreteTypeMunger> relevantItdFields = getITDSubset(clazz, ResolvedTypeMunger.Field);
  1177. if (relevantItdFields != null) {
  1178. typeIsChanged = weaveAtFieldRepeatedly(allDecafs, relevantItdFields, reportedProblems);
  1179. }
  1180. List<DeclareAnnotation> decafs = getMatchingSubset(allDecafs, clazz.getType());
  1181. if (decafs.isEmpty()) {
  1182. return typeIsChanged;
  1183. }
  1184. List<BcelField> fields = clazz.getFieldGens();
  1185. if (fields != null) {
  1186. Set<DeclareAnnotation> unusedDecafs = new HashSet<DeclareAnnotation>();
  1187. unusedDecafs.addAll(decafs);
  1188. for (BcelField field : fields) {
  1189. if (!field.getName().startsWith(NameMangler.PREFIX)) {
  1190. // Single first pass
  1191. Set<DeclareAnnotation> worthRetrying = new LinkedHashSet<DeclareAnnotation>();
  1192. boolean modificationOccured = false;
  1193. AnnotationAJ[] dontAddMeTwice = field.getAnnotations();
  1194. // go through all the declare @field statements
  1195. for (DeclareAnnotation decaf : decafs) {
  1196. if (decaf.getAnnotation() == null) {
  1197. return false;
  1198. }
  1199. if (decaf.matches(field, world)) {
  1200. if (decaf.isRemover()) {
  1201. AnnotationAJ annotation = decaf.getAnnotation();
  1202. if (field.hasAnnotation(annotation.getType())) {
  1203. // something to remove
  1204. typeIsChanged = true;
  1205. field.removeAnnotation(annotation);
  1206. AsmRelationshipProvider.addDeclareAnnotationFieldRelationship(world.getModelAsAsmManager(),
  1207. decaf.getSourceLocation(), clazz.getName(), field, true);
  1208. reportFieldAnnotationWeavingMessage(clazz, field, decaf, true);
  1209. } else {
  1210. worthRetrying.add(decaf);
  1211. }
  1212. unusedDecafs.remove(decaf);
  1213. } else {
  1214. if (!dontAddTwice(decaf, dontAddMeTwice)) {
  1215. if (doesAlreadyHaveAnnotation(field, decaf, reportedProblems)) {
  1216. // remove the declare @field since don't want an error when the annotation is already there
  1217. unusedDecafs.remove(decaf);
  1218. continue;
  1219. }
  1220. field.addAnnotation(decaf.getAnnotation());
  1221. }
  1222. AsmRelationshipProvider.addDeclareAnnotationFieldRelationship(world.getModelAsAsmManager(),
  1223. decaf.getSourceLocation(), clazz.getName(), field, false);
  1224. reportFieldAnnotationWeavingMessage(clazz, field, decaf, false);
  1225. typeIsChanged = true;
  1226. modificationOccured = true;
  1227. unusedDecafs.remove(decaf);
  1228. }
  1229. } else if (!decaf.isStarredAnnotationPattern() || decaf.isRemover()) {
  1230. worthRetrying.add(decaf); // an annotation is specified that might be put on by a subsequent decaf
  1231. }
  1232. }
  1233. // Multiple secondary passes
  1234. while (!worthRetrying.isEmpty() && modificationOccured) {
  1235. modificationOccured = false;
  1236. // lets have another go with any remaining ones
  1237. List<DeclareAnnotation> forRemoval = new ArrayList<DeclareAnnotation>();
  1238. for (Iterator<DeclareAnnotation> iter = worthRetrying.iterator(); iter.hasNext();) {
  1239. DeclareAnnotation decaF = iter.next();
  1240. if (decaF.matches(field, world)) {
  1241. if (decaF.isRemover()) {
  1242. AnnotationAJ annotation = decaF.getAnnotation();
  1243. if (field.hasAnnotation(annotation.getType())) {
  1244. // something to remove
  1245. typeIsChanged = modificationOccured = true;
  1246. forRemoval.add(decaF);
  1247. field.removeAnnotation(annotation);
  1248. AsmRelationshipProvider.addDeclareAnnotationFieldRelationship(world.getModelAsAsmManager(),
  1249. decaF.getSourceLocation(), clazz.getName(), field, true);
  1250. reportFieldAnnotationWeavingMessage(clazz, field, decaF, true);
  1251. }
  1252. } else {
  1253. // below code is for recursive things
  1254. unusedDecafs.remove(decaF);
  1255. if (doesAlreadyHaveAnnotation(field, decaF, reportedProblems)) {
  1256. continue;
  1257. }
  1258. field.addAnnotation(decaF.getAnnotation());
  1259. AsmRelationshipProvider.addDeclareAnnotationFieldRelationship(world.getModelAsAsmManager(),
  1260. decaF.getSourceLocation(), clazz.getName(), field, false);
  1261. typeIsChanged = modificationOccured = true;
  1262. forRemoval.add(decaF);
  1263. }
  1264. }
  1265. }
  1266. worthRetrying.removeAll(forRemoval);
  1267. }
  1268. }
  1269. }
  1270. checkUnusedDeclareAts(unusedDecafs, true);
  1271. }
  1272. return typeIsChanged;
  1273. }
  1274. // bug 99191 - put out an error message if the type doesn't exist
  1275. /**
  1276. * Report an error if the reason a "declare @method/ctor/field" was not used was because the member specified does not exist.
  1277. * This method is passed some set of declare statements that didn't match and a flag indicating whether the set contains declare @field
  1278. * or declare @method/ctor entries.
  1279. */
  1280. private void checkUnusedDeclareAts(Set<DeclareAnnotation> unusedDecaTs, boolean isDeclareAtField) {
  1281. for (DeclareAnnotation declA : unusedDecaTs) {
  1282. // Error if an exact type pattern was specified
  1283. boolean shouldCheck = declA.isExactPattern() || declA.getSignaturePattern().getExactDeclaringTypes().size() != 0;
  1284. if (shouldCheck && declA.getKind() != DeclareAnnotation.AT_CONSTRUCTOR) {
  1285. if (declA.getSignaturePattern().isMatchOnAnyName()) {
  1286. shouldCheck = false;
  1287. } else {
  1288. List<ExactTypePattern> declaringTypePatterns = declA.getSignaturePattern().getExactDeclaringTypes();
  1289. if (declaringTypePatterns.size() == 0) {
  1290. shouldCheck = false;
  1291. } else {
  1292. for (ExactTypePattern exactTypePattern : declaringTypePatterns) {
  1293. if (exactTypePattern.isIncludeSubtypes()) {
  1294. shouldCheck = false;
  1295. break;
  1296. }
  1297. }
  1298. }
  1299. }
  1300. }
  1301. if (shouldCheck) {
  1302. // Quickly check if an ITD supplies the 'missing' member
  1303. boolean itdMatch = false;
  1304. List<ConcreteTypeMunger> lst = clazz.getType().getInterTypeMungers();
  1305. for (Iterator<ConcreteTypeMunger> iterator = lst.iterator(); iterator.hasNext() && !itdMatch;) {
  1306. ConcreteTypeMunger element = iterator.next();
  1307. if (element.getMunger() instanceof NewFieldTypeMunger) {
  1308. NewFieldTypeMunger nftm = (NewFieldTypeMunger) element.getMunger();
  1309. itdMatch = declA.matches(nftm.getSignature(), world);
  1310. } else if (element.getMunger() instanceof NewMethodTypeMunger) {
  1311. NewMethodTypeMunger nmtm = (NewMethodTypeMunger) element.getMunger();
  1312. itdMatch = declA.matches(nmtm.getSignature(), world);
  1313. } else if (element.getMunger() instanceof NewConstructorTypeMunger) {
  1314. NewConstructorTypeMunger nctm = (NewConstructorTypeMunger) element.getMunger();
  1315. itdMatch = declA.matches(nctm.getSignature(), world);
  1316. }
  1317. }
  1318. if (!itdMatch) {
  1319. IMessage message = null;
  1320. if (isDeclareAtField) {
  1321. message = new Message("The field '" + declA.getSignaturePattern().toString() + "' does not exist",
  1322. declA.getSourceLocation(), true);
  1323. } else {
  1324. message = new Message("The method '" + declA.getSignaturePattern().toString() + "' does not exist",
  1325. declA.getSourceLocation(), true);
  1326. }
  1327. world.getMessageHandler().handleMessage(message);
  1328. }
  1329. }
  1330. }
  1331. }
  1332. // TAG: WeavingMessage
  1333. private void reportFieldAnnotationWeavingMessage(LazyClassGen clazz, BcelField theField, DeclareAnnotation decaf,
  1334. boolean isRemove) {
  1335. if (!getWorld().getMessageHandler().isIgnoring(IMessage.WEAVEINFO)) {
  1336. world.getMessageHandler().handleMessage(
  1337. WeaveMessage.constructWeavingMessage(
  1338. isRemove ? WeaveMessage.WEAVEMESSAGE_REMOVES_ANNOTATION : WeaveMessage.WEAVEMESSAGE_ANNOTATES,
  1339. new String[] { theField.getFieldAsIs().toString() + "' of type '" + clazz.getName(),
  1340. clazz.getFileName(), decaf.getAnnotationString(), "field", decaf.getAspect().toString(),
  1341. Utility.beautifyLocation(decaf.getSourceLocation()) }));
  1342. }
  1343. }
  1344. /**
  1345. * Check if a resolved member (field/method/ctor) already has an annotation, if it does then put out a warning and return true
  1346. */
  1347. private boolean doesAlreadyHaveAnnotation(ResolvedMember rm, DeclareAnnotation deca, List<Integer> reportedProblems) {
  1348. if (rm.hasAnnotation(deca.getAnnotationType())) {
  1349. if (world.getLint().elementAlreadyAnnotated.isEnabled()) {
  1350. Integer uniqueID = new Integer(rm.hashCode() * deca.hashCode());
  1351. if (!reportedProblems.contains(uniqueID)) {
  1352. reportedProblems.add(uniqueID);
  1353. world.getLint().elementAlreadyAnnotated.signal(new String[] { rm.toString(),
  1354. deca.getAnnotationType().toString() }, rm.getSourceLocation(),
  1355. new ISourceLocation[] { deca.getSourceLocation() });
  1356. }
  1357. }
  1358. return true;
  1359. }
  1360. return false;
  1361. }
  1362. private boolean doesAlreadyHaveAnnotation(LazyMethodGen rm, ResolvedMember itdfieldsig, DeclareAnnotation deca,
  1363. List<Integer> reportedProblems) {
  1364. if (rm != null && rm.hasAnnotation(deca.getAnnotationType())) {
  1365. if (world.getLint().elementAlreadyAnnotated.isEnabled()) {
  1366. Integer uniqueID = new Integer(rm.hashCode() * deca.hashCode());
  1367. if (!reportedProblems.contains(uniqueID)) {
  1368. reportedProblems.add(uniqueID);
  1369. reportedProblems.add(new Integer(itdfieldsig.hashCode() * deca.hashCode()));
  1370. world.getLint().elementAlreadyAnnotated.signal(new String[] { itdfieldsig.toString(),
  1371. deca.getAnnotationType().toString() }, rm.getSourceLocation(),
  1372. new ISourceLocation[] { deca.getSourceLocation() });
  1373. }
  1374. }
  1375. return true;
  1376. }
  1377. return false;
  1378. }
  1379. private Set<String> findAspectsForMungers(LazyMethodGen mg) {
  1380. Set<String> aspectsAffectingType = new HashSet<String>();
  1381. for (BcelShadow shadow : mg.matchedShadows) {
  1382. for (ShadowMunger munger : shadow.getMungers()) {
  1383. if (munger instanceof BcelAdvice) {
  1384. BcelAdvice bcelAdvice = (BcelAdvice) munger;
  1385. if (bcelAdvice.getConcreteAspect() != null) {
  1386. aspectsAffectingType.add(bcelAdvice.getConcreteAspect().getSignature());
  1387. }
  1388. } else {
  1389. // It is a 'Checker' - we don't need to remember aspects
  1390. // that only contributed Checkers...
  1391. }
  1392. }
  1393. }
  1394. return aspectsAffectingType;
  1395. }
  1396. private boolean inlineSelfConstructors(List<LazyMethodGen> methodGens, List<LazyMethodGen> recursiveCtors) {
  1397. boolean inlinedSomething = false;
  1398. List<LazyMethodGen> newRecursiveCtors = new ArrayList<LazyMethodGen>();
  1399. for (LazyMethodGen methodGen : methodGens) {
  1400. if (!methodGen.getName().equals("<init>")) {
  1401. continue;
  1402. }
  1403. InstructionHandle ih = findSuperOrThisCall(methodGen);
  1404. if (ih != null && isThisCall(ih)) {
  1405. LazyMethodGen donor = getCalledMethod(ih);
  1406. if (donor.equals(methodGen)) {
  1407. newRecursiveCtors.add(donor);
  1408. } else {
  1409. if (!recursiveCtors.contains(donor)) {
  1410. inlineMethod(donor, methodGen, ih);
  1411. inlinedSomething = true;
  1412. }
  1413. }
  1414. }
  1415. }
  1416. recursiveCtors.addAll(newRecursiveCtors);
  1417. return inlinedSomething;
  1418. }
  1419. private void positionAndImplement(List<BcelShadow> initializationShadows) {
  1420. for (BcelShadow s : initializationShadows) {
  1421. positionInitializationShadow(s);
  1422. // s.getEnclosingMethod().print();
  1423. s.implement();
  1424. }
  1425. }
  1426. private void positionInitializationShadow(BcelShadow s) {
  1427. LazyMethodGen mg = s.getEnclosingMethod();
  1428. InstructionHandle call = findSuperOrThisCall(mg);
  1429. InstructionList body = mg.getBody();
  1430. ShadowRange r = new ShadowRange(body);
  1431. r.associateWithShadow(s);
  1432. if (s.getKind() == Shadow.PreInitialization) {
  1433. // XXX assert first instruction is an ALOAD_0.
  1434. // a pre shadow goes from AFTER the first instruction (which we
  1435. // believe to
  1436. // be an ALOAD_0) to just before the call to super
  1437. r.associateWithTargets(Range.genStart(body, body.getStart().getNext()), Range.genEnd(body, call.getPrev()));
  1438. } else {
  1439. // assert s.getKind() == Shadow.Initialization
  1440. r.associateWithTargets(Range.genStart(body, call.getNext()), Range.genEnd(body));
  1441. }
  1442. }
  1443. private boolean isThisCall(InstructionHandle ih) {
  1444. InvokeInstruction inst = (InvokeInstruction) ih.getInstruction();
  1445. return inst.getClassName(cpg).equals(clazz.getName());
  1446. }
  1447. /**
  1448. * inline a particular call in bytecode.
  1449. *
  1450. * @param donor the method we want to inline
  1451. * @param recipient the method containing the call we want to inline
  1452. * @param call the instructionHandle in recipient's body holding the call we want to inline.
  1453. */
  1454. public static void inlineMethod(LazyMethodGen donor, LazyMethodGen recipient, InstructionHandle call) {
  1455. // assert recipient.contains(call)
  1456. /*
  1457. * Implementation notes:
  1458. *
  1459. * We allocate two slots for every tempvar so we don't screw up longs and doubles which may share space. This could be
  1460. * conservatively avoided (no reference to a long/double instruction, don't do it) or packed later. Right now we don't
  1461. * bother to pack.
  1462. *
  1463. * Allocate a new var for each formal param of the inlined. Fill with stack contents. Then copy the inlined instructions in
  1464. * with the appropriate remap table. Any framelocs used by locals in inlined are reallocated to top of frame,
  1465. */
  1466. final InstructionFactory fact = recipient.getEnclosingClass().getFactory();
  1467. IntMap frameEnv = new IntMap();
  1468. // this also sets up the initial environment
  1469. InstructionList argumentStores = genArgumentStores(donor, recipient, frameEnv, fact);
  1470. InstructionList inlineInstructions = genInlineInstructions(donor, recipient, frameEnv, fact, false);
  1471. inlineInstructions.insert(argumentStores);
  1472. recipient.getBody().append(call, inlineInstructions);
  1473. Utility.deleteInstruction(call, recipient);
  1474. }
  1475. // public BcelVar genTempVar(UnresolvedType typeX) {
  1476. // return new BcelVar(typeX.resolve(world),
  1477. // genTempVarIndex(typeX.getSize()));
  1478. // }
  1479. //
  1480. // private int genTempVarIndex(int size) {
  1481. // return enclosingMethod.allocateLocal(size);
  1482. // }
  1483. /**
  1484. * Input method is a synchronized method, we remove the bit flag for synchronized and then insert a try..finally block
  1485. *
  1486. * Some jumping through firey hoops required - depending on the input code level (1.5 or not) we may or may not be able to use
  1487. * the LDC instruction that takes a class literal (doesnt on <1.5).
  1488. *
  1489. * FIXME asc Before promoting -Xjoinpoints:synchronization to be a standard option, this needs a bunch of tidying up - there is
  1490. * some duplication that can be removed.
  1491. */
  1492. public static void transformSynchronizedMethod(LazyMethodGen synchronizedMethod) {
  1493. if (trace.isTraceEnabled()) {
  1494. trace.enter("transformSynchronizedMethod", synchronizedMethod);
  1495. }
  1496. // System.err.println("DEBUG: Transforming synchronized method: "+
  1497. // synchronizedMethod.getName());
  1498. final InstructionFactory fact = synchronizedMethod.getEnclosingClass().getFactory();
  1499. InstructionList body = synchronizedMethod.getBody();
  1500. InstructionList prepend = new InstructionList();
  1501. Type enclosingClassType = BcelWorld.makeBcelType(synchronizedMethod.getEnclosingClass().getType());
  1502. // STATIC METHOD TRANSFORMATION
  1503. if (synchronizedMethod.isStatic()) {
  1504. // What to do here depends on the level of the class file!
  1505. // LDC can handle class literals in Java5 and above *sigh*
  1506. if (synchronizedMethod.getEnclosingClass().isAtLeastJava5()) {
  1507. // MONITORENTER logic:
  1508. // 0: ldc #2; //class C
  1509. // 2: dup
  1510. // 3: astore_0
  1511. // 4: monitorenter
  1512. int slotForLockObject = synchronizedMethod.allocateLocal(enclosingClassType);
  1513. prepend.append(fact.createConstant(enclosingClassType));
  1514. prepend.append(InstructionFactory.createDup(1));
  1515. prepend.append(InstructionFactory.createStore(enclosingClassType, slotForLockObject));
  1516. prepend.append(InstructionFactory.MONITORENTER);
  1517. // MONITOREXIT logic:
  1518. // We basically need to wrap the code from the method in a
  1519. // finally block that
  1520. // will ensure monitorexit is called. Content on the finally
  1521. // block seems to
  1522. // be always:
  1523. //
  1524. // E1: ALOAD_1
  1525. // MONITOREXIT
  1526. // ATHROW
  1527. //
  1528. // so lets build that:
  1529. InstructionList finallyBlock = new InstructionList();
  1530. finallyBlock.append(InstructionFactory.createLoad(Type.getType(java.lang.Class.class), slotForLockObject));
  1531. finallyBlock.append(InstructionConstants.MONITOREXIT);
  1532. finallyBlock.append(InstructionConstants.ATHROW);
  1533. // finally -> E1
  1534. // | GETSTATIC java.lang.System.out Ljava/io/PrintStream; (line
  1535. // 21)
  1536. // | LDC "hello"
  1537. // | INVOKEVIRTUAL java.io.PrintStream.println
  1538. // (Ljava/lang/String;)V
  1539. // | ALOAD_1 (line 20)
  1540. // | MONITOREXIT
  1541. // finally -> E1
  1542. // GOTO L0
  1543. // finally -> E1
  1544. // | E1: ALOAD_1
  1545. // | MONITOREXIT
  1546. // finally -> E1
  1547. // ATHROW
  1548. // L0: RETURN (line 23)
  1549. // search for 'returns' and make them jump to the
  1550. // aload_<n>,monitorexit
  1551. InstructionHandle walker = body.getStart();
  1552. List<InstructionHandle> rets = new ArrayList<InstructionHandle>();
  1553. while (walker != null) {
  1554. if (walker.getInstruction().isReturnInstruction()) {
  1555. rets.add(walker);
  1556. }
  1557. walker = walker.getNext();
  1558. }
  1559. if (!rets.isEmpty()) {
  1560. // need to ensure targeters for 'return' now instead target
  1561. // the load instruction
  1562. // (so we never jump over the monitorexit logic)
  1563. for (Iterator<InstructionHandle> iter = rets.iterator(); iter.hasNext();) {
  1564. InstructionHandle element = iter.next();
  1565. InstructionList monitorExitBlock = new InstructionList();
  1566. monitorExitBlock.append(InstructionFactory.createLoad(enclosingClassType, slotForLockObject));
  1567. monitorExitBlock.append(InstructionConstants.MONITOREXIT);
  1568. // monitorExitBlock.append(Utility.copyInstruction(element
  1569. // .getInstruction()));
  1570. // element.setInstruction(InstructionFactory.createLoad(
  1571. // classType,slotForThis));
  1572. InstructionHandle monitorExitBlockStart = body.insert(element, monitorExitBlock);
  1573. // now move the targeters from the RET to the start of
  1574. // the monitorexit block
  1575. for (InstructionTargeter targeter : element.getTargetersCopy()) {
  1576. // what kinds are there?
  1577. if (targeter instanceof LocalVariableTag) {
  1578. // ignore
  1579. } else if (targeter instanceof LineNumberTag) {
  1580. // ignore
  1581. // } else if (targeter instanceof
  1582. // InstructionBranch &&
  1583. // ((InstructionBranch)targeter).isGoto()) {
  1584. // // move it...
  1585. // targeter.updateTarget(element,
  1586. // monitorExitBlockStart);
  1587. } else if (targeter instanceof InstructionBranch) {
  1588. // move it
  1589. targeter.updateTarget(element, monitorExitBlockStart);
  1590. } else {
  1591. throw new BCException("Unexpected targeter encountered during transform: " + targeter);
  1592. }
  1593. }
  1594. }
  1595. }
  1596. // now the magic, putting the finally block around the code
  1597. InstructionHandle finallyStart = finallyBlock.getStart();
  1598. InstructionHandle tryPosition = body.getStart();
  1599. InstructionHandle catchPosition = body.getEnd();
  1600. body.insert(body.getStart(), prepend); // now we can put the
  1601. // monitorenter stuff on
  1602. synchronizedMethod.getBody().append(finallyBlock);
  1603. synchronizedMethod.addExceptionHandler(tryPosition, catchPosition, finallyStart, null/* ==finally */, false);
  1604. synchronizedMethod.addExceptionHandler(finallyStart, finallyStart.getNext(), finallyStart, null, false);
  1605. } else {
  1606. // TRANSFORMING STATIC METHOD ON PRE JAVA5
  1607. // Hideous nightmare, class literal references prior to Java5
  1608. // YIKES! this is just the code for MONITORENTER !
  1609. // 0: getstatic #59; //Field class$1:Ljava/lang/Class;
  1610. // 3: dup
  1611. // 4: ifnonnull 32
  1612. // 7: pop
  1613. // try
  1614. // 8: ldc #61; //String java.lang.String
  1615. // 10: invokestatic #44; //Method
  1616. // java/lang/Class.forName:(Ljava/lang/String;)Ljava/lang/Class;
  1617. // 13: dup
  1618. // catch
  1619. // 14: putstatic #59; //Field class$1:Ljava/lang/Class;
  1620. // 17: goto 32
  1621. // 20: new #46; //class java/lang/NoClassDefFoundError
  1622. // 23: dup_x1
  1623. // 24: swap
  1624. // 25: invokevirtual #52; //Method
  1625. // java/lang/Throwable.getMessage:()Ljava/lang/String;
  1626. // 28: invokespecial #54; //Method
  1627. // java/lang/NoClassDefFoundError."<init>":(Ljava/lang/String;)V
  1628. // 31: athrow
  1629. // 32: dup <-- partTwo (branch target)
  1630. // 33: astore_0
  1631. // 34: monitorenter
  1632. //
  1633. // plus exceptiontable entry!
  1634. // 8 13 20 Class java/lang/ClassNotFoundException
  1635. Type classType = BcelWorld.makeBcelType(synchronizedMethod.getEnclosingClass().getType());
  1636. Type clazzType = Type.getType(Class.class);
  1637. InstructionList parttwo = new InstructionList();
  1638. parttwo.append(InstructionFactory.createDup(1));
  1639. int slotForThis = synchronizedMethod.allocateLocal(classType);
  1640. parttwo.append(InstructionFactory.createStore(clazzType, slotForThis)); // ? should be the real type ? String or
  1641. // something?
  1642. parttwo.append(InstructionFactory.MONITORENTER);
  1643. String fieldname = synchronizedMethod.getEnclosingClass().allocateField("class$");
  1644. FieldGen f = new FieldGen(Modifier.STATIC | Modifier.PRIVATE, Type.getType(Class.class), fieldname,
  1645. synchronizedMethod.getEnclosingClass().getConstantPool());
  1646. synchronizedMethod.getEnclosingClass().addField(f, null);
  1647. // 10: invokestatic #44; //Method
  1648. // java/lang/Class.forName:(Ljava/lang/String;)Ljava/lang/Class;
  1649. // 13: dup
  1650. // 14: putstatic #59; //Field class$1:Ljava/lang/Class;
  1651. // 17: goto 32
  1652. // 20: new #46; //class java/lang/NoClassDefFoundError
  1653. // 23: dup_x1
  1654. // 24: swap
  1655. // 25: invokevirtual #52; //Method
  1656. // java/lang/Throwable.getMessage:()Ljava/lang/String;
  1657. // 28: invokespecial #54; //Method
  1658. // java/lang/NoClassDefFoundError."<init>":(Ljava/lang/String;)V
  1659. // 31: athrow
  1660. String name = synchronizedMethod.getEnclosingClass().getName();
  1661. prepend.append(fact.createGetStatic(name, fieldname, Type.getType(Class.class)));
  1662. prepend.append(InstructionFactory.createDup(1));
  1663. prepend.append(InstructionFactory.createBranchInstruction(Constants.IFNONNULL, parttwo.getStart()));
  1664. prepend.append(InstructionFactory.POP);
  1665. prepend.append(fact.createConstant(name));
  1666. InstructionHandle tryInstruction = prepend.getEnd();
  1667. prepend.append(fact.createInvoke("java.lang.Class", "forName", clazzType,
  1668. new Type[] { Type.getType(String.class) }, Constants.INVOKESTATIC));
  1669. InstructionHandle catchInstruction = prepend.getEnd();
  1670. prepend.append(InstructionFactory.createDup(1));
  1671. prepend.append(fact.createPutStatic(synchronizedMethod.getEnclosingClass().getType().getName(), fieldname,
  1672. Type.getType(Class.class)));
  1673. prepend.append(InstructionFactory.createBranchInstruction(Constants.GOTO, parttwo.getStart()));
  1674. // start of catch block
  1675. InstructionList catchBlockForLiteralLoadingFail = new InstructionList();
  1676. catchBlockForLiteralLoadingFail.append(fact.createNew((ObjectType) Type.getType(NoClassDefFoundError.class)));
  1677. catchBlockForLiteralLoadingFail.append(InstructionFactory.createDup_1(1));
  1678. catchBlockForLiteralLoadingFail.append(InstructionFactory.SWAP);
  1679. catchBlockForLiteralLoadingFail.append(fact.createInvoke("java.lang.Throwable", "getMessage",
  1680. Type.getType(String.class), new Type[] {}, Constants.INVOKEVIRTUAL));
  1681. catchBlockForLiteralLoadingFail.append(fact.createInvoke("java.lang.NoClassDefFoundError", "<init>", Type.VOID,
  1682. new Type[] { Type.getType(String.class) }, Constants.INVOKESPECIAL));
  1683. catchBlockForLiteralLoadingFail.append(InstructionFactory.ATHROW);
  1684. InstructionHandle catchBlockStart = catchBlockForLiteralLoadingFail.getStart();
  1685. prepend.append(catchBlockForLiteralLoadingFail);
  1686. prepend.append(parttwo);
  1687. // MONITORENTER
  1688. // pseudocode: load up 'this' (var0), dup it, store it in a new
  1689. // local var (for use with monitorexit) and call
  1690. // monitorenter:
  1691. // ALOAD_0, DUP, ASTORE_<n>, MONITORENTER
  1692. // prepend.append(InstructionFactory.createLoad(classType,0));
  1693. // prepend.append(InstructionFactory.createDup(1));
  1694. // int slotForThis =
  1695. // synchronizedMethod.allocateLocal(classType);
  1696. // prepend.append(InstructionFactory.createStore(classType,
  1697. // slotForThis));
  1698. // prepend.append(InstructionFactory.MONITORENTER);
  1699. // MONITOREXIT
  1700. // here be dragons
  1701. // We basically need to wrap the code from the method in a
  1702. // finally block that
  1703. // will ensure monitorexit is called. Content on the finally
  1704. // block seems to
  1705. // be always:
  1706. //
  1707. // E1: ALOAD_1
  1708. // MONITOREXIT
  1709. // ATHROW
  1710. //
  1711. // so lets build that:
  1712. InstructionList finallyBlock = new InstructionList();
  1713. finallyBlock.append(InstructionFactory.createLoad(Type.getType(java.lang.Class.class), slotForThis));
  1714. finallyBlock.append(InstructionConstants.MONITOREXIT);
  1715. finallyBlock.append(InstructionConstants.ATHROW);
  1716. // finally -> E1
  1717. // | GETSTATIC java.lang.System.out Ljava/io/PrintStream; (line
  1718. // 21)
  1719. // | LDC "hello"
  1720. // | INVOKEVIRTUAL java.io.PrintStream.println
  1721. // (Ljava/lang/String;)V
  1722. // | ALOAD_1 (line 20)
  1723. // | MONITOREXIT
  1724. // finally -> E1
  1725. // GOTO L0
  1726. // finally -> E1
  1727. // | E1: ALOAD_1
  1728. // | MONITOREXIT
  1729. // finally -> E1
  1730. // ATHROW
  1731. // L0: RETURN (line 23)
  1732. // frameEnv.put(donorFramePos, thisSlot);
  1733. // search for 'returns' and make them to the
  1734. // aload_<n>,monitorexit
  1735. InstructionHandle walker = body.getStart();
  1736. List<InstructionHandle> rets = new ArrayList<InstructionHandle>();
  1737. while (walker != null) { // !walker.equals(body.getEnd())) {
  1738. if (walker.getInstruction().isReturnInstruction()) {
  1739. rets.add(walker);
  1740. }
  1741. walker = walker.getNext();
  1742. }
  1743. if (rets.size() > 0) {
  1744. // need to ensure targeters for 'return' now instead target
  1745. // the load instruction
  1746. // (so we never jump over the monitorexit logic)
  1747. for (InstructionHandle ret : rets) {
  1748. // System.err.println("Adding monitor exit block at "+
  1749. // element);
  1750. InstructionList monitorExitBlock = new InstructionList();
  1751. monitorExitBlock.append(InstructionFactory.createLoad(classType, slotForThis));
  1752. monitorExitBlock.append(InstructionConstants.MONITOREXIT);
  1753. // monitorExitBlock.append(Utility.copyInstruction(element
  1754. // .getInstruction()));
  1755. // element.setInstruction(InstructionFactory.createLoad(
  1756. // classType,slotForThis));
  1757. InstructionHandle monitorExitBlockStart = body.insert(ret, monitorExitBlock);
  1758. // now move the targeters from the RET to the start of
  1759. // the monitorexit block
  1760. for (InstructionTargeter targeter : ret.getTargetersCopy()) {
  1761. // what kinds are there?
  1762. if (targeter instanceof LocalVariableTag) {
  1763. // ignore
  1764. } else if (targeter instanceof LineNumberTag) {
  1765. // ignore
  1766. // } else if (targeter instanceof GOTO ||
  1767. // targeter instanceof GOTO_W) {
  1768. // // move it...
  1769. // targeter.updateTarget(element,
  1770. // monitorExitBlockStart);
  1771. } else if (targeter instanceof InstructionBranch) {
  1772. // move it
  1773. targeter.updateTarget(ret, monitorExitBlockStart);
  1774. } else {
  1775. throw new BCException("Unexpected targeter encountered during transform: " + targeter);
  1776. }
  1777. }
  1778. }
  1779. }
  1780. // body =
  1781. // rewriteWithMonitorExitCalls(body,fact,true,slotForThis,
  1782. // classType);
  1783. // synchronizedMethod.setBody(body);
  1784. // now the magic, putting the finally block around the code
  1785. InstructionHandle finallyStart = finallyBlock.getStart();
  1786. InstructionHandle tryPosition = body.getStart();
  1787. InstructionHandle catchPosition = body.getEnd();
  1788. body.insert(body.getStart(), prepend); // now we can put the
  1789. // monitorenter stuff on
  1790. synchronizedMethod.getBody().append(finallyBlock);
  1791. synchronizedMethod.addExceptionHandler(tryPosition, catchPosition, finallyStart, null/* ==finally */, false);
  1792. synchronizedMethod.addExceptionHandler(tryInstruction, catchInstruction, catchBlockStart,
  1793. (ObjectType) Type.getType(ClassNotFoundException.class), true);
  1794. synchronizedMethod.addExceptionHandler(finallyStart, finallyStart.getNext(), finallyStart, null, false);
  1795. }
  1796. } else {
  1797. // TRANSFORMING NON STATIC METHOD
  1798. Type classType = BcelWorld.makeBcelType(synchronizedMethod.getEnclosingClass().getType());
  1799. // MONITORENTER
  1800. // pseudocode: load up 'this' (var0), dup it, store it in a new
  1801. // local var (for use with monitorexit) and call
  1802. // monitorenter:
  1803. // ALOAD_0, DUP, ASTORE_<n>, MONITORENTER
  1804. prepend.append(InstructionFactory.createLoad(classType, 0));
  1805. prepend.append(InstructionFactory.createDup(1));
  1806. int slotForThis = synchronizedMethod.allocateLocal(classType);
  1807. prepend.append(InstructionFactory.createStore(classType, slotForThis));
  1808. prepend.append(InstructionFactory.MONITORENTER);
  1809. // body.insert(body.getStart(),prepend);
  1810. // MONITOREXIT
  1811. // We basically need to wrap the code from the method in a finally
  1812. // block that
  1813. // will ensure monitorexit is called. Content on the finally block
  1814. // seems to
  1815. // be always:
  1816. //
  1817. // E1: ALOAD_1
  1818. // MONITOREXIT
  1819. // ATHROW
  1820. //
  1821. // so lets build that:
  1822. InstructionList finallyBlock = new InstructionList();
  1823. finallyBlock.append(InstructionFactory.createLoad(classType, slotForThis));
  1824. finallyBlock.append(InstructionConstants.MONITOREXIT);
  1825. finallyBlock.append(InstructionConstants.ATHROW);
  1826. // finally -> E1
  1827. // | GETSTATIC java.lang.System.out Ljava/io/PrintStream; (line 21)
  1828. // | LDC "hello"
  1829. // | INVOKEVIRTUAL java.io.PrintStream.println (Ljava/lang/String;)V
  1830. // | ALOAD_1 (line 20)
  1831. // | MONITOREXIT
  1832. // finally -> E1
  1833. // GOTO L0
  1834. // finally -> E1
  1835. // | E1: ALOAD_1
  1836. // | MONITOREXIT
  1837. // finally -> E1
  1838. // ATHROW
  1839. // L0: RETURN (line 23)
  1840. // frameEnv.put(donorFramePos, thisSlot);
  1841. // search for 'returns' and make them to the aload_<n>,monitorexit
  1842. InstructionHandle walker = body.getStart();
  1843. List<InstructionHandle> rets = new ArrayList<InstructionHandle>();
  1844. while (walker != null) { // !walker.equals(body.getEnd())) {
  1845. if (walker.getInstruction().isReturnInstruction()) {
  1846. rets.add(walker);
  1847. }
  1848. walker = walker.getNext();
  1849. }
  1850. if (!rets.isEmpty()) {
  1851. // need to ensure targeters for 'return' now instead target the
  1852. // load instruction
  1853. // (so we never jump over the monitorexit logic)
  1854. for (Iterator<InstructionHandle> iter = rets.iterator(); iter.hasNext();) {
  1855. InstructionHandle element = iter.next();
  1856. // System.err.println("Adding monitor exit block at "+element
  1857. // );
  1858. InstructionList monitorExitBlock = new InstructionList();
  1859. monitorExitBlock.append(InstructionFactory.createLoad(classType, slotForThis));
  1860. monitorExitBlock.append(InstructionConstants.MONITOREXIT);
  1861. // monitorExitBlock.append(Utility.copyInstruction(element.
  1862. // getInstruction()));
  1863. // element.setInstruction(InstructionFactory.createLoad(
  1864. // classType,slotForThis));
  1865. InstructionHandle monitorExitBlockStart = body.insert(element, monitorExitBlock);
  1866. // now move the targeters from the RET to the start of the
  1867. // monitorexit block
  1868. for (InstructionTargeter targeter : element.getTargetersCopy()) {
  1869. // what kinds are there?
  1870. if (targeter instanceof LocalVariableTag) {
  1871. // ignore
  1872. } else if (targeter instanceof LineNumberTag) {
  1873. // ignore
  1874. // } else if (targeter instanceof GOTO ||
  1875. // targeter instanceof GOTO_W) {
  1876. // // move it...
  1877. // targeter.updateTarget(element,
  1878. // monitorExitBlockStart);
  1879. } else if (targeter instanceof InstructionBranch) {
  1880. // move it
  1881. targeter.updateTarget(element, monitorExitBlockStart);
  1882. } else {
  1883. throw new BCException("Unexpected targeter encountered during transform: " + targeter);
  1884. }
  1885. }
  1886. }
  1887. }
  1888. // now the magic, putting the finally block around the code
  1889. InstructionHandle finallyStart = finallyBlock.getStart();
  1890. InstructionHandle tryPosition = body.getStart();
  1891. InstructionHandle catchPosition = body.getEnd();
  1892. body.insert(body.getStart(), prepend); // now we can put the
  1893. // monitorenter stuff on
  1894. synchronizedMethod.getBody().append(finallyBlock);
  1895. synchronizedMethod.addExceptionHandler(tryPosition, catchPosition, finallyStart, null/* ==finally */, false);
  1896. synchronizedMethod.addExceptionHandler(finallyStart, finallyStart.getNext(), finallyStart, null, false);
  1897. // also the exception handling for the finally block jumps to itself
  1898. // max locals will already have been modified in the allocateLocal()
  1899. // call
  1900. // synchronized bit is removed on LazyMethodGen.pack()
  1901. }
  1902. // gonna have to go through and change all aload_0s to load the var from
  1903. // a variable,
  1904. // going to add a new variable for the this var
  1905. if (trace.isTraceEnabled()) {
  1906. trace.exit("transformSynchronizedMethod");
  1907. }
  1908. }
  1909. /**
  1910. * generate the instructions to be inlined.
  1911. *
  1912. * @param donor the method from which we will copy (and adjust frame and jumps) instructions.
  1913. * @param recipient the method the instructions will go into. Used to get the frame size so we can allocate new frame locations
  1914. * for locals in donor.
  1915. * @param frameEnv an environment to map from donor frame to recipient frame, initially populated with argument locations.
  1916. * @param fact an instruction factory for recipient
  1917. */
  1918. static InstructionList genInlineInstructions(LazyMethodGen donor, LazyMethodGen recipient, IntMap frameEnv,
  1919. InstructionFactory fact, boolean keepReturns) {
  1920. InstructionList footer = new InstructionList();
  1921. InstructionHandle end = footer.append(InstructionConstants.NOP);
  1922. InstructionList ret = new InstructionList();
  1923. InstructionList sourceList = donor.getBody();
  1924. Map<InstructionHandle, InstructionHandle> srcToDest = new HashMap<InstructionHandle, InstructionHandle>();
  1925. ConstantPool donorCpg = donor.getEnclosingClass().getConstantPool();
  1926. ConstantPool recipientCpg = recipient.getEnclosingClass().getConstantPool();
  1927. boolean isAcrossClass = donorCpg != recipientCpg;
  1928. // first pass: copy the instructions directly, populate the srcToDest
  1929. // map,
  1930. // fix frame instructions
  1931. for (InstructionHandle src = sourceList.getStart(); src != null; src = src.getNext()) {
  1932. Instruction fresh = Utility.copyInstruction(src.getInstruction());
  1933. InstructionHandle dest;
  1934. // OPTIMIZE optimize this stuff?
  1935. if (fresh.isConstantPoolInstruction()) {
  1936. // need to reset index to go to new constant pool. This is
  1937. // totally
  1938. // a computation leak... we're testing this LOTS of times. Sigh.
  1939. if (isAcrossClass) {
  1940. InstructionCP cpi = (InstructionCP) fresh;
  1941. cpi.setIndex(recipientCpg.addConstant(donorCpg.getConstant(cpi.getIndex()), donorCpg));
  1942. }
  1943. }
  1944. if (src.getInstruction() == Range.RANGEINSTRUCTION) {
  1945. dest = ret.append(Range.RANGEINSTRUCTION);
  1946. } else if (fresh.isReturnInstruction()) {
  1947. if (keepReturns) {
  1948. dest = ret.append(fresh);
  1949. } else {
  1950. dest = ret.append(InstructionFactory.createBranchInstruction(Constants.GOTO, end));
  1951. }
  1952. } else if (fresh instanceof InstructionBranch) {
  1953. dest = ret.append((InstructionBranch) fresh);
  1954. } else if (fresh.isLocalVariableInstruction() || fresh instanceof RET) {
  1955. // IndexedInstruction indexed = (IndexedInstruction) fresh;
  1956. int oldIndex = fresh.getIndex();
  1957. int freshIndex;
  1958. if (!frameEnv.hasKey(oldIndex)) {
  1959. freshIndex = recipient.allocateLocal(2);
  1960. frameEnv.put(oldIndex, freshIndex);
  1961. } else {
  1962. freshIndex = frameEnv.get(oldIndex);
  1963. }
  1964. if (fresh instanceof RET) {
  1965. fresh.setIndex(freshIndex);
  1966. } else {
  1967. fresh = ((InstructionLV) fresh).setIndexAndCopyIfNecessary(freshIndex);
  1968. }
  1969. dest = ret.append(fresh);
  1970. } else {
  1971. dest = ret.append(fresh);
  1972. }
  1973. srcToDest.put(src, dest);
  1974. }
  1975. // second pass: retarget branch instructions, copy ranges and tags
  1976. Map<Tag, Tag> tagMap = new HashMap<Tag, Tag>();
  1977. Map<BcelShadow, BcelShadow> shadowMap = new HashMap<BcelShadow, BcelShadow>();
  1978. for (InstructionHandle dest = ret.getStart(), src = sourceList.getStart(); dest != null; dest = dest.getNext(), src = src
  1979. .getNext()) {
  1980. Instruction inst = dest.getInstruction();
  1981. // retarget branches
  1982. if (inst instanceof InstructionBranch) {
  1983. InstructionBranch branch = (InstructionBranch) inst;
  1984. InstructionHandle oldTarget = branch.getTarget();
  1985. InstructionHandle newTarget = srcToDest.get(oldTarget);
  1986. if (newTarget == null) {
  1987. // assert this is a GOTO
  1988. // this was a return instruction we previously replaced
  1989. } else {
  1990. branch.setTarget(newTarget);
  1991. if (branch instanceof InstructionSelect) {
  1992. InstructionSelect select = (InstructionSelect) branch;
  1993. InstructionHandle[] oldTargets = select.getTargets();
  1994. for (int k = oldTargets.length - 1; k >= 0; k--) {
  1995. select.setTarget(k, srcToDest.get(oldTargets[k]));
  1996. }
  1997. }
  1998. }
  1999. }
  2000. // copy over tags and range attributes
  2001. Iterator<InstructionTargeter> tIter = src.getTargeters().iterator();
  2002. while (tIter.hasNext()) {
  2003. InstructionTargeter old = tIter.next();
  2004. if (old instanceof Tag) {
  2005. Tag oldTag = (Tag) old;
  2006. Tag fresh = tagMap.get(oldTag);
  2007. if (fresh == null) {
  2008. fresh = oldTag.copy();
  2009. if (old instanceof LocalVariableTag) {
  2010. // LocalVariable
  2011. LocalVariableTag lvTag = (LocalVariableTag) old;
  2012. LocalVariableTag lvTagFresh = (LocalVariableTag) fresh;
  2013. if (lvTag.getSlot() == 0) {
  2014. fresh = new LocalVariableTag(lvTag.getRealType().getSignature(), "ajc$aspectInstance",
  2015. frameEnv.get(lvTag.getSlot()), 0);
  2016. } else {
  2017. // // Do not move it - when copying the code from the aspect to the affected target, 'this' is
  2018. // // going to change from aspect to affected type. So just fix the type
  2019. // System.out.println("For local variable tag at instruction " + src + " changing slot from "
  2020. // + lvTag.getSlot() + " > " + frameEnv.get(lvTag.getSlot()));
  2021. lvTagFresh.updateSlot(frameEnv.get(lvTag.getSlot()));
  2022. }
  2023. }
  2024. tagMap.put(oldTag, fresh);
  2025. }
  2026. dest.addTargeter(fresh);
  2027. } else if (old instanceof ExceptionRange) {
  2028. ExceptionRange er = (ExceptionRange) old;
  2029. if (er.getStart() == src) {
  2030. ExceptionRange freshEr = new ExceptionRange(recipient.getBody(), er.getCatchType(), er.getPriority());
  2031. freshEr.associateWithTargets(dest, srcToDest.get(er.getEnd()), srcToDest.get(er.getHandler()));
  2032. }
  2033. } else if (old instanceof ShadowRange) {
  2034. ShadowRange oldRange = (ShadowRange) old;
  2035. if (oldRange.getStart() == src) {
  2036. BcelShadow oldShadow = oldRange.getShadow();
  2037. BcelShadow freshEnclosing = oldShadow.getEnclosingShadow() == null ? null : (BcelShadow) shadowMap
  2038. .get(oldShadow.getEnclosingShadow());
  2039. BcelShadow freshShadow = oldShadow.copyInto(recipient, freshEnclosing);
  2040. ShadowRange freshRange = new ShadowRange(recipient.getBody());
  2041. freshRange.associateWithShadow(freshShadow);
  2042. freshRange.associateWithTargets(dest, srcToDest.get(oldRange.getEnd()));
  2043. shadowMap.put(oldShadow, freshShadow); // oldRange, freshRange
  2044. // recipient.matchedShadows.add(freshShadow);
  2045. // XXX should go through the NEW copied shadow and
  2046. // update
  2047. // the thisVar, targetVar, and argsVar
  2048. // ??? Might want to also go through at this time and
  2049. // add
  2050. // "extra" vars to the shadow.
  2051. }
  2052. }
  2053. }
  2054. }
  2055. if (!keepReturns) {
  2056. ret.append(footer);
  2057. }
  2058. return ret;
  2059. }
  2060. // static InstructionList rewriteWithMonitorExitCalls(InstructionList
  2061. // sourceList,InstructionFactory fact,boolean keepReturns,int
  2062. // monitorVarSlot,Type monitorVarType)
  2063. // {
  2064. // InstructionList footer = new InstructionList();
  2065. // InstructionHandle end = footer.append(InstructionConstants.NOP);
  2066. //
  2067. // InstructionList newList = new InstructionList();
  2068. //
  2069. // Map srcToDest = new HashMap();
  2070. //
  2071. // // first pass: copy the instructions directly, populate the srcToDest
  2072. // map,
  2073. // // fix frame instructions
  2074. // for (InstructionHandle src = sourceList.getStart(); src != null; src =
  2075. // src.getNext()) {
  2076. // Instruction fresh = Utility.copyInstruction(src.getInstruction());
  2077. // InstructionHandle dest;
  2078. // if (src.getInstruction() == Range.RANGEINSTRUCTION) {
  2079. // dest = newList.append(Range.RANGEINSTRUCTION);
  2080. // } else if (fresh.isReturnInstruction()) {
  2081. // if (keepReturns) {
  2082. // newList.append(InstructionFactory.createLoad(monitorVarType,monitorVarSlot
  2083. // ));
  2084. // newList.append(InstructionConstants.MONITOREXIT);
  2085. // dest = newList.append(fresh);
  2086. // } else {
  2087. // dest =
  2088. // newList.append(InstructionFactory.createBranchInstruction(Constants.GOTO,
  2089. // end));
  2090. // }
  2091. // } else if (fresh instanceof InstructionBranch) {
  2092. // dest = newList.append((InstructionBranch) fresh);
  2093. // } else if (
  2094. // fresh.isLocalVariableInstruction() || fresh instanceof RET) {
  2095. // //IndexedInstruction indexed = (IndexedInstruction) fresh;
  2096. // int oldIndex = fresh.getIndex();
  2097. // int freshIndex;
  2098. // // if (!frameEnv.hasKey(oldIndex)) {
  2099. // // freshIndex = recipient.allocateLocal(2);
  2100. // // frameEnv.put(oldIndex, freshIndex);
  2101. // // } else {
  2102. // freshIndex = oldIndex;//frameEnv.get(oldIndex);
  2103. // // }
  2104. // if (fresh instanceof RET) {
  2105. // fresh.setIndex(freshIndex);
  2106. // } else {
  2107. // fresh = ((InstructionLV)fresh).setIndexAndCopyIfNecessary(freshIndex);
  2108. // }
  2109. // dest = newList.append(fresh);
  2110. // } else {
  2111. // dest = newList.append(fresh);
  2112. // }
  2113. // srcToDest.put(src, dest);
  2114. // }
  2115. //
  2116. // // second pass: retarget branch instructions, copy ranges and tags
  2117. // Map tagMap = new HashMap();
  2118. // for (InstructionHandle dest = newList.getStart(), src =
  2119. // sourceList.getStart();
  2120. // dest != null;
  2121. // dest = dest.getNext(), src = src.getNext()) {
  2122. // Instruction inst = dest.getInstruction();
  2123. //
  2124. // // retarget branches
  2125. // if (inst instanceof InstructionBranch) {
  2126. // InstructionBranch branch = (InstructionBranch) inst;
  2127. // InstructionHandle oldTarget = branch.getTarget();
  2128. // InstructionHandle newTarget =
  2129. // (InstructionHandle) srcToDest.get(oldTarget);
  2130. // if (newTarget == null) {
  2131. // // assert this is a GOTO
  2132. // // this was a return instruction we previously replaced
  2133. // } else {
  2134. // branch.setTarget(newTarget);
  2135. // if (branch instanceof InstructionSelect) {
  2136. // InstructionSelect select = (InstructionSelect) branch;
  2137. // InstructionHandle[] oldTargets = select.getTargets();
  2138. // for (int k = oldTargets.length - 1; k >= 0; k--) {
  2139. // select.setTarget(
  2140. // k,
  2141. // (InstructionHandle) srcToDest.get(oldTargets[k]));
  2142. // }
  2143. // }
  2144. // }
  2145. // }
  2146. //
  2147. // //copy over tags and range attributes
  2148. // Iterator tIter = src.getTargeters().iterator();
  2149. //
  2150. // while (tIter.hasNext()) {
  2151. // InstructionTargeter old = (InstructionTargeter)tIter.next();
  2152. // if (old instanceof Tag) {
  2153. // Tag oldTag = (Tag) old;
  2154. // Tag fresh = (Tag) tagMap.get(oldTag);
  2155. // if (fresh == null) {
  2156. // fresh = oldTag.copy();
  2157. // tagMap.put(oldTag, fresh);
  2158. // }
  2159. // dest.addTargeter(fresh);
  2160. // } else if (old instanceof ExceptionRange) {
  2161. // ExceptionRange er = (ExceptionRange) old;
  2162. // if (er.getStart() == src) {
  2163. // ExceptionRange freshEr =
  2164. // new ExceptionRange(newList/*recipient.getBody()*/,er.getCatchType(),er.
  2165. // getPriority());
  2166. // freshEr.associateWithTargets(
  2167. // dest,
  2168. // (InstructionHandle)srcToDest.get(er.getEnd()),
  2169. // (InstructionHandle)srcToDest.get(er.getHandler()));
  2170. // }
  2171. // }
  2172. // /*else if (old instanceof ShadowRange) {
  2173. // ShadowRange oldRange = (ShadowRange) old;
  2174. // if (oldRange.getStart() == src) {
  2175. // BcelShadow oldShadow = oldRange.getShadow();
  2176. // BcelShadow freshEnclosing =
  2177. // oldShadow.getEnclosingShadow() == null
  2178. // ? null
  2179. // : (BcelShadow) shadowMap.get(oldShadow.getEnclosingShadow());
  2180. // BcelShadow freshShadow =
  2181. // oldShadow.copyInto(recipient, freshEnclosing);
  2182. // ShadowRange freshRange = new ShadowRange(recipient.getBody());
  2183. // freshRange.associateWithShadow(freshShadow);
  2184. // freshRange.associateWithTargets(
  2185. // dest,
  2186. // (InstructionHandle) srcToDest.get(oldRange.getEnd()));
  2187. // shadowMap.put(oldRange, freshRange);
  2188. // //recipient.matchedShadows.add(freshShadow);
  2189. // // XXX should go through the NEW copied shadow and update
  2190. // // the thisVar, targetVar, and argsVar
  2191. // // ??? Might want to also go through at this time and add
  2192. // // "extra" vars to the shadow.
  2193. // }
  2194. // }*/
  2195. // }
  2196. // }
  2197. // if (!keepReturns) newList.append(footer);
  2198. // return newList;
  2199. // }
  2200. /**
  2201. * generate the argument stores in preparation for inlining.
  2202. *
  2203. * @param donor the method we will inline from. Used to get the signature.
  2204. * @param recipient the method we will inline into. Used to get the frame size so we can allocate fresh locations.
  2205. * @param frameEnv an empty environment we populate with a map from donor frame to recipient frame.
  2206. * @param fact an instruction factory for recipient
  2207. */
  2208. private static InstructionList genArgumentStores(LazyMethodGen donor, LazyMethodGen recipient, IntMap frameEnv,
  2209. InstructionFactory fact) {
  2210. InstructionList ret = new InstructionList();
  2211. int donorFramePos = 0;
  2212. // writing ret back to front because we're popping.
  2213. if (!donor.isStatic()) {
  2214. int targetSlot = recipient.allocateLocal(Type.OBJECT);
  2215. ret.insert(InstructionFactory.createStore(Type.OBJECT, targetSlot));
  2216. frameEnv.put(donorFramePos, targetSlot);
  2217. donorFramePos += 1;
  2218. }
  2219. Type[] argTypes = donor.getArgumentTypes();
  2220. for (int i = 0, len = argTypes.length; i < len; i++) {
  2221. Type argType = argTypes[i];
  2222. int argSlot = recipient.allocateLocal(argType);
  2223. ret.insert(InstructionFactory.createStore(argType, argSlot));
  2224. frameEnv.put(donorFramePos, argSlot);
  2225. donorFramePos += argType.getSize();
  2226. }
  2227. return ret;
  2228. }
  2229. /**
  2230. * get a called method: Assumes the called method is in this class, and the reference to it is exact (a la INVOKESPECIAL).
  2231. *
  2232. * @param ih The InvokeInstruction instructionHandle pointing to the called method.
  2233. */
  2234. private LazyMethodGen getCalledMethod(InstructionHandle ih) {
  2235. InvokeInstruction inst = (InvokeInstruction) ih.getInstruction();
  2236. String methodName = inst.getName(cpg);
  2237. String signature = inst.getSignature(cpg);
  2238. return clazz.getLazyMethodGen(methodName, signature);
  2239. }
  2240. private void weaveInAddedMethods() {
  2241. Collections.sort(addedLazyMethodGens, new Comparator<LazyMethodGen>() {
  2242. public int compare(LazyMethodGen aa, LazyMethodGen bb) {
  2243. int i = aa.getName().compareTo(bb.getName());
  2244. if (i != 0) {
  2245. return i;
  2246. }
  2247. return aa.getSignature().compareTo(bb.getSignature());
  2248. }
  2249. });
  2250. for (LazyMethodGen addedMember : addedLazyMethodGens) {
  2251. clazz.addMethodGen(addedMember);
  2252. }
  2253. }
  2254. // void addPerSingletonField(Member field) {
  2255. // ObjectType aspectType = (ObjectType)
  2256. // BcelWorld.makeBcelType(field.getReturnType());
  2257. // String aspectName = field.getReturnType().getName();
  2258. //
  2259. // LazyMethodGen clinit = clazz.getStaticInitializer();
  2260. // InstructionList setup = new InstructionList();
  2261. // InstructionFactory fact = clazz.getFactory();
  2262. //
  2263. // setup.append(fact.createNew(aspectType));
  2264. // setup.append(InstructionFactory.createDup(1));
  2265. // setup.append(fact.createInvoke(aspectName, "<init>", Type.VOID, new
  2266. // Type[0], Constants.INVOKESPECIAL));
  2267. // setup.append(fact.createFieldAccess(aspectName, field.getName(),
  2268. // aspectType, Constants.PUTSTATIC));
  2269. // clinit.getBody().insert(setup);
  2270. // }
  2271. /**
  2272. * Returns null if this is not a Java constructor, and then we won't weave into it at all
  2273. */
  2274. private InstructionHandle findSuperOrThisCall(LazyMethodGen mg) {
  2275. int depth = 1;
  2276. InstructionHandle start = mg.getBody().getStart();
  2277. while (true) {
  2278. if (start == null) {
  2279. return null;
  2280. }
  2281. Instruction inst = start.getInstruction();
  2282. if (inst.opcode == Constants.INVOKESPECIAL && ((InvokeInstruction) inst).getName(cpg).equals("<init>")) {
  2283. depth--;
  2284. if (depth == 0) {
  2285. return start;
  2286. }
  2287. } else if (inst.opcode == Constants.NEW) {
  2288. depth++;
  2289. }
  2290. start = start.getNext();
  2291. }
  2292. }
  2293. // ----
  2294. private boolean match(LazyMethodGen mg) {
  2295. BcelShadow enclosingShadow;
  2296. List<BcelShadow> shadowAccumulator = new ArrayList<BcelShadow>();
  2297. boolean isOverweaving = world.isOverWeaving();
  2298. boolean startsAngly = mg.getName().charAt(0) == '<';
  2299. // we want to match ajsynthetic constructors...
  2300. if (startsAngly && mg.getName().equals("<init>")) {
  2301. return matchInit(mg, shadowAccumulator);
  2302. } else if (!shouldWeaveBody(mg)) {
  2303. return false;
  2304. } else {
  2305. if (startsAngly && mg.getName().equals("<clinit>")) {
  2306. // clinitShadow =
  2307. enclosingShadow = BcelShadow.makeStaticInitialization(world, mg);
  2308. // System.err.println(enclosingShadow);
  2309. } else if (mg.isAdviceMethod()) {
  2310. enclosingShadow = BcelShadow.makeAdviceExecution(world, mg);
  2311. } else {
  2312. AjAttribute.EffectiveSignatureAttribute effective = mg.getEffectiveSignature();
  2313. if (effective == null) {
  2314. // Don't want ajc$preClinit to be considered for matching
  2315. if (isOverweaving && mg.getName().startsWith(NameMangler.PREFIX)) {
  2316. return false;
  2317. }
  2318. enclosingShadow = BcelShadow.makeMethodExecution(world, mg, !canMatchBodyShadows);
  2319. } else if (effective.isWeaveBody()) {
  2320. ResolvedMember rm = effective.getEffectiveSignature();
  2321. // Annotations for things with effective signatures are
  2322. // never stored in the effective
  2323. // signature itself - we have to hunt for them. Storing them
  2324. // in the effective signature
  2325. // would mean keeping two sets up to date (no way!!)
  2326. fixParameterNamesForResolvedMember(rm, mg.getMemberView());
  2327. fixAnnotationsForResolvedMember(rm, mg.getMemberView());
  2328. enclosingShadow = BcelShadow.makeShadowForMethod(world, mg, effective.getShadowKind(), rm);
  2329. } else {
  2330. return false;
  2331. }
  2332. }
  2333. if (canMatchBodyShadows) {
  2334. for (InstructionHandle h = mg.getBody().getStart(); h != null; h = h.getNext()) {
  2335. match(mg, h, enclosingShadow, shadowAccumulator);
  2336. }
  2337. }
  2338. // FIXME asc change from string match if we can, rather brittle.
  2339. // this check actually prevents field-exec jps
  2340. if (canMatch(enclosingShadow.getKind())
  2341. && !(mg.getName().charAt(0) == 'a' && mg.getName().startsWith("ajc$interFieldInit"))) {
  2342. if (match(enclosingShadow, shadowAccumulator)) {
  2343. enclosingShadow.init();
  2344. }
  2345. }
  2346. mg.matchedShadows = shadowAccumulator;
  2347. return !shadowAccumulator.isEmpty();
  2348. }
  2349. }
  2350. private boolean matchInit(LazyMethodGen mg, List<BcelShadow> shadowAccumulator) {
  2351. BcelShadow enclosingShadow;
  2352. // XXX the enclosing join point is wrong for things before ignoreMe.
  2353. InstructionHandle superOrThisCall = findSuperOrThisCall(mg);
  2354. // we don't walk bodies of things where it's a wrong constructor thingie
  2355. if (superOrThisCall == null) {
  2356. return false;
  2357. }
  2358. enclosingShadow = BcelShadow.makeConstructorExecution(world, mg, superOrThisCall);
  2359. if (mg.getEffectiveSignature() != null) {
  2360. enclosingShadow.setMatchingSignature(mg.getEffectiveSignature().getEffectiveSignature());
  2361. }
  2362. // walk the body
  2363. boolean beforeSuperOrThisCall = true;
  2364. if (shouldWeaveBody(mg)) {
  2365. if (canMatchBodyShadows) {
  2366. for (InstructionHandle h = mg.getBody().getStart(); h != null; h = h.getNext()) {
  2367. if (h == superOrThisCall) {
  2368. beforeSuperOrThisCall = false;
  2369. continue;
  2370. }
  2371. match(mg, h, beforeSuperOrThisCall ? null : enclosingShadow, shadowAccumulator);
  2372. }
  2373. }
  2374. if (canMatch(Shadow.ConstructorExecution)) {
  2375. match(enclosingShadow, shadowAccumulator);
  2376. }
  2377. }
  2378. // XXX we don't do pre-inits of interfaces
  2379. // now add interface inits
  2380. if (!isThisCall(superOrThisCall)) {
  2381. InstructionHandle curr = enclosingShadow.getRange().getStart();
  2382. for (Iterator<IfaceInitList> i = addedSuperInitializersAsList.iterator(); i.hasNext();) {
  2383. IfaceInitList l = i.next();
  2384. Member ifaceInitSig = AjcMemberMaker.interfaceConstructor(l.onType);
  2385. BcelShadow initShadow = BcelShadow.makeIfaceInitialization(world, mg, ifaceInitSig);
  2386. // insert code in place
  2387. InstructionList inits = genInitInstructions(l.list, false);
  2388. if (match(initShadow, shadowAccumulator) || !inits.isEmpty()) {
  2389. initShadow.initIfaceInitializer(curr);
  2390. initShadow.getRange().insert(inits, Range.OutsideBefore);
  2391. }
  2392. }
  2393. // now we add our initialization code
  2394. InstructionList inits = genInitInstructions(addedThisInitializers, false);
  2395. enclosingShadow.getRange().insert(inits, Range.OutsideBefore);
  2396. }
  2397. // actually, you only need to inline the self constructors that are
  2398. // in a particular group (partition the constructors into groups where
  2399. // members
  2400. // call or are called only by those in the group). Then only inline
  2401. // constructors
  2402. // in groups where at least one initialization jp matched. Future work.
  2403. boolean addedInitialization = match(BcelShadow.makeUnfinishedInitialization(world, mg), initializationShadows);
  2404. addedInitialization |= match(BcelShadow.makeUnfinishedPreinitialization(world, mg), initializationShadows);
  2405. mg.matchedShadows = shadowAccumulator;
  2406. return addedInitialization || !shadowAccumulator.isEmpty();
  2407. }
  2408. private boolean shouldWeaveBody(LazyMethodGen mg) {
  2409. if (mg.isBridgeMethod()) {
  2410. return false;
  2411. }
  2412. if (mg.isAjSynthetic()) {
  2413. return mg.getName().equals("<clinit>");
  2414. }
  2415. AjAttribute.EffectiveSignatureAttribute a = mg.getEffectiveSignature();
  2416. if (a != null) {
  2417. return a.isWeaveBody();
  2418. }
  2419. return true;
  2420. }
  2421. /**
  2422. * first sorts the mungers, then gens the initializers in the right order
  2423. */
  2424. private InstructionList genInitInstructions(List<ConcreteTypeMunger> list, boolean isStatic) {
  2425. list = PartialOrder.sort(list);
  2426. if (list == null) {
  2427. throw new BCException("circularity in inter-types");
  2428. }
  2429. InstructionList ret = new InstructionList();
  2430. for (ConcreteTypeMunger cmunger : list) {
  2431. NewFieldTypeMunger munger = (NewFieldTypeMunger) cmunger.getMunger();
  2432. ResolvedMember initMethod = munger.getInitMethod(cmunger.getAspectType());
  2433. if (!isStatic) {
  2434. ret.append(InstructionConstants.ALOAD_0);
  2435. }
  2436. ret.append(Utility.createInvoke(fact, world, initMethod));
  2437. }
  2438. return ret;
  2439. }
  2440. private void match(LazyMethodGen mg, InstructionHandle ih, BcelShadow enclosingShadow, List<BcelShadow> shadowAccumulator) {
  2441. Instruction i = ih.getInstruction();
  2442. // Exception handlers (pr230817)
  2443. if (canMatch(Shadow.ExceptionHandler) && !Range.isRangeHandle(ih)) {
  2444. Set<InstructionTargeter> targeters = ih.getTargetersCopy();
  2445. // If in Java7 there may be overlapping exception ranges for multi catch - we should recognize that
  2446. for (InstructionTargeter t : targeters) {
  2447. if (t instanceof ExceptionRange) {
  2448. // assert t.getHandler() == ih
  2449. ExceptionRange er = (ExceptionRange) t;
  2450. if (er.getCatchType() == null) {
  2451. continue;
  2452. }
  2453. if (isInitFailureHandler(ih)) {
  2454. return;
  2455. }
  2456. if (!ih.getInstruction().isStoreInstruction() && ih.getInstruction().getOpcode() != Constants.NOP) {
  2457. // If using cobertura, the catch block stats with
  2458. // INVOKESTATIC rather than ASTORE, in order that the ranges
  2459. // for the methodcall and exceptionhandler shadows
  2460. // that occur at this same
  2461. // line, we need to modify the instruction list to
  2462. // split them - adding a
  2463. // NOP before the invokestatic that gets all the targeters
  2464. // that were aimed at the INVOKESTATIC
  2465. mg.getBody().insert(ih, InstructionConstants.NOP);
  2466. InstructionHandle newNOP = ih.getPrev();
  2467. // what about a try..catch that starts at the start
  2468. // of the exception handler? need to only include
  2469. // certain targeters really.
  2470. er.updateTarget(ih, newNOP, mg.getBody());
  2471. for (InstructionTargeter t2 : targeters) {
  2472. newNOP.addTargeter(t2);
  2473. }
  2474. ih.removeAllTargeters();
  2475. match(BcelShadow.makeExceptionHandler(world, er, mg, newNOP, enclosingShadow), shadowAccumulator);
  2476. } else {
  2477. match(BcelShadow.makeExceptionHandler(world, er, mg, ih, enclosingShadow), shadowAccumulator);
  2478. }
  2479. }
  2480. }
  2481. }
  2482. if ((i instanceof FieldInstruction) && (canMatch(Shadow.FieldGet) || canMatch(Shadow.FieldSet))) {
  2483. FieldInstruction fi = (FieldInstruction) i;
  2484. if (fi.opcode == Constants.PUTFIELD || fi.opcode == Constants.PUTSTATIC) {
  2485. // check for sets of constant fields. We first check the
  2486. // previous
  2487. // instruction. If the previous instruction is a LD_WHATEVER
  2488. // (push
  2489. // constant on the stack) then we must resolve the field to
  2490. // determine
  2491. // if it's final. If it is final, then we don't generate a
  2492. // shadow.
  2493. InstructionHandle prevHandle = ih.getPrev();
  2494. Instruction prevI = prevHandle.getInstruction();
  2495. if (Utility.isConstantPushInstruction(prevI)) {
  2496. Member field = BcelWorld.makeFieldJoinPointSignature(clazz, (FieldInstruction) i);
  2497. ResolvedMember resolvedField = field.resolve(world);
  2498. if (resolvedField == null) {
  2499. // we can't find the field, so it's not a join point.
  2500. } else if (Modifier.isFinal(resolvedField.getModifiers())) {
  2501. // it's final, so it's the set of a final constant, so
  2502. // it's
  2503. // not a join point according to 1.0.6 and 1.1.
  2504. } else {
  2505. if (canMatch(Shadow.FieldSet)) {
  2506. matchSetInstruction(mg, ih, enclosingShadow, shadowAccumulator);
  2507. }
  2508. }
  2509. } else {
  2510. if (canMatch(Shadow.FieldSet)) {
  2511. matchSetInstruction(mg, ih, enclosingShadow, shadowAccumulator);
  2512. }
  2513. }
  2514. } else {
  2515. if (canMatch(Shadow.FieldGet)) {
  2516. matchGetInstruction(mg, ih, enclosingShadow, shadowAccumulator);
  2517. }
  2518. }
  2519. } else if (i instanceof InvokeInstruction) {
  2520. InvokeInstruction ii = (InvokeInstruction) i;
  2521. if (ii.getMethodName(clazz.getConstantPool()).equals("<init>")) {
  2522. if (canMatch(Shadow.ConstructorCall)) {
  2523. match(BcelShadow.makeConstructorCall(world, mg, ih, enclosingShadow), shadowAccumulator);
  2524. }
  2525. } else if (ii.opcode == Constants.INVOKESPECIAL) {
  2526. String onTypeName = ii.getClassName(cpg);
  2527. if (onTypeName.equals(mg.getEnclosingClass().getName())) {
  2528. // we are private
  2529. matchInvokeInstruction(mg, ih, ii, enclosingShadow, shadowAccumulator);
  2530. } else {
  2531. // we are a super call, and this is not a join point in
  2532. // AspectJ-1.{0,1}
  2533. }
  2534. } else {
  2535. matchInvokeInstruction(mg, ih, ii, enclosingShadow, shadowAccumulator);
  2536. }
  2537. } else if (world.isJoinpointArrayConstructionEnabled() && i.isArrayCreationInstruction()) {
  2538. if (canMatch(Shadow.ConstructorCall)) {
  2539. if (i.opcode == Constants.ANEWARRAY) {
  2540. // ANEWARRAY arrayInstruction = (ANEWARRAY)i;
  2541. // ObjectType arrayType = i.getLoadClassType(clazz.getConstantPool());
  2542. BcelShadow ctorCallShadow = BcelShadow.makeArrayConstructorCall(world, mg, ih, enclosingShadow);
  2543. match(ctorCallShadow, shadowAccumulator);
  2544. } else if (i.opcode == Constants.NEWARRAY) {
  2545. // NEWARRAY arrayInstruction = (NEWARRAY)i;
  2546. // Type arrayType = i.getType();
  2547. BcelShadow ctorCallShadow = BcelShadow.makeArrayConstructorCall(world, mg, ih, enclosingShadow);
  2548. match(ctorCallShadow, shadowAccumulator);
  2549. } else if (i instanceof MULTIANEWARRAY) {
  2550. // MULTIANEWARRAY arrayInstruction = (MULTIANEWARRAY) i;
  2551. // ObjectType arrayType = arrayInstruction.getLoadClassType(clazz.getConstantPool());
  2552. BcelShadow ctorCallShadow = BcelShadow.makeArrayConstructorCall(world, mg, ih, enclosingShadow);
  2553. match(ctorCallShadow, shadowAccumulator);
  2554. }
  2555. }
  2556. // see pr77166 if you are thinking about implementing this
  2557. // } else if (i instanceof AALOAD ) {
  2558. // AALOAD arrayLoad = (AALOAD)i;
  2559. // Type arrayType = arrayLoad.getType(clazz.getConstantPoolGen());
  2560. // BcelShadow arrayLoadShadow =
  2561. // BcelShadow.makeArrayLoadCall(world,mg,ih,enclosingShadow);
  2562. // match(arrayLoadShadow,shadowAccumulator);
  2563. // } else if (i instanceof AASTORE) {
  2564. // // ... magic required
  2565. } else if (world.isJoinpointSynchronizationEnabled()
  2566. && ((i.getOpcode() == Constants.MONITORENTER) || (i.getOpcode() == Constants.MONITOREXIT))) {
  2567. // if (canMatch(Shadow.Monitoring)) {
  2568. if (i.getOpcode() == Constants.MONITORENTER) {
  2569. BcelShadow monitorEntryShadow = BcelShadow.makeMonitorEnter(world, mg, ih, enclosingShadow);
  2570. match(monitorEntryShadow, shadowAccumulator);
  2571. } else {
  2572. BcelShadow monitorExitShadow = BcelShadow.makeMonitorExit(world, mg, ih, enclosingShadow);
  2573. match(monitorExitShadow, shadowAccumulator);
  2574. }
  2575. // }
  2576. }
  2577. }
  2578. private boolean isInitFailureHandler(InstructionHandle ih) {
  2579. // Skip the astore_0 and aload_0 at the start of the handler and
  2580. // then check if the instruction following these is
  2581. // 'putstatic ajc$initFailureCause'. If it is then we are
  2582. // in the handler we created in AspectClinit.generatePostSyntheticCode()
  2583. InstructionHandle twoInstructionsAway = ih.getNext().getNext();
  2584. if (twoInstructionsAway.getInstruction().opcode == Constants.PUTSTATIC) {
  2585. String name = ((FieldInstruction) twoInstructionsAway.getInstruction()).getFieldName(cpg);
  2586. if (name.equals(NameMangler.INITFAILURECAUSE_FIELD_NAME)) {
  2587. return true;
  2588. }
  2589. }
  2590. return false;
  2591. }
  2592. private void matchSetInstruction(LazyMethodGen mg, InstructionHandle ih, BcelShadow enclosingShadow,
  2593. List<BcelShadow> shadowAccumulator) {
  2594. FieldInstruction fi = (FieldInstruction) ih.getInstruction();
  2595. Member field = BcelWorld.makeFieldJoinPointSignature(clazz, fi);
  2596. // synthetic fields are never join points
  2597. if (field.getName().startsWith(NameMangler.PREFIX)) {
  2598. return;
  2599. }
  2600. ResolvedMember resolvedField = field.resolve(world);
  2601. if (resolvedField == null) {
  2602. // we can't find the field, so it's not a join point.
  2603. return;
  2604. } else if (Modifier.isFinal(resolvedField.getModifiers())
  2605. && Utility.isConstantPushInstruction(ih.getPrev().getInstruction())) {
  2606. // it's the set of a final constant, so it's
  2607. // not a join point according to 1.0.6 and 1.1.
  2608. return;
  2609. } else if (resolvedField.isSynthetic()) {
  2610. // sets of synthetics aren't join points in 1.1
  2611. return;
  2612. } else {
  2613. // Fix for bug 172107 (similar the "get" fix for bug 109728)
  2614. BcelShadow bs = BcelShadow.makeFieldSet(world, resolvedField, mg, ih, enclosingShadow);
  2615. String cname = fi.getClassName(cpg);
  2616. if (!resolvedField.getDeclaringType().getName().equals(cname)) {
  2617. bs.setActualTargetType(cname);
  2618. }
  2619. match(bs, shadowAccumulator);
  2620. }
  2621. }
  2622. private void matchGetInstruction(LazyMethodGen mg, InstructionHandle ih, BcelShadow enclosingShadow,
  2623. List<BcelShadow> shadowAccumulator) {
  2624. FieldInstruction fi = (FieldInstruction) ih.getInstruction();
  2625. Member field = BcelWorld.makeFieldJoinPointSignature(clazz, fi);
  2626. // synthetic fields are never join points
  2627. if (field.getName().startsWith(NameMangler.PREFIX)) {
  2628. return;
  2629. }
  2630. ResolvedMember resolvedField = field.resolve(world);
  2631. if (resolvedField == null) {
  2632. // we can't find the field, so it's not a join point.
  2633. return;
  2634. } else if (resolvedField.isSynthetic()) {
  2635. // sets of synthetics aren't join points in 1.1
  2636. return;
  2637. } else {
  2638. BcelShadow bs = BcelShadow.makeFieldGet(world, resolvedField, mg, ih, enclosingShadow);
  2639. String cname = fi.getClassName(cpg);
  2640. if (!resolvedField.getDeclaringType().getName().equals(cname)) {
  2641. bs.setActualTargetType(cname);
  2642. }
  2643. match(bs, shadowAccumulator);
  2644. }
  2645. }
  2646. /**
  2647. * For some named resolved type, this method looks for a member with a particular name - it should only be used when you truly
  2648. * believe there is only one member with that name in the type as it returns the first one it finds.
  2649. */
  2650. private ResolvedMember findResolvedMemberNamed(ResolvedType type, String methodName) {
  2651. ResolvedMember[] allMethods = type.getDeclaredMethods();
  2652. for (int i = 0; i < allMethods.length; i++) {
  2653. ResolvedMember member = allMethods[i];
  2654. if (member.getName().equals(methodName)) {
  2655. return member;
  2656. }
  2657. }
  2658. return null;
  2659. }
  2660. /**
  2661. * Find the specified member in the specified type.
  2662. *
  2663. * @param type the type to search for the member
  2664. * @param methodName the name of the method to find
  2665. * @param params the method parameters that the discovered method should have
  2666. */
  2667. private ResolvedMember findResolvedMemberNamed(ResolvedType type, String methodName, UnresolvedType[] params) {
  2668. ResolvedMember[] allMethods = type.getDeclaredMethods();
  2669. List<ResolvedMember> candidates = new ArrayList<ResolvedMember>();
  2670. for (int i = 0; i < allMethods.length; i++) {
  2671. ResolvedMember candidate = allMethods[i];
  2672. if (candidate.getName().equals(methodName)) {
  2673. if (candidate.getArity() == params.length) {
  2674. candidates.add(candidate);
  2675. }
  2676. }
  2677. }
  2678. if (candidates.size() == 0) {
  2679. return null;
  2680. } else if (candidates.size() == 1) {
  2681. return candidates.get(0);
  2682. } else {
  2683. // multiple candidates
  2684. for (ResolvedMember candidate : candidates) {
  2685. // These checks will break down with generics... but that would need two ITDs with the same name, same arity and
  2686. // generics
  2687. boolean allOK = true;
  2688. UnresolvedType[] candidateParams = candidate.getParameterTypes();
  2689. for (int p = 0; p < candidateParams.length; p++) {
  2690. if (!candidateParams[p].getErasureSignature().equals(params[p].getErasureSignature())) {
  2691. allOK = false;
  2692. break;
  2693. }
  2694. }
  2695. if (allOK) {
  2696. return candidate;
  2697. }
  2698. }
  2699. }
  2700. return null;
  2701. }
  2702. /**
  2703. * For a given resolvedmember, this will discover the real annotations for it. <b>Should only be used when the resolvedmember is
  2704. * the contents of an effective signature attribute, as thats the only time when the annotations aren't stored directly in the
  2705. * resolvedMember</b>
  2706. *
  2707. * @param rm the sig we want it to pretend to be 'int A.m()' or somesuch ITD like thing
  2708. * @param declaredSig the real sig 'blah.ajc$xxx'
  2709. */
  2710. private void fixParameterNamesForResolvedMember(ResolvedMember rm, ResolvedMember declaredSig) {
  2711. UnresolvedType memberHostType = declaredSig.getDeclaringType();
  2712. String methodName = declaredSig.getName();
  2713. String[] pnames = null;
  2714. if (rm.getKind() == Member.METHOD && !rm.isAbstract()) {
  2715. if (methodName.startsWith("ajc$inlineAccessMethod") || methodName.startsWith("ajc$superDispatch")) {
  2716. ResolvedMember resolvedDooberry = world.resolve(declaredSig);
  2717. pnames = resolvedDooberry.getParameterNames();
  2718. } else {
  2719. ResolvedMember realthing = AjcMemberMaker.interMethodDispatcher(rm.resolve(world), memberHostType).resolve(world);
  2720. ResolvedMember theRealMember = findResolvedMemberNamed(memberHostType.resolve(world), realthing.getName());
  2721. if (theRealMember != null) {
  2722. pnames = theRealMember.getParameterNames();
  2723. // static ITDs don't need any parameter shifting
  2724. if (pnames.length > 0 && pnames[0].equals("ajc$this_")) {
  2725. String[] pnames2 = new String[pnames.length - 1];
  2726. System.arraycopy(pnames, 1, pnames2, 0, pnames2.length);
  2727. pnames = pnames2;
  2728. }
  2729. }
  2730. }
  2731. // i think ctors are missing from here... copy code from below...
  2732. }
  2733. rm.setParameterNames(pnames);
  2734. }
  2735. /**
  2736. * For a given resolvedmember, this will discover the real annotations for it. <b>Should only be used when the resolvedmember is
  2737. * the contents of an effective signature attribute, as thats the only time when the annotations aren't stored directly in the
  2738. * resolvedMember</b>
  2739. *
  2740. * @param rm the sig we want it to pretend to be 'int A.m()' or somesuch ITD like thing
  2741. * @param declaredSig the real sig 'blah.ajc$xxx'
  2742. */
  2743. private void fixAnnotationsForResolvedMember(ResolvedMember rm, ResolvedMember declaredSig) {
  2744. try {
  2745. UnresolvedType memberHostType = declaredSig.getDeclaringType();
  2746. ResolvedType[] annotations = mapToAnnotations.get(rm);
  2747. String methodName = declaredSig.getName();
  2748. // FIXME asc shouldnt really rely on string names !
  2749. if (annotations == null) {
  2750. if (rm.getKind() == Member.FIELD) {
  2751. if (methodName.startsWith("ajc$inlineAccessField")) {
  2752. ResolvedMember resolvedDooberry = world.resolve(rm);
  2753. annotations = resolvedDooberry.getAnnotationTypes();
  2754. } else {
  2755. ResolvedMember realthing = AjcMemberMaker.interFieldInitializer(rm, memberHostType);
  2756. ResolvedMember resolvedDooberry = world.resolve(realthing);
  2757. annotations = resolvedDooberry.getAnnotationTypes();
  2758. }
  2759. } else if (rm.getKind() == Member.METHOD && !rm.isAbstract()) {
  2760. if (methodName.startsWith("ajc$inlineAccessMethod") || methodName.startsWith("ajc$superDispatch")) {
  2761. ResolvedMember resolvedDooberry = world.resolve(declaredSig);
  2762. annotations = resolvedDooberry.getAnnotationTypes();
  2763. } else {
  2764. ResolvedMember realthing = AjcMemberMaker.interMethodDispatcher(rm.resolve(world), memberHostType).resolve(
  2765. world);
  2766. ResolvedMember theRealMember = findResolvedMemberNamed(memberHostType.resolve(world), realthing.getName(),
  2767. realthing.getParameterTypes());
  2768. if (theRealMember == null) {
  2769. throw new UnsupportedOperationException(
  2770. "Known limitation in M4 - can't find ITD members when type variable is used as an argument and has upper bound specified");
  2771. }
  2772. annotations = theRealMember.getAnnotationTypes();
  2773. }
  2774. } else if (rm.getKind() == Member.CONSTRUCTOR) {
  2775. ResolvedMember realThing = AjcMemberMaker.postIntroducedConstructor(memberHostType.resolve(world),
  2776. rm.getDeclaringType(), rm.getParameterTypes());
  2777. ResolvedMember resolvedDooberry = world.resolve(realThing);
  2778. // AMC temp guard for M4
  2779. if (resolvedDooberry == null) {
  2780. throw new UnsupportedOperationException(
  2781. "Known limitation in M4 - can't find ITD members when type variable is used as an argument and has upper bound specified");
  2782. }
  2783. annotations = resolvedDooberry.getAnnotationTypes();
  2784. }
  2785. if (annotations == null) {
  2786. annotations = new ResolvedType[0];
  2787. }
  2788. mapToAnnotations.put(rm, annotations);
  2789. }
  2790. rm.setAnnotationTypes(annotations);
  2791. } catch (UnsupportedOperationException ex) {
  2792. throw ex;
  2793. } catch (Throwable t) {
  2794. // FIXME asc remove this catch after more testing has confirmed the
  2795. // above stuff is OK
  2796. throw new BCException("Unexpectedly went bang when searching for annotations on " + rm, t);
  2797. }
  2798. }
  2799. private void matchInvokeInstruction(LazyMethodGen mg, InstructionHandle ih, InvokeInstruction invoke,
  2800. BcelShadow enclosingShadow, List<BcelShadow> shadowAccumulator) {
  2801. String methodName = invoke.getName(cpg);
  2802. if (methodName.startsWith(NameMangler.PREFIX)) {
  2803. Member jpSig = world.makeJoinPointSignatureForMethodInvocation(clazz, invoke);
  2804. ResolvedMember declaredSig = jpSig.resolve(world);
  2805. // System.err.println(method + ", declaredSig: " +declaredSig);
  2806. if (declaredSig == null) {
  2807. return;
  2808. }
  2809. if (declaredSig.getKind() == Member.FIELD) {
  2810. Shadow.Kind kind;
  2811. if (jpSig.getReturnType().equals(UnresolvedType.VOID)) {
  2812. kind = Shadow.FieldSet;
  2813. } else {
  2814. kind = Shadow.FieldGet;
  2815. }
  2816. if (canMatch(Shadow.FieldGet) || canMatch(Shadow.FieldSet)) {
  2817. match(BcelShadow.makeShadowForMethodCall(world, mg, ih, enclosingShadow, kind, declaredSig), shadowAccumulator);
  2818. }
  2819. } else {
  2820. AjAttribute.EffectiveSignatureAttribute effectiveSig = declaredSig.getEffectiveSignature();
  2821. if (effectiveSig == null) {
  2822. return;
  2823. }
  2824. // System.err.println("call to inter-type member: " +
  2825. // effectiveSig);
  2826. if (effectiveSig.isWeaveBody()) {
  2827. return;
  2828. }
  2829. ResolvedMember rm = effectiveSig.getEffectiveSignature();
  2830. fixParameterNamesForResolvedMember(rm, declaredSig);
  2831. fixAnnotationsForResolvedMember(rm, declaredSig); // abracadabra
  2832. if (canMatch(effectiveSig.getShadowKind())) {
  2833. match(BcelShadow.makeShadowForMethodCall(world, mg, ih, enclosingShadow, effectiveSig.getShadowKind(), rm),
  2834. shadowAccumulator);
  2835. }
  2836. }
  2837. } else {
  2838. if (canMatch(Shadow.MethodCall)) {
  2839. boolean proceed = true;
  2840. // overweaving needs to ignore some calls added by the previous weave
  2841. if (world.isOverWeaving()) {
  2842. String s = invoke.getClassName(mg.getConstantPool());
  2843. // skip all the inc/dec/isValid/etc
  2844. if (s.length() > 4
  2845. && s.charAt(4) == 'a'
  2846. && (s.equals("org.aspectj.runtime.internal.CFlowCounter")
  2847. || s.equals("org.aspectj.runtime.internal.CFlowStack") || s
  2848. .equals("org.aspectj.runtime.reflect.Factory"))) {
  2849. proceed = false;
  2850. } else {
  2851. if (methodName.equals("aspectOf")) {
  2852. proceed = false;
  2853. }
  2854. }
  2855. }
  2856. if (proceed) {
  2857. match(BcelShadow.makeMethodCall(world, mg, ih, enclosingShadow), shadowAccumulator);
  2858. }
  2859. }
  2860. }
  2861. }
  2862. // static ... so all worlds will share the config for the first one
  2863. // created...
  2864. private static boolean checkedXsetForLowLevelContextCapturing = false;
  2865. private static boolean captureLowLevelContext = false;
  2866. private boolean match(BcelShadow shadow, List<BcelShadow> shadowAccumulator) {
  2867. // Duplicate blocks - one with context one without, seems faster than multiple 'ifs'
  2868. if (captureLowLevelContext) {
  2869. ContextToken shadowMatchToken = CompilationAndWeavingContext.enteringPhase(
  2870. CompilationAndWeavingContext.MATCHING_SHADOW, shadow);
  2871. boolean isMatched = false;
  2872. Shadow.Kind shadowKind = shadow.getKind();
  2873. List<ShadowMunger> candidateMungers = indexedShadowMungers[shadowKind.getKey()];
  2874. // System.out.println("Candidates " + candidateMungers);
  2875. if (candidateMungers != null) {
  2876. for (ShadowMunger munger : candidateMungers) {
  2877. ContextToken mungerMatchToken = CompilationAndWeavingContext.enteringPhase(
  2878. CompilationAndWeavingContext.MATCHING_POINTCUT, munger.getPointcut());
  2879. if (munger.match(shadow, world)) {
  2880. shadow.addMunger(munger);
  2881. isMatched = true;
  2882. if (shadow.getKind() == Shadow.StaticInitialization) {
  2883. clazz.warnOnAddedStaticInitializer(shadow, munger.getSourceLocation());
  2884. }
  2885. }
  2886. CompilationAndWeavingContext.leavingPhase(mungerMatchToken);
  2887. }
  2888. if (isMatched) {
  2889. shadowAccumulator.add(shadow);
  2890. }
  2891. }
  2892. CompilationAndWeavingContext.leavingPhase(shadowMatchToken);
  2893. return isMatched;
  2894. } else {
  2895. boolean isMatched = false;
  2896. Shadow.Kind shadowKind = shadow.getKind();
  2897. List<ShadowMunger> candidateMungers = indexedShadowMungers[shadowKind.getKey()];
  2898. // System.out.println("Candidates at " + shadowKind + " are " + candidateMungers);
  2899. if (candidateMungers != null) {
  2900. for (ShadowMunger munger : candidateMungers) {
  2901. if (munger.match(shadow, world)) {
  2902. shadow.addMunger(munger);
  2903. isMatched = true;
  2904. if (shadow.getKind() == Shadow.StaticInitialization) {
  2905. clazz.warnOnAddedStaticInitializer(shadow, munger.getSourceLocation());
  2906. }
  2907. }
  2908. }
  2909. if (isMatched) {
  2910. shadowAccumulator.add(shadow);
  2911. }
  2912. }
  2913. return isMatched;
  2914. }
  2915. }
  2916. // ----
  2917. private void implement(LazyMethodGen mg) {
  2918. List<BcelShadow> shadows = mg.matchedShadows;
  2919. if (shadows == null) {
  2920. return;
  2921. }
  2922. // We depend on a partial order such that inner shadows are earlier on
  2923. // the list than outer shadows. That's fine. This order is preserved if:
  2924. // A preceeds B iff B.getStart() is LATER THAN A.getStart().
  2925. for (BcelShadow shadow : shadows) {
  2926. ContextToken tok = CompilationAndWeavingContext.enteringPhase(CompilationAndWeavingContext.IMPLEMENTING_ON_SHADOW,
  2927. shadow);
  2928. shadow.implement();
  2929. CompilationAndWeavingContext.leavingPhase(tok);
  2930. }
  2931. // int ii =
  2932. mg.getMaxLocals();
  2933. mg.matchedShadows = null;
  2934. }
  2935. // ----
  2936. public LazyClassGen getLazyClassGen() {
  2937. return clazz;
  2938. }
  2939. public BcelWorld getWorld() {
  2940. return world;
  2941. }
  2942. public void setReweavableMode(boolean mode) {
  2943. inReweavableMode = mode;
  2944. }
  2945. public boolean getReweavableMode() {
  2946. return inReweavableMode;
  2947. }
  2948. @Override
  2949. public String toString() {
  2950. return "BcelClassWeaver instance for : " + clazz;
  2951. }
  2952. }