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

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