You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BcelShadow.java 137KB

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