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

BcelClassWeaver.java 131KB

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