Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

BcelShadow.java 140KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556
  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. * Alexandre Vasseur support for @AJ aspects
  12. * ******************************************************************/
  13. package org.aspectj.weaver.bcel;
  14. import java.lang.reflect.Modifier;
  15. import java.util.ArrayList;
  16. import java.util.Collections;
  17. import java.util.HashMap;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import java.util.Map;
  21. import org.aspectj.apache.bcel.Constants;
  22. import org.aspectj.apache.bcel.classfile.Field;
  23. import org.aspectj.apache.bcel.generic.ACONST_NULL;
  24. import org.aspectj.apache.bcel.generic.ALOAD;
  25. import org.aspectj.apache.bcel.generic.ANEWARRAY;
  26. import org.aspectj.apache.bcel.generic.ArrayType;
  27. import org.aspectj.apache.bcel.generic.BranchInstruction;
  28. import org.aspectj.apache.bcel.generic.ConstantPoolGen;
  29. import org.aspectj.apache.bcel.generic.DUP;
  30. import org.aspectj.apache.bcel.generic.DUP_X1;
  31. import org.aspectj.apache.bcel.generic.FieldInstruction;
  32. import org.aspectj.apache.bcel.generic.INVOKEINTERFACE;
  33. import org.aspectj.apache.bcel.generic.INVOKESPECIAL;
  34. import org.aspectj.apache.bcel.generic.INVOKESTATIC;
  35. import org.aspectj.apache.bcel.generic.Instruction;
  36. import org.aspectj.apache.bcel.generic.InstructionConstants;
  37. import org.aspectj.apache.bcel.generic.InstructionFactory;
  38. import org.aspectj.apache.bcel.generic.InstructionHandle;
  39. import org.aspectj.apache.bcel.generic.InstructionList;
  40. import org.aspectj.apache.bcel.generic.InstructionTargeter;
  41. import org.aspectj.apache.bcel.generic.InvokeInstruction;
  42. import org.aspectj.apache.bcel.generic.LoadInstruction;
  43. import org.aspectj.apache.bcel.generic.LocalVariableTag;
  44. import org.aspectj.apache.bcel.generic.MULTIANEWARRAY;
  45. import org.aspectj.apache.bcel.generic.NEW;
  46. import org.aspectj.apache.bcel.generic.ObjectType;
  47. import org.aspectj.apache.bcel.generic.PUSH;
  48. import org.aspectj.apache.bcel.generic.RETURN;
  49. import org.aspectj.apache.bcel.generic.ReturnInstruction;
  50. import org.aspectj.apache.bcel.generic.SWAP;
  51. import org.aspectj.apache.bcel.generic.StoreInstruction;
  52. import org.aspectj.apache.bcel.generic.TargetLostException;
  53. import org.aspectj.apache.bcel.generic.Type;
  54. import org.aspectj.bridge.ISourceLocation;
  55. import org.aspectj.weaver.Advice;
  56. import org.aspectj.weaver.AdviceKind;
  57. import org.aspectj.weaver.AjcMemberMaker;
  58. import org.aspectj.weaver.BCException;
  59. import org.aspectj.weaver.IntMap;
  60. import org.aspectj.weaver.Member;
  61. import org.aspectj.weaver.MemberImpl;
  62. import org.aspectj.weaver.NameMangler;
  63. import org.aspectj.weaver.NewConstructorTypeMunger;
  64. import org.aspectj.weaver.NewFieldTypeMunger;
  65. import org.aspectj.weaver.NewMethodTypeMunger;
  66. import org.aspectj.weaver.ResolvedMember;
  67. import org.aspectj.weaver.ResolvedMemberImpl;
  68. import org.aspectj.weaver.ResolvedType;
  69. import org.aspectj.weaver.Shadow;
  70. import org.aspectj.weaver.ShadowMunger;
  71. import org.aspectj.weaver.UnresolvedType;
  72. import org.aspectj.weaver.WeaverMessages;
  73. import org.aspectj.weaver.World;
  74. import org.aspectj.weaver.ast.Var;
  75. import org.aspectj.weaver.patterns.AndPointcut;
  76. import org.aspectj.weaver.patterns.IdentityPointcutVisitor;
  77. import org.aspectj.weaver.patterns.NotPointcut;
  78. import org.aspectj.weaver.patterns.OrPointcut;
  79. import org.aspectj.weaver.patterns.Pointcut;
  80. import org.aspectj.weaver.patterns.ThisOrTargetPointcut;
  81. /*
  82. * Some fun implementation stuff:
  83. *
  84. * * expressionKind advice is non-execution advice
  85. * * may have a target.
  86. * * if the body is extracted, it will be extracted into
  87. * a static method. The first argument to the static
  88. * method is the target
  89. * * advice may expose a this object, but that's the advice's
  90. * consideration, not ours. This object will NOT be cached in another
  91. * local, but will always come from frame zero.
  92. *
  93. * * non-expressionKind advice is execution advice
  94. * * may have a this.
  95. * * target is same as this, and is exposed that way to advice
  96. * (i.e., target will not be cached, will always come from frame zero)
  97. * * if the body is extracted, it will be extracted into a method
  98. * with same static/dynamic modifier as enclosing method. If non-static,
  99. * target of callback call will be this.
  100. *
  101. * * because of these two facts, the setup of the actual arguments (including
  102. * possible target) callback method is the same for both kinds of advice:
  103. * push the targetVar, if it exists (it will not exist for advice on static
  104. * things), then push all the argVars.
  105. *
  106. * Protected things:
  107. *
  108. * * the above is sufficient for non-expressionKind advice for protected things,
  109. * since the target will always be this.
  110. *
  111. * * For expressionKind things, we have to modify the signature of the callback
  112. * method slightly. For non-static expressionKind things, we modify
  113. * the first argument of the callback method NOT to be the type specified
  114. * by the method/field signature (the owner), but rather we type it to
  115. * the currentlyEnclosing type. We are guaranteed this will be fine,
  116. * since the verifier verifies that the target is a subtype of the currently
  117. * enclosingType.
  118. *
  119. * Worries:
  120. *
  121. * * ConstructorCalls will be weirder than all of these, since they
  122. * supposedly don't have a target (according to AspectJ), but they clearly
  123. * do have a target of sorts, just one that needs to be pushed on the stack,
  124. * dupped, and not touched otherwise until the constructor runs.
  125. *
  126. * @author Jim Hugunin
  127. * @author Erik Hilsdale
  128. *
  129. */
  130. public class BcelShadow extends Shadow {
  131. private ShadowRange range;
  132. private final BcelWorld world;
  133. private final LazyMethodGen enclosingMethod;
  134. // private boolean fallsThrough; //XXX not used anymore
  135. // SECRETAPI - for testing, this will tell us if the optimization succeeded *on the last shadow processed*
  136. public static boolean appliedLazyTjpOptimization;
  137. // Some instructions have a target type that will vary
  138. // from the signature (pr109728) (1.4 declaring type issue)
  139. private String actualInstructionTargetType;
  140. // ---- initialization
  141. /**
  142. * This generates an unassociated shadow, rooted in a particular method but not rooted
  143. * to any particular point in the code. It should be given to a rooted ShadowRange
  144. * in the {@link ShadowRange#associateWithShadow(BcelShadow)} method.
  145. */
  146. public BcelShadow(
  147. BcelWorld world,
  148. Kind kind,
  149. Member signature,
  150. LazyMethodGen enclosingMethod,
  151. BcelShadow enclosingShadow)
  152. {
  153. super(kind, signature, enclosingShadow);
  154. this.world = world;
  155. this.enclosingMethod = enclosingMethod;
  156. // fallsThrough = kind.argsOnStack();
  157. }
  158. // ---- copies all state, including Shadow's mungers...
  159. public BcelShadow copyInto(LazyMethodGen recipient, BcelShadow enclosing) {
  160. BcelShadow s = new BcelShadow(world, getKind(), getSignature(), recipient, enclosing);
  161. if (mungers.size()>0) {
  162. List src = mungers;
  163. if (s.mungers==Collections.EMPTY_LIST) s.mungers = new ArrayList();
  164. List dest = s.mungers;
  165. for (Iterator i = src.iterator(); i.hasNext(); ) {
  166. dest.add(i.next());
  167. }
  168. }
  169. return s;
  170. }
  171. // ---- overridden behaviour
  172. public World getIWorld() {
  173. return world;
  174. }
  175. private void deleteNewAndDup() {
  176. final ConstantPoolGen cpg = getEnclosingClass().getConstantPoolGen();
  177. int depth = 1;
  178. InstructionHandle ih = range.getStart();
  179. while (true) {
  180. Instruction inst = ih.getInstruction();
  181. if (inst instanceof INVOKESPECIAL
  182. && ((INVOKESPECIAL) inst).getName(cpg).equals("<init>")) {
  183. depth++;
  184. } else if (inst instanceof NEW) {
  185. depth--;
  186. if (depth == 0) break;
  187. }
  188. ih = ih.getPrev();
  189. }
  190. // now IH points to the NEW. We're followed by the DUP, and that is followed
  191. // by the actual instruction we care about.
  192. InstructionHandle newHandle = ih;
  193. InstructionHandle endHandle = newHandle.getNext();
  194. InstructionHandle nextHandle;
  195. if (endHandle.getInstruction() instanceof DUP) {
  196. nextHandle = endHandle.getNext();
  197. retargetFrom(newHandle, nextHandle);
  198. retargetFrom(endHandle, nextHandle);
  199. } else if (endHandle.getInstruction() instanceof DUP_X1) {
  200. InstructionHandle dupHandle = endHandle;
  201. endHandle = endHandle.getNext();
  202. nextHandle = endHandle.getNext();
  203. if (endHandle.getInstruction() instanceof SWAP) {}
  204. else {
  205. // XXX see next XXX comment
  206. throw new RuntimeException("Unhandled kind of new " + endHandle);
  207. }
  208. // Now make any jumps to the 'new', the 'dup' or the 'end' now target the nextHandle
  209. retargetFrom(newHandle, nextHandle);
  210. retargetFrom(dupHandle, nextHandle);
  211. retargetFrom(endHandle, nextHandle);
  212. } else {
  213. endHandle = newHandle;
  214. nextHandle = endHandle.getNext();
  215. retargetFrom(newHandle, nextHandle);
  216. // add a POP here... we found a NEW w/o a dup or anything else, so
  217. // we must be in statement context.
  218. getRange().insert(InstructionConstants.POP, Range.OutsideAfter);
  219. }
  220. // assert (dupHandle.getInstruction() instanceof DUP);
  221. try {
  222. range.getBody().delete(newHandle, endHandle);
  223. } catch (TargetLostException e) {
  224. throw new BCException("shouldn't happen");
  225. }
  226. }
  227. private void retargetFrom(InstructionHandle old, InstructionHandle fresh) {
  228. InstructionTargeter[] sources = old.getTargeters();
  229. if (sources != null) {
  230. for (int i = sources.length - 1; i >= 0; i--) {
  231. if (sources[i] instanceof ExceptionRange) {
  232. ExceptionRange it = (ExceptionRange)sources[i];
  233. System.err.println("...");
  234. it.updateTarget(old,fresh,it.getBody());
  235. } else {
  236. sources[i].updateTarget(old, fresh);
  237. }
  238. }
  239. }
  240. }
  241. // records advice that is stopping us doing the lazyTjp optimization
  242. private List badAdvice = null;
  243. public void addAdvicePreventingLazyTjp(BcelAdvice advice) {
  244. if (badAdvice == null) badAdvice = new ArrayList();
  245. badAdvice.add(advice);
  246. }
  247. protected void prepareForMungers() {
  248. // if we're a constructor call, we need to remove the new:dup or the new:dup_x1:swap,
  249. // and store all our
  250. // arguments on the frame.
  251. // ??? This is a bit of a hack (for the Java langauge). We do this because
  252. // we sometime add code "outsideBefore" when dealing with weaving join points. We only
  253. // do this for exposing state that is on the stack. It turns out to just work for
  254. // everything except for constructor calls and exception handlers. If we were to clean
  255. // this up, every ShadowRange would have three instructionHandle points, the start of
  256. // the arg-setup code, the start of the running code, and the end of the running code.
  257. if (getKind() == ConstructorCall) {
  258. if (!world.isJoinpointArrayConstructionEnabled() || !this.getSignature().getDeclaringType().isArray())
  259. deleteNewAndDup(); // no new/dup for new array construction
  260. initializeArgVars();
  261. } else if (getKind() == PreInitialization) { // pr74952
  262. ShadowRange range = getRange();
  263. range.insert(InstructionConstants.NOP,Range.InsideAfter);
  264. } else if (getKind() == ExceptionHandler) {
  265. ShadowRange range = getRange();
  266. InstructionList body = range.getBody();
  267. InstructionHandle start = range.getStart();
  268. // Create a store instruction to put the value from the top of the
  269. // stack into a local variable slot. This is a trimmed version of
  270. // what is in initializeArgVars() (since there is only one argument
  271. // at a handler jp and only before advice is supported) (pr46298)
  272. argVars = new BcelVar[1];
  273. //int positionOffset = (hasTarget() ? 1 : 0) + ((hasThis() && !getKind().isTargetSameAsThis()) ? 1 : 0);
  274. UnresolvedType tx = getArgType(0);
  275. argVars[0] = genTempVar(tx, "ajc$arg0");
  276. InstructionHandle insertedInstruction =
  277. range.insert(argVars[0].createStore(getFactory()), Range.OutsideBefore);
  278. // Now the exception range starts just after our new instruction.
  279. // The next bit of code changes the exception range to point at
  280. // the store instruction
  281. InstructionTargeter[] targeters = start.getTargeters();
  282. for (int i = 0; i < targeters.length; i++) {
  283. InstructionTargeter t = targeters[i];
  284. if (t instanceof ExceptionRange) {
  285. ExceptionRange er = (ExceptionRange) t;
  286. er.updateTarget(start, insertedInstruction, body);
  287. }
  288. }
  289. }
  290. // now we ask each munger to request our state
  291. isThisJoinPointLazy = true;//world.isXlazyTjp(); // lazy is default now
  292. badAdvice = null;
  293. for (Iterator iter = mungers.iterator(); iter.hasNext();) {
  294. ShadowMunger munger = (ShadowMunger) iter.next();
  295. munger.specializeOn(this);
  296. }
  297. initializeThisJoinPoint();
  298. if (thisJoinPointVar!=null && !isThisJoinPointLazy && badAdvice!=null && badAdvice.size()>1) {
  299. // something stopped us making it a lazy tjp
  300. // can't build tjp lazily, no suitable test...
  301. int valid = 0;
  302. for (Iterator iter = badAdvice.iterator(); iter.hasNext();) {
  303. BcelAdvice element = (BcelAdvice) iter.next();
  304. ISourceLocation sLoc = element.getSourceLocation();
  305. if (sLoc!=null && sLoc.getLine()>0) valid++;
  306. }
  307. if (valid!=0) {
  308. ISourceLocation[] badLocs = new ISourceLocation[valid];
  309. int i = 0;
  310. for (Iterator iter = badAdvice.iterator(); iter.hasNext();) {
  311. BcelAdvice element = (BcelAdvice) iter.next();
  312. ISourceLocation sLoc = element.getSourceLocation();
  313. if (sLoc!=null) badLocs[i++]=sLoc;
  314. }
  315. world.getLint().multipleAdviceStoppingLazyTjp.signal(
  316. new String[] {this.toString()},
  317. getSourceLocation(),badLocs
  318. );
  319. }
  320. }
  321. badAdvice=null;
  322. // If we are an expression kind, we require our target/arguments on the stack
  323. // before we do our actual thing. However, they may have been removed
  324. // from the stack as the shadowMungers have requested state.
  325. // if any of our shadowMungers requested either the arguments or target,
  326. // the munger will have added code
  327. // to pop the target/arguments into temporary variables, represented by
  328. // targetVar and argVars. In such a case, we must make sure to re-push the
  329. // values.
  330. // If we are nonExpressionKind, we don't expect arguments on the stack
  331. // so this is moot. If our argVars happen to be null, then we know that
  332. // no ShadowMunger has squirrelled away our arguments, so they're still
  333. // on the stack.
  334. InstructionFactory fact = getFactory();
  335. if (getKind().argsOnStack() && argVars != null) {
  336. // Special case first (pr46298). If we are an exception handler and the instruction
  337. // just after the shadow is a POP then we should remove the pop. The code
  338. // above which generated the store instruction has already cleared the stack.
  339. // We also don't generate any code for the arguments in this case as it would be
  340. // an incorrect aload.
  341. if (getKind() == ExceptionHandler
  342. && range.getEnd().getNext().getInstruction().equals(InstructionConstants.POP)) {
  343. // easier than deleting it ...
  344. range.getEnd().getNext().setInstruction(InstructionConstants.NOP);
  345. } else {
  346. range.insert(
  347. BcelRenderer.renderExprs(fact, world, argVars),
  348. Range.InsideBefore);
  349. if (targetVar != null) {
  350. range.insert(
  351. BcelRenderer.renderExpr(fact, world, targetVar),
  352. Range.InsideBefore);
  353. }
  354. if (getKind() == ConstructorCall) {
  355. if (!world.isJoinpointArrayConstructionEnabled() || !this.getSignature().getDeclaringType().isArray()) {
  356. range.insert((Instruction) InstructionFactory.createDup(1), Range.InsideBefore);
  357. range.insert(
  358. fact.createNew(
  359. (ObjectType) BcelWorld.makeBcelType(
  360. getSignature().getDeclaringType())),
  361. Range.InsideBefore);
  362. }
  363. }
  364. }
  365. }
  366. }
  367. // ---- getters
  368. public ShadowRange getRange() {
  369. return range;
  370. }
  371. public void setRange(ShadowRange range) {
  372. this.range = range;
  373. }
  374. public int getSourceLine() {
  375. // if the kind of join point for which we are a shadow represents
  376. // a method or constructor execution, then the best source line is
  377. // the one from the enclosingMethod declarationLineNumber if available.
  378. Kind kind = getKind();
  379. if ( (kind == MethodExecution) ||
  380. (kind == ConstructorExecution) ||
  381. (kind == AdviceExecution) ||
  382. (kind == StaticInitialization) ||
  383. (kind == PreInitialization) ||
  384. (kind == Initialization)) {
  385. if (getEnclosingMethod().hasDeclaredLineNumberInfo()) {
  386. return getEnclosingMethod().getDeclarationLineNumber();
  387. }
  388. }
  389. if (range == null) {
  390. if (getEnclosingMethod().hasBody()) {
  391. return Utility.getSourceLine(getEnclosingMethod().getBody().getStart());
  392. } else {
  393. return 0;
  394. }
  395. }
  396. int ret = Utility.getSourceLine(range.getStart());
  397. if (ret < 0) return 0;
  398. return ret;
  399. }
  400. // overrides
  401. public UnresolvedType getEnclosingType() {
  402. return getEnclosingClass().getType();
  403. }
  404. public LazyClassGen getEnclosingClass() {
  405. return enclosingMethod.getEnclosingClass();
  406. }
  407. public BcelWorld getWorld() {
  408. return world;
  409. }
  410. // ---- factory methods
  411. public static BcelShadow makeConstructorExecution(
  412. BcelWorld world,
  413. LazyMethodGen enclosingMethod,
  414. InstructionHandle justBeforeStart)
  415. {
  416. final InstructionList body = enclosingMethod.getBody();
  417. BcelShadow s =
  418. new BcelShadow(
  419. world,
  420. ConstructorExecution,
  421. world.makeJoinPointSignature(enclosingMethod),
  422. enclosingMethod,
  423. null);
  424. ShadowRange r = new ShadowRange(body);
  425. r.associateWithShadow(s);
  426. r.associateWithTargets(
  427. Range.genStart(body, justBeforeStart.getNext()),
  428. Range.genEnd(body));
  429. return s;
  430. }
  431. public static BcelShadow makeStaticInitialization(
  432. BcelWorld world,
  433. LazyMethodGen enclosingMethod)
  434. {
  435. InstructionList body = enclosingMethod.getBody();
  436. // move the start past ajc$preClinit
  437. InstructionHandle clinitStart = body.getStart();
  438. if (clinitStart.getInstruction() instanceof InvokeInstruction) {
  439. InvokeInstruction ii = (InvokeInstruction)clinitStart.getInstruction();
  440. if (ii
  441. .getName(enclosingMethod.getEnclosingClass().getConstantPoolGen())
  442. .equals(NameMangler.AJC_PRE_CLINIT_NAME)) {
  443. clinitStart = clinitStart.getNext();
  444. }
  445. }
  446. InstructionHandle clinitEnd = body.getEnd();
  447. //XXX should move the end before the postClinit, but the return is then tricky...
  448. // if (clinitEnd.getInstruction() instanceof InvokeInstruction) {
  449. // InvokeInstruction ii = (InvokeInstruction)clinitEnd.getInstruction();
  450. // if (ii.getName(enclosingMethod.getEnclosingClass().getConstantPoolGen()).equals(NameMangler.AJC_POST_CLINIT_NAME)) {
  451. // clinitEnd = clinitEnd.getPrev();
  452. // }
  453. // }
  454. BcelShadow s =
  455. new BcelShadow(
  456. world,
  457. StaticInitialization,
  458. world.makeJoinPointSignature(enclosingMethod),
  459. enclosingMethod,
  460. null);
  461. ShadowRange r = new ShadowRange(body);
  462. r.associateWithShadow(s);
  463. r.associateWithTargets(
  464. Range.genStart(body, clinitStart),
  465. Range.genEnd(body, clinitEnd));
  466. return s;
  467. }
  468. /** Make the shadow for an exception handler. Currently makes an empty shadow that
  469. * only allows before advice to be woven into it.
  470. */
  471. public static BcelShadow makeExceptionHandler(
  472. BcelWorld world,
  473. ExceptionRange exceptionRange,
  474. LazyMethodGen enclosingMethod,
  475. InstructionHandle startOfHandler,
  476. BcelShadow enclosingShadow)
  477. {
  478. InstructionList body = enclosingMethod.getBody();
  479. UnresolvedType catchType = exceptionRange.getCatchType();
  480. UnresolvedType inType = enclosingMethod.getEnclosingClass().getType();
  481. ResolvedMemberImpl sig = MemberImpl.makeExceptionHandlerSignature(inType, catchType);
  482. sig.setParameterNames(new String[] {findHandlerParamName(startOfHandler)});
  483. BcelShadow s =
  484. new BcelShadow(
  485. world,
  486. ExceptionHandler,
  487. sig,
  488. enclosingMethod,
  489. enclosingShadow);
  490. ShadowRange r = new ShadowRange(body);
  491. r.associateWithShadow(s);
  492. InstructionHandle start = Range.genStart(body, startOfHandler);
  493. InstructionHandle end = Range.genEnd(body, start);
  494. r.associateWithTargets(start, end);
  495. exceptionRange.updateTarget(startOfHandler, start, body);
  496. return s;
  497. }
  498. private static String findHandlerParamName(InstructionHandle startOfHandler) {
  499. if (startOfHandler.getInstruction() instanceof StoreInstruction &&
  500. startOfHandler.getNext() != null)
  501. {
  502. int slot = ((StoreInstruction)startOfHandler.getInstruction()).getIndex();
  503. //System.out.println("got store: " + startOfHandler.getInstruction() + ", " + index);
  504. InstructionTargeter[] targeters = startOfHandler.getNext().getTargeters();
  505. if (targeters!=null) {
  506. for (int i=targeters.length-1; i >= 0; i--) {
  507. if (targeters[i] instanceof LocalVariableTag) {
  508. LocalVariableTag t = (LocalVariableTag)targeters[i];
  509. if (t.getSlot() == slot) {
  510. return t.getName();
  511. }
  512. //System.out.println("tag: " + targeters[i]);
  513. }
  514. }
  515. }
  516. }
  517. return "<missing>";
  518. }
  519. /** create an init join point associated w/ an interface in the body of a constructor */
  520. public static BcelShadow makeIfaceInitialization(
  521. BcelWorld world,
  522. LazyMethodGen constructor,
  523. Member interfaceConstructorSignature)
  524. {
  525. // this call marks the instruction list as changed
  526. constructor.getBody();
  527. // UnresolvedType inType = constructor.getEnclosingClass().getType();
  528. BcelShadow s =
  529. new BcelShadow(
  530. world,
  531. Initialization,
  532. interfaceConstructorSignature,
  533. constructor,
  534. null);
  535. // s.fallsThrough = true;
  536. // ShadowRange r = new ShadowRange(body);
  537. // r.associateWithShadow(s);
  538. // InstructionHandle start = Range.genStart(body, handle);
  539. // InstructionHandle end = Range.genEnd(body, handle);
  540. //
  541. // r.associateWithTargets(start, end);
  542. return s;
  543. }
  544. public void initIfaceInitializer(InstructionHandle end) {
  545. final InstructionList body = enclosingMethod.getBody();
  546. ShadowRange r = new ShadowRange(body);
  547. r.associateWithShadow(this);
  548. InstructionHandle nop = body.insert(end, InstructionConstants.NOP);
  549. r.associateWithTargets(
  550. Range.genStart(body, nop),
  551. Range.genEnd(body, nop));
  552. }
  553. // public static BcelShadow makeIfaceConstructorExecution(
  554. // BcelWorld world,
  555. // LazyMethodGen constructor,
  556. // InstructionHandle next,
  557. // Member interfaceConstructorSignature)
  558. // {
  559. // // final InstructionFactory fact = constructor.getEnclosingClass().getFactory();
  560. // InstructionList body = constructor.getBody();
  561. // // UnresolvedType inType = constructor.getEnclosingClass().getType();
  562. // BcelShadow s =
  563. // new BcelShadow(
  564. // world,
  565. // ConstructorExecution,
  566. // interfaceConstructorSignature,
  567. // constructor,
  568. // null);
  569. // s.fallsThrough = true;
  570. // ShadowRange r = new ShadowRange(body);
  571. // r.associateWithShadow(s);
  572. // // ??? this may or may not work
  573. // InstructionHandle start = Range.genStart(body, next);
  574. // //InstructionHandle end = Range.genEnd(body, body.append(start, fact.NOP));
  575. // InstructionHandle end = Range.genStart(body, next);
  576. // //body.append(start, fact.NOP);
  577. //
  578. // r.associateWithTargets(start, end);
  579. // return s;
  580. // }
  581. /** Create an initialization join point associated with a constructor, but not
  582. * with any body of code yet. If this is actually matched, it's range will be set
  583. * when we inline self constructors.
  584. *
  585. * @param constructor The constructor starting this initialization.
  586. */
  587. public static BcelShadow makeUnfinishedInitialization(
  588. BcelWorld world,
  589. LazyMethodGen constructor)
  590. {
  591. BcelShadow ret = new BcelShadow(
  592. world,
  593. Initialization,
  594. world.makeJoinPointSignature(constructor),
  595. constructor,
  596. null);
  597. if (constructor.getEffectiveSignature() != null) {
  598. ret.setMatchingSignature(constructor.getEffectiveSignature().getEffectiveSignature());
  599. }
  600. return ret;
  601. }
  602. public static BcelShadow makeUnfinishedPreinitialization(
  603. BcelWorld world,
  604. LazyMethodGen constructor)
  605. {
  606. BcelShadow ret = new BcelShadow(
  607. world,
  608. PreInitialization,
  609. world.makeJoinPointSignature(constructor),
  610. constructor,
  611. null);
  612. // ret.fallsThrough = true;
  613. if (constructor.getEffectiveSignature() != null) {
  614. ret.setMatchingSignature(constructor.getEffectiveSignature().getEffectiveSignature());
  615. }
  616. return ret;
  617. }
  618. public static BcelShadow makeMethodExecution(
  619. BcelWorld world,
  620. LazyMethodGen enclosingMethod,
  621. boolean lazyInit)
  622. {
  623. if (!lazyInit) return makeMethodExecution(world, enclosingMethod);
  624. BcelShadow s =
  625. new BcelShadow(
  626. world,
  627. MethodExecution,
  628. enclosingMethod.getMemberView(),
  629. enclosingMethod,
  630. null);
  631. return s;
  632. }
  633. public void init() {
  634. if (range != null) return;
  635. final InstructionList body = enclosingMethod.getBody();
  636. ShadowRange r = new ShadowRange(body);
  637. r.associateWithShadow(this);
  638. r.associateWithTargets(
  639. Range.genStart(body),
  640. Range.genEnd(body));
  641. }
  642. public static BcelShadow makeMethodExecution(
  643. BcelWorld world,
  644. LazyMethodGen enclosingMethod)
  645. {
  646. return makeShadowForMethod(world, enclosingMethod, MethodExecution,
  647. enclosingMethod.getMemberView()); //world.makeMethodSignature(enclosingMethod));
  648. }
  649. public static BcelShadow makeShadowForMethod(BcelWorld world,
  650. LazyMethodGen enclosingMethod, Shadow.Kind kind, Member sig)
  651. {
  652. final InstructionList body = enclosingMethod.getBody();
  653. BcelShadow s =
  654. new BcelShadow(
  655. world,
  656. kind,
  657. sig,
  658. enclosingMethod,
  659. null);
  660. ShadowRange r = new ShadowRange(body);
  661. r.associateWithShadow(s);
  662. r.associateWithTargets(
  663. Range.genStart(body),
  664. Range.genEnd(body));
  665. return s;
  666. }
  667. public static BcelShadow makeAdviceExecution(
  668. BcelWorld world,
  669. LazyMethodGen enclosingMethod)
  670. {
  671. final InstructionList body = enclosingMethod.getBody();
  672. BcelShadow s =
  673. new BcelShadow(
  674. world,
  675. AdviceExecution,
  676. world.makeJoinPointSignatureFromMethod(enclosingMethod, Member.ADVICE),
  677. enclosingMethod,
  678. null);
  679. ShadowRange r = new ShadowRange(body);
  680. r.associateWithShadow(s);
  681. r.associateWithTargets(Range.genStart(body), Range.genEnd(body));
  682. return s;
  683. }
  684. // constructor call shadows are <em>initially</em> just around the
  685. // call to the constructor. If ANY advice gets put on it, we move
  686. // the NEW instruction inside the join point, which involves putting
  687. // all the arguments in temps.
  688. public static BcelShadow makeConstructorCall(
  689. BcelWorld world,
  690. LazyMethodGen enclosingMethod,
  691. InstructionHandle callHandle,
  692. BcelShadow enclosingShadow)
  693. {
  694. final InstructionList body = enclosingMethod.getBody();
  695. Member sig = world.makeJoinPointSignatureForMethodInvocation(
  696. enclosingMethod.getEnclosingClass(),
  697. (InvokeInstruction) callHandle.getInstruction());
  698. BcelShadow s =
  699. new BcelShadow(
  700. world,
  701. ConstructorCall,
  702. sig,
  703. enclosingMethod,
  704. enclosingShadow);
  705. ShadowRange r = new ShadowRange(body);
  706. r.associateWithShadow(s);
  707. r.associateWithTargets(
  708. Range.genStart(body, callHandle),
  709. Range.genEnd(body, callHandle));
  710. retargetAllBranches(callHandle, r.getStart());
  711. return s;
  712. }
  713. public static BcelShadow makeArrayConstructorCall(BcelWorld world,LazyMethodGen enclosingMethod,InstructionHandle arrayInstruction,BcelShadow enclosingShadow) {
  714. final InstructionList body = enclosingMethod.getBody();
  715. Member sig = world.makeJoinPointSignatureForArrayConstruction(enclosingMethod.getEnclosingClass(),arrayInstruction);
  716. BcelShadow s =
  717. new BcelShadow(
  718. world,
  719. ConstructorCall,
  720. sig,
  721. enclosingMethod,
  722. enclosingShadow);
  723. ShadowRange r = new ShadowRange(body);
  724. r.associateWithShadow(s);
  725. r.associateWithTargets(
  726. Range.genStart(body, arrayInstruction),
  727. Range.genEnd(body, arrayInstruction));
  728. retargetAllBranches(arrayInstruction, r.getStart());
  729. return s;
  730. }
  731. public static BcelShadow makeMonitorEnter(BcelWorld world,LazyMethodGen enclosingMethod,InstructionHandle monitorInstruction,BcelShadow enclosingShadow) {
  732. final InstructionList body = enclosingMethod.getBody();
  733. Member sig = world.makeJoinPointSignatureForMonitorEnter(enclosingMethod.getEnclosingClass(),monitorInstruction);
  734. BcelShadow s = new BcelShadow(world,SynchronizationLock,sig,enclosingMethod,enclosingShadow);
  735. ShadowRange r = new ShadowRange(body);
  736. r.associateWithShadow(s);
  737. r.associateWithTargets(
  738. Range.genStart(body, monitorInstruction),
  739. Range.genEnd(body, monitorInstruction));
  740. retargetAllBranches(monitorInstruction, r.getStart());
  741. return s;
  742. }
  743. public static BcelShadow makeMonitorExit(BcelWorld world,LazyMethodGen enclosingMethod,InstructionHandle monitorInstruction,BcelShadow enclosingShadow) {
  744. final InstructionList body = enclosingMethod.getBody();
  745. Member sig = world.makeJoinPointSignatureForMonitorExit(enclosingMethod.getEnclosingClass(),monitorInstruction);
  746. BcelShadow s = new BcelShadow(world,SynchronizationUnlock,sig,enclosingMethod,enclosingShadow);
  747. ShadowRange r = new ShadowRange(body);
  748. r.associateWithShadow(s);
  749. r.associateWithTargets(
  750. Range.genStart(body, monitorInstruction),
  751. Range.genEnd(body, monitorInstruction));
  752. retargetAllBranches(monitorInstruction, r.getStart());
  753. return s;
  754. }
  755. // see pr77166
  756. // public static BcelShadow makeArrayLoadCall(
  757. // BcelWorld world,
  758. // LazyMethodGen enclosingMethod,
  759. // InstructionHandle arrayInstruction,
  760. // BcelShadow enclosingShadow)
  761. // {
  762. // final InstructionList body = enclosingMethod.getBody();
  763. // Member sig = world.makeJoinPointSignatureForArrayLoad(enclosingMethod.getEnclosingClass(),arrayInstruction);
  764. // BcelShadow s =
  765. // new BcelShadow(
  766. // world,
  767. // MethodCall,
  768. // sig,
  769. // enclosingMethod,
  770. // enclosingShadow);
  771. // ShadowRange r = new ShadowRange(body);
  772. // r.associateWithShadow(s);
  773. // r.associateWithTargets(
  774. // Range.genStart(body, arrayInstruction),
  775. // Range.genEnd(body, arrayInstruction));
  776. // retargetAllBranches(arrayInstruction, r.getStart());
  777. // return s;
  778. // }
  779. public static BcelShadow makeMethodCall(
  780. BcelWorld world,
  781. LazyMethodGen enclosingMethod,
  782. InstructionHandle callHandle,
  783. BcelShadow enclosingShadow)
  784. {
  785. final InstructionList body = enclosingMethod.getBody();
  786. BcelShadow s =
  787. new BcelShadow(
  788. world,
  789. MethodCall,
  790. world.makeJoinPointSignatureForMethodInvocation(
  791. enclosingMethod.getEnclosingClass(),
  792. (InvokeInstruction) callHandle.getInstruction()),
  793. enclosingMethod,
  794. enclosingShadow);
  795. ShadowRange r = new ShadowRange(body);
  796. r.associateWithShadow(s);
  797. r.associateWithTargets(
  798. Range.genStart(body, callHandle),
  799. Range.genEnd(body, callHandle));
  800. retargetAllBranches(callHandle, r.getStart());
  801. return s;
  802. }
  803. public static BcelShadow makeShadowForMethodCall(
  804. BcelWorld world,
  805. LazyMethodGen enclosingMethod,
  806. InstructionHandle callHandle,
  807. BcelShadow enclosingShadow,
  808. Kind kind,
  809. ResolvedMember sig)
  810. {
  811. final InstructionList body = enclosingMethod.getBody();
  812. BcelShadow s =
  813. new BcelShadow(
  814. world,
  815. kind,
  816. sig,
  817. enclosingMethod,
  818. enclosingShadow);
  819. ShadowRange r = new ShadowRange(body);
  820. r.associateWithShadow(s);
  821. r.associateWithTargets(
  822. Range.genStart(body, callHandle),
  823. Range.genEnd(body, callHandle));
  824. retargetAllBranches(callHandle, r.getStart());
  825. return s;
  826. }
  827. public static BcelShadow makeFieldGet(
  828. BcelWorld world,
  829. ResolvedMember field,
  830. LazyMethodGen enclosingMethod,
  831. InstructionHandle getHandle,
  832. BcelShadow enclosingShadow)
  833. {
  834. final InstructionList body = enclosingMethod.getBody();
  835. BcelShadow s =
  836. new BcelShadow(
  837. world,
  838. FieldGet,
  839. field,
  840. // BcelWorld.makeFieldSignature(
  841. // enclosingMethod.getEnclosingClass(),
  842. // (FieldInstruction) getHandle.getInstruction()),
  843. enclosingMethod,
  844. enclosingShadow);
  845. ShadowRange r = new ShadowRange(body);
  846. r.associateWithShadow(s);
  847. r.associateWithTargets(
  848. Range.genStart(body, getHandle),
  849. Range.genEnd(body, getHandle));
  850. retargetAllBranches(getHandle, r.getStart());
  851. return s;
  852. }
  853. public static BcelShadow makeFieldSet(
  854. BcelWorld world,
  855. LazyMethodGen enclosingMethod,
  856. InstructionHandle setHandle,
  857. BcelShadow enclosingShadow)
  858. {
  859. final InstructionList body = enclosingMethod.getBody();
  860. BcelShadow s =
  861. new BcelShadow(
  862. world,
  863. FieldSet,
  864. BcelWorld.makeFieldJoinPointSignature(
  865. enclosingMethod.getEnclosingClass(),
  866. (FieldInstruction) setHandle.getInstruction()),
  867. enclosingMethod,
  868. enclosingShadow);
  869. ShadowRange r = new ShadowRange(body);
  870. r.associateWithShadow(s);
  871. r.associateWithTargets(
  872. Range.genStart(body, setHandle),
  873. Range.genEnd(body, setHandle));
  874. retargetAllBranches(setHandle, r.getStart());
  875. return s;
  876. }
  877. public static void retargetAllBranches(InstructionHandle from, InstructionHandle to) {
  878. InstructionTargeter[] sources = from.getTargeters();
  879. if (sources != null) {
  880. for (int i = sources.length - 1; i >= 0; i--) {
  881. InstructionTargeter source = sources[i];
  882. if (source instanceof BranchInstruction) {
  883. source.updateTarget(from, to);
  884. }
  885. }
  886. }
  887. }
  888. // // ---- type access methods
  889. // private ObjectType getTargetBcelType() {
  890. // return (ObjectType) BcelWorld.makeBcelType(getTargetType());
  891. // }
  892. // private Type getArgBcelType(int arg) {
  893. // return BcelWorld.makeBcelType(getArgType(arg));
  894. // }
  895. // ---- kinding
  896. /**
  897. * If the end of my range has no real instructions following then
  898. * my context needs a return at the end.
  899. */
  900. public boolean terminatesWithReturn() {
  901. return getRange().getRealNext() == null;
  902. }
  903. /**
  904. * Is arg0 occupied with the value of this
  905. */
  906. public boolean arg0HoldsThis() {
  907. if (getKind().isEnclosingKind()) {
  908. return !getSignature().isStatic();
  909. } else if (enclosingShadow == null) {
  910. //XXX this is mostly right
  911. // this doesn't do the right thing for calls in the pre part of introduced constructors.
  912. return !enclosingMethod.isStatic();
  913. } else {
  914. return ((BcelShadow)enclosingShadow).arg0HoldsThis();
  915. }
  916. }
  917. // ---- argument getting methods
  918. private BcelVar thisVar = null;
  919. private BcelVar targetVar = null;
  920. private BcelVar[] argVars = null;
  921. private Map/*<UnresolvedType,BcelVar>*/ kindedAnnotationVars = null;
  922. private Map/*<UnresolvedType,BcelVar>*/ thisAnnotationVars = null;
  923. private Map/*<UnresolvedType,BcelVar>*/ targetAnnotationVars = null;
  924. private Map/*<UnresolvedType,BcelVar>*/[] argAnnotationVars = null;
  925. private Map/*<UnresolvedType,BcelVar>*/ withinAnnotationVars = null;
  926. private Map/*<UnresolvedType,BcelVar>*/ withincodeAnnotationVars = null;
  927. public Var getThisVar() {
  928. if (!hasThis()) {
  929. throw new IllegalStateException("no this");
  930. }
  931. initializeThisVar();
  932. return thisVar;
  933. }
  934. public Var getThisAnnotationVar(UnresolvedType forAnnotationType) {
  935. if (!hasThis()) {
  936. throw new IllegalStateException("no this");
  937. }
  938. initializeThisAnnotationVars(); // FIXME asc Why bother with this if we always return one?
  939. // Even if we can't find one, we have to return one as we might have this annotation at runtime
  940. Var v = (Var) thisAnnotationVars.get(forAnnotationType);
  941. if (v==null)
  942. v = new TypeAnnotationAccessVar(forAnnotationType.resolve(world),(BcelVar)getThisVar());
  943. return v;
  944. }
  945. public Var getTargetVar() {
  946. if (!hasTarget()) {
  947. throw new IllegalStateException("no target");
  948. }
  949. initializeTargetVar();
  950. return targetVar;
  951. }
  952. public Var getTargetAnnotationVar(UnresolvedType forAnnotationType) {
  953. if (!hasTarget()) {
  954. throw new IllegalStateException("no target");
  955. }
  956. initializeTargetAnnotationVars(); // FIXME asc why bother with this if we always return one?
  957. Var v =(Var) targetAnnotationVars.get(forAnnotationType);
  958. // Even if we can't find one, we have to return one as we might have this annotation at runtime
  959. if (v==null)
  960. v = new TypeAnnotationAccessVar(forAnnotationType.resolve(world),(BcelVar)getTargetVar());
  961. return v;
  962. }
  963. public Var getArgVar(int i) {
  964. initializeArgVars();
  965. return argVars[i];
  966. }
  967. public Var getArgAnnotationVar(int i,UnresolvedType forAnnotationType) {
  968. initializeArgAnnotationVars();
  969. Var v= (Var) argAnnotationVars[i].get(forAnnotationType);
  970. if (v==null)
  971. v = new TypeAnnotationAccessVar(forAnnotationType.resolve(world),(BcelVar)getArgVar(i));
  972. return v;
  973. }
  974. public Var getKindedAnnotationVar(UnresolvedType forAnnotationType) {
  975. initializeKindedAnnotationVars();
  976. return (Var) kindedAnnotationVars.get(forAnnotationType);
  977. }
  978. public Var getWithinAnnotationVar(UnresolvedType forAnnotationType) {
  979. initializeWithinAnnotationVars();
  980. return (Var) withinAnnotationVars.get(forAnnotationType);
  981. }
  982. public Var getWithinCodeAnnotationVar(UnresolvedType forAnnotationType) {
  983. initializeWithinCodeAnnotationVars();
  984. return (Var) withincodeAnnotationVars.get(forAnnotationType);
  985. }
  986. // reflective thisJoinPoint support
  987. private BcelVar thisJoinPointVar = null;
  988. private boolean isThisJoinPointLazy;
  989. private int lazyTjpConsumers = 0;
  990. private BcelVar thisJoinPointStaticPartVar = null;
  991. // private BcelVar thisEnclosingJoinPointStaticPartVar = null;
  992. public final Var getThisJoinPointStaticPartVar() {
  993. return getThisJoinPointStaticPartBcelVar();
  994. }
  995. public final Var getThisEnclosingJoinPointStaticPartVar() {
  996. return getThisEnclosingJoinPointStaticPartBcelVar();
  997. }
  998. public void requireThisJoinPoint(boolean hasGuardTest, boolean isAround) {
  999. if (!isAround){
  1000. if (!hasGuardTest) {
  1001. isThisJoinPointLazy = false;
  1002. } else {
  1003. lazyTjpConsumers++;
  1004. }
  1005. }
  1006. // if (!hasGuardTest) {
  1007. // isThisJoinPointLazy = false;
  1008. // } else {
  1009. // lazyTjpConsumers++;
  1010. // }
  1011. if (thisJoinPointVar == null) {
  1012. thisJoinPointVar = genTempVar(UnresolvedType.forName("org.aspectj.lang.JoinPoint"));
  1013. }
  1014. }
  1015. public Var getThisJoinPointVar() {
  1016. requireThisJoinPoint(false,false);
  1017. return thisJoinPointVar;
  1018. }
  1019. void initializeThisJoinPoint() {
  1020. if (thisJoinPointVar == null) return;
  1021. if (isThisJoinPointLazy) {
  1022. isThisJoinPointLazy = checkLazyTjp();
  1023. }
  1024. if (isThisJoinPointLazy) {
  1025. appliedLazyTjpOptimization = true;
  1026. createThisJoinPoint(); // make sure any state needed is initialized, but throw the instructions out
  1027. if (lazyTjpConsumers == 1) return; // special case only one lazyTjpUser
  1028. InstructionFactory fact = getFactory();
  1029. InstructionList il = new InstructionList();
  1030. il.append(InstructionConstants.ACONST_NULL);
  1031. il.append(thisJoinPointVar.createStore(fact));
  1032. range.insert(il, Range.OutsideBefore);
  1033. } else {
  1034. appliedLazyTjpOptimization = false;
  1035. InstructionFactory fact = getFactory();
  1036. InstructionList il = createThisJoinPoint();
  1037. il.append(thisJoinPointVar.createStore(fact));
  1038. range.insert(il, Range.OutsideBefore);
  1039. }
  1040. }
  1041. private boolean checkLazyTjp() {
  1042. // check for around advice
  1043. for (Iterator i = mungers.iterator(); i.hasNext();) {
  1044. ShadowMunger munger = (ShadowMunger) i.next();
  1045. if (munger instanceof Advice) {
  1046. if ( ((Advice)munger).getKind() == AdviceKind.Around) {
  1047. if (munger.getSourceLocation()!=null) { // do we know enough to bother reporting?
  1048. if (world.getLint().canNotImplementLazyTjp.isEnabled()) {
  1049. world.getLint().canNotImplementLazyTjp.signal(
  1050. new String[] {toString()},
  1051. getSourceLocation(),
  1052. new ISourceLocation[] { munger.getSourceLocation() }
  1053. );
  1054. }
  1055. }
  1056. return false;
  1057. }
  1058. }
  1059. }
  1060. return true;
  1061. }
  1062. InstructionList loadThisJoinPoint() {
  1063. InstructionFactory fact = getFactory();
  1064. InstructionList il = new InstructionList();
  1065. if (isThisJoinPointLazy) {
  1066. // If we're lazy, build the join point right here.
  1067. il.append(createThisJoinPoint());
  1068. // Does someone else need it? If so, store it for later retrieval
  1069. if (lazyTjpConsumers > 1) {
  1070. il.append(thisJoinPointVar.createStore(fact));
  1071. InstructionHandle end = il.append(thisJoinPointVar.createLoad(fact));
  1072. il.insert(InstructionFactory.createBranchInstruction(Constants.IFNONNULL, end));
  1073. il.insert(thisJoinPointVar.createLoad(fact));
  1074. }
  1075. } else {
  1076. // If not lazy, its already been built and stored, just retrieve it
  1077. thisJoinPointVar.appendLoad(il, fact);
  1078. }
  1079. return il;
  1080. }
  1081. InstructionList createThisJoinPoint() {
  1082. InstructionFactory fact = getFactory();
  1083. InstructionList il = new InstructionList();
  1084. BcelVar staticPart = getThisJoinPointStaticPartBcelVar();
  1085. staticPart.appendLoad(il, fact);
  1086. if (hasThis()) {
  1087. ((BcelVar)getThisVar()).appendLoad(il, fact);
  1088. } else {
  1089. il.append(new ACONST_NULL());
  1090. }
  1091. if (hasTarget()) {
  1092. ((BcelVar)getTargetVar()).appendLoad(il, fact);
  1093. } else {
  1094. il.append(new ACONST_NULL());
  1095. }
  1096. switch(getArgCount()) {
  1097. case 0:
  1098. il.append(fact.createInvoke("org.aspectj.runtime.reflect.Factory",
  1099. "makeJP", LazyClassGen.tjpType,
  1100. new Type[] { LazyClassGen.staticTjpType,
  1101. Type.OBJECT, Type.OBJECT},
  1102. Constants.INVOKESTATIC));
  1103. break;
  1104. case 1:
  1105. ((BcelVar)getArgVar(0)).appendLoadAndConvert(il, fact, world.getCoreType(ResolvedType.OBJECT));
  1106. il.append(fact.createInvoke("org.aspectj.runtime.reflect.Factory",
  1107. "makeJP", LazyClassGen.tjpType,
  1108. new Type[] { LazyClassGen.staticTjpType,
  1109. Type.OBJECT, Type.OBJECT, Type.OBJECT},
  1110. Constants.INVOKESTATIC));
  1111. break;
  1112. case 2:
  1113. ((BcelVar)getArgVar(0)).appendLoadAndConvert(il, fact, world.getCoreType(ResolvedType.OBJECT));
  1114. ((BcelVar)getArgVar(1)).appendLoadAndConvert(il, fact, world.getCoreType(ResolvedType.OBJECT));
  1115. il.append(fact.createInvoke("org.aspectj.runtime.reflect.Factory",
  1116. "makeJP", LazyClassGen.tjpType,
  1117. new Type[] { LazyClassGen.staticTjpType,
  1118. Type.OBJECT, Type.OBJECT, Type.OBJECT, Type.OBJECT},
  1119. Constants.INVOKESTATIC));
  1120. break;
  1121. default:
  1122. il.append(makeArgsObjectArray());
  1123. il.append(fact.createInvoke("org.aspectj.runtime.reflect.Factory",
  1124. "makeJP", LazyClassGen.tjpType,
  1125. new Type[] { LazyClassGen.staticTjpType,
  1126. Type.OBJECT, Type.OBJECT, new ArrayType(Type.OBJECT, 1)},
  1127. Constants.INVOKESTATIC));
  1128. break;
  1129. }
  1130. return il;
  1131. }
  1132. /**
  1133. * Get the Var for the jpStaticPart
  1134. * @return
  1135. */
  1136. public BcelVar getThisJoinPointStaticPartBcelVar() {
  1137. return getThisJoinPointStaticPartBcelVar(false);
  1138. }
  1139. /**
  1140. * Get the Var for the xxxxJpStaticPart, xxx = this or enclosing
  1141. * @param isEnclosingJp true to have the enclosingJpStaticPart
  1142. * @return
  1143. */
  1144. public BcelVar getThisJoinPointStaticPartBcelVar(final boolean isEnclosingJp) {
  1145. if (thisJoinPointStaticPartVar == null) {
  1146. Field field = getEnclosingClass().getTjpField(this, isEnclosingJp);
  1147. ResolvedType sjpType = null;
  1148. if (world.isTargettingAspectJRuntime12()) { // TAG:SUPPORTING12: We didn't have different jpsp types in 1.2
  1149. sjpType = world.getCoreType(UnresolvedType.forName("org.aspectj.lang.JoinPoint$StaticPart"));
  1150. } else {
  1151. sjpType = isEnclosingJp?
  1152. world.getCoreType(UnresolvedType.forName("org.aspectj.lang.JoinPoint$EnclosingStaticPart")):
  1153. world.getCoreType(UnresolvedType.forName("org.aspectj.lang.JoinPoint$StaticPart"));
  1154. }
  1155. thisJoinPointStaticPartVar = new BcelFieldRef(
  1156. sjpType,
  1157. getEnclosingClass().getClassName(),
  1158. field.getName());
  1159. // getEnclosingClass().warnOnAddedStaticInitializer(this,munger.getSourceLocation());
  1160. }
  1161. return thisJoinPointStaticPartVar;
  1162. }
  1163. /**
  1164. * Get the Var for the enclosingJpStaticPart
  1165. * @return
  1166. */
  1167. public BcelVar getThisEnclosingJoinPointStaticPartBcelVar() {
  1168. if (enclosingShadow == null) {
  1169. // the enclosing of an execution is itself
  1170. return getThisJoinPointStaticPartBcelVar(true);
  1171. } else {
  1172. return ((BcelShadow)enclosingShadow).getThisJoinPointStaticPartBcelVar(true);
  1173. }
  1174. }
  1175. //??? need to better understand all the enclosing variants
  1176. public Member getEnclosingCodeSignature() {
  1177. if (getKind().isEnclosingKind()) {
  1178. return getSignature();
  1179. } else if (getKind() == Shadow.PreInitialization) {
  1180. // PreInit doesn't enclose code but its signature
  1181. // is correctly the signature of the ctor.
  1182. return getSignature();
  1183. } else if (enclosingShadow == null) {
  1184. return getEnclosingMethod().getMemberView();
  1185. } else {
  1186. return enclosingShadow.getSignature();
  1187. }
  1188. }
  1189. private InstructionList makeArgsObjectArray() {
  1190. InstructionFactory fact = getFactory();
  1191. BcelVar arrayVar = genTempVar(UnresolvedType.OBJECTARRAY);
  1192. final InstructionList il = new InstructionList();
  1193. int alen = getArgCount() ;
  1194. il.append(Utility.createConstant(fact, alen));
  1195. il.append((Instruction)fact.createNewArray(Type.OBJECT, (short)1));
  1196. arrayVar.appendStore(il, fact);
  1197. int stateIndex = 0;
  1198. for (int i = 0, len = getArgCount(); i<len; i++) {
  1199. arrayVar.appendConvertableArrayStore(il, fact, stateIndex, (BcelVar)getArgVar(i));
  1200. stateIndex++;
  1201. }
  1202. arrayVar.appendLoad(il, fact);
  1203. return il;
  1204. }
  1205. // ---- initializing var tables
  1206. /* initializing this is doesn't do anything, because this
  1207. * is protected from side-effects, so we don't need to copy its location
  1208. */
  1209. private void initializeThisVar() {
  1210. if (thisVar != null) return;
  1211. thisVar = new BcelVar(getThisType().resolve(world), 0);
  1212. thisVar.setPositionInAroundState(0);
  1213. }
  1214. public void initializeTargetVar() {
  1215. InstructionFactory fact = getFactory();
  1216. if (targetVar != null) return;
  1217. if (getKind().isTargetSameAsThis()) {
  1218. if (hasThis()) initializeThisVar();
  1219. targetVar = thisVar;
  1220. } else {
  1221. initializeArgVars(); // gotta pop off the args before we find the target
  1222. UnresolvedType type = getTargetType();
  1223. type = ensureTargetTypeIsCorrect(type);
  1224. targetVar = genTempVar(type, "ajc$target");
  1225. range.insert(targetVar.createStore(fact), Range.OutsideBefore);
  1226. targetVar.setPositionInAroundState(hasThis() ? 1 : 0);
  1227. }
  1228. }
  1229. /* PR 72528
  1230. * This method double checks the target type under certain conditions. The Java 1.4
  1231. * compilers seem to take calls to clone methods on array types and create bytecode that
  1232. * looks like clone is being called on Object. If we advise a clone call with around
  1233. * advice we extract the call into a helper method which we can then refer to. Because the
  1234. * type in the bytecode for the call to clone is Object we create a helper method with
  1235. * an Object parameter - this is not correct as we have lost the fact that the actual
  1236. * type is an array type. If we don't do the check below we will create code that fails
  1237. * java verification. This method checks for the peculiar set of conditions and if they
  1238. * are true, it has a sneak peek at the code before the call to see what is on the stack.
  1239. */
  1240. public UnresolvedType ensureTargetTypeIsCorrect(UnresolvedType tx) {
  1241. Member msig = getSignature();
  1242. if (msig.getArity()==0 &&
  1243. getKind() == MethodCall &&
  1244. msig.getName().charAt(0) == 'c' &&
  1245. tx.equals(ResolvedType.OBJECT) &&
  1246. msig.getReturnType().equals(ResolvedType.OBJECT) &&
  1247. msig.getName().equals("clone")) {
  1248. // Lets go back through the code from the start of the shadow
  1249. InstructionHandle searchPtr = range.getStart().getPrev();
  1250. while (Range.isRangeHandle(searchPtr) ||
  1251. searchPtr.getInstruction() instanceof StoreInstruction) { // ignore this instruction - it doesnt give us the info we want
  1252. searchPtr = searchPtr.getPrev();
  1253. }
  1254. // A load instruction may tell us the real type of what the clone() call is on
  1255. if (searchPtr.getInstruction() instanceof LoadInstruction) {
  1256. LoadInstruction li = (LoadInstruction)searchPtr.getInstruction();
  1257. li.getIndex();
  1258. LocalVariableTag lvt = LazyMethodGen.getLocalVariableTag(searchPtr,li.getIndex());
  1259. if (lvt!=null) return UnresolvedType.forSignature(lvt.getType());
  1260. }
  1261. // A field access instruction may tell us the real type of what the clone() call is on
  1262. if (searchPtr.getInstruction() instanceof FieldInstruction) {
  1263. FieldInstruction si = (FieldInstruction)searchPtr.getInstruction();
  1264. Type t = si.getFieldType(getEnclosingClass().getConstantPoolGen());
  1265. return BcelWorld.fromBcel(t);
  1266. }
  1267. // A new array instruction obviously tells us it is an array type !
  1268. if (searchPtr.getInstruction() instanceof ANEWARRAY) {
  1269. //ANEWARRAY ana = (ANEWARRAY)searchPoint.getInstruction();
  1270. //Type t = ana.getType(getEnclosingClass().getConstantPoolGen());
  1271. // Just use a standard java.lang.object array - that will work fine
  1272. return BcelWorld.fromBcel(new ArrayType(Type.OBJECT,1));
  1273. }
  1274. // A multi new array instruction obviously tells us it is an array type !
  1275. if (searchPtr.getInstruction() instanceof MULTIANEWARRAY) {
  1276. MULTIANEWARRAY ana = (MULTIANEWARRAY)searchPtr.getInstruction();
  1277. // Type t = ana.getType(getEnclosingClass().getConstantPoolGen());
  1278. // t = new ArrayType(t,ana.getDimensions());
  1279. // Just use a standard java.lang.object array - that will work fine
  1280. return BcelWorld.fromBcel(new ArrayType(Type.OBJECT,ana.getDimensions()));
  1281. }
  1282. throw new BCException("Can't determine real target of clone() when processing instruction "+
  1283. searchPtr.getInstruction()+". Perhaps avoid selecting clone with your pointcut?");
  1284. }
  1285. return tx;
  1286. }
  1287. public void initializeArgVars() {
  1288. if (argVars != null) return;
  1289. InstructionFactory fact = getFactory();
  1290. int len = getArgCount();
  1291. argVars = new BcelVar[len];
  1292. int positionOffset = (hasTarget() ? 1 : 0) +
  1293. ((hasThis() && !getKind().isTargetSameAsThis()) ? 1 : 0);
  1294. if (getKind().argsOnStack()) {
  1295. // we move backwards because we're popping off the stack
  1296. for (int i = len - 1; i >= 0; i--) {
  1297. UnresolvedType type = getArgType(i);
  1298. BcelVar tmp = genTempVar(type, "ajc$arg" + i);
  1299. range.insert(tmp.createStore(getFactory()), Range.OutsideBefore);
  1300. int position = i;
  1301. position += positionOffset;
  1302. tmp.setPositionInAroundState(position);
  1303. argVars[i] = tmp;
  1304. }
  1305. } else {
  1306. int index = 0;
  1307. if (arg0HoldsThis()) index++;
  1308. for (int i = 0; i < len; i++) {
  1309. UnresolvedType type = getArgType(i);
  1310. BcelVar tmp = genTempVar(type, "ajc$arg" + i);
  1311. range.insert(tmp.createCopyFrom(fact, index), Range.OutsideBefore);
  1312. argVars[i] = tmp;
  1313. int position = i;
  1314. position += positionOffset;
  1315. // System.out.println("set position: " + tmp + ", " + position + " in " + this);
  1316. // System.out.println(" hasThis: " + hasThis() + ", hasTarget: " + hasTarget());
  1317. tmp.setPositionInAroundState(position);
  1318. index += type.getSize();
  1319. }
  1320. }
  1321. }
  1322. public void initializeForAroundClosure() {
  1323. initializeArgVars();
  1324. if (hasTarget()) initializeTargetVar();
  1325. if (hasThis()) initializeThisVar();
  1326. // System.out.println("initialized: " + this + " thisVar = " + thisVar);
  1327. }
  1328. public void initializeThisAnnotationVars() {
  1329. if (thisAnnotationVars != null) return;
  1330. thisAnnotationVars = new HashMap();
  1331. // populate..
  1332. }
  1333. public void initializeTargetAnnotationVars() {
  1334. if (targetAnnotationVars != null) return;
  1335. if (getKind().isTargetSameAsThis()) {
  1336. if (hasThis()) initializeThisAnnotationVars();
  1337. targetAnnotationVars = thisAnnotationVars;
  1338. } else {
  1339. targetAnnotationVars = new HashMap();
  1340. ResolvedType[] rtx = this.getTargetType().resolve(world).getAnnotationTypes(); // what about annotations we havent gotten yet but we will get in subclasses?
  1341. for (int i = 0; i < rtx.length; i++) {
  1342. ResolvedType typeX = rtx[i];
  1343. targetAnnotationVars.put(typeX,new TypeAnnotationAccessVar(typeX,(BcelVar)getTargetVar()));
  1344. }
  1345. // populate.
  1346. }
  1347. }
  1348. public void initializeArgAnnotationVars() {
  1349. if (argAnnotationVars != null) return;
  1350. int numArgs = getArgCount();
  1351. argAnnotationVars = new Map[numArgs];
  1352. for (int i = 0; i < argAnnotationVars.length; i++) {
  1353. argAnnotationVars[i] = new HashMap();
  1354. //FIXME asc just delete this logic - we always build the Var on demand, as we don't know at weave time
  1355. // what the full set of annotations could be (due to static/dynamic type differences...)
  1356. }
  1357. }
  1358. protected Member getRelevantMember(Member foundMember, Member relevantMember, ResolvedType relevantType){
  1359. if (foundMember != null){
  1360. return foundMember;
  1361. }
  1362. foundMember = getSignature().resolve(world);
  1363. if (foundMember == null && relevantMember != null) {
  1364. foundMember = relevantType.lookupMemberWithSupersAndITDs(relevantMember);
  1365. }
  1366. // check the ITD'd dooberries
  1367. List mungers = relevantType.resolve(world).getInterTypeMungers();
  1368. for (Iterator iter = mungers.iterator(); iter.hasNext();) {
  1369. BcelTypeMunger typeMunger = (BcelTypeMunger) iter.next();
  1370. if (typeMunger.getMunger() instanceof NewMethodTypeMunger ||
  1371. typeMunger.getMunger() instanceof NewConstructorTypeMunger) {
  1372. ResolvedMember fakerm = typeMunger.getSignature();
  1373. if (fakerm.getName().equals(getSignature().getName()) &&
  1374. fakerm.getParameterSignature().equals(getSignature().getParameterSignature())){
  1375. if (foundMember.getKind()==ResolvedMember.CONSTRUCTOR){
  1376. foundMember = AjcMemberMaker.interConstructor(
  1377. relevantType,
  1378. (ResolvedMember)foundMember,
  1379. typeMunger.getAspectType());
  1380. } else {
  1381. foundMember = AjcMemberMaker.interMethod((ResolvedMember)foundMember,
  1382. typeMunger.getAspectType(), false);
  1383. }
  1384. // in the above.. what about if it's on an Interface? Can that happen?
  1385. // then the last arg of the above should be true
  1386. return foundMember;
  1387. }
  1388. }
  1389. }
  1390. return foundMember;
  1391. }
  1392. protected ResolvedType [] getAnnotations(Member foundMember, Member relevantMember, ResolvedType relevantType){
  1393. if (foundMember == null){
  1394. // check the ITD'd dooberries
  1395. List mungers = relevantType.resolve(world).getInterTypeMungers();
  1396. for (Iterator iter = mungers.iterator(); iter.hasNext();) {
  1397. BcelTypeMunger typeMunger = (BcelTypeMunger) iter.next();
  1398. if (typeMunger.getMunger() instanceof NewMethodTypeMunger ||
  1399. typeMunger.getMunger() instanceof NewConstructorTypeMunger) {
  1400. ResolvedMember fakerm = typeMunger.getSignature();
  1401. //if (fakerm.hasAnnotations())
  1402. ResolvedMember ajcMethod = (getSignature().getKind()==ResolvedMember.CONSTRUCTOR?
  1403. AjcMemberMaker.postIntroducedConstructor(typeMunger.getAspectType(),fakerm.getDeclaringType(),fakerm.getParameterTypes()):
  1404. AjcMemberMaker.interMethodDispatcher(fakerm,typeMunger.getAspectType()));
  1405. //AjcMemberMaker.interMethodBody(fakerm,typeMunger.getAspectType()));
  1406. ResolvedMember rmm = findMethod(typeMunger.getAspectType(),ajcMethod);
  1407. if (fakerm.getName().equals(getSignature().getName()) &&
  1408. fakerm.getParameterSignature().equals(getSignature().getParameterSignature())) {
  1409. relevantType = typeMunger.getAspectType();
  1410. foundMember = rmm;
  1411. return foundMember.getAnnotationTypes();
  1412. }
  1413. }
  1414. }
  1415. // didn't find in ITDs, look in supers
  1416. foundMember = relevantType.lookupMemberWithSupersAndITDs(relevantMember);
  1417. if (foundMember == null) {
  1418. throw new IllegalStateException("Couldn't find member " + relevantMember + " for type " + relevantType);
  1419. }
  1420. }
  1421. return foundMember.getAnnotationTypes();
  1422. }
  1423. public void initializeKindedAnnotationVars() {
  1424. if (kindedAnnotationVars != null) return;
  1425. kindedAnnotationVars = new HashMap();
  1426. // by determining what "kind" of shadow we are, we can find out the
  1427. // annotations on the appropriate element (method, field, constructor, type).
  1428. // Then create one BcelVar entry in the map for each annotation, keyed by
  1429. // annotation type (UnresolvedType).
  1430. // FIXME asc Refactor this code, there is duplication
  1431. ResolvedType[] annotations = null;
  1432. Member relevantMember = getSignature();
  1433. ResolvedType relevantType = relevantMember.getDeclaringType().resolve(world);
  1434. if (relevantType.isRawType() || relevantType.isParameterizedType()) relevantType = relevantType.getGenericType();
  1435. if (getKind() == Shadow.StaticInitialization) {
  1436. annotations = relevantType.resolve(world).getAnnotationTypes();
  1437. } else if (getKind() == Shadow.MethodCall || getKind() == Shadow.ConstructorCall) {
  1438. Member foundMember = findMethod2(relevantType.resolve(world).getDeclaredMethods(),getSignature());
  1439. annotations = getAnnotations(foundMember, relevantMember, relevantType);
  1440. relevantMember = getRelevantMember(foundMember,relevantMember,relevantType);
  1441. relevantType = relevantMember.getDeclaringType().resolve(world);
  1442. } else if (getKind() == Shadow.FieldSet || getKind() == Shadow.FieldGet) {
  1443. relevantMember = findField(relevantType.getDeclaredFields(),getSignature());
  1444. if (relevantMember==null) {
  1445. // check the ITD'd dooberries
  1446. List mungers = relevantType.resolve(world).getInterTypeMungers();
  1447. for (Iterator iter = mungers.iterator(); iter.hasNext();) {
  1448. BcelTypeMunger typeMunger = (BcelTypeMunger) iter.next();
  1449. if (typeMunger.getMunger() instanceof NewFieldTypeMunger) {
  1450. ResolvedMember fakerm = typeMunger.getSignature();
  1451. //if (fakerm.hasAnnotations())
  1452. ResolvedMember ajcMethod = AjcMemberMaker.interFieldInitializer(fakerm,typeMunger.getAspectType());
  1453. ResolvedMember rmm = findMethod(typeMunger.getAspectType(),ajcMethod);
  1454. if (fakerm.equals(getSignature())) {
  1455. relevantType = typeMunger.getAspectType();
  1456. relevantMember = rmm;
  1457. }
  1458. }
  1459. }
  1460. }
  1461. annotations = relevantMember.getAnnotationTypes();
  1462. } else if (getKind() == Shadow.MethodExecution || getKind() == Shadow.ConstructorExecution ||
  1463. getKind() == Shadow.AdviceExecution) {
  1464. //ResolvedMember rm[] = relevantType.getDeclaredMethods();
  1465. Member foundMember = findMethod2(relevantType.getDeclaredMethods(),getSignature());
  1466. annotations = getAnnotations(foundMember, relevantMember, relevantType);
  1467. relevantMember = foundMember;
  1468. relevantMember = getRelevantMember(foundMember, relevantMember,relevantType);
  1469. } else if (getKind() == Shadow.ExceptionHandler) {
  1470. relevantType = getSignature().getParameterTypes()[0].resolve(world);
  1471. annotations = relevantType.getAnnotationTypes();
  1472. } else if (getKind() == Shadow.PreInitialization || getKind() == Shadow.Initialization) {
  1473. ResolvedMember found = findMethod2(relevantType.getDeclaredMethods(),getSignature());
  1474. annotations = found.getAnnotationTypes();
  1475. }
  1476. if (annotations == null) {
  1477. // We can't have recognized the shadow - should blow up now to be on the safe side
  1478. throw new BCException("Couldn't discover annotations for shadow: "+getKind());
  1479. }
  1480. for (int i = 0; i < annotations.length; i++) {
  1481. ResolvedType aTX = annotations[i];
  1482. KindedAnnotationAccessVar kaav = new KindedAnnotationAccessVar(getKind(),aTX.resolve(world),relevantType,relevantMember);
  1483. kindedAnnotationVars.put(aTX,kaav);
  1484. }
  1485. }
  1486. //FIXME asc whats the real diff between this one and the version in findMethod()?
  1487. ResolvedMember findMethod2(ResolvedMember rm[], Member sig) {
  1488. ResolvedMember found = null;
  1489. // String searchString = getSignature().getName()+getSignature().getParameterSignature();
  1490. for (int i = 0; i < rm.length && found==null; i++) {
  1491. ResolvedMember member = rm[i];
  1492. if (member.getName().equals(sig.getName()) && member.getParameterSignature().equals(sig.getParameterSignature()))
  1493. found = member;
  1494. }
  1495. return found;
  1496. }
  1497. private ResolvedMember findMethod(ResolvedType aspectType, ResolvedMember ajcMethod) {
  1498. ResolvedMember decMethods[] = aspectType.getDeclaredMethods();
  1499. for (int i = 0; i < decMethods.length; i++) {
  1500. ResolvedMember member = decMethods[i];
  1501. if (member.equals(ajcMethod)) return member;
  1502. }
  1503. return null;
  1504. }
  1505. private ResolvedMember findField(ResolvedMember[] members,Member lookingFor) {
  1506. for (int i = 0; i < members.length; i++) {
  1507. ResolvedMember member = members[i];
  1508. if ( member.getName().equals(getSignature().getName()) &&
  1509. member.getType().equals(getSignature().getType())) {
  1510. return member;
  1511. }
  1512. }
  1513. return null;
  1514. }
  1515. public void initializeWithinAnnotationVars() {
  1516. if (withinAnnotationVars != null) return;
  1517. withinAnnotationVars = new HashMap();
  1518. ResolvedType[] annotations = getEnclosingType().resolve(world).getAnnotationTypes();
  1519. for (int i = 0; i < annotations.length; i++) {
  1520. ResolvedType ann = annotations[i];
  1521. Kind k = Shadow.StaticInitialization;
  1522. withinAnnotationVars.put(ann,new KindedAnnotationAccessVar(k,ann,getEnclosingType(),null));
  1523. }
  1524. }
  1525. public void initializeWithinCodeAnnotationVars() {
  1526. if (withincodeAnnotationVars != null) return;
  1527. withincodeAnnotationVars = new HashMap();
  1528. // For some shadow we are interested in annotations on the method containing that shadow.
  1529. ResolvedType[] annotations = getEnclosingMethod().getMemberView().getAnnotationTypes();
  1530. for (int i = 0; i < annotations.length; i++) {
  1531. ResolvedType ann = annotations[i];
  1532. Kind k = (getEnclosingMethod().getMemberView().getKind()==Member.CONSTRUCTOR?
  1533. Shadow.ConstructorExecution:Shadow.MethodExecution);
  1534. withincodeAnnotationVars.put(ann,
  1535. new KindedAnnotationAccessVar(k,ann,getEnclosingType(),getEnclosingCodeSignature()));
  1536. }
  1537. }
  1538. // ---- weave methods
  1539. void weaveBefore(BcelAdvice munger) {
  1540. range.insert(
  1541. munger.getAdviceInstructions(this, null, range.getRealStart()),
  1542. Range.InsideBefore);
  1543. }
  1544. public void weaveAfter(BcelAdvice munger) {
  1545. weaveAfterThrowing(munger, UnresolvedType.THROWABLE);
  1546. weaveAfterReturning(munger);
  1547. }
  1548. /**
  1549. * The basic strategy here is to add a set of instructions at the end of
  1550. * the shadow range that dispatch the advice, and then return whatever the
  1551. * shadow was going to return anyway.
  1552. *
  1553. * To achieve this, we note all the return statements in the advice, and
  1554. * replace them with code that:
  1555. * 1) stores the return value on top of the stack in a temp var
  1556. * 2) jumps to the start of our advice block
  1557. * 3) restores the return value at the end of the advice block before
  1558. * ultimately returning
  1559. *
  1560. * We also need to bind the return value into a returning parameter, if the
  1561. * advice specified one.
  1562. */
  1563. public void weaveAfterReturning(BcelAdvice munger) {
  1564. List returns = findReturnInstructions();
  1565. boolean hasReturnInstructions = !returns.isEmpty();
  1566. // list of instructions that handle the actual return from the join point
  1567. InstructionList retList = new InstructionList();
  1568. // variable that holds the return value
  1569. BcelVar returnValueVar = null;
  1570. if (hasReturnInstructions) {
  1571. returnValueVar = generateReturnInstructions(returns,retList);
  1572. } else {
  1573. // we need at least one instruction, as the target for jumps
  1574. retList.append(InstructionConstants.NOP);
  1575. }
  1576. // list of instructions for dispatching to the advice itself
  1577. InstructionList advice = getAfterReturningAdviceDispatchInstructions(
  1578. munger, retList.getStart());
  1579. if (hasReturnInstructions) {
  1580. InstructionHandle gotoTarget = advice.getStart();
  1581. for (Iterator i = returns.iterator(); i.hasNext();) {
  1582. InstructionHandle ih = (InstructionHandle) i.next();
  1583. retargetReturnInstruction(munger.hasExtraParameter(), returnValueVar, gotoTarget, ih);
  1584. }
  1585. }
  1586. range.append(advice);
  1587. range.append(retList);
  1588. }
  1589. /**
  1590. * @return a list of all the return instructions in the range of this shadow
  1591. */
  1592. private List findReturnInstructions() {
  1593. List returns = new ArrayList();
  1594. for (InstructionHandle ih = range.getStart(); ih != range.getEnd(); ih = ih.getNext()) {
  1595. if (ih.getInstruction() instanceof ReturnInstruction) {
  1596. returns.add(ih);
  1597. }
  1598. }
  1599. return returns;
  1600. }
  1601. /**
  1602. * Given a list containing all the return instruction handles for this shadow,
  1603. * finds the last return instruction and copies it, making this the ultimate
  1604. * return. If the shadow has a non-void return type, we also create a temporary
  1605. * variable to hold the return value, and load the value from this var before
  1606. * returning (see pr148007 for why we do this - it works around a JRockit bug,
  1607. * and is also closer to what javac generates)
  1608. *
  1609. * Sometimes the 'last return' isnt the right one - some rogue code can
  1610. * include the real return from the body of a subroutine that exists at the end
  1611. * of the method. In this case the last return is RETURN but that may not be
  1612. * correct for a method with a non-void return type... pr151673
  1613. *
  1614. * @param returns list of all the return instructions in the shadow
  1615. * @param returnInstructions instruction list into which the return instructions should
  1616. * be generated
  1617. * @return the variable holding the return value, if needed
  1618. */
  1619. private BcelVar generateReturnInstructions(List returns, InstructionList returnInstructions) {
  1620. BcelVar returnValueVar = null;
  1621. if (this.hasANonVoidReturnType()) {
  1622. // Find the last *correct* return - this is a method with a non-void return type
  1623. // so ignore RETURN
  1624. Instruction newReturnInstruction = null;
  1625. int i=returns.size()-1;
  1626. while (newReturnInstruction == null && i>=0) {
  1627. InstructionHandle ih = (InstructionHandle)returns.get(i);
  1628. if (!(ih.getInstruction() instanceof RETURN)) {
  1629. newReturnInstruction = Utility.copyInstruction(ih.getInstruction());
  1630. }
  1631. i--;
  1632. }
  1633. returnValueVar = genTempVar(this.getReturnType());
  1634. returnValueVar.appendLoad(returnInstructions,getFactory());
  1635. returnInstructions.append(newReturnInstruction);
  1636. } else {
  1637. InstructionHandle lastReturnHandle = (InstructionHandle)returns.get(returns.size() - 1);
  1638. Instruction newReturnInstruction = Utility.copyInstruction(lastReturnHandle.getInstruction());
  1639. returnInstructions.append(newReturnInstruction);
  1640. }
  1641. return returnValueVar;
  1642. }
  1643. /**
  1644. * @return true, iff this shadow returns a value
  1645. */
  1646. private boolean hasANonVoidReturnType() {
  1647. return this.getReturnType() != ResolvedType.VOID;
  1648. }
  1649. /**
  1650. * Get the list of instructions used to dispatch to the after advice
  1651. * @param munger
  1652. * @param firstInstructionInReturnSequence
  1653. * @return
  1654. */
  1655. private InstructionList getAfterReturningAdviceDispatchInstructions(BcelAdvice munger, InstructionHandle firstInstructionInReturnSequence) {
  1656. InstructionList advice = new InstructionList();
  1657. BcelVar tempVar = null;
  1658. if (munger.hasExtraParameter()) {
  1659. tempVar = insertAdviceInstructionsForBindingReturningParameter(advice);
  1660. }
  1661. advice.append(munger.getAdviceInstructions(this, tempVar, firstInstructionInReturnSequence));
  1662. return advice;
  1663. }
  1664. /**
  1665. * If the after() returning(Foo f) form is used, bind the return value to the parameter.
  1666. * If the shadow returns void, bind null.
  1667. * @param advice
  1668. * @return
  1669. */
  1670. private BcelVar insertAdviceInstructionsForBindingReturningParameter(InstructionList advice) {
  1671. BcelVar tempVar;
  1672. UnresolvedType tempVarType = getReturnType();
  1673. if (tempVarType.equals(ResolvedType.VOID)) {
  1674. tempVar = genTempVar(UnresolvedType.OBJECT);
  1675. advice.append(InstructionConstants.ACONST_NULL);
  1676. tempVar.appendStore(advice, getFactory());
  1677. } else {
  1678. tempVar = genTempVar(tempVarType);
  1679. advice.append(InstructionFactory.createDup(tempVarType.getSize()));
  1680. tempVar.appendStore(advice, getFactory());
  1681. }
  1682. return tempVar;
  1683. }
  1684. /**
  1685. * Helper method for weaveAfterReturning
  1686. *
  1687. * Each return instruction in the method body is retargeted by calling this method.
  1688. * The return instruction is replaced by up to three instructions:
  1689. * 1) if the shadow returns a value, and that value is bound to an after returning
  1690. * parameter, then we DUP the return value on the top of the stack
  1691. * 2) if the shadow returns a value, we store it in the returnValueVar (it will
  1692. * be retrieved from here when we ultimately return after the advice dispatch)
  1693. * 3) if the return was the last instruction, we add a NOP (it will fall through
  1694. * to the advice dispatch), otherwise we add a GOTO that branches to the
  1695. * supplied gotoTarget (start of the advice dispatch)
  1696. */
  1697. private void retargetReturnInstruction(boolean hasReturningParameter, BcelVar returnValueVar, InstructionHandle gotoTarget, InstructionHandle returnHandle) {
  1698. // pr148007, work around JRockit bug
  1699. // replace ret with store into returnValueVar, followed by goto if not
  1700. // at the end of the instruction list...
  1701. InstructionList newInstructions = new InstructionList();
  1702. if (returnValueVar != null) {
  1703. if (hasReturningParameter) {
  1704. // we have to dup the return val before consuming it...
  1705. newInstructions.append(InstructionFactory.createDup(this.getReturnType().getSize()));
  1706. }
  1707. // store the return value into this var
  1708. returnValueVar.appendStore(newInstructions,getFactory());
  1709. }
  1710. if (!isLastInstructionInRange(returnHandle,range)) {
  1711. newInstructions.append(InstructionFactory.createBranchInstruction(
  1712. Constants.GOTO,
  1713. gotoTarget));
  1714. }
  1715. if (newInstructions.isEmpty()) {
  1716. newInstructions.append(InstructionConstants.NOP);
  1717. }
  1718. Utility.replaceInstruction(returnHandle,newInstructions,enclosingMethod);
  1719. }
  1720. private boolean isLastInstructionInRange(InstructionHandle ih, ShadowRange aRange) {
  1721. return ih.getNext() == aRange.getEnd();
  1722. }
  1723. public void weaveAfterThrowing(BcelAdvice munger, UnresolvedType catchType) {
  1724. // a good optimization would be not to generate anything here
  1725. // if the shadow is GUARANTEED empty (i.e., there's NOTHING, not even
  1726. // a shadow, inside me).
  1727. if (getRange().getStart().getNext() == getRange().getEnd()) return;
  1728. InstructionFactory fact = getFactory();
  1729. InstructionList handler = new InstructionList();
  1730. BcelVar exceptionVar = genTempVar(catchType);
  1731. exceptionVar.appendStore(handler, fact);
  1732. // pr62642
  1733. // I will now jump through some firey BCEL hoops to generate a trivial bit of code:
  1734. // if (exc instanceof ExceptionInInitializerError)
  1735. // throw (ExceptionInInitializerError)exc;
  1736. if (this.getEnclosingMethod().getName().equals("<clinit>")) {
  1737. ResolvedType eiieType = world.resolve("java.lang.ExceptionInInitializerError");
  1738. ObjectType eiieBcelType = (ObjectType)BcelWorld.makeBcelType(eiieType);
  1739. InstructionList ih = new InstructionList(InstructionConstants.NOP);
  1740. handler.append(exceptionVar.createLoad(fact));
  1741. handler.append(fact.createInstanceOf(eiieBcelType));
  1742. BranchInstruction bi =
  1743. InstructionFactory.createBranchInstruction(Constants.IFEQ,ih.getStart());
  1744. handler.append(bi);
  1745. handler.append(exceptionVar.createLoad(fact));
  1746. handler.append(fact.createCheckCast(eiieBcelType));
  1747. handler.append(InstructionConstants.ATHROW);
  1748. handler.append(ih);
  1749. }
  1750. InstructionList endHandler = new InstructionList(
  1751. exceptionVar.createLoad(fact));
  1752. handler.append(munger.getAdviceInstructions(this, exceptionVar, endHandler.getStart()));
  1753. handler.append(endHandler);
  1754. handler.append(InstructionConstants.ATHROW);
  1755. InstructionHandle handlerStart = handler.getStart();
  1756. if (isFallsThrough()) {
  1757. InstructionHandle jumpTarget = handler.append(InstructionConstants.NOP);
  1758. handler.insert(InstructionFactory.createBranchInstruction(Constants.GOTO, jumpTarget));
  1759. }
  1760. InstructionHandle protectedEnd = handler.getStart();
  1761. range.insert(handler, Range.InsideAfter);
  1762. enclosingMethod.addExceptionHandler(range.getStart().getNext(), protectedEnd.getPrev(),
  1763. handlerStart, (ObjectType)BcelWorld.makeBcelType(catchType), //???Type.THROWABLE,
  1764. // high priority if our args are on the stack
  1765. getKind().hasHighPriorityExceptions());
  1766. }
  1767. //??? this shares a lot of code with the above weaveAfterThrowing
  1768. //??? would be nice to abstract that to say things only once
  1769. public void weaveSoftener(BcelAdvice munger, UnresolvedType catchType) {
  1770. // a good optimization would be not to generate anything here
  1771. // if the shadow is GUARANTEED empty (i.e., there's NOTHING, not even
  1772. // a shadow, inside me).
  1773. if (getRange().getStart().getNext() == getRange().getEnd()) return;
  1774. InstructionFactory fact = getFactory();
  1775. InstructionList handler = new InstructionList();
  1776. InstructionList rtExHandler = new InstructionList();
  1777. BcelVar exceptionVar = genTempVar(catchType);
  1778. handler.append(fact.createNew(NameMangler.SOFT_EXCEPTION_TYPE));
  1779. handler.append(InstructionFactory.createDup(1));
  1780. handler.append(exceptionVar.createLoad(fact));
  1781. handler.append(fact.createInvoke(NameMangler.SOFT_EXCEPTION_TYPE, "<init>",
  1782. Type.VOID, new Type[] { Type.THROWABLE }, Constants.INVOKESPECIAL)); //??? special
  1783. handler.append(InstructionConstants.ATHROW);
  1784. // ENH 42737
  1785. exceptionVar.appendStore(rtExHandler, fact);
  1786. // aload_1
  1787. rtExHandler.append(exceptionVar.createLoad(fact));
  1788. // instanceof class java/lang/RuntimeException
  1789. rtExHandler.append(fact.createInstanceOf(new ObjectType("java.lang.RuntimeException")));
  1790. // ifeq go to new SOFT_EXCEPTION_TYPE instruction
  1791. rtExHandler.append(InstructionFactory.createBranchInstruction(Constants.IFEQ,handler.getStart()));
  1792. // aload_1
  1793. rtExHandler.append(exceptionVar.createLoad(fact));
  1794. // athrow
  1795. rtExHandler.append(InstructionFactory.ATHROW);
  1796. InstructionHandle handlerStart = rtExHandler.getStart();
  1797. if (isFallsThrough()) {
  1798. InstructionHandle jumpTarget = range.getEnd();//handler.append(fact.NOP);
  1799. rtExHandler.insert(InstructionFactory.createBranchInstruction(Constants.GOTO, jumpTarget));
  1800. }
  1801. rtExHandler.append(handler);
  1802. InstructionHandle protectedEnd = rtExHandler.getStart();
  1803. range.insert(rtExHandler, Range.InsideAfter);
  1804. enclosingMethod.addExceptionHandler(range.getStart().getNext(), protectedEnd.getPrev(),
  1805. handlerStart, (ObjectType)BcelWorld.makeBcelType(catchType),
  1806. // high priority if our args are on the stack
  1807. getKind().hasHighPriorityExceptions());
  1808. }
  1809. public void weavePerObjectEntry(final BcelAdvice munger, final BcelVar onVar) {
  1810. final InstructionFactory fact = getFactory();
  1811. InstructionList entryInstructions = new InstructionList();
  1812. InstructionList entrySuccessInstructions = new InstructionList();
  1813. onVar.appendLoad(entrySuccessInstructions, fact);
  1814. entrySuccessInstructions.append(
  1815. Utility.createInvoke(fact, world,
  1816. AjcMemberMaker.perObjectBind(munger.getConcreteAspect())));
  1817. InstructionList testInstructions =
  1818. munger.getTestInstructions(this, entrySuccessInstructions.getStart(),
  1819. range.getRealStart(),
  1820. entrySuccessInstructions.getStart());
  1821. entryInstructions.append(testInstructions);
  1822. entryInstructions.append(entrySuccessInstructions);
  1823. range.insert(entryInstructions, Range.InsideBefore);
  1824. }
  1825. // PTWIMPL Create static initializer to call the aspect factory
  1826. /**
  1827. * Causes the aspect instance to be *set* for later retrievable through localAspectof()/aspectOf()
  1828. */
  1829. public void weavePerTypeWithinAspectInitialization(final BcelAdvice munger,UnresolvedType t) {
  1830. if (t.resolve(world).isInterface()) return; // Don't initialize statics in
  1831. final InstructionFactory fact = getFactory();
  1832. InstructionList entryInstructions = new InstructionList();
  1833. InstructionList entrySuccessInstructions = new InstructionList();
  1834. BcelWorld.getBcelObjectType(munger.getConcreteAspect());
  1835. String aspectname = munger.getConcreteAspect().getName();
  1836. String ptwField = NameMangler.perTypeWithinFieldForTarget(munger.getConcreteAspect());
  1837. entrySuccessInstructions.append(new PUSH(fact.getConstantPool(),t.getName()));
  1838. entrySuccessInstructions.append(fact.createInvoke(aspectname,"ajc$createAspectInstance",new ObjectType(aspectname),
  1839. new Type[]{new ObjectType("java.lang.String")},Constants.INVOKESTATIC));
  1840. entrySuccessInstructions.append(fact.createPutStatic(t.getName(),ptwField,
  1841. new ObjectType(aspectname)));
  1842. entryInstructions.append(entrySuccessInstructions);
  1843. range.insert(entryInstructions, Range.InsideBefore);
  1844. }
  1845. public void weaveCflowEntry(final BcelAdvice munger, final Member cflowField) {
  1846. final boolean isPer = munger.getKind() == AdviceKind.PerCflowBelowEntry ||
  1847. munger.getKind() == AdviceKind.PerCflowEntry;
  1848. final Type objectArrayType = new ArrayType(Type.OBJECT, 1);
  1849. final InstructionFactory fact = getFactory();
  1850. final BcelVar testResult = genTempVar(ResolvedType.BOOLEAN);
  1851. InstructionList entryInstructions = new InstructionList();
  1852. {
  1853. InstructionList entrySuccessInstructions = new InstructionList();
  1854. if (munger.hasDynamicTests()) {
  1855. entryInstructions.append(Utility.createConstant(fact, 0));
  1856. testResult.appendStore(entryInstructions, fact);
  1857. entrySuccessInstructions.append(Utility.createConstant(fact, 1));
  1858. testResult.appendStore(entrySuccessInstructions, fact);
  1859. }
  1860. if (isPer) {
  1861. entrySuccessInstructions.append(
  1862. fact.createInvoke(munger.getConcreteAspect().getName(),
  1863. NameMangler.PERCFLOW_PUSH_METHOD,
  1864. Type.VOID,
  1865. new Type[] { },
  1866. Constants.INVOKESTATIC));
  1867. } else {
  1868. BcelVar[] cflowStateVars = munger.getExposedStateAsBcelVars(false);
  1869. if (cflowStateVars.length == 0) {
  1870. // This should be getting managed by a counter - lets make sure.
  1871. if (!cflowField.getType().getName().endsWith("CFlowCounter"))
  1872. throw new RuntimeException("Incorrectly attempting counter operation on stacked cflow");
  1873. entrySuccessInstructions.append(
  1874. Utility.createGet(fact, cflowField));
  1875. //arrayVar.appendLoad(entrySuccessInstructions, fact);
  1876. entrySuccessInstructions.append(fact.createInvoke(NameMangler.CFLOW_COUNTER_TYPE,"inc",Type.VOID,new Type[] { },Constants.INVOKEVIRTUAL));
  1877. } else {
  1878. BcelVar arrayVar = genTempVar(UnresolvedType.OBJECTARRAY);
  1879. int alen = cflowStateVars.length;
  1880. entrySuccessInstructions.append(Utility.createConstant(fact, alen));
  1881. entrySuccessInstructions.append(
  1882. (Instruction) fact.createNewArray(Type.OBJECT, (short) 1));
  1883. arrayVar.appendStore(entrySuccessInstructions, fact);
  1884. for (int i = 0; i < alen; i++) {
  1885. arrayVar.appendConvertableArrayStore(
  1886. entrySuccessInstructions,
  1887. fact,
  1888. i,
  1889. cflowStateVars[i]);
  1890. }
  1891. entrySuccessInstructions.append(
  1892. Utility.createGet(fact, cflowField));
  1893. arrayVar.appendLoad(entrySuccessInstructions, fact);
  1894. entrySuccessInstructions.append(
  1895. fact.createInvoke(NameMangler.CFLOW_STACK_TYPE, "push", Type.VOID,
  1896. new Type[] { objectArrayType },
  1897. Constants.INVOKEVIRTUAL));
  1898. }
  1899. }
  1900. InstructionList testInstructions =
  1901. munger.getTestInstructions(this, entrySuccessInstructions.getStart(),
  1902. range.getRealStart(),
  1903. entrySuccessInstructions.getStart());
  1904. entryInstructions.append(testInstructions);
  1905. entryInstructions.append(entrySuccessInstructions);
  1906. }
  1907. // this is the same for both per and non-per
  1908. weaveAfter(new BcelAdvice(null, null, null, 0, 0, 0, null, null) {
  1909. public InstructionList getAdviceInstructions(
  1910. BcelShadow s,
  1911. BcelVar extraArgVar,
  1912. InstructionHandle ifNoAdvice) {
  1913. InstructionList exitInstructions = new InstructionList();
  1914. if (munger.hasDynamicTests()) {
  1915. testResult.appendLoad(exitInstructions, fact);
  1916. exitInstructions.append(
  1917. InstructionFactory.createBranchInstruction(
  1918. Constants.IFEQ,
  1919. ifNoAdvice));
  1920. }
  1921. exitInstructions.append(Utility.createGet(fact, cflowField));
  1922. if (munger.getKind() != AdviceKind.PerCflowEntry &&
  1923. munger.getKind() != AdviceKind.PerCflowBelowEntry &&
  1924. munger.getExposedStateAsBcelVars(false).length==0) {
  1925. exitInstructions
  1926. .append(
  1927. fact
  1928. .createInvoke(
  1929. NameMangler.CFLOW_COUNTER_TYPE,
  1930. "dec",
  1931. Type.VOID,
  1932. new Type[] {
  1933. }, Constants.INVOKEVIRTUAL));
  1934. } else {
  1935. exitInstructions
  1936. .append(
  1937. fact
  1938. .createInvoke(
  1939. NameMangler.CFLOW_STACK_TYPE,
  1940. "pop",
  1941. Type.VOID,
  1942. new Type[] {
  1943. }, Constants.INVOKEVIRTUAL));
  1944. }
  1945. return exitInstructions;
  1946. }
  1947. });
  1948. range.insert(entryInstructions, Range.InsideBefore);
  1949. }
  1950. /* Implementation notes:
  1951. *
  1952. * AroundInline still extracts the instructions of the original shadow into
  1953. * an extracted method. This allows inlining of even that advice that doesn't
  1954. * call proceed or calls proceed more than once.
  1955. *
  1956. * It extracts the instructions of the original shadow into a method.
  1957. *
  1958. * Then it extracts the instructions of the advice into a new method defined on
  1959. * this enclosing class. This new method can then be specialized as below.
  1960. *
  1961. * Then it searches in the instructions of the advice for any call to the
  1962. * proceed method.
  1963. *
  1964. * At such a call, there is stuff on the stack representing the arguments to
  1965. * proceed. Pop these into the frame.
  1966. *
  1967. * Now build the stack for the call to the extracted method, taking values
  1968. * either from the join point state or from the new frame locs from proceed.
  1969. * Now call the extracted method. The right return value should be on the
  1970. * stack, so no cast is necessary.
  1971. *
  1972. * If only one call to proceed is made, we can re-inline the original shadow.
  1973. * We are not doing that presently.
  1974. *
  1975. * If the body of the advice can be determined to not alter the stack, or if
  1976. * this shadow doesn't care about the stack, i.e. method-execution, then the
  1977. * new method for the advice can also be re-lined. We are not doing that
  1978. * presently.
  1979. */
  1980. public void weaveAroundInline(BcelAdvice munger,boolean hasDynamicTest) {
  1981. // !!! THIS BLOCK OF CODE SHOULD BE IN A METHOD CALLED weaveAround(...);
  1982. Member mungerSig = munger.getSignature();
  1983. //Member originalSig = mungerSig; // If mungerSig is on a parameterized type, originalSig is the member on the generic type
  1984. if (mungerSig instanceof ResolvedMember) {
  1985. ResolvedMember rm = (ResolvedMember)mungerSig;
  1986. if (rm.hasBackingGenericMember()) mungerSig = rm.getBackingGenericMember();
  1987. }
  1988. ResolvedType declaringType = world.resolve(mungerSig.getDeclaringType(),true);
  1989. if (declaringType.isMissing()) {
  1990. world.getLint().cantFindType.signal(
  1991. new String[] {WeaverMessages.format(WeaverMessages.CANT_FIND_TYPE_DURING_AROUND_WEAVE,declaringType.getClassName())},
  1992. getSourceLocation(),
  1993. new ISourceLocation[]{ munger.getSourceLocation()}
  1994. );
  1995. // IMessage msg = new Message(
  1996. // WeaverMessages.format(WeaverMessages.CANT_FIND_TYPE_DURING_AROUND_WEAVE,declaringType.getClassName()),
  1997. // "",IMessage.ERROR,getSourceLocation(),null,
  1998. // new ISourceLocation[]{ munger.getSourceLocation()});
  1999. // world.getMessageHandler().handleMessage(msg);
  2000. }
  2001. //??? might want some checks here to give better errors
  2002. ResolvedType rt = (declaringType.isParameterizedType()?declaringType.getGenericType():declaringType);
  2003. BcelObjectType ot = BcelWorld.getBcelObjectType(rt);
  2004. // if (ot==null) {
  2005. // world.getMessageHandler().handleMessage(
  2006. // MessageUtil.warn("Unable to find modifiable delegate for the aspect '"+rt.getName()+"' containing around advice - cannot implement inlining",munger.getSourceLocation()));
  2007. // weaveAroundClosure(munger, hasDynamicTest);
  2008. // return;
  2009. // }
  2010. LazyMethodGen adviceMethod = ot.getLazyClassGen().getLazyMethodGen(mungerSig);
  2011. if (!adviceMethod.getCanInline()) {
  2012. weaveAroundClosure(munger, hasDynamicTest);
  2013. return;
  2014. }
  2015. // specific test for @AJ proceedInInners
  2016. if (munger.getConcreteAspect().isAnnotationStyleAspect()) {
  2017. // if we can't find one proceed() we suspect that the call
  2018. // is happening in an inner class so we don't inline it.
  2019. // Note: for code style, this is done at Aspect compilation time.
  2020. boolean canSeeProceedPassedToOther = false;
  2021. InstructionHandle curr = adviceMethod.getBody().getStart();
  2022. InstructionHandle end = adviceMethod.getBody().getEnd();
  2023. ConstantPoolGen cpg = adviceMethod.getEnclosingClass().getConstantPoolGen();
  2024. while (curr != end) {
  2025. InstructionHandle next = curr.getNext();
  2026. Instruction inst = curr.getInstruction();
  2027. if ((inst instanceof InvokeInstruction)
  2028. && ((InvokeInstruction)inst).getSignature(cpg).indexOf("Lorg/aspectj/lang/ProceedingJoinPoint;") > 0) {
  2029. // we may want to refine to exclude stuff returning jp ?
  2030. // does code style skip inline if i write dump(thisJoinPoint) ?
  2031. canSeeProceedPassedToOther = true;// we see one pjp passed around - dangerous
  2032. break;
  2033. }
  2034. curr = next;
  2035. }
  2036. if (canSeeProceedPassedToOther) {
  2037. // remember this decision to avoid re-analysis
  2038. adviceMethod.setCanInline(false);
  2039. weaveAroundClosure(munger, hasDynamicTest);
  2040. return;
  2041. }
  2042. }
  2043. // We can't inline around methods if they have around advice on them, this
  2044. // is because the weaving will extract the body and hence the proceed call.
  2045. //??? should consider optimizations to recognize simple cases that don't require body extraction
  2046. enclosingMethod.setCanInline(false);
  2047. // start by exposing various useful things into the frame
  2048. final InstructionFactory fact = getFactory();
  2049. // now generate the aroundBody method
  2050. // eg. "private static final void method_aroundBody0(M, M, String, org.aspectj.lang.JoinPoint)"
  2051. LazyMethodGen extractedMethod =
  2052. extractMethod(
  2053. NameMangler.aroundCallbackMethodName(getSignature(),getEnclosingClass()),
  2054. Modifier.PRIVATE,
  2055. munger);
  2056. // now extract the advice into its own method
  2057. String adviceMethodName =
  2058. NameMangler.aroundCallbackMethodName(getSignature(),getEnclosingClass()) + "$advice";
  2059. List argVarList = new ArrayList();
  2060. List proceedVarList = new ArrayList();
  2061. int extraParamOffset = 0;
  2062. // Create the extra parameters that are needed for passing to proceed
  2063. // This code is very similar to that found in makeCallToCallback and should
  2064. // be rationalized in the future
  2065. if (thisVar != null) {
  2066. argVarList.add(thisVar);
  2067. proceedVarList.add(new BcelVar(thisVar.getType(), extraParamOffset));
  2068. extraParamOffset += thisVar.getType().getSize();
  2069. }
  2070. if (targetVar != null && targetVar != thisVar) {
  2071. argVarList.add(targetVar);
  2072. proceedVarList.add(new BcelVar(targetVar.getType(), extraParamOffset));
  2073. extraParamOffset += targetVar.getType().getSize();
  2074. }
  2075. for (int i = 0, len = getArgCount(); i < len; i++) {
  2076. argVarList.add(argVars[i]);
  2077. proceedVarList.add(new BcelVar(argVars[i].getType(), extraParamOffset));
  2078. extraParamOffset += argVars[i].getType().getSize();
  2079. }
  2080. if (thisJoinPointVar != null) {
  2081. argVarList.add(thisJoinPointVar);
  2082. proceedVarList.add(new BcelVar(thisJoinPointVar.getType(), extraParamOffset));
  2083. extraParamOffset += thisJoinPointVar.getType().getSize();
  2084. }
  2085. Type[] adviceParameterTypes = adviceMethod.getArgumentTypes();
  2086. Type[] extractedMethodParameterTypes = extractedMethod.getArgumentTypes();
  2087. Type[] parameterTypes =
  2088. new Type[extractedMethodParameterTypes.length
  2089. + adviceParameterTypes.length
  2090. + 1];
  2091. int parameterIndex = 0;
  2092. System.arraycopy(
  2093. extractedMethodParameterTypes,
  2094. 0,
  2095. parameterTypes,
  2096. parameterIndex,
  2097. extractedMethodParameterTypes.length);
  2098. parameterIndex += extractedMethodParameterTypes.length;
  2099. parameterTypes[parameterIndex++] =
  2100. BcelWorld.makeBcelType(adviceMethod.getEnclosingClass().getType());
  2101. System.arraycopy(
  2102. adviceParameterTypes,
  2103. 0,
  2104. parameterTypes,
  2105. parameterIndex,
  2106. adviceParameterTypes.length);
  2107. LazyMethodGen localAdviceMethod =
  2108. new LazyMethodGen(
  2109. Modifier.PRIVATE | Modifier.FINAL | Modifier.STATIC,
  2110. BcelWorld.makeBcelType(mungerSig.getReturnType()),
  2111. adviceMethodName,
  2112. parameterTypes,
  2113. new String[0],
  2114. getEnclosingClass());
  2115. String donorFileName = adviceMethod.getEnclosingClass().getInternalFileName();
  2116. String recipientFileName = getEnclosingClass().getInternalFileName();
  2117. // System.err.println("donor " + donorFileName);
  2118. // System.err.println("recip " + recipientFileName);
  2119. if (! donorFileName.equals(recipientFileName)) {
  2120. localAdviceMethod.fromFilename = donorFileName;
  2121. getEnclosingClass().addInlinedSourceFileInfo(
  2122. donorFileName,
  2123. adviceMethod.highestLineNumber);
  2124. }
  2125. getEnclosingClass().addMethodGen(localAdviceMethod);
  2126. // create a map that will move all slots in advice method forward by extraParamOffset
  2127. // in order to make room for the new proceed-required arguments that are added at
  2128. // the beginning of the parameter list
  2129. int nVars = adviceMethod.getMaxLocals() + extraParamOffset;
  2130. IntMap varMap = IntMap.idMap(nVars);
  2131. for (int i=extraParamOffset; i < nVars; i++) {
  2132. varMap.put(i-extraParamOffset, i);
  2133. }
  2134. localAdviceMethod.getBody().insert(
  2135. BcelClassWeaver.genInlineInstructions(adviceMethod,
  2136. localAdviceMethod, varMap, fact, true));
  2137. localAdviceMethod.setMaxLocals(nVars);
  2138. //System.err.println(localAdviceMethod);
  2139. // the shadow is now empty. First, create a correct call
  2140. // to the around advice. This includes both the call (which may involve
  2141. // value conversion of the advice arguments) and the return
  2142. // (which may involve value conversion of the return value). Right now
  2143. // we push a null for the unused closure. It's sad, but there it is.
  2144. InstructionList advice = new InstructionList();
  2145. // InstructionHandle adviceMethodInvocation;
  2146. {
  2147. for (Iterator i = argVarList.iterator(); i.hasNext(); ) {
  2148. BcelVar var = (BcelVar)i.next();
  2149. var.appendLoad(advice, fact);
  2150. }
  2151. // ??? we don't actually need to push NULL for the closure if we take care
  2152. advice.append(
  2153. munger.getAdviceArgSetup(
  2154. this,
  2155. null,
  2156. (munger.getConcreteAspect().isAnnotationStyleAspect() && munger.getDeclaringAspect()!=null && munger.getDeclaringAspect().resolve(world).isAnnotationStyleAspect())?
  2157. this.loadThisJoinPoint():
  2158. new InstructionList(InstructionConstants.ACONST_NULL)));
  2159. // adviceMethodInvocation =
  2160. advice.append(
  2161. Utility.createInvoke(fact, localAdviceMethod)); //(fact, getWorld(), munger.getSignature()));
  2162. advice.append(
  2163. Utility.createConversion(
  2164. getFactory(),
  2165. BcelWorld.makeBcelType(mungerSig.getReturnType()),
  2166. extractedMethod.getReturnType(),world.isInJava5Mode()));
  2167. if (! isFallsThrough()) {
  2168. advice.append(InstructionFactory.createReturn(extractedMethod.getReturnType()));
  2169. }
  2170. }
  2171. // now, situate the call inside the possible dynamic tests,
  2172. // and actually add the whole mess to the shadow
  2173. if (! hasDynamicTest) {
  2174. range.append(advice);
  2175. } else {
  2176. InstructionList afterThingie = new InstructionList(InstructionConstants.NOP);
  2177. InstructionList callback = makeCallToCallback(extractedMethod);
  2178. if (terminatesWithReturn()) {
  2179. callback.append(
  2180. InstructionFactory.createReturn(extractedMethod.getReturnType()));
  2181. } else {
  2182. //InstructionHandle endNop = range.insert(fact.NOP, Range.InsideAfter);
  2183. advice.append(
  2184. InstructionFactory.createBranchInstruction(
  2185. Constants.GOTO,
  2186. afterThingie.getStart()));
  2187. }
  2188. range.append(
  2189. munger.getTestInstructions(
  2190. this,
  2191. advice.getStart(),
  2192. callback.getStart(),
  2193. advice.getStart()));
  2194. range.append(advice);
  2195. range.append(callback);
  2196. range.append(afterThingie);
  2197. }
  2198. // now search through the advice, looking for a call to PROCEED.
  2199. // Then we replace the call to proceed with some argument setup, and a
  2200. // call to the extracted method.
  2201. // inlining support for code style aspects
  2202. if (!munger.getConcreteAspect().isAnnotationStyleAspect()) {
  2203. String proceedName =
  2204. NameMangler.proceedMethodName(munger.getSignature().getName());
  2205. InstructionHandle curr = localAdviceMethod.getBody().getStart();
  2206. InstructionHandle end = localAdviceMethod.getBody().getEnd();
  2207. ConstantPoolGen cpg = localAdviceMethod.getEnclosingClass().getConstantPoolGen();
  2208. while (curr != end) {
  2209. InstructionHandle next = curr.getNext();
  2210. Instruction inst = curr.getInstruction();
  2211. if ((inst instanceof INVOKESTATIC)
  2212. && proceedName.equals(((INVOKESTATIC) inst).getMethodName(cpg))) {
  2213. localAdviceMethod.getBody().append(
  2214. curr,
  2215. getRedoneProceedCall(
  2216. fact,
  2217. extractedMethod,
  2218. munger,
  2219. localAdviceMethod,
  2220. proceedVarList));
  2221. Utility.deleteInstruction(curr, localAdviceMethod);
  2222. }
  2223. curr = next;
  2224. }
  2225. // and that's it.
  2226. } else {
  2227. //ATAJ inlining support for @AJ aspects
  2228. // [TODO document @AJ code rule: don't manipulate 2 jps proceed at the same time.. in an advice body]
  2229. InstructionHandle curr = localAdviceMethod.getBody().getStart();
  2230. InstructionHandle end = localAdviceMethod.getBody().getEnd();
  2231. ConstantPoolGen cpg = localAdviceMethod.getEnclosingClass().getConstantPoolGen();
  2232. while (curr != end) {
  2233. InstructionHandle next = curr.getNext();
  2234. Instruction inst = curr.getInstruction();
  2235. if ((inst instanceof INVOKEINTERFACE)
  2236. && "proceed".equals(((INVOKEINTERFACE) inst).getMethodName(cpg))) {
  2237. final boolean isProceedWithArgs;
  2238. if (((INVOKEINTERFACE) inst).getArgumentTypes(cpg).length == 1) {
  2239. // proceed with args as a boxed Object[]
  2240. isProceedWithArgs = true;
  2241. } else {
  2242. isProceedWithArgs = false;
  2243. }
  2244. InstructionList insteadProceedIl = getRedoneProceedCallForAnnotationStyle(
  2245. fact,
  2246. extractedMethod,
  2247. munger,
  2248. localAdviceMethod,
  2249. proceedVarList,
  2250. isProceedWithArgs
  2251. );
  2252. localAdviceMethod.getBody().append(curr, insteadProceedIl);
  2253. Utility.deleteInstruction(curr, localAdviceMethod);
  2254. }
  2255. curr = next;
  2256. }
  2257. }
  2258. }
  2259. private InstructionList getRedoneProceedCall(
  2260. InstructionFactory fact,
  2261. LazyMethodGen callbackMethod,
  2262. BcelAdvice munger,
  2263. LazyMethodGen localAdviceMethod,
  2264. List argVarList)
  2265. {
  2266. InstructionList ret = new InstructionList();
  2267. // we have on stack all the arguments for the ADVICE call.
  2268. // we have in frame somewhere all the arguments for the non-advice call.
  2269. BcelVar[] adviceVars = munger.getExposedStateAsBcelVars(true);
  2270. IntMap proceedMap = makeProceedArgumentMap(adviceVars);
  2271. // System.out.println(proceedMap + " for " + this);
  2272. // System.out.println(argVarList);
  2273. ResolvedType[] proceedParamTypes =
  2274. world.resolve(munger.getSignature().getParameterTypes());
  2275. // remove this*JoinPoint* as arguments to proceed
  2276. if (munger.getBaseParameterCount()+1 < proceedParamTypes.length) {
  2277. int len = munger.getBaseParameterCount()+1;
  2278. ResolvedType[] newTypes = new ResolvedType[len];
  2279. System.arraycopy(proceedParamTypes, 0, newTypes, 0, len);
  2280. proceedParamTypes = newTypes;
  2281. }
  2282. //System.out.println("stateTypes: " + Arrays.asList(stateTypes));
  2283. BcelVar[] proceedVars =
  2284. Utility.pushAndReturnArrayOfVars(proceedParamTypes, ret, fact, localAdviceMethod);
  2285. Type[] stateTypes = callbackMethod.getArgumentTypes();
  2286. // System.out.println("stateTypes: " + Arrays.asList(stateTypes));
  2287. for (int i=0, len=stateTypes.length; i < len; i++) {
  2288. Type stateType = stateTypes[i];
  2289. ResolvedType stateTypeX = BcelWorld.fromBcel(stateType).resolve(world);
  2290. if (proceedMap.hasKey(i)) {
  2291. //throw new RuntimeException("unimplemented");
  2292. proceedVars[proceedMap.get(i)].appendLoadAndConvert(ret, fact, stateTypeX);
  2293. } else {
  2294. ((BcelVar) argVarList.get(i)).appendLoad(ret, fact);
  2295. }
  2296. }
  2297. ret.append(Utility.createInvoke(fact, callbackMethod));
  2298. ret.append(Utility.createConversion(fact, callbackMethod.getReturnType(),
  2299. BcelWorld.makeBcelType(munger.getSignature().getReturnType())));
  2300. return ret;
  2301. }
  2302. private static boolean bindsThisOrTarget(Pointcut pointcut) {
  2303. ThisTargetFinder visitor = new ThisTargetFinder();
  2304. pointcut.accept(visitor, null);
  2305. return visitor.bindsThisOrTarget;
  2306. }
  2307. private static class ThisTargetFinder extends IdentityPointcutVisitor {
  2308. boolean bindsThisOrTarget = false;
  2309. public Object visit(ThisOrTargetPointcut node, Object data) {
  2310. if (node.isBinding()) {
  2311. bindsThisOrTarget = true;
  2312. }
  2313. return node;
  2314. }
  2315. public Object visit(AndPointcut node, Object data) {
  2316. if (!bindsThisOrTarget) node.getLeft().accept(this, data);
  2317. if (!bindsThisOrTarget) node.getRight().accept(this, data);
  2318. return node;
  2319. }
  2320. public Object visit(NotPointcut node, Object data) {
  2321. if (!bindsThisOrTarget) node.getNegatedPointcut().accept(this, data);
  2322. return node;
  2323. }
  2324. public Object visit(OrPointcut node, Object data) {
  2325. if (!bindsThisOrTarget) node.getLeft().accept(this, data);
  2326. if (!bindsThisOrTarget) node.getRight().accept(this, data);
  2327. return node;
  2328. }
  2329. }
  2330. /**
  2331. * ATAJ Handle the inlining for @AJ aspects
  2332. *
  2333. */
  2334. private InstructionList getRedoneProceedCallForAnnotationStyle(
  2335. InstructionFactory fact,
  2336. LazyMethodGen callbackMethod,
  2337. BcelAdvice munger,
  2338. LazyMethodGen localAdviceMethod,
  2339. List argVarList,
  2340. boolean isProceedWithArgs)
  2341. {
  2342. // Notes:
  2343. // proceedingjp is on stack (since user was calling pjp.proceed(...)
  2344. // new Object[]{new Integer(argAdvice1-1)};// arg of proceed
  2345. // call to proceed(..) is NOT made
  2346. // instead we do
  2347. // itar callback args i
  2348. // get from array i, convert it to the callback arg i
  2349. // if ask for JP, push the one we got on the stack
  2350. // invoke callback, create conversion back to Object/Integer
  2351. // rest of method -- (hence all those conversions)
  2352. // intValue() from original code
  2353. // int res = .. from original code
  2354. //Note: we just don't care about the proceed map etc
  2355. // (we would need to care if we allow repositioning of arguments in advice signature)
  2356. InstructionList ret = new InstructionList();
  2357. // store the Object[] array on stack if proceed with args
  2358. if (isProceedWithArgs) {
  2359. // STORE the Object[] into a local variable
  2360. Type objectArrayType = Type.getType("[Ljava/lang/Object;");
  2361. int localProceedArgArray = localAdviceMethod.allocateLocal(objectArrayType);
  2362. ret.append(InstructionFactory.createStore(objectArrayType, localProceedArgArray));
  2363. // STORE the ProceedingJoinPoint instance into a local variable
  2364. Type proceedingJpType = Type.getType("Lorg/aspectj/lang/ProceedingJoinPoint;");
  2365. int localJp = localAdviceMethod.allocateLocal(proceedingJpType);
  2366. ret.append(InstructionFactory.createStore(proceedingJpType, localJp));
  2367. // push on stack each element of the object array
  2368. // that is assumed to be consistent with the callback argument (ie munger args)
  2369. // TODO do we want to try catch ClassCast and AOOBE exception ?
  2370. // special logic when withincode is static or not
  2371. // This next bit of code probably makes more sense if you read its implementation for
  2372. // weaveAroundClosure() - see JoinPointImpl.proceed(Object[]). Basically depending
  2373. // on whether the join point has a this/target and whether the pointcut binds this/target
  2374. // then the arguments to the 'new' proceed call need to be reorganized. (pr126167)
  2375. boolean relatedPointcutBindsThis = bindsThis(munger);
  2376. boolean relatedPointcutBindsTarget = bindsTarget(munger);
  2377. boolean targetIsSameAsThis = getKind().isTargetSameAsThis();
  2378. // two numbers can differ because a pointcut may bind both this/target and yet at the
  2379. // join point this and target are the same (eg. call)
  2380. int indexIntoObjectArrayForArguments=0;
  2381. int indexIntoCallbackMethodForArguments = 0;
  2382. if (hasThis()) {
  2383. if (relatedPointcutBindsThis) {
  2384. if (!(relatedPointcutBindsTarget && targetIsSameAsThis)) {
  2385. // they have supplied new this as first entry in object array
  2386. ret.append(InstructionFactory.createLoad(objectArrayType, localProceedArgArray));
  2387. ret.append(Utility.createConstant(fact, 0));
  2388. ret.append(InstructionFactory.createArrayLoad(Type.OBJECT));
  2389. ret.append(Utility.createConversion(fact,Type.OBJECT,callbackMethod.getArgumentTypes()[0]));
  2390. indexIntoCallbackMethodForArguments++;
  2391. }
  2392. indexIntoObjectArrayForArguments=1;
  2393. } else {
  2394. // use local variable 0 (which is 'this' for a non-static method)
  2395. ret.append(new ALOAD(0));
  2396. indexIntoCallbackMethodForArguments++;
  2397. }
  2398. }
  2399. if (hasTarget()) {
  2400. if (relatedPointcutBindsTarget) {
  2401. if (getKind().isTargetSameAsThis()) {
  2402. ret.append(InstructionFactory.createLoad(objectArrayType, localProceedArgArray));
  2403. ret.append(Utility.createConstant(fact, relatedPointcutBindsThis?1:0));
  2404. ret.append(InstructionFactory.createArrayLoad(Type.OBJECT));
  2405. ret.append(Utility.createConversion(fact,Type.OBJECT,callbackMethod.getArgumentTypes()[0]));
  2406. indexIntoObjectArrayForArguments++;
  2407. indexIntoCallbackMethodForArguments++;
  2408. } else {
  2409. int position =(hasThis()&& relatedPointcutBindsThis?1:0);
  2410. ret.append(InstructionFactory.createLoad(objectArrayType, localProceedArgArray));
  2411. ret.append(Utility.createConstant(fact, position));
  2412. ret.append(InstructionFactory.createArrayLoad(Type.OBJECT));
  2413. ret.append(Utility.createConversion(fact,Type.OBJECT,callbackMethod.getArgumentTypes()[position]));
  2414. indexIntoObjectArrayForArguments=position+1;
  2415. indexIntoCallbackMethodForArguments++;
  2416. }
  2417. } else {
  2418. if (getKind().isTargetSameAsThis()) {
  2419. //ret.append(new ALOAD(0));
  2420. } else {
  2421. ret.append(InstructionFactory.createLoad(localAdviceMethod.getArgumentTypes()[0],hasThis()?1:0));
  2422. indexIntoCallbackMethodForArguments++;
  2423. }
  2424. }
  2425. }
  2426. for (int i = indexIntoCallbackMethodForArguments, len=callbackMethod.getArgumentTypes().length; i < len; i++) {
  2427. Type stateType = callbackMethod.getArgumentTypes()[i];
  2428. BcelWorld.fromBcel(stateType).resolve(world);
  2429. if ("Lorg/aspectj/lang/JoinPoint;".equals(stateType.getSignature())) {
  2430. ret.append(new ALOAD(localJp));// from localAdvice signature
  2431. } else {
  2432. ret.append(InstructionFactory.createLoad(objectArrayType, localProceedArgArray));
  2433. ret.append(Utility.createConstant(fact, i-indexIntoCallbackMethodForArguments +indexIntoObjectArrayForArguments));
  2434. ret.append(InstructionFactory.createArrayLoad(Type.OBJECT));
  2435. ret.append(Utility.createConversion(
  2436. fact,
  2437. Type.OBJECT,
  2438. stateType
  2439. ));
  2440. }
  2441. }
  2442. } else {
  2443. Type proceedingJpType = Type.getType("Lorg/aspectj/lang/ProceedingJoinPoint;");
  2444. int localJp = localAdviceMethod.allocateLocal(proceedingJpType);
  2445. ret.append(InstructionFactory.createStore(proceedingJpType, localJp));
  2446. for (int i = 0, len=callbackMethod.getArgumentTypes().length; i < len; i++) {
  2447. Type stateType = callbackMethod.getArgumentTypes()[i];
  2448. /*ResolvedType stateTypeX =*/
  2449. BcelWorld.fromBcel(stateType).resolve(world);
  2450. if ("Lorg/aspectj/lang/JoinPoint;".equals(stateType.getSignature())) {
  2451. ret.append(new ALOAD(localJp));// from localAdvice signature
  2452. // } else if ("Lorg/aspectj/lang/ProceedingJoinPoint;".equals(stateType.getSignature())) {
  2453. // //FIXME ALEX?
  2454. // ret.append(new ALOAD(localJp));// from localAdvice signature
  2455. //// ret.append(fact.createCheckCast(
  2456. //// (ReferenceType) BcelWorld.makeBcelType(stateTypeX)
  2457. //// ));
  2458. // // cast ?
  2459. //
  2460. } else {
  2461. ret.append(InstructionFactory.createLoad(stateType, i));
  2462. }
  2463. }
  2464. }
  2465. // do the callback invoke
  2466. ret.append(Utility.createInvoke(fact, callbackMethod));
  2467. // box it again. Handles cases where around advice does return something else than Object
  2468. if (!UnresolvedType.OBJECT.equals(munger.getSignature().getReturnType())) {
  2469. ret.append(Utility.createConversion(
  2470. fact,
  2471. callbackMethod.getReturnType(),
  2472. Type.OBJECT
  2473. ));
  2474. }
  2475. ret.append(Utility.createConversion(
  2476. fact,
  2477. callbackMethod.getReturnType(),
  2478. BcelWorld.makeBcelType(munger.getSignature().getReturnType())
  2479. ));
  2480. return ret;
  2481. //
  2482. //
  2483. //
  2484. // if (proceedMap.hasKey(i)) {
  2485. // ret.append(new ALOAD(i));
  2486. // //throw new RuntimeException("unimplemented");
  2487. // //proceedVars[proceedMap.get(i)].appendLoadAndConvert(ret, fact, stateTypeX);
  2488. // } else {
  2489. // //((BcelVar) argVarList.get(i)).appendLoad(ret, fact);
  2490. // //ret.append(new ALOAD(i));
  2491. // if ("Lorg/aspectj/lang/JoinPoint;".equals(stateType.getSignature())) {
  2492. // ret.append(new ALOAD(i));
  2493. // } else {
  2494. // ret.append(new ALOAD(i));
  2495. // }
  2496. // }
  2497. // }
  2498. //
  2499. // ret.append(Utility.createInvoke(fact, callbackMethod));
  2500. // ret.append(Utility.createConversion(fact, callbackMethod.getReturnType(),
  2501. // BcelWorld.makeBcelType(munger.getSignature().getReturnType())));
  2502. //
  2503. // //ret.append(new ACONST_NULL());//will be POPed
  2504. // if (true) return ret;
  2505. //
  2506. //
  2507. //
  2508. // // we have on stack all the arguments for the ADVICE call.
  2509. // // we have in frame somewhere all the arguments for the non-advice call.
  2510. //
  2511. // BcelVar[] adviceVars = munger.getExposedStateAsBcelVars();
  2512. // IntMap proceedMap = makeProceedArgumentMap(adviceVars);
  2513. //
  2514. // System.out.println(proceedMap + " for " + this);
  2515. // System.out.println(argVarList);
  2516. //
  2517. // ResolvedType[] proceedParamTypes =
  2518. // world.resolve(munger.getSignature().getParameterTypes());
  2519. // // remove this*JoinPoint* as arguments to proceed
  2520. // if (munger.getBaseParameterCount()+1 < proceedParamTypes.length) {
  2521. // int len = munger.getBaseParameterCount()+1;
  2522. // ResolvedType[] newTypes = new ResolvedType[len];
  2523. // System.arraycopy(proceedParamTypes, 0, newTypes, 0, len);
  2524. // proceedParamTypes = newTypes;
  2525. // }
  2526. //
  2527. // //System.out.println("stateTypes: " + Arrays.asList(stateTypes));
  2528. // BcelVar[] proceedVars =
  2529. // Utility.pushAndReturnArrayOfVars(proceedParamTypes, ret, fact, localAdviceMethod);
  2530. //
  2531. // Type[] stateTypes = callbackMethod.getArgumentTypes();
  2532. //// System.out.println("stateTypes: " + Arrays.asList(stateTypes));
  2533. //
  2534. // for (int i=0, len=stateTypes.length; i < len; i++) {
  2535. // Type stateType = stateTypes[i];
  2536. // ResolvedType stateTypeX = BcelWorld.fromBcel(stateType).resolve(world);
  2537. // if (proceedMap.hasKey(i)) {
  2538. // //throw new RuntimeException("unimplemented");
  2539. // proceedVars[proceedMap.get(i)].appendLoadAndConvert(ret, fact, stateTypeX);
  2540. // } else {
  2541. // ((BcelVar) argVarList.get(i)).appendLoad(ret, fact);
  2542. // }
  2543. // }
  2544. //
  2545. // ret.append(Utility.createInvoke(fact, callbackMethod));
  2546. // ret.append(Utility.createConversion(fact, callbackMethod.getReturnType(),
  2547. // BcelWorld.makeBcelType(munger.getSignature().getReturnType())));
  2548. // return ret;
  2549. }
  2550. private boolean bindsThis(BcelAdvice munger) {
  2551. UsesThisVisitor utv = new UsesThisVisitor();
  2552. munger.getPointcut().accept(utv, null);
  2553. return utv.usesThis;
  2554. }
  2555. private boolean bindsTarget(BcelAdvice munger) {
  2556. UsesTargetVisitor utv = new UsesTargetVisitor();
  2557. munger.getPointcut().accept(utv, null);
  2558. return utv.usesTarget;
  2559. }
  2560. private static class UsesThisVisitor extends IdentityPointcutVisitor {
  2561. boolean usesThis = false;
  2562. public Object visit(ThisOrTargetPointcut node, Object data) {
  2563. if (node.isThis() && node.isBinding()) usesThis=true;
  2564. return node;
  2565. }
  2566. public Object visit(AndPointcut node, Object data) {
  2567. if (!usesThis) node.getLeft().accept(this, data);
  2568. if (!usesThis) node.getRight().accept(this, data);
  2569. return node;
  2570. }
  2571. public Object visit(NotPointcut node, Object data) {
  2572. if (!usesThis) node.getNegatedPointcut().accept(this, data);
  2573. return node;
  2574. }
  2575. public Object visit(OrPointcut node, Object data) {
  2576. if (!usesThis) node.getLeft().accept(this, data);
  2577. if (!usesThis) node.getRight().accept(this, data);
  2578. return node;
  2579. }
  2580. }
  2581. private static class UsesTargetVisitor extends IdentityPointcutVisitor {
  2582. boolean usesTarget = false;
  2583. public Object visit(ThisOrTargetPointcut node, Object data) {
  2584. if (!node.isThis() && node.isBinding()) usesTarget=true;
  2585. return node;
  2586. }
  2587. public Object visit(AndPointcut node, Object data) {
  2588. if (!usesTarget) node.getLeft().accept(this, data);
  2589. if (!usesTarget) node.getRight().accept(this, data);
  2590. return node;
  2591. }
  2592. public Object visit(NotPointcut node, Object data) {
  2593. if (!usesTarget) node.getNegatedPointcut().accept(this, data);
  2594. return node;
  2595. }
  2596. public Object visit(OrPointcut node, Object data) {
  2597. if (!usesTarget) node.getLeft().accept(this, data);
  2598. if (!usesTarget) node.getRight().accept(this, data);
  2599. return node;
  2600. }
  2601. }
  2602. public void weaveAroundClosure(BcelAdvice munger, boolean hasDynamicTest) {
  2603. InstructionFactory fact = getFactory();
  2604. enclosingMethod.setCanInline(false);
  2605. // MOVE OUT ALL THE INSTRUCTIONS IN MY SHADOW INTO ANOTHER METHOD!
  2606. LazyMethodGen callbackMethod =
  2607. extractMethod(
  2608. NameMangler.aroundCallbackMethodName(
  2609. getSignature(),
  2610. getEnclosingClass()),
  2611. 0,
  2612. munger);
  2613. BcelVar[] adviceVars = munger.getExposedStateAsBcelVars(true);
  2614. String closureClassName =
  2615. NameMangler.makeClosureClassName(
  2616. getEnclosingClass().getType(),
  2617. getEnclosingClass().getNewGeneratedNameTag());
  2618. Member constructorSig = new MemberImpl(Member.CONSTRUCTOR,
  2619. UnresolvedType.forName(closureClassName), 0, "<init>",
  2620. "([Ljava/lang/Object;)V");
  2621. BcelVar closureHolder = null;
  2622. // This is not being used currently since getKind() == preinitializaiton
  2623. // cannot happen in around advice
  2624. if (getKind() == PreInitialization) {
  2625. closureHolder = genTempVar(AjcMemberMaker.AROUND_CLOSURE_TYPE);
  2626. }
  2627. InstructionList closureInstantiation =
  2628. makeClosureInstantiation(constructorSig, closureHolder);
  2629. /*LazyMethodGen constructor = */
  2630. makeClosureClassAndReturnConstructor(
  2631. closureClassName,
  2632. callbackMethod,
  2633. makeProceedArgumentMap(adviceVars)
  2634. );
  2635. InstructionList returnConversionCode;
  2636. if (getKind() == PreInitialization) {
  2637. returnConversionCode = new InstructionList();
  2638. BcelVar stateTempVar = genTempVar(UnresolvedType.OBJECTARRAY);
  2639. closureHolder.appendLoad(returnConversionCode, fact);
  2640. returnConversionCode.append(
  2641. Utility.createInvoke(
  2642. fact,
  2643. world,
  2644. AjcMemberMaker.aroundClosurePreInitializationGetter()));
  2645. stateTempVar.appendStore(returnConversionCode, fact);
  2646. Type[] stateTypes = getSuperConstructorParameterTypes();
  2647. returnConversionCode.append(InstructionConstants.ALOAD_0); // put "this" back on the stack
  2648. for (int i = 0, len = stateTypes.length; i < len; i++) {
  2649. UnresolvedType bcelTX = BcelWorld.fromBcel(stateTypes[i]);
  2650. ResolvedType stateRTX = world.resolve(bcelTX,true);
  2651. if (stateRTX.isMissing()) {
  2652. world.getLint().cantFindType.signal(
  2653. new String[] {WeaverMessages.format(WeaverMessages.CANT_FIND_TYPE_DURING_AROUND_WEAVE_PREINIT,bcelTX.getClassName())},
  2654. getSourceLocation(),
  2655. new ISourceLocation[]{ munger.getSourceLocation()}
  2656. );
  2657. // IMessage msg = new Message(
  2658. // WeaverMessages.format(WeaverMessages.CANT_FIND_TYPE_DURING_AROUND_WEAVE_PREINIT,bcelTX.getClassName()),
  2659. // "",IMessage.ERROR,getSourceLocation(),null,
  2660. // new ISourceLocation[]{ munger.getSourceLocation()});
  2661. // world.getMessageHandler().handleMessage(msg);
  2662. }
  2663. stateTempVar.appendConvertableArrayLoad(
  2664. returnConversionCode,
  2665. fact,
  2666. i,
  2667. stateRTX);
  2668. }
  2669. } else {
  2670. returnConversionCode =
  2671. Utility.createConversion(
  2672. getFactory(),
  2673. BcelWorld.makeBcelType(munger.getSignature().getReturnType()),
  2674. callbackMethod.getReturnType(),world.isInJava5Mode());
  2675. if (!isFallsThrough()) {
  2676. returnConversionCode.append(
  2677. InstructionFactory.createReturn(callbackMethod.getReturnType()));
  2678. }
  2679. }
  2680. // initialize the bit flags for this shadow
  2681. int bitflags =0x000000;
  2682. if (getKind().isTargetSameAsThis()) bitflags|=0x010000;
  2683. if (hasThis()) bitflags|=0x001000;
  2684. if (bindsThis(munger)) bitflags|=0x000100;
  2685. if (hasTarget()) bitflags|=0x000010;
  2686. if (bindsTarget(munger)) bitflags|=0x000001;
  2687. // ATAJ for @AJ aspect we need to link the closure with the joinpoint instance
  2688. if (munger.getConcreteAspect()!=null && munger.getConcreteAspect().isAnnotationStyleAspect()
  2689. && munger.getDeclaringAspect()!=null && munger.getDeclaringAspect().resolve(world).isAnnotationStyleAspect()) {
  2690. // stick the bitflags on the stack and call the variant of linkClosureAndJoinPoint that takes an int
  2691. closureInstantiation.append(fact.createConstant(new Integer(bitflags)));
  2692. closureInstantiation.append(Utility.createInvoke(
  2693. getFactory(),
  2694. getWorld(),
  2695. new MemberImpl(
  2696. Member.METHOD,
  2697. UnresolvedType.forName("org.aspectj.runtime.internal.AroundClosure"),
  2698. Modifier.PUBLIC,
  2699. "linkClosureAndJoinPoint",
  2700. "(I)Lorg/aspectj/lang/ProceedingJoinPoint;"
  2701. )
  2702. ));
  2703. }
  2704. InstructionList advice = new InstructionList();
  2705. advice.append(munger.getAdviceArgSetup(this, null, closureInstantiation));
  2706. // invoke the advice
  2707. advice.append(munger.getNonTestAdviceInstructions(this));
  2708. advice.append(returnConversionCode);
  2709. if (!hasDynamicTest) {
  2710. range.append(advice);
  2711. } else {
  2712. InstructionList callback = makeCallToCallback(callbackMethod);
  2713. InstructionList postCallback = new InstructionList();
  2714. if (terminatesWithReturn()) {
  2715. callback.append(
  2716. InstructionFactory.createReturn(callbackMethod.getReturnType()));
  2717. } else {
  2718. advice.append(
  2719. InstructionFactory.createBranchInstruction(
  2720. Constants.GOTO,
  2721. postCallback.append(InstructionConstants.NOP)));
  2722. }
  2723. range.append(
  2724. munger.getTestInstructions(
  2725. this,
  2726. advice.getStart(),
  2727. callback.getStart(),
  2728. advice.getStart()));
  2729. range.append(advice);
  2730. range.append(callback);
  2731. range.append(postCallback);
  2732. }
  2733. }
  2734. // exposed for testing
  2735. InstructionList makeCallToCallback(LazyMethodGen callbackMethod) {
  2736. InstructionFactory fact = getFactory();
  2737. InstructionList callback = new InstructionList();
  2738. if (thisVar != null) {
  2739. callback.append(InstructionConstants.ALOAD_0);
  2740. }
  2741. if (targetVar != null && targetVar != thisVar) {
  2742. callback.append(BcelRenderer.renderExpr(fact, world, targetVar));
  2743. }
  2744. callback.append(BcelRenderer.renderExprs(fact, world, argVars));
  2745. // remember to render tjps
  2746. if (thisJoinPointVar != null) {
  2747. callback.append(BcelRenderer.renderExpr(fact, world, thisJoinPointVar));
  2748. }
  2749. callback.append(Utility.createInvoke(fact, callbackMethod));
  2750. return callback;
  2751. }
  2752. /** side-effect-free */
  2753. private InstructionList makeClosureInstantiation(Member constructor, BcelVar holder) {
  2754. // LazyMethodGen constructor) {
  2755. InstructionFactory fact = getFactory();
  2756. BcelVar arrayVar = genTempVar(UnresolvedType.OBJECTARRAY);
  2757. //final Type objectArrayType = new ArrayType(Type.OBJECT, 1);
  2758. final InstructionList il = new InstructionList();
  2759. int alen = getArgCount() + (thisVar == null ? 0 : 1) +
  2760. ((targetVar != null && targetVar != thisVar) ? 1 : 0) +
  2761. (thisJoinPointVar == null ? 0 : 1);
  2762. il.append(Utility.createConstant(fact, alen));
  2763. il.append((Instruction)fact.createNewArray(Type.OBJECT, (short)1));
  2764. arrayVar.appendStore(il, fact);
  2765. int stateIndex = 0;
  2766. if (thisVar != null) {
  2767. arrayVar.appendConvertableArrayStore(il, fact, stateIndex, thisVar);
  2768. thisVar.setPositionInAroundState(stateIndex);
  2769. stateIndex++;
  2770. }
  2771. if (targetVar != null && targetVar != thisVar) {
  2772. arrayVar.appendConvertableArrayStore(il, fact, stateIndex, targetVar);
  2773. targetVar.setPositionInAroundState(stateIndex);
  2774. stateIndex++;
  2775. }
  2776. for (int i = 0, len = getArgCount(); i<len; i++) {
  2777. arrayVar.appendConvertableArrayStore(il, fact, stateIndex, argVars[i]);
  2778. argVars[i].setPositionInAroundState(stateIndex);
  2779. stateIndex++;
  2780. }
  2781. if (thisJoinPointVar != null) {
  2782. arrayVar.appendConvertableArrayStore(il, fact, stateIndex, thisJoinPointVar);
  2783. thisJoinPointVar.setPositionInAroundState(stateIndex);
  2784. stateIndex++;
  2785. }
  2786. il.append(fact.createNew(new ObjectType(constructor.getDeclaringType().getName())));
  2787. il.append(new DUP());
  2788. arrayVar.appendLoad(il, fact);
  2789. il.append(Utility.createInvoke(fact, world, constructor));
  2790. if (getKind() == PreInitialization) {
  2791. il.append(InstructionConstants.DUP);
  2792. holder.appendStore(il, fact);
  2793. }
  2794. return il;
  2795. }
  2796. private IntMap makeProceedArgumentMap(BcelVar[] adviceArgs) {
  2797. //System.err.println("coming in with " + Arrays.asList(adviceArgs));
  2798. IntMap ret = new IntMap();
  2799. for(int i = 0, len = adviceArgs.length; i < len; i++) {
  2800. BcelVar v = (BcelVar) adviceArgs[i];
  2801. if (v == null) continue; // XXX we don't know why this is required
  2802. int pos = v.getPositionInAroundState();
  2803. if (pos >= 0) { // need this test to avoid args bound via cflow
  2804. ret.put(pos, i);
  2805. }
  2806. }
  2807. //System.err.println("returning " + ret);
  2808. return ret;
  2809. }
  2810. /**
  2811. *
  2812. *
  2813. * @param callbackMethod the method we will call back to when our run method gets called.
  2814. *
  2815. * @param proceedMap A map from state position to proceed argument position. May be
  2816. * non covering on state position.
  2817. */
  2818. private LazyMethodGen makeClosureClassAndReturnConstructor(
  2819. String closureClassName,
  2820. LazyMethodGen callbackMethod,
  2821. IntMap proceedMap)
  2822. {
  2823. String superClassName = "org.aspectj.runtime.internal.AroundClosure";
  2824. Type objectArrayType = new ArrayType(Type.OBJECT, 1);
  2825. LazyClassGen closureClass = new LazyClassGen(closureClassName,
  2826. superClassName,
  2827. getEnclosingClass().getFileName(),
  2828. Modifier.PUBLIC,
  2829. new String[] {},
  2830. getWorld());
  2831. InstructionFactory fact = new InstructionFactory(closureClass.getConstantPoolGen());
  2832. // constructor
  2833. LazyMethodGen constructor = new LazyMethodGen(Modifier.PUBLIC,
  2834. Type.VOID,
  2835. "<init>",
  2836. new Type[] {objectArrayType},
  2837. new String[] {},
  2838. closureClass);
  2839. InstructionList cbody = constructor.getBody();
  2840. cbody.append(InstructionFactory.createLoad(Type.OBJECT, 0));
  2841. cbody.append(InstructionFactory.createLoad(objectArrayType, 1));
  2842. cbody.append(fact.createInvoke(superClassName, "<init>", Type.VOID,
  2843. new Type[] {objectArrayType}, Constants.INVOKESPECIAL));
  2844. cbody.append(InstructionFactory.createReturn(Type.VOID));
  2845. closureClass.addMethodGen(constructor);
  2846. // method
  2847. LazyMethodGen runMethod = new LazyMethodGen(Modifier.PUBLIC,
  2848. Type.OBJECT,
  2849. "run",
  2850. new Type[] {objectArrayType},
  2851. new String[] {},
  2852. closureClass);
  2853. InstructionList mbody = runMethod.getBody();
  2854. BcelVar proceedVar = new BcelVar(UnresolvedType.OBJECTARRAY.resolve(world), 1);
  2855. // int proceedVarIndex = 1;
  2856. BcelVar stateVar =
  2857. new BcelVar(UnresolvedType.OBJECTARRAY.resolve(world), runMethod.allocateLocal(1));
  2858. // int stateVarIndex = runMethod.allocateLocal(1);
  2859. mbody.append(InstructionFactory.createThis());
  2860. mbody.append(fact.createGetField(superClassName, "state", objectArrayType));
  2861. mbody.append(stateVar.createStore(fact));
  2862. // mbody.append(fact.createStore(objectArrayType, stateVarIndex));
  2863. Type[] stateTypes = callbackMethod.getArgumentTypes();
  2864. for (int i=0, len=stateTypes.length; i < len; i++) {
  2865. Type stateType = stateTypes[i];
  2866. ResolvedType stateTypeX = BcelWorld.fromBcel(stateType).resolve(world);
  2867. if (proceedMap.hasKey(i)) {
  2868. mbody.append(
  2869. proceedVar.createConvertableArrayLoad(fact, proceedMap.get(i),
  2870. stateTypeX));
  2871. } else {
  2872. mbody.append(
  2873. stateVar.createConvertableArrayLoad(fact, i,
  2874. stateTypeX));
  2875. }
  2876. }
  2877. mbody.append(Utility.createInvoke(fact, callbackMethod));
  2878. if (getKind() == PreInitialization) {
  2879. mbody.append(Utility.createSet(
  2880. fact,
  2881. AjcMemberMaker.aroundClosurePreInitializationField()));
  2882. mbody.append(InstructionConstants.ACONST_NULL);
  2883. } else {
  2884. mbody.append(
  2885. Utility.createConversion(
  2886. fact,
  2887. callbackMethod.getReturnType(),
  2888. Type.OBJECT));
  2889. }
  2890. mbody.append(InstructionFactory.createReturn(Type.OBJECT));
  2891. closureClass.addMethodGen(runMethod);
  2892. // class
  2893. getEnclosingClass().addGeneratedInner(closureClass);
  2894. return constructor;
  2895. }
  2896. // ---- extraction methods
  2897. public LazyMethodGen extractMethod(String newMethodName, int visibilityModifier, ShadowMunger munger) {
  2898. LazyMethodGen.assertGoodBody(range.getBody(), newMethodName);
  2899. if (!getKind().allowsExtraction()) throw new BCException("Attempt to extract method from a shadow kind that does not support this operation (" + getKind() + ")");
  2900. LazyMethodGen freshMethod = createMethodGen(newMethodName,visibilityModifier);
  2901. // System.err.println("******");
  2902. // System.err.println("ABOUT TO EXTRACT METHOD for" + this);
  2903. // enclosingMethod.print(System.err);
  2904. // System.err.println("INTO");
  2905. // freshMethod.print(System.err);
  2906. // System.err.println("WITH REMAP");
  2907. // System.err.println(makeRemap());
  2908. range.extractInstructionsInto(freshMethod, makeRemap(),
  2909. (getKind() != PreInitialization) &&
  2910. isFallsThrough());
  2911. if (getKind() == PreInitialization) {
  2912. addPreInitializationReturnCode(
  2913. freshMethod,
  2914. getSuperConstructorParameterTypes());
  2915. }
  2916. getEnclosingClass().addMethodGen(freshMethod,munger.getSourceLocation());
  2917. return freshMethod;
  2918. }
  2919. private void addPreInitializationReturnCode(
  2920. LazyMethodGen extractedMethod,
  2921. Type[] superConstructorTypes)
  2922. {
  2923. InstructionList body = extractedMethod.getBody();
  2924. final InstructionFactory fact = getFactory();
  2925. BcelVar arrayVar = new BcelVar(
  2926. world.getCoreType(UnresolvedType.OBJECTARRAY),
  2927. extractedMethod.allocateLocal(1));
  2928. int len = superConstructorTypes.length;
  2929. body.append(Utility.createConstant(fact, len));
  2930. body.append((Instruction)fact.createNewArray(Type.OBJECT, (short)1));
  2931. arrayVar.appendStore(body, fact);
  2932. for (int i = len - 1; i >= 0; i++) {
  2933. // convert thing on top of stack to object
  2934. body.append(
  2935. Utility.createConversion(fact, superConstructorTypes[i], Type.OBJECT));
  2936. // push object array
  2937. arrayVar.appendLoad(body, fact);
  2938. // swap
  2939. body.append(InstructionConstants.SWAP);
  2940. // do object array store.
  2941. body.append(Utility.createConstant(fact, i));
  2942. body.append(InstructionConstants.SWAP);
  2943. body.append(InstructionFactory.createArrayStore(Type.OBJECT));
  2944. }
  2945. arrayVar.appendLoad(body, fact);
  2946. body.append(InstructionConstants.ARETURN);
  2947. }
  2948. private Type[] getSuperConstructorParameterTypes() {
  2949. // assert getKind() == PreInitialization
  2950. InstructionHandle superCallHandle = getRange().getEnd().getNext();
  2951. InvokeInstruction superCallInstruction =
  2952. (InvokeInstruction) superCallHandle.getInstruction();
  2953. return superCallInstruction.getArgumentTypes(
  2954. getEnclosingClass().getConstantPoolGen());
  2955. }
  2956. /** make a map from old frame location to new frame location. Any unkeyed frame
  2957. * location picks out a copied local */
  2958. private IntMap makeRemap() {
  2959. IntMap ret = new IntMap(5);
  2960. int reti = 0;
  2961. if (thisVar != null) {
  2962. ret.put(0, reti++); // thisVar guaranteed to be 0
  2963. }
  2964. if (targetVar != null && targetVar != thisVar) {
  2965. ret.put(targetVar.getSlot(), reti++);
  2966. }
  2967. for (int i = 0, len = argVars.length; i < len; i++) {
  2968. ret.put(argVars[i].getSlot(), reti);
  2969. reti += argVars[i].getType().getSize();
  2970. }
  2971. if (thisJoinPointVar != null) {
  2972. ret.put(thisJoinPointVar.getSlot(), reti++);
  2973. }
  2974. // we not only need to put the arguments, we also need to remap their
  2975. // aliases, which we so helpfully put into temps at the beginning of this join
  2976. // point.
  2977. if (! getKind().argsOnStack()) {
  2978. int oldi = 0;
  2979. int newi = 0;
  2980. // if we're passing in a this and we're not argsOnStack we're always
  2981. // passing in a target too
  2982. if (arg0HoldsThis()) { ret.put(0, 0); oldi++; newi+=1; }
  2983. //assert targetVar == thisVar
  2984. for (int i = 0; i < getArgCount(); i++) {
  2985. UnresolvedType type = getArgType(i);
  2986. ret.put(oldi, newi);
  2987. oldi += type.getSize();
  2988. newi += type.getSize();
  2989. }
  2990. }
  2991. // System.err.println("making remap for : " + this);
  2992. // if (targetVar != null) System.err.println("target slot : " + targetVar.getSlot());
  2993. // if (thisVar != null) System.err.println(" this slot : " + thisVar.getSlot());
  2994. // System.err.println(ret);
  2995. return ret;
  2996. }
  2997. /**
  2998. * The new method always static.
  2999. * It may take some extra arguments: this, target.
  3000. * If it's argsOnStack, then it must take both this/target
  3001. * If it's argsOnFrame, it shares this and target.
  3002. * ??? rewrite this to do less array munging, please
  3003. */
  3004. private LazyMethodGen createMethodGen(String newMethodName, int visibilityModifier) {
  3005. Type[] parameterTypes = BcelWorld.makeBcelTypes(getArgTypes());
  3006. int modifiers = Modifier.FINAL | visibilityModifier;
  3007. // XXX some bug
  3008. // if (! isExpressionKind() && getSignature().isStrict(world)) {
  3009. // modifiers |= Modifier.STRICT;
  3010. // }
  3011. modifiers |= Modifier.STATIC;
  3012. if (targetVar != null && targetVar != thisVar) {
  3013. UnresolvedType targetType = getTargetType();
  3014. targetType = ensureTargetTypeIsCorrect(targetType);
  3015. // see pr109728 - this fixes the case when the declaring class is sometype 'X' but the getfield
  3016. // in the bytecode refers to a subtype of 'X'. This makes sure we use the type originally
  3017. // mentioned in the fieldget instruction as the method parameter and *not* the type upon which the
  3018. // field is declared because when the instructions are extracted into the new around body,
  3019. // they will still refer to the subtype.
  3020. if (getKind()==FieldGet && getActualTargetType()!=null &&
  3021. !getActualTargetType().equals(targetType.getName())) {
  3022. targetType = UnresolvedType.forName(getActualTargetType()).resolve(world);
  3023. }
  3024. ResolvedMember resolvedMember = getSignature().resolve(world);
  3025. if (resolvedMember != null && Modifier.isProtected(resolvedMember.getModifiers()) &&
  3026. !samePackage(targetType.getPackageName(), getEnclosingType().getPackageName()) &&
  3027. !resolvedMember.getName().equals("clone"))
  3028. {
  3029. if (!targetType.resolve(world).isAssignableFrom(getThisType().resolve(world))) {
  3030. throw new BCException("bad bytecode");
  3031. }
  3032. targetType = getThisType();
  3033. }
  3034. parameterTypes = addType(BcelWorld.makeBcelType(targetType), parameterTypes);
  3035. }
  3036. if (thisVar != null) {
  3037. UnresolvedType thisType = getThisType();
  3038. parameterTypes = addType(BcelWorld.makeBcelType(thisType), parameterTypes);
  3039. }
  3040. // We always want to pass down thisJoinPoint in case we have already woven
  3041. // some advice in here. If we only have a single piece of around advice on a
  3042. // join point, it is unnecessary to accept (and pass) tjp.
  3043. if (thisJoinPointVar != null) {
  3044. parameterTypes = addTypeToEnd(LazyClassGen.tjpType, parameterTypes);
  3045. //FIXME ALEX? which one
  3046. //parameterTypes = addTypeToEnd(LazyClassGen.proceedingTjpType, parameterTypes);
  3047. }
  3048. UnresolvedType returnType;
  3049. if (getKind() == PreInitialization) {
  3050. returnType = UnresolvedType.OBJECTARRAY;
  3051. } else {
  3052. if (getKind() == ConstructorCall) returnType = getSignature().getDeclaringType();
  3053. else if (getKind() == FieldSet) returnType = ResolvedType.VOID;
  3054. else returnType = getSignature().getReturnType().resolve(world);
  3055. // returnType = getReturnType(); // for this and above lines, see pr137496
  3056. }
  3057. return
  3058. new LazyMethodGen(
  3059. modifiers,
  3060. BcelWorld.makeBcelType(returnType),
  3061. newMethodName,
  3062. parameterTypes,
  3063. new String[0],
  3064. // XXX again, we need to look up methods!
  3065. // UnresolvedType.getNames(getSignature().getExceptions(world)),
  3066. getEnclosingClass());
  3067. }
  3068. private boolean samePackage(String p1, String p2) {
  3069. if (p1 == null) return p2 == null;
  3070. if (p2 == null) return false;
  3071. return p1.equals(p2);
  3072. }
  3073. private Type[] addType(Type type, Type[] types) {
  3074. int len = types.length;
  3075. Type[] ret = new Type[len+1];
  3076. ret[0] = type;
  3077. System.arraycopy(types, 0, ret, 1, len);
  3078. return ret;
  3079. }
  3080. private Type[] addTypeToEnd(Type type, Type[] types) {
  3081. int len = types.length;
  3082. Type[] ret = new Type[len+1];
  3083. ret[len] = type;
  3084. System.arraycopy(types, 0, ret, 0, len);
  3085. return ret;
  3086. }
  3087. public BcelVar genTempVar(UnresolvedType typeX) {
  3088. return new BcelVar(typeX.resolve(world), genTempVarIndex(typeX.getSize()));
  3089. }
  3090. // public static final boolean CREATE_TEMP_NAMES = true;
  3091. public BcelVar genTempVar(UnresolvedType typeX, String localName) {
  3092. BcelVar tv = genTempVar(typeX);
  3093. // if (CREATE_TEMP_NAMES) {
  3094. // for (InstructionHandle ih = range.getStart(); ih != range.getEnd(); ih = ih.getNext()) {
  3095. // if (Range.isRangeHandle(ih)) continue;
  3096. // ih.addTargeter(new LocalVariableTag(typeX, localName, tv.getSlot()));
  3097. // }
  3098. // }
  3099. return tv;
  3100. }
  3101. // eh doesn't think we need to garbage collect these (64K is a big number...)
  3102. private int genTempVarIndex(int size) {
  3103. return enclosingMethod.allocateLocal(size);
  3104. }
  3105. public InstructionFactory getFactory() {
  3106. return getEnclosingClass().getFactory();
  3107. }
  3108. public ISourceLocation getSourceLocation() {
  3109. int sourceLine = getSourceLine();
  3110. if (sourceLine == 0 || sourceLine == -1) {
  3111. // Thread.currentThread().dumpStack();
  3112. // System.err.println(this + ": " + range);
  3113. return getEnclosingClass().getType().getSourceLocation();
  3114. } else {
  3115. // For staticinitialization, if we have a nice offset, don't build a new source loc
  3116. if (getKind()==Shadow.StaticInitialization && getEnclosingClass().getType().getSourceLocation().getOffset()!=0) {
  3117. return getEnclosingClass().getType().getSourceLocation();
  3118. } else {
  3119. int offset = 0;
  3120. Kind kind = getKind();
  3121. if ( (kind == MethodExecution) ||
  3122. (kind == ConstructorExecution) ||
  3123. (kind == AdviceExecution) ||
  3124. (kind == StaticInitialization) ||
  3125. (kind == PreInitialization) ||
  3126. (kind == Initialization)) {
  3127. if (getEnclosingMethod().hasDeclaredLineNumberInfo()) {
  3128. offset = getEnclosingMethod().getDeclarationOffset();
  3129. }
  3130. }
  3131. return getEnclosingClass().getType().getSourceContext().makeSourceLocation(sourceLine, offset);
  3132. }
  3133. }
  3134. }
  3135. public Shadow getEnclosingShadow() {
  3136. return enclosingShadow;
  3137. }
  3138. public LazyMethodGen getEnclosingMethod() {
  3139. return enclosingMethod;
  3140. }
  3141. public boolean isFallsThrough() {
  3142. return !terminatesWithReturn(); //fallsThrough;
  3143. }
  3144. public void setActualTargetType(String className) {
  3145. this.actualInstructionTargetType = className;
  3146. }
  3147. public String getActualTargetType() {
  3148. return actualInstructionTargetType;
  3149. }
  3150. }