Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.bcel;
  13. import java.io.ByteArrayOutputStream;
  14. import java.io.PrintStream;
  15. import java.lang.reflect.Modifier;
  16. import java.util.ArrayList;
  17. import java.util.HashMap;
  18. import java.util.HashSet;
  19. import java.util.Iterator;
  20. import java.util.LinkedList;
  21. import java.util.List;
  22. import java.util.ListIterator;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import java.util.Stack;
  26. import org.aspectj.apache.bcel.Constants;
  27. import org.aspectj.apache.bcel.classfile.Attribute;
  28. import org.aspectj.apache.bcel.classfile.ConstantPool;
  29. import org.aspectj.apache.bcel.classfile.Method;
  30. import org.aspectj.apache.bcel.classfile.Synthetic;
  31. import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen;
  32. import org.aspectj.apache.bcel.generic.BranchHandle;
  33. import org.aspectj.apache.bcel.generic.ClassGenException;
  34. import org.aspectj.apache.bcel.generic.CodeExceptionGen;
  35. import org.aspectj.apache.bcel.generic.Instruction;
  36. import org.aspectj.apache.bcel.generic.InstructionBranch;
  37. import org.aspectj.apache.bcel.generic.InstructionHandle;
  38. import org.aspectj.apache.bcel.generic.InstructionList;
  39. import org.aspectj.apache.bcel.generic.InstructionSelect;
  40. import org.aspectj.apache.bcel.generic.InstructionTargeter;
  41. import org.aspectj.apache.bcel.generic.LineNumberTag;
  42. import org.aspectj.apache.bcel.generic.LocalVariableTag;
  43. import org.aspectj.apache.bcel.generic.MethodGen;
  44. import org.aspectj.apache.bcel.generic.ObjectType;
  45. import org.aspectj.apache.bcel.generic.Tag;
  46. import org.aspectj.apache.bcel.generic.TargetLostException;
  47. import org.aspectj.apache.bcel.generic.Type;
  48. import org.aspectj.bridge.IMessage;
  49. import org.aspectj.bridge.ISourceLocation;
  50. import org.aspectj.weaver.AjAttribute;
  51. import org.aspectj.weaver.AjAttribute.WeaverVersionInfo;
  52. import org.aspectj.weaver.AnnotationAJ;
  53. import org.aspectj.weaver.BCException;
  54. import org.aspectj.weaver.ISourceContext;
  55. import org.aspectj.weaver.MemberImpl;
  56. import org.aspectj.weaver.NameMangler;
  57. import org.aspectj.weaver.ResolvedMember;
  58. import org.aspectj.weaver.ResolvedType;
  59. import org.aspectj.weaver.Shadow;
  60. import org.aspectj.weaver.UnresolvedType;
  61. import org.aspectj.weaver.WeaverMessages;
  62. import org.aspectj.weaver.tools.Traceable;
  63. /**
  64. * A LazyMethodGen should be treated as a MethodGen. It's our way of abstracting over the low-level Method objects. It converts
  65. * through {@link MethodGen} to create and to serialize, but that's it.
  66. *
  67. * <p>
  68. * At any rate, there are two ways to create LazyMethodGens. One is from a method, which does work through MethodGen to do the
  69. * correct thing. The other is the creation of a completely empty LazyMethodGen, and it is used when we're constructing code from
  70. * scratch.
  71. *
  72. * <p>
  73. * We stay away from targeters for rangey things like Shadows and Exceptions.
  74. */
  75. public final class LazyMethodGen implements Traceable {
  76. private int modifiers;
  77. private Type returnType;
  78. private final String name;
  79. private Type[] argumentTypes;
  80. // private final String[] argumentNames;
  81. private String[] declaredExceptions;
  82. private InstructionList body;
  83. private List<Attribute> attributes;
  84. private List<AnnotationAJ> newAnnotations;
  85. private List<ResolvedType> annotationsForRemoval;
  86. private AnnotationAJ[][] newParameterAnnotations;
  87. private final LazyClassGen enclosingClass;
  88. private BcelMethod memberView;
  89. private AjAttribute.EffectiveSignatureAttribute effectiveSignature;
  90. int highestLineNumber = 0;
  91. boolean wasPackedOptimally = false;
  92. private Method savedMethod = null;
  93. private static final AnnotationAJ[] NO_ANNOTATIONAJ = new AnnotationAJ[] {};
  94. /*
  95. * We use LineNumberTags and not Gens.
  96. *
  97. * This option specifies whether we let the BCEL classes create LineNumberGens and LocalVariableGens or if we make it create
  98. * LineNumberTags and LocalVariableTags. Up until 1.5.1 we always created Gens - then on return from the MethodGen ctor we took
  99. * them apart, reprocessed them all and created Tags. (see unpackLocals/unpackLineNumbers). As we have our own copy of Bcel, why
  100. * not create the right thing straightaway? So setting this to true will call the MethodGen ctor() in such a way that it creates
  101. * Tags - removing the need for unpackLocals/unpackLineNumbers - HOWEVER see the ensureAllLineNumberSetup() method for some
  102. * other relevant info.
  103. *
  104. * Whats the difference between a Tag and a Gen? A Tag is more lightweight, it doesn't know which instructions it targets, it
  105. * relies on the instructions targettingit - this reduces the amount of targeter manipulation we have to do.
  106. */
  107. /**
  108. * This is nonnull if this method is the result of an "inlining". We currently copy methods into other classes for around
  109. * advice. We add this field so we can get JSR45 information correct. If/when we do _actual_ inlining, we'll need to subtype
  110. * LineNumberTag to have external line numbers.
  111. */
  112. String fromFilename = null;
  113. private int maxLocals;
  114. private boolean canInline = true;
  115. private boolean isSynthetic = false;
  116. List<BcelShadow> matchedShadows;
  117. // Used for interface introduction - this is the type of the interface the method is technically on
  118. public ResolvedType definingType = null;
  119. static class LightweightBcelMethod extends BcelMethod {
  120. LightweightBcelMethod(BcelObjectType declaringType, Method method) {
  121. super(declaringType, method);
  122. // TODO Auto-generated constructor stub
  123. }
  124. }
  125. public LazyMethodGen(int modifiers, Type returnType, String name, Type[] paramTypes, String[] declaredExceptions,
  126. LazyClassGen enclosingClass) {
  127. // enclosingClass.getName() + ", " + returnType);
  128. this.memberView = null; // should be okay, since constructed ones aren't woven into
  129. this.modifiers = modifiers;
  130. this.returnType = returnType;
  131. this.name = name;
  132. this.argumentTypes = paramTypes;
  133. // this.argumentNames = Utility.makeArgNames(paramTypes.length);
  134. this.declaredExceptions = declaredExceptions;
  135. if (!Modifier.isAbstract(modifiers)) {
  136. body = new InstructionList();
  137. setMaxLocals(calculateMaxLocals());
  138. } else {
  139. body = null;
  140. }
  141. this.attributes = new ArrayList<Attribute>();
  142. this.enclosingClass = enclosingClass;
  143. assertGoodBody();
  144. // @AJ advice are not inlined by default since requires further analysis and weaving ordering control
  145. // TODO AV - improve - note: no room for improvement as long as aspects are reweavable
  146. // since the inlined version with wrappers and an to be done annotation to keep
  147. // inline state will be garbaged due to reweavable impl
  148. if (memberView != null && isAdviceMethod()) {
  149. if (enclosingClass.getType().isAnnotationStyleAspect()) {
  150. // TODO we could check for @Around advice as well
  151. this.canInline = false;
  152. }
  153. }
  154. }
  155. private int calculateMaxLocals() {
  156. int ret = Modifier.isStatic(modifiers) ? 0 : 1; // will there be a 'this'?
  157. for (Type type : argumentTypes) {
  158. ret += type.getSize();
  159. }
  160. return ret;
  161. }
  162. // build from an existing method, lazy build saves most work for
  163. // initialization
  164. public LazyMethodGen(Method m, LazyClassGen enclosingClass) {
  165. savedMethod = m;
  166. this.enclosingClass = enclosingClass;
  167. if (!(m.isAbstract() || m.isNative()) && m.getCode() == null) {
  168. throw new RuntimeException("bad non-abstract method with no code: " + m + " on " + enclosingClass);
  169. }
  170. if ((m.isAbstract() || m.isNative()) && m.getCode() != null) {
  171. throw new RuntimeException("bad abstract method with code: " + m + " on " + enclosingClass);
  172. }
  173. this.memberView = new BcelMethod(enclosingClass.getBcelObjectType(), m);
  174. this.modifiers = m.getModifiers();
  175. this.name = m.getName();
  176. // @AJ advice are not inlined by default since requires further analysis
  177. // and weaving ordering control
  178. // TODO AV - improve - note: no room for improvement as long as aspects
  179. // are reweavable
  180. // since the inlined version with wrappers and an to be done annotation
  181. // to keep
  182. // inline state will be garbaged due to reweavable impl
  183. if (memberView != null && isAdviceMethod()) {
  184. if (enclosingClass.getType().isAnnotationStyleAspect()) {
  185. // TODO we could check for @Around advice as well
  186. this.canInline = false;
  187. }
  188. }
  189. }
  190. private boolean isAbstractOrNative(int modifiers) {
  191. return Modifier.isAbstract(modifiers) || Modifier.isNative(modifiers);
  192. }
  193. public LazyMethodGen(BcelMethod m, LazyClassGen enclosingClass) {
  194. savedMethod = m.getMethod();
  195. this.enclosingClass = enclosingClass;
  196. if (!isAbstractOrNative(m.getModifiers()) && savedMethod.getCode() == null) {
  197. throw new RuntimeException("bad non-abstract method with no code: " + m + " on " + enclosingClass);
  198. }
  199. if (isAbstractOrNative(m.getModifiers()) && savedMethod.getCode() != null) {
  200. throw new RuntimeException("bad abstract method with code: " + m + " on " + enclosingClass);
  201. }
  202. // this.memberView = new BcelMethod(enclosingClass.getBcelObjectType(),
  203. // m);
  204. this.memberView = m;
  205. this.modifiers = savedMethod.getModifiers();
  206. this.name = m.getName();
  207. // @AJ advice are not inlined by default since requires further analysis
  208. // and weaving ordering control
  209. // TODO AV - improve - note: no room for improvement as long as aspects
  210. // are reweavable
  211. // since the inlined version with wrappers and an to be done annotation
  212. // to keep
  213. // inline state will be garbaged due to reweavable impl
  214. if (memberView != null && isAdviceMethod()) {
  215. if (enclosingClass.getType().isAnnotationStyleAspect()) {
  216. // TODO we could check for @Around advice as well
  217. this.canInline = false;
  218. }
  219. }
  220. }
  221. public boolean hasDeclaredLineNumberInfo() {
  222. return (memberView != null && memberView.hasDeclarationLineNumberInfo());
  223. }
  224. public int getDeclarationLineNumber() {
  225. if (hasDeclaredLineNumberInfo()) {
  226. return memberView.getDeclarationLineNumber();
  227. } else {
  228. return -1;
  229. }
  230. }
  231. public int getDeclarationOffset() {
  232. if (hasDeclaredLineNumberInfo()) {
  233. return memberView.getDeclarationOffset();
  234. } else {
  235. return 0;
  236. }
  237. }
  238. public void addAnnotation(AnnotationAJ ax) {
  239. initialize();
  240. if (memberView == null) {
  241. // If member view is null, we manage them in newAnnotations
  242. if (newAnnotations == null) {
  243. newAnnotations = new ArrayList<AnnotationAJ>();
  244. }
  245. newAnnotations.add(ax);
  246. } else {
  247. memberView.addAnnotation(ax);
  248. }
  249. }
  250. public void removeAnnotation(ResolvedType annotationType) {
  251. initialize();
  252. if (memberView == null) {
  253. // If member view is null, we manage them in newAnnotations
  254. if (annotationsForRemoval == null) {
  255. annotationsForRemoval = new ArrayList<ResolvedType>();
  256. }
  257. annotationsForRemoval.add(annotationType);
  258. } else {
  259. memberView.removeAnnotation(annotationType);
  260. }
  261. }
  262. public void addParameterAnnotation(int parameterNumber, AnnotationAJ anno) {
  263. initialize();
  264. if (memberView == null) {
  265. if (newParameterAnnotations == null) {
  266. // time to create it
  267. int pcount = getArgumentTypes().length;
  268. newParameterAnnotations = new AnnotationAJ[pcount][];
  269. for (int i = 0; i < pcount; i++) {
  270. if (i == parameterNumber) {
  271. newParameterAnnotations[i] = new AnnotationAJ[1];
  272. newParameterAnnotations[i][0] = anno;
  273. } else {
  274. newParameterAnnotations[i] = NO_ANNOTATIONAJ;
  275. }
  276. }
  277. } else {
  278. AnnotationAJ[] currentAnnoArray = newParameterAnnotations[parameterNumber];
  279. AnnotationAJ[] newAnnoArray = new AnnotationAJ[currentAnnoArray.length + 1];
  280. System.arraycopy(currentAnnoArray, 0, newAnnoArray, 0, currentAnnoArray.length);
  281. newAnnoArray[currentAnnoArray.length] = anno;
  282. newParameterAnnotations[parameterNumber] = newAnnoArray;
  283. }
  284. } else {
  285. memberView.addParameterAnnotation(parameterNumber, anno);
  286. }
  287. }
  288. public ResolvedType[] getAnnotationTypes() {
  289. initialize();
  290. if (memberView == null && newAnnotations!=null && newAnnotations.size()!=0) {
  291. // TODO Ignoring removed annotations for now
  292. ResolvedType[] annotationTypes = new ResolvedType[newAnnotations.size()];
  293. for (int a=0,len=newAnnotations.size();a<len;a++) {
  294. annotationTypes[a] = newAnnotations.get(a).getType();
  295. }
  296. return annotationTypes;
  297. }
  298. return null;
  299. }
  300. public AnnotationAJ[] getAnnotations() {
  301. initialize();
  302. if (memberView == null && newAnnotations!=null && newAnnotations.size()!=0) {
  303. return newAnnotations.toArray(new AnnotationAJ[newAnnotations.size()]);
  304. }
  305. return null;
  306. }
  307. public boolean hasAnnotation(UnresolvedType annotationType) {
  308. initialize();
  309. if (memberView == null) {
  310. if (annotationsForRemoval != null) {
  311. for (ResolvedType at : annotationsForRemoval) {
  312. if (at.equals(annotationType)) {
  313. return false;
  314. }
  315. }
  316. }
  317. // Check local annotations first
  318. if (newAnnotations != null) {
  319. for (AnnotationAJ annotation : newAnnotations) {
  320. if (annotation.getTypeSignature().equals(annotationType.getSignature())) {
  321. return true;
  322. }
  323. }
  324. }
  325. memberView = new BcelMethod(getEnclosingClass().getBcelObjectType(), getMethod());
  326. return memberView.hasAnnotation(annotationType);
  327. }
  328. return memberView.hasAnnotation(annotationType);
  329. }
  330. private void initialize() {
  331. if (returnType != null) {
  332. return;
  333. }
  334. MethodGen gen = new MethodGen(savedMethod, enclosingClass.getName(), enclosingClass.getConstantPool(), true);
  335. this.returnType = gen.getReturnType();
  336. this.argumentTypes = gen.getArgumentTypes();
  337. this.declaredExceptions = gen.getExceptions();
  338. this.attributes = gen.getAttributes();
  339. // this.annotations = gen.getAnnotations();
  340. this.maxLocals = gen.getMaxLocals();
  341. // this.returnType = BcelWorld.makeBcelType(memberView.getReturnType());
  342. // this.argumentTypes =
  343. // BcelWorld.makeBcelTypes(memberView.getParameterTypes());
  344. //
  345. // this.declaredExceptions =
  346. // UnresolvedType.getNames(memberView.getExceptions());
  347. // //gen.getExceptions();
  348. // this.attributes = new Attribute[0]; //gen.getAttributes();
  349. // this.maxLocals = savedMethod.getCode().getMaxLocals();
  350. if (gen.isAbstract() || gen.isNative()) {
  351. body = null;
  352. } else {
  353. // body = new InstructionList(savedMethod.getCode().getCode());
  354. body = gen.getInstructionList();
  355. unpackHandlers(gen);
  356. ensureAllLineNumberSetup();
  357. highestLineNumber = gen.getHighestlinenumber();
  358. }
  359. assertGoodBody();
  360. }
  361. // XXX we're relying on the javac promise I've just made up that we won't
  362. // have an early exception
  363. // in the list mask a later exception: That is, for two exceptions E and F,
  364. // if E preceeds F, then either E \cup F = {}, or E \nonstrictsubset F. So
  365. // when we add F,
  366. // we add it on the _OUTSIDE_ of any handlers that share starts or ends with
  367. // it.
  368. // with that in mind, we merrily go adding ranges for exceptions.
  369. private void unpackHandlers(MethodGen gen) {
  370. CodeExceptionGen[] exns = gen.getExceptionHandlers();
  371. if (exns != null) {
  372. int len = exns.length;
  373. // if (len > 0) hasExceptionHandlers = true;
  374. int priority = len - 1;
  375. for (int i = 0; i < len; i++, priority--) {
  376. CodeExceptionGen exn = exns[i];
  377. InstructionHandle start = Range.genStart(body, getOutermostExceptionStart(exn.getStartPC()));
  378. InstructionHandle end = Range.genEnd(body, getOutermostExceptionEnd(exn.getEndPC()));
  379. // this doesn't necessarily handle overlapping correctly!!!
  380. ExceptionRange er = new ExceptionRange(body, exn.getCatchType() == null ? null : BcelWorld.fromBcel(exn
  381. .getCatchType()), priority);
  382. er.associateWithTargets(start, end, exn.getHandlerPC());
  383. exn.setStartPC(null); // also removes from target
  384. exn.setEndPC(null); // also removes from target
  385. exn.setHandlerPC(null); // also removes from target
  386. }
  387. gen.removeExceptionHandlers();
  388. }
  389. }
  390. private InstructionHandle getOutermostExceptionStart(InstructionHandle ih) {
  391. while (true) {
  392. if (ExceptionRange.isExceptionStart(ih.getPrev())) {
  393. ih = ih.getPrev();
  394. } else {
  395. return ih;
  396. }
  397. }
  398. }
  399. private InstructionHandle getOutermostExceptionEnd(InstructionHandle ih) {
  400. while (true) {
  401. if (ExceptionRange.isExceptionEnd(ih.getNext())) {
  402. ih = ih.getNext();
  403. } else {
  404. return ih;
  405. }
  406. }
  407. }
  408. /**
  409. * On entry to this method we have a method whose instruction stream contains a few instructions that have line numbers assigned
  410. * to them (LineNumberTags). The aim is to ensure every instruction has the right line number. This is necessary because some of
  411. * them may be extracted out into other methods - and it'd be useful for them to maintain the source line number for debugging.
  412. */
  413. public void ensureAllLineNumberSetup() {
  414. LineNumberTag lastKnownLineNumberTag = null;
  415. boolean skip = false;
  416. for (InstructionHandle ih = body.getStart(); ih != null; ih = ih.getNext()) {
  417. skip = false;
  418. for (InstructionTargeter targeter : ih.getTargeters()) {
  419. if (targeter instanceof LineNumberTag) {
  420. lastKnownLineNumberTag = (LineNumberTag) targeter;
  421. skip = true;
  422. }
  423. }
  424. if (lastKnownLineNumberTag != null && !skip) {
  425. ih.addTargeter(lastKnownLineNumberTag);
  426. }
  427. }
  428. }
  429. // ===============
  430. public int allocateLocal(Type type) {
  431. return allocateLocal(type.getSize());
  432. }
  433. public int allocateLocal(int slots) {
  434. int max = getMaxLocals();
  435. setMaxLocals(max + slots);
  436. return max;
  437. }
  438. public Method getMethod() {
  439. if (savedMethod != null) {
  440. return savedMethod; // ??? this relies on gentle treatment of
  441. // constant pool
  442. }
  443. try {
  444. MethodGen gen = pack();
  445. savedMethod = gen.getMethod();
  446. return savedMethod;
  447. } catch (ClassGenException e) {
  448. enclosingClass
  449. .getBcelObjectType()
  450. .getResolvedTypeX()
  451. .getWorld()
  452. .showMessage(
  453. IMessage.ERROR,
  454. WeaverMessages.format(WeaverMessages.PROBLEM_GENERATING_METHOD, this.getClassName(), this.getName(),
  455. e.getMessage()),
  456. this.getMemberView() == null ? null : this.getMemberView().getSourceLocation(), null);
  457. // throw e; PR 70201.... let the normal problem reporting
  458. // infrastructure deal with this rather than crashing.
  459. body = null;
  460. MethodGen gen = pack();
  461. return gen.getMethod();
  462. } catch (RuntimeException re) {
  463. if (re.getCause() instanceof ClassGenException) {
  464. enclosingClass
  465. .getBcelObjectType()
  466. .getResolvedTypeX()
  467. .getWorld()
  468. .showMessage(
  469. IMessage.ERROR,
  470. WeaverMessages.format(WeaverMessages.PROBLEM_GENERATING_METHOD, this.getClassName(),
  471. this.getName(), re.getCause().getMessage()),
  472. this.getMemberView() == null ? null : this.getMemberView().getSourceLocation(), null);
  473. // throw e; PR 70201.... let the normal problem reporting
  474. // infrastructure deal with this rather than crashing.
  475. body = null;
  476. MethodGen gen = pack();
  477. return gen.getMethod();
  478. }
  479. throw re;
  480. }
  481. }
  482. public void markAsChanged() {
  483. if (wasPackedOptimally) {
  484. throw new RuntimeException("Already packed method is being re-modified: " + getClassName() + " " + toShortString());
  485. }
  486. initialize();
  487. savedMethod = null;
  488. }
  489. // =============================
  490. @Override
  491. public String toString() {
  492. BcelObjectType bot = enclosingClass.getBcelObjectType();
  493. WeaverVersionInfo weaverVersion = (bot == null ? WeaverVersionInfo.CURRENT : bot.getWeaverVersionAttribute());
  494. return toLongString(weaverVersion);
  495. }
  496. public String toShortString() {
  497. String access = org.aspectj.apache.bcel.classfile.Utility.accessToString(getAccessFlags());
  498. StringBuffer buf = new StringBuffer();
  499. if (!access.equals("")) {
  500. buf.append(access);
  501. buf.append(" ");
  502. }
  503. buf.append(org.aspectj.apache.bcel.classfile.Utility.signatureToString(getReturnType().getSignature(), true));
  504. buf.append(" ");
  505. buf.append(getName());
  506. buf.append("(");
  507. {
  508. int len = argumentTypes.length;
  509. if (len > 0) {
  510. buf.append(org.aspectj.apache.bcel.classfile.Utility.signatureToString(argumentTypes[0].getSignature(), true));
  511. for (int i = 1; i < argumentTypes.length; i++) {
  512. buf.append(", ");
  513. buf.append(org.aspectj.apache.bcel.classfile.Utility.signatureToString(argumentTypes[i].getSignature(), true));
  514. }
  515. }
  516. }
  517. buf.append(")");
  518. {
  519. int len = declaredExceptions != null ? declaredExceptions.length : 0;
  520. if (len > 0) {
  521. buf.append(" throws ");
  522. buf.append(declaredExceptions[0]);
  523. for (int i = 1; i < declaredExceptions.length; i++) {
  524. buf.append(", ");
  525. buf.append(declaredExceptions[i]);
  526. }
  527. }
  528. }
  529. return buf.toString();
  530. }
  531. public String toLongString(WeaverVersionInfo weaverVersion) {
  532. ByteArrayOutputStream s = new ByteArrayOutputStream();
  533. print(new PrintStream(s), weaverVersion);
  534. return new String(s.toByteArray());
  535. }
  536. public void print(WeaverVersionInfo weaverVersion) {
  537. print(System.out, weaverVersion);
  538. }
  539. public void print(PrintStream out, WeaverVersionInfo weaverVersion) {
  540. out.print(" " + toShortString());
  541. printAspectAttributes(out, weaverVersion);
  542. InstructionList body = getBody();
  543. if (body == null) {
  544. out.println(";");
  545. return;
  546. }
  547. out.println(":");
  548. new BodyPrinter(out).run();
  549. out.println(" end " + toShortString());
  550. }
  551. private void printAspectAttributes(PrintStream out, WeaverVersionInfo weaverVersion) {
  552. ISourceContext context = null;
  553. if (enclosingClass != null && enclosingClass.getType() != null) {
  554. context = enclosingClass.getType().getSourceContext();
  555. }
  556. List<AjAttribute> as = Utility.readAjAttributes(getClassName(), attributes.toArray(new Attribute[] {}), context, null, weaverVersion,
  557. new BcelConstantPoolReader(this.enclosingClass.getConstantPool()));
  558. if (!as.isEmpty()) {
  559. out.println(" " + as.get(0)); // XXX assuming exactly one
  560. // attribute, munger...
  561. }
  562. }
  563. private class BodyPrinter {
  564. Map<InstructionHandle, String> labelMap = new HashMap<InstructionHandle, String>();
  565. InstructionList body;
  566. PrintStream out;
  567. ConstantPool pool;
  568. BodyPrinter(PrintStream out) {
  569. this.pool = enclosingClass.getConstantPool();
  570. this.body = getBodyForPrint();
  571. this.out = out;
  572. }
  573. BodyPrinter(PrintStream out, InstructionList il) {
  574. this.pool = enclosingClass.getConstantPool();
  575. this.body = il;
  576. this.out = out;
  577. }
  578. void run() {
  579. // killNops();
  580. assignLabels();
  581. print();
  582. }
  583. // label assignment
  584. void assignLabels() {
  585. LinkedList<ExceptionRange> exnTable = new LinkedList<ExceptionRange>();
  586. String pendingLabel = null;
  587. // boolean hasPendingTargeters = false;
  588. int lcounter = 0;
  589. for (InstructionHandle ih = body.getStart(); ih != null; ih = ih.getNext()) {
  590. Iterator<InstructionTargeter> tIter = ih.getTargeters().iterator();
  591. while (tIter.hasNext()) {
  592. InstructionTargeter t = tIter.next();// targeters
  593. // [
  594. // i
  595. // ]
  596. // ;
  597. if (t instanceof ExceptionRange) {
  598. // assert isRangeHandle(h);
  599. ExceptionRange r = (ExceptionRange) t;
  600. if (r.getStart() == ih) {
  601. insertHandler(r, exnTable);
  602. }
  603. } else if (t instanceof InstructionBranch) {
  604. if (pendingLabel == null) {
  605. pendingLabel = "L" + lcounter++;
  606. }
  607. } else {
  608. // assert isRangeHandle(h)
  609. }
  610. }
  611. if (pendingLabel != null) {
  612. labelMap.put(ih, pendingLabel);
  613. if (!Range.isRangeHandle(ih)) {
  614. pendingLabel = null;
  615. }
  616. }
  617. }
  618. int ecounter = 0;
  619. for (Iterator i = exnTable.iterator(); i.hasNext();) {
  620. ExceptionRange er = (ExceptionRange) i.next();
  621. String exceptionLabel = "E" + ecounter++;
  622. labelMap.put(Range.getRealStart(er.getHandler()), exceptionLabel);
  623. labelMap.put(er.getHandler(), exceptionLabel);
  624. }
  625. }
  626. // printing
  627. void print() {
  628. int depth = 0;
  629. int currLine = -1;
  630. bodyPrint: for (InstructionHandle ih = body.getStart(); ih != null; ih = ih.getNext()) {
  631. if (Range.isRangeHandle(ih)) {
  632. Range r = Range.getRange(ih);
  633. // don't print empty ranges, that is, ranges who contain no
  634. // actual instructions
  635. for (InstructionHandle xx = r.getStart(); Range.isRangeHandle(xx); xx = xx.getNext()) {
  636. if (xx == r.getEnd()) {
  637. continue bodyPrint;
  638. }
  639. }
  640. // doesn't handle nested: if (r.getStart().getNext() ==
  641. // r.getEnd()) continue;
  642. if (r.getStart() == ih) {
  643. printRangeString(r, depth++);
  644. } else {
  645. if (r.getEnd() != ih) {
  646. throw new RuntimeException("bad");
  647. }
  648. printRangeString(r, --depth);
  649. }
  650. } else {
  651. printInstruction(ih, depth);
  652. int line = getLineNumber(ih, currLine);
  653. if (line != currLine) {
  654. currLine = line;
  655. out.println(" (line " + line + ")");
  656. } else {
  657. out.println();
  658. }
  659. }
  660. }
  661. }
  662. void printRangeString(Range r, int depth) {
  663. printDepth(depth);
  664. out.println(getRangeString(r, labelMap));
  665. }
  666. String getRangeString(Range r, Map<InstructionHandle, String> labelMap) {
  667. if (r instanceof ExceptionRange) {
  668. ExceptionRange er = (ExceptionRange) r;
  669. return er.toString() + " -> " + labelMap.get(er.getHandler());
  670. //
  671. // + " PRI " + er.getPriority();
  672. } else {
  673. return r.toString();
  674. }
  675. }
  676. void printDepth(int depth) {
  677. pad(BODY_INDENT);
  678. while (depth > 0) {
  679. out.print("| ");
  680. depth--;
  681. }
  682. }
  683. void printLabel(String s, int depth) {
  684. int space = Math.max(CODE_INDENT - depth * 2, 0);
  685. if (s == null) {
  686. pad(space);
  687. } else {
  688. space = Math.max(space - (s.length() + 2), 0);
  689. pad(space);
  690. out.print(s);
  691. out.print(": ");
  692. }
  693. }
  694. void printInstruction(InstructionHandle h, int depth) {
  695. printDepth(depth);
  696. printLabel(labelMap.get(h), depth);
  697. Instruction inst = h.getInstruction();
  698. if (inst.isConstantPoolInstruction()) {
  699. out.print(Constants.OPCODE_NAMES[inst.opcode].toUpperCase());
  700. out.print(" ");
  701. out.print(pool.constantToString(pool.getConstant(inst.getIndex())));
  702. } else if (inst instanceof InstructionSelect) {
  703. InstructionSelect sinst = (InstructionSelect) inst;
  704. out.println(Constants.OPCODE_NAMES[sinst.opcode].toUpperCase());
  705. int[] matches = sinst.getMatchs();
  706. InstructionHandle[] targets = sinst.getTargets();
  707. InstructionHandle defaultTarget = sinst.getTarget();
  708. for (int i = 0, len = matches.length; i < len; i++) {
  709. printDepth(depth);
  710. printLabel(null, depth);
  711. out.print(" ");
  712. out.print(matches[i]);
  713. out.print(": \t");
  714. out.println(labelMap.get(targets[i]));
  715. }
  716. printDepth(depth);
  717. printLabel(null, depth);
  718. out.print(" ");
  719. out.print("default: \t");
  720. out.print(labelMap.get(defaultTarget));
  721. } else if (inst instanceof InstructionBranch) {
  722. InstructionBranch brinst = (InstructionBranch) inst;
  723. out.print(Constants.OPCODE_NAMES[brinst.getOpcode()].toUpperCase());
  724. out.print(" ");
  725. out.print(labelMap.get(brinst.getTarget()));
  726. } else if (inst.isLocalVariableInstruction()) {
  727. // LocalVariableInstruction lvinst = (LocalVariableInstruction)
  728. // inst;
  729. out.print(inst.toString(false).toUpperCase());
  730. int index = inst.getIndex();
  731. LocalVariableTag tag = getLocalVariableTag(h, index);
  732. if (tag != null) {
  733. out.print(" // ");
  734. out.print(tag.getType());
  735. out.print(" ");
  736. out.print(tag.getName());
  737. }
  738. } else {
  739. out.print(inst.toString(false).toUpperCase());
  740. }
  741. }
  742. static final int BODY_INDENT = 4;
  743. static final int CODE_INDENT = 16;
  744. void pad(int size) {
  745. for (int i = 0; i < size; i++) {
  746. out.print(" ");
  747. }
  748. }
  749. }
  750. static LocalVariableTag getLocalVariableTag(InstructionHandle ih, int index) {
  751. for (InstructionTargeter t : ih.getTargeters()) {
  752. if (t instanceof LocalVariableTag) {
  753. LocalVariableTag lvt = (LocalVariableTag) t;
  754. if (lvt.getSlot() == index) {
  755. return lvt;
  756. }
  757. }
  758. }
  759. return null;
  760. }
  761. static int getLineNumber(InstructionHandle ih, int prevLine) {
  762. for (InstructionTargeter t : ih.getTargeters()) {
  763. if (t instanceof LineNumberTag) {
  764. return ((LineNumberTag) t).getLineNumber();
  765. }
  766. }
  767. return prevLine;
  768. }
  769. public boolean isStatic() {
  770. return Modifier.isStatic(getAccessFlags());
  771. }
  772. public boolean isAbstract() {
  773. return Modifier.isAbstract(getAccessFlags());
  774. }
  775. public boolean isBridgeMethod() {
  776. return (getAccessFlags() & Constants.ACC_BRIDGE) != 0;
  777. }
  778. public void addExceptionHandler(InstructionHandle start, InstructionHandle end, InstructionHandle handlerStart,
  779. ObjectType catchType, boolean highPriority) {
  780. InstructionHandle start1 = Range.genStart(body, start);
  781. InstructionHandle end1 = Range.genEnd(body, end);
  782. ExceptionRange er = new ExceptionRange(body, (catchType == null ? null : BcelWorld.fromBcel(catchType)), highPriority);
  783. er.associateWithTargets(start1, end1, handlerStart);
  784. }
  785. public int getAccessFlags() {
  786. return modifiers;
  787. }
  788. public int getAccessFlagsWithoutSynchronized() {
  789. if (isSynchronized()) {
  790. return modifiers - Modifier.SYNCHRONIZED;
  791. }
  792. return modifiers;
  793. }
  794. public boolean isSynchronized() {
  795. return (modifiers & Modifier.SYNCHRONIZED) != 0;
  796. }
  797. public void setAccessFlags(int newFlags) {
  798. this.modifiers = newFlags;
  799. }
  800. public Type[] getArgumentTypes() {
  801. initialize();
  802. return argumentTypes;
  803. }
  804. public LazyClassGen getEnclosingClass() {
  805. return enclosingClass;
  806. }
  807. public int getMaxLocals() {
  808. return maxLocals;
  809. }
  810. public String getName() {
  811. return name;
  812. }
  813. public String getGenericReturnTypeSignature() {
  814. if (memberView == null) {
  815. return getReturnType().getSignature();
  816. } else {
  817. return memberView.getGenericReturnType().getSignature();
  818. }
  819. }
  820. public Type getReturnType() {
  821. initialize();
  822. return returnType;
  823. }
  824. public void setMaxLocals(int maxLocals) {
  825. this.maxLocals = maxLocals;
  826. }
  827. public InstructionList getBody() {
  828. markAsChanged();
  829. return body;
  830. }
  831. public InstructionList getBodyForPrint() {
  832. return body;
  833. }
  834. public boolean hasBody() {
  835. if (savedMethod != null) {
  836. return savedMethod.getCode() != null;
  837. }
  838. return body != null;
  839. }
  840. public List<Attribute> getAttributes() {
  841. return attributes;
  842. }
  843. public String[] getDeclaredExceptions() {
  844. return declaredExceptions;
  845. }
  846. public String getClassName() {
  847. return enclosingClass.getName();
  848. }
  849. // ---- packing!
  850. public MethodGen pack() {
  851. forceSyntheticForAjcMagicMembers();
  852. // killNops();
  853. int flags = getAccessFlags();
  854. if (enclosingClass.getWorld().isJoinpointSynchronizationEnabled()
  855. && enclosingClass.getWorld().areSynchronizationPointcutsInUse()) {
  856. flags = getAccessFlagsWithoutSynchronized();
  857. }
  858. MethodGen gen = new MethodGen(flags, getReturnType(), getArgumentTypes(), null, // getArgumentNames(),
  859. getName(), getEnclosingClass().getName(), new InstructionList(), getEnclosingClass().getConstantPool());
  860. for (int i = 0, len = declaredExceptions.length; i < len; i++) {
  861. gen.addException(declaredExceptions[i]);
  862. }
  863. for (Attribute attr : attributes) {
  864. gen.addAttribute(attr);
  865. }
  866. if (newAnnotations != null) {
  867. for (AnnotationAJ element : newAnnotations) {
  868. gen.addAnnotation(new AnnotationGen(((BcelAnnotation) element).getBcelAnnotation(), gen.getConstantPool(), true));
  869. }
  870. }
  871. if (newParameterAnnotations != null) {
  872. for (int i = 0; i < newParameterAnnotations.length; i++) {
  873. AnnotationAJ[] annos = newParameterAnnotations[i];
  874. for (int j = 0; j < annos.length; j++) {
  875. gen.addParameterAnnotation(i,
  876. new AnnotationGen(((BcelAnnotation) annos[j]).getBcelAnnotation(), gen.getConstantPool(), true));
  877. }
  878. }
  879. }
  880. if (memberView != null && memberView.getAnnotations() != null && memberView.getAnnotations().length != 0) {
  881. AnnotationAJ[] ans = memberView.getAnnotations();
  882. for (int i = 0, len = ans.length; i < len; i++) {
  883. AnnotationGen a = ((BcelAnnotation) ans[i]).getBcelAnnotation();
  884. gen.addAnnotation(new AnnotationGen(a, gen.getConstantPool(), true));
  885. }
  886. }
  887. if (isSynthetic) {
  888. if (enclosingClass.getWorld().isInJava5Mode()) {
  889. gen.setModifiers(gen.getModifiers() | Constants.ACC_SYNTHETIC);
  890. }
  891. if (!hasAttribute("Synthetic")) {
  892. // belt and braces, do the attribute even on Java 5 in addition to the modifier flag
  893. ConstantPool cpg = gen.getConstantPool();
  894. int index = cpg.addUtf8("Synthetic");
  895. gen.addAttribute(new Synthetic(index, 0, new byte[0], cpg));
  896. }
  897. }
  898. if (hasBody()) {
  899. if (this.enclosingClass.getWorld().shouldFastPackMethods()) {
  900. if (isAdviceMethod() || getName().equals("<clinit>")) {
  901. packBody(gen);
  902. } else {
  903. optimizedPackBody(gen);
  904. }
  905. } else {
  906. packBody(gen);
  907. }
  908. gen.setMaxLocals();
  909. gen.setMaxStack();
  910. } else {
  911. gen.setInstructionList(null);
  912. }
  913. return gen;
  914. }
  915. private boolean hasAttribute(String attributeName) {
  916. for (Attribute attr: attributes) {
  917. if (attr.getName().equals(attributeName)) {
  918. return true;
  919. }
  920. }
  921. return false;
  922. }
  923. private void forceSyntheticForAjcMagicMembers() {
  924. if (NameMangler.isSyntheticMethod(getName(), inAspect())) {
  925. makeSynthetic();
  926. }
  927. }
  928. private boolean inAspect() {
  929. BcelObjectType objectType = enclosingClass.getBcelObjectType();
  930. return (objectType == null ? false : objectType.isAspect());
  931. }
  932. public void makeSynthetic() {
  933. isSynthetic = true;
  934. }
  935. private static class LVPosition {
  936. InstructionHandle start = null;
  937. InstructionHandle end = null;
  938. }
  939. /**
  940. * fill the newly created method gen with our body, inspired by InstructionList.copy()
  941. */
  942. public void packBody(MethodGen gen) {
  943. InstructionList fresh = gen.getInstructionList();
  944. Map<InstructionHandle, InstructionHandle> map = copyAllInstructionsExceptRangeInstructionsInto(fresh);
  945. // at this point, no rangeHandles are in fresh. Let's use that...
  946. /*
  947. * Update branch targets and insert various attributes. Insert our exceptionHandlers into a sorted list, so they can be
  948. * added in order later.
  949. */
  950. InstructionHandle oldInstructionHandle = getBody().getStart();
  951. InstructionHandle newInstructionHandle = fresh.getStart();
  952. LinkedList<ExceptionRange> exceptionList = new LinkedList<ExceptionRange>();
  953. Map<LocalVariableTag, LVPosition> localVariables = new HashMap<LocalVariableTag, LVPosition>();
  954. int currLine = -1;
  955. int lineNumberOffset = (fromFilename == null) ? 0 : getEnclosingClass().getSourceDebugExtensionOffset(fromFilename);
  956. while (oldInstructionHandle != null) {
  957. if (map.get(oldInstructionHandle) == null) {
  958. // must be a range instruction since they're the only things we
  959. // didn't copy across
  960. handleRangeInstruction(oldInstructionHandle, exceptionList);
  961. // just increment ih.
  962. oldInstructionHandle = oldInstructionHandle.getNext();
  963. } else {
  964. // assert map.get(ih) == jh
  965. Instruction oldInstruction = oldInstructionHandle.getInstruction();
  966. Instruction newInstruction = newInstructionHandle.getInstruction();
  967. if (oldInstruction instanceof InstructionBranch) {
  968. handleBranchInstruction(map, oldInstruction, newInstruction);
  969. }
  970. // now deal with line numbers
  971. // and store up info for local variables
  972. for (InstructionTargeter targeter : oldInstructionHandle.getTargeters()) {
  973. if (targeter instanceof LineNumberTag) {
  974. int line = ((LineNumberTag) targeter).getLineNumber();
  975. if (line != currLine) {
  976. gen.addLineNumber(newInstructionHandle, line + lineNumberOffset);
  977. currLine = line;
  978. }
  979. } else if (targeter instanceof LocalVariableTag) {
  980. LocalVariableTag lvt = (LocalVariableTag) targeter;
  981. LVPosition p = localVariables.get(lvt);
  982. // If we don't know about it, create a new position and
  983. // store
  984. // If we do know about it - update its end position
  985. if (p == null) {
  986. LVPosition newp = new LVPosition();
  987. newp.start = newp.end = newInstructionHandle;
  988. localVariables.put(lvt, newp);
  989. } else {
  990. p.end = newInstructionHandle;
  991. }
  992. }
  993. }
  994. // now continue
  995. oldInstructionHandle = oldInstructionHandle.getNext();
  996. newInstructionHandle = newInstructionHandle.getNext();
  997. }
  998. }
  999. addExceptionHandlers(gen, map, exceptionList);
  1000. if (localVariables.size() == 0) {
  1001. // Might be a case of 173978 where around advice on an execution join point
  1002. // has caused everything to be extracted from the method and thus we
  1003. // are left with no local variables, not even the ones for 'this' and
  1004. // parameters passed to the method
  1005. createNewLocalVariables(gen);
  1006. } else {
  1007. addLocalVariables(gen, localVariables);
  1008. }
  1009. // JAVAC adds line number tables (with just one entry) to generated
  1010. // accessor methods - this
  1011. // keeps some tools that rely on finding at least some form of
  1012. // linenumbertable happy.
  1013. // Let's check if we have one - if we don't then let's add one.
  1014. // TODO Could be made conditional on whether line debug info is being
  1015. // produced
  1016. if (gen.getLineNumbers().length == 0) {
  1017. gen.addLineNumber(gen.getInstructionList().getStart(), 1);
  1018. }
  1019. }
  1020. private void createNewLocalVariables(MethodGen gen) {
  1021. gen.removeLocalVariables();
  1022. // ignore <clinit> or <init> for now
  1023. if (!getName().startsWith("<")) {
  1024. int slot = 0;
  1025. InstructionHandle start = gen.getInstructionList().getStart();
  1026. InstructionHandle end = gen.getInstructionList().getEnd();
  1027. // Add a 'this' if non-static
  1028. if (!isStatic()) {
  1029. String cname = this.enclosingClass.getClassName();
  1030. if (cname == null) {
  1031. return; // give up for now
  1032. }
  1033. Type enclosingType = BcelWorld.makeBcelType(UnresolvedType.forName(cname));
  1034. gen.addLocalVariable("this", enclosingType, slot++, start, end);
  1035. }
  1036. // Add entries for the method arguments
  1037. String[] paramNames = (memberView == null ? null : memberView.getParameterNames());
  1038. if (paramNames != null) {
  1039. for (int i = 0; i < argumentTypes.length; i++) {
  1040. String pname = paramNames[i];
  1041. if (pname == null) {
  1042. pname = "arg" + i;
  1043. }
  1044. gen.addLocalVariable(pname, argumentTypes[i], slot, start, end);
  1045. slot += argumentTypes[i].getSize();
  1046. }
  1047. }
  1048. }
  1049. }
  1050. /*
  1051. * Optimized packing that does a 'local packing' of the code rather than building a brand new method and packing into it. Only
  1052. * usable when the packing is going to be done just once.
  1053. */
  1054. public void optimizedPackBody(MethodGen gen) {
  1055. InstructionList theBody = getBody();
  1056. InstructionHandle iHandle = theBody.getStart();
  1057. int currLine = -1;
  1058. int lineNumberOffset = (fromFilename == null) ? 0 : getEnclosingClass().getSourceDebugExtensionOffset(fromFilename);
  1059. Map<LocalVariableTag, LVPosition> localVariables = new HashMap<LocalVariableTag, LVPosition>();
  1060. LinkedList<ExceptionRange> exceptionList = new LinkedList<ExceptionRange>();
  1061. Set<InstructionHandle> forDeletion = new HashSet<InstructionHandle>();
  1062. Set<BranchHandle> branchInstructions = new HashSet<BranchHandle>();
  1063. // OPTIMIZE sort out in here: getRange()/insertHandler() and type of
  1064. // exceptionList
  1065. while (iHandle != null) {
  1066. Instruction inst = iHandle.getInstruction();
  1067. // InstructionHandle nextInst = iHandle.getNext();
  1068. // OPTIMIZE remove this instructionhandle as it now points to
  1069. // nowhere?
  1070. if (inst == Range.RANGEINSTRUCTION) {
  1071. Range r = Range.getRange(iHandle);
  1072. if (r instanceof ExceptionRange) {
  1073. ExceptionRange er = (ExceptionRange) r;
  1074. if (er.getStart() == iHandle) {
  1075. if (!er.isEmpty()) {
  1076. // order is important, insert handlers in order of start
  1077. insertHandler(er, exceptionList);
  1078. }
  1079. }
  1080. }
  1081. forDeletion.add(iHandle);
  1082. } else {
  1083. if (inst instanceof InstructionBranch) {
  1084. branchInstructions.add((BranchHandle) iHandle);
  1085. }
  1086. for (InstructionTargeter targeter : iHandle.getTargetersCopy()) {
  1087. if (targeter instanceof LineNumberTag) {
  1088. int line = ((LineNumberTag) targeter).getLineNumber();
  1089. if (line != currLine) {
  1090. gen.addLineNumber(iHandle, line + lineNumberOffset);
  1091. currLine = line;
  1092. }
  1093. } else if (targeter instanceof LocalVariableTag) {
  1094. LocalVariableTag lvt = (LocalVariableTag) targeter;
  1095. LVPosition p = localVariables.get(lvt);
  1096. // If we don't know about it, create a new position
  1097. // and store
  1098. // If we do know about it - update its end position
  1099. if (p == null) {
  1100. LVPosition newp = new LVPosition();
  1101. newp.start = newp.end = iHandle;
  1102. localVariables.put(lvt, newp);
  1103. } else {
  1104. p.end = iHandle;
  1105. }
  1106. }
  1107. }
  1108. }
  1109. iHandle = iHandle.getNext();
  1110. }
  1111. for (BranchHandle branchHandle : branchInstructions) {
  1112. handleBranchInstruction(branchHandle, forDeletion);
  1113. }
  1114. // now add exception handlers
  1115. for (ExceptionRange r : exceptionList) {
  1116. if (r.isEmpty()) {
  1117. continue;
  1118. }
  1119. gen.addExceptionHandler(jumpForward(r.getRealStart(), forDeletion), jumpForward(r.getRealEnd(), forDeletion),
  1120. jumpForward(r.getHandler(), forDeletion),
  1121. (r.getCatchType() == null) ? null : (ObjectType) BcelWorld.makeBcelType(r.getCatchType()));
  1122. }
  1123. for (InstructionHandle handle : forDeletion) {
  1124. try {
  1125. theBody.delete(handle);
  1126. } catch (TargetLostException e) {
  1127. e.printStackTrace();
  1128. }
  1129. }
  1130. gen.setInstructionList(theBody);
  1131. if (localVariables.size() == 0) {
  1132. // Might be a case of 173978 where around advice on an execution join point
  1133. // has caused everything to be extracted from the method and thus we
  1134. // are left with no local variables, not even the ones for 'this' and
  1135. // parameters passed to the method
  1136. createNewLocalVariables(gen);
  1137. } else {
  1138. addLocalVariables(gen, localVariables);
  1139. }
  1140. // JAVAC adds line number tables (with just one entry) to generated
  1141. // accessor methods - this
  1142. // keeps some tools that rely on finding at least some form of
  1143. // linenumbertable happy.
  1144. // Let's check if we have one - if we don't then let's add one.
  1145. // TODO Could be made conditional on whether line debug info is being
  1146. // produced
  1147. if (gen.getLineNumbers().length == 0) {
  1148. gen.addLineNumber(gen.getInstructionList().getStart(), 1);
  1149. }
  1150. wasPackedOptimally = true;
  1151. }
  1152. private void addLocalVariables(MethodGen gen, Map<LocalVariableTag, LVPosition> localVariables) {
  1153. // now add local variables
  1154. gen.removeLocalVariables();
  1155. // this next iteration _might_ be overkill, but we had problems with
  1156. // bcel before with duplicate local variables. Now that we're patching
  1157. // bcel we should be able to do without it if we're paranoid enough
  1158. // through the rest of the compiler.
  1159. InstructionHandle methodStart = gen.getInstructionList().getStart();
  1160. InstructionHandle methodEnd = gen.getInstructionList().getEnd();
  1161. // Determine how many 'slots' are used by parameters to the method.
  1162. // Then below we can determine if a local variable is a parameter variable, if it is
  1163. // we force its range to from the method start (as it may have been shuffled down
  1164. // due to insertion of advice like cflow entry)
  1165. int paramSlots = gen.isStatic() ? 0 : 1;
  1166. Type[] argTypes = gen.getArgumentTypes();
  1167. if (argTypes != null) {
  1168. for (int i = 0; i < argTypes.length; i++) {
  1169. if (argTypes[i].getSize() == 2) {
  1170. paramSlots += 2;
  1171. } else {
  1172. paramSlots += 1;
  1173. }
  1174. }
  1175. }
  1176. Map<InstructionHandle, Set<Integer>> duplicatedLocalMap = new HashMap<InstructionHandle, Set<Integer>>();
  1177. for (LocalVariableTag tag : localVariables.keySet()) {
  1178. // have we already added one with the same slot number and start
  1179. // location?
  1180. // if so, just continue.
  1181. LVPosition lvpos = localVariables.get(tag);
  1182. InstructionHandle start = (tag.getSlot() < paramSlots ? methodStart : lvpos.start);
  1183. InstructionHandle end = (tag.getSlot() < paramSlots ? methodEnd : lvpos.end);
  1184. Set<Integer> slots = duplicatedLocalMap.get(start);
  1185. if (slots == null) {
  1186. slots = new HashSet<Integer>();
  1187. duplicatedLocalMap.put(start, slots);
  1188. } else if (slots.contains(new Integer(tag.getSlot()))) {
  1189. // we already have a var starting at this tag with this slot
  1190. continue;
  1191. }
  1192. slots.add(Integer.valueOf(tag.getSlot()));
  1193. Type t = tag.getRealType();
  1194. if (t == null) {
  1195. t = BcelWorld.makeBcelType(UnresolvedType.forSignature(tag.getType()));
  1196. }
  1197. gen.addLocalVariable(tag.getName(), t, tag.getSlot(), start, end);
  1198. }
  1199. }
  1200. private void addExceptionHandlers(MethodGen gen, Map<InstructionHandle, InstructionHandle> map,
  1201. LinkedList<ExceptionRange> exnList) {
  1202. // now add exception handlers
  1203. for (ExceptionRange r : exnList) {
  1204. if (r.isEmpty()) {
  1205. continue;
  1206. }
  1207. InstructionHandle rMappedStart = remap(r.getRealStart(), map);
  1208. InstructionHandle rMappedEnd = remap(r.getRealEnd(), map);
  1209. InstructionHandle rMappedHandler = remap(r.getHandler(), map);
  1210. gen.addExceptionHandler(rMappedStart, rMappedEnd, rMappedHandler, (r.getCatchType() == null) ? null
  1211. : (ObjectType) BcelWorld.makeBcelType(r.getCatchType()));
  1212. }
  1213. }
  1214. private void handleBranchInstruction(Map<InstructionHandle, InstructionHandle> map, Instruction oldInstruction,
  1215. Instruction newInstruction) {
  1216. InstructionBranch oldBranchInstruction = (InstructionBranch) oldInstruction;
  1217. InstructionBranch newBranchInstruction = (InstructionBranch) newInstruction;
  1218. InstructionHandle oldTarget = oldBranchInstruction.getTarget(); // old
  1219. // target
  1220. // New target is in hash map
  1221. newBranchInstruction.setTarget(remap(oldTarget, map));
  1222. if (oldBranchInstruction instanceof InstructionSelect) {
  1223. // Either LOOKUPSWITCH or TABLESWITCH
  1224. InstructionHandle[] oldTargets = ((InstructionSelect) oldBranchInstruction).getTargets();
  1225. InstructionHandle[] newTargets = ((InstructionSelect) newBranchInstruction).getTargets();
  1226. for (int k = oldTargets.length - 1; k >= 0; k--) {
  1227. // Update all targets
  1228. newTargets[k] = remap(oldTargets[k], map);
  1229. newTargets[k].addTargeter(newBranchInstruction);
  1230. }
  1231. }
  1232. }
  1233. private InstructionHandle jumpForward(InstructionHandle t, Set<InstructionHandle> handlesForDeletion) {
  1234. InstructionHandle target = t;
  1235. if (handlesForDeletion.contains(target)) {
  1236. do {
  1237. target = target.getNext();
  1238. } while (handlesForDeletion.contains(target));
  1239. }
  1240. return target;
  1241. }
  1242. /**
  1243. * Process a branch instruction with respect to instructions that are about to be deleted. If the target for the branch is a
  1244. * candidate for deletion, move it to the next valid instruction after the deleted target.
  1245. */
  1246. private void handleBranchInstruction(BranchHandle branchHandle, Set<InstructionHandle> handlesForDeletion) {
  1247. InstructionBranch branchInstruction = (InstructionBranch) branchHandle.getInstruction();
  1248. InstructionHandle target = branchInstruction.getTarget(); // old target
  1249. if (handlesForDeletion.contains(target)) {
  1250. do {
  1251. target = target.getNext();
  1252. } while (handlesForDeletion.contains(target));
  1253. branchInstruction.setTarget(target);
  1254. }
  1255. if (branchInstruction instanceof InstructionSelect) {
  1256. // Either LOOKUPSWITCH or TABLESWITCH
  1257. InstructionSelect iSelect = (InstructionSelect) branchInstruction;
  1258. InstructionHandle[] targets = iSelect.getTargets();
  1259. for (int k = targets.length - 1; k >= 0; k--) {
  1260. InstructionHandle oneTarget = targets[k];
  1261. if (handlesForDeletion.contains(oneTarget)) {
  1262. do {
  1263. oneTarget = oneTarget.getNext();
  1264. } while (handlesForDeletion.contains(oneTarget));
  1265. iSelect.setTarget(k, oneTarget);
  1266. oneTarget.addTargeter(branchInstruction);
  1267. }
  1268. }
  1269. }
  1270. }
  1271. private void handleRangeInstruction(InstructionHandle ih, LinkedList<ExceptionRange> exnList) {
  1272. // we're a range instruction
  1273. Range r = Range.getRange(ih);
  1274. if (r instanceof ExceptionRange) {
  1275. ExceptionRange er = (ExceptionRange) r;
  1276. if (er.getStart() == ih) {
  1277. // System.err.println("er " + er);
  1278. if (!er.isEmpty()) {
  1279. // order is important, insert handlers in order of start
  1280. insertHandler(er, exnList);
  1281. }
  1282. }
  1283. } else {
  1284. // we must be a shadow range or something equally useless,
  1285. // so forget about doing anything
  1286. }
  1287. }
  1288. /*
  1289. * Make copies of all instructions, append them to the new list and associate old instruction references with the new ones,
  1290. * i.e., a 1:1 mapping.
  1291. */
  1292. private Map<InstructionHandle, InstructionHandle> copyAllInstructionsExceptRangeInstructionsInto(InstructionList intoList) {
  1293. Map<InstructionHandle, InstructionHandle> map = new HashMap<InstructionHandle, InstructionHandle>();
  1294. for (InstructionHandle ih = getBody().getStart(); ih != null; ih = ih.getNext()) {
  1295. if (Range.isRangeHandle(ih)) {
  1296. continue;
  1297. }
  1298. Instruction inst = ih.getInstruction();
  1299. Instruction copy = Utility.copyInstruction(inst);
  1300. if (copy instanceof InstructionBranch) {
  1301. map.put(ih, intoList.append((InstructionBranch) copy));
  1302. } else {
  1303. map.put(ih, intoList.append(copy));
  1304. }
  1305. }
  1306. return map;
  1307. }
  1308. /**
  1309. * This procedure should not currently be used.
  1310. */
  1311. // public void killNops() {
  1312. // InstructionHandle curr = body.getStart();
  1313. // while (true) {
  1314. // if (curr == null) break;
  1315. // InstructionHandle next = curr.getNext();
  1316. // if (curr.getInstruction() instanceof NOP) {
  1317. // InstructionTargeter[] targeters = curr.getTargeters();
  1318. // if (targeters != null) {
  1319. // for (int i = 0, len = targeters.length; i < len; i++) {
  1320. // InstructionTargeter targeter = targeters[i];
  1321. // targeter.updateTarget(curr, next);
  1322. // }
  1323. // }
  1324. // try {
  1325. // body.delete(curr);
  1326. // } catch (TargetLostException e) {
  1327. // }
  1328. // }
  1329. // curr = next;
  1330. // }
  1331. // }
  1332. // private static InstructionHandle fNext(InstructionHandle ih) {
  1333. // while (true) {
  1334. // if (ih.getInstruction()==Range.RANGEINSTRUCTION) ih = ih.getNext();
  1335. // else return ih;
  1336. // }
  1337. // }
  1338. private static InstructionHandle remap(InstructionHandle handle, Map<InstructionHandle, InstructionHandle> map) {
  1339. while (true) {
  1340. InstructionHandle ret = map.get(handle);
  1341. if (ret == null) {
  1342. handle = handle.getNext();
  1343. } else {
  1344. return ret;
  1345. }
  1346. }
  1347. }
  1348. // Update to all these comments, ASC 11-01-2005
  1349. // The right thing to do may be to do more with priorities as
  1350. // we create new exception handlers, but that is a relatively
  1351. // complex task. In the meantime, just taking account of the
  1352. // priority here enables a couple of bugs to be fixed to do
  1353. // with using return or break in code that contains a finally
  1354. // block (pr78021,pr79554).
  1355. // exception ordering.
  1356. // What we should be doing is dealing with priority inversions way earlier
  1357. // than we are
  1358. // and counting on the tree structure. In which case, the below code is in
  1359. // fact right.
  1360. // XXX THIS COMMENT BELOW IS CURRENTLY WRONG.
  1361. // An exception A preceeds an exception B in the exception table iff:
  1362. // * A and B were in the original method, and A preceeded B in the original
  1363. // exception table
  1364. // * If A has a higher priority than B, than it preceeds B.
  1365. // * If A and B have the same priority, then the one whose START happens
  1366. // EARLIEST has LEAST priority.
  1367. // in short, the outermost exception has least priority.
  1368. // we implement this with a LinkedList. We could possibly implement this
  1369. // with a java.util.SortedSet,
  1370. // but I don't trust the only implementation, TreeSet, to do the right
  1371. // thing.
  1372. /* private */static void insertHandler(ExceptionRange fresh, LinkedList<ExceptionRange> l) {
  1373. // Old implementation, simply: l.add(0,fresh);
  1374. for (ListIterator<ExceptionRange> iter = l.listIterator(); iter.hasNext();) {
  1375. ExceptionRange r = iter.next();
  1376. // int freal = fresh.getRealStart().getPosition();
  1377. // int rreal = r.getRealStart().getPosition();
  1378. if (fresh.getPriority() >= r.getPriority()) {
  1379. iter.previous();
  1380. iter.add(fresh);
  1381. return;
  1382. }
  1383. }
  1384. // we have reached the end
  1385. l.add(fresh);
  1386. }
  1387. public boolean isPrivate() {
  1388. return Modifier.isPrivate(getAccessFlags());
  1389. }
  1390. public boolean isProtected() {
  1391. return Modifier.isProtected(getAccessFlags());
  1392. }
  1393. public boolean isDefault() {
  1394. return !(isProtected() || isPrivate() || isPublic());
  1395. }
  1396. public boolean isPublic() {
  1397. return Modifier.isPublic(getAccessFlags());
  1398. }
  1399. // ----
  1400. /**
  1401. * A good body is a body with the following properties:
  1402. *
  1403. * <ul>
  1404. * <li>For each branch instruction S in body, target T of S is in body.
  1405. * <li>For each branch instruction S in body, target T of S has S as a targeter.
  1406. * <li>For each instruction T in body, for each branch instruction S that is a targeter of T, S is in body.
  1407. * <li>For each non-range-handle instruction T in body, for each instruction S that is a targeter of T, S is either a branch
  1408. * instruction, an exception range or a tag
  1409. * <li>For each range-handle instruction T in body, there is exactly one targeter S that is a range.
  1410. * <li>For each range-handle instruction T in body, the range R targeting T is in body.
  1411. * <li>For each instruction T in body, for each exception range R targeting T, R is in body.
  1412. * <li>For each exception range R in body, let T := R.handler. T is in body, and R is one of T's targeters
  1413. * <li>All ranges are properly nested: For all ranges Q and R, if Q.start preceeds R.start, then R.end preceeds Q.end.
  1414. * </ul>
  1415. *
  1416. * Where the shorthand "R is in body" means "R.start is in body, R.end is in body, and any InstructionHandle stored in a field
  1417. * of R (such as an exception handle) is in body".
  1418. */
  1419. public void assertGoodBody() {
  1420. if (true) {
  1421. return; // only enable for debugging
  1422. }
  1423. assertGoodBody(getBody(), toString());
  1424. }
  1425. public static void assertGoodBody(InstructionList il, String from) {
  1426. if (true) {
  1427. return; // only to be enabled for debugging
  1428. }
  1429. // if (il == null) {
  1430. // return;
  1431. // }
  1432. // Set body = new HashSet();
  1433. // Stack<Range> ranges = new Stack<Range>();
  1434. // for (InstructionHandle ih = il.getStart(); ih != null; ih = ih.getNext()) {
  1435. // body.add(ih);
  1436. // if (ih.getInstruction() instanceof InstructionBranch) {
  1437. // body.add(ih.getInstruction());
  1438. // }
  1439. // }
  1440. //
  1441. // for (InstructionHandle ih = il.getStart(); ih != null; ih = ih.getNext()) {
  1442. // assertGoodHandle(ih, body, ranges, from);
  1443. // Iterator<InstructionTargeter> tIter = ih.getTargeters().iterator();
  1444. // while (tIter.hasNext()) {
  1445. // assertGoodTargeter(tIter.next(), ih, body, from);
  1446. // }
  1447. // }
  1448. }
  1449. // private static void assertGoodHandle(InstructionHandle ih, Set body, Stack<Range> ranges, String from) {
  1450. // Instruction inst = ih.getInstruction();
  1451. // if ((inst instanceof InstructionBranch) ^ (ih instanceof BranchHandle)) {
  1452. // throw new BCException("bad instruction/handle pair in " + from);
  1453. // }
  1454. // if (Range.isRangeHandle(ih)) {
  1455. // assertGoodRangeHandle(ih, body, ranges, from);
  1456. // } else if (inst instanceof InstructionBranch) {
  1457. // assertGoodBranchInstruction((BranchHandle) ih, (InstructionBranch) inst, body, ranges, from);
  1458. // }
  1459. // }
  1460. // private static void assertGoodBranchInstruction(BranchHandle ih, InstructionBranch inst, Set body, Stack<Range> ranges,
  1461. // String from) {
  1462. // if (ih.getTarget() != inst.getTarget()) {
  1463. // throw new BCException("bad branch instruction/handle pair in " + from);
  1464. // }
  1465. // InstructionHandle target = ih.getTarget();
  1466. // assertInBody(target, body, from);
  1467. // assertTargetedBy(target, inst, from);
  1468. // if (inst instanceof InstructionSelect) {
  1469. // InstructionSelect sel = (InstructionSelect) inst;
  1470. // InstructionHandle[] itargets = sel.getTargets();
  1471. // for (int k = itargets.length - 1; k >= 0; k--) {
  1472. // assertInBody(itargets[k], body, from);
  1473. // assertTargetedBy(itargets[k], inst, from);
  1474. // }
  1475. // }
  1476. // }
  1477. /** ih is an InstructionHandle or a BranchInstruction */
  1478. // private static void assertInBody(Object ih, Set body, String from) {
  1479. // if (!body.contains(ih)) {
  1480. // throw new BCException("thing not in body in " + from);
  1481. // }
  1482. // }
  1483. // private static void assertGoodRangeHandle(InstructionHandle ih, Set body, Stack ranges, String from) {
  1484. // Range r = getRangeAndAssertExactlyOne(ih, from);
  1485. // assertGoodRange(r, body, from);
  1486. // if (r.getStart() == ih) {
  1487. // ranges.push(r);
  1488. // } else if (r.getEnd() == ih) {
  1489. // if (ranges.peek() != r) {
  1490. // throw new BCException("bad range inclusion in " + from);
  1491. // }
  1492. // ranges.pop();
  1493. // }
  1494. // }
  1495. // private static void assertGoodRange(Range r, Set body, String from) {
  1496. // assertInBody(r.getStart(), body, from);
  1497. // assertRangeHandle(r.getStart(), from);
  1498. // assertTargetedBy(r.getStart(), r, from);
  1499. //
  1500. // assertInBody(r.getEnd(), body, from);
  1501. // assertRangeHandle(r.getEnd(), from);
  1502. // assertTargetedBy(r.getEnd(), r, from);
  1503. //
  1504. // if (r instanceof ExceptionRange) {
  1505. // ExceptionRange er = (ExceptionRange) r;
  1506. // assertInBody(er.getHandler(), body, from);
  1507. // assertTargetedBy(er.getHandler(), r, from);
  1508. // }
  1509. // }
  1510. // private static void assertRangeHandle(InstructionHandle ih, String from) {
  1511. // if (!Range.isRangeHandle(ih)) {
  1512. // throw new BCException("bad range handle " + ih + " in " + from);
  1513. // }
  1514. // }
  1515. private static void assertTargetedBy(InstructionHandle target, InstructionTargeter targeter, String from) {
  1516. Iterator tIter = target.getTargeters().iterator();
  1517. while (tIter.hasNext()) {
  1518. if (((InstructionTargeter) tIter.next()) == targeter) {
  1519. return;
  1520. }
  1521. }
  1522. throw new RuntimeException("bad targeting relationship in " + from);
  1523. }
  1524. private static void assertTargets(InstructionTargeter targeter, InstructionHandle target, String from) {
  1525. if (targeter instanceof Range) {
  1526. Range r = (Range) targeter;
  1527. if (r.getStart() == target || r.getEnd() == target) {
  1528. return;
  1529. }
  1530. if (r instanceof ExceptionRange) {
  1531. if (((ExceptionRange) r).getHandler() == target) {
  1532. return;
  1533. }
  1534. }
  1535. } else if (targeter instanceof InstructionBranch) {
  1536. InstructionBranch bi = (InstructionBranch) targeter;
  1537. if (bi.getTarget() == target) {
  1538. return;
  1539. }
  1540. if (targeter instanceof InstructionSelect) {
  1541. InstructionSelect sel = (InstructionSelect) targeter;
  1542. InstructionHandle[] itargets = sel.getTargets();
  1543. for (int k = itargets.length - 1; k >= 0; k--) {
  1544. if (itargets[k] == target) {
  1545. return;
  1546. }
  1547. }
  1548. }
  1549. } else if (targeter instanceof Tag) {
  1550. return;
  1551. }
  1552. throw new BCException(targeter + " doesn't target " + target + " in " + from);
  1553. }
  1554. private static Range getRangeAndAssertExactlyOne(InstructionHandle ih, String from) {
  1555. Range ret = null;
  1556. Iterator<InstructionTargeter> tIter = ih.getTargeters().iterator();
  1557. if (!tIter.hasNext()) {
  1558. throw new BCException("range handle with no range in " + from);
  1559. }
  1560. while (tIter.hasNext()) {
  1561. InstructionTargeter ts = tIter.next();
  1562. if (ts instanceof Range) {
  1563. if (ret != null) {
  1564. throw new BCException("range handle with multiple ranges in " + from);
  1565. }
  1566. ret = (Range) ts;
  1567. }
  1568. }
  1569. if (ret == null) {
  1570. throw new BCException("range handle with no range in " + from);
  1571. }
  1572. return ret;
  1573. }
  1574. // private static void assertGoodTargeter(InstructionTargeter t, InstructionHandle ih, Set body, String from) {
  1575. // assertTargets(t, ih, from);
  1576. // if (t instanceof Range) {
  1577. // assertGoodRange((Range) t, body, from);
  1578. // } else if (t instanceof InstructionBranch) {
  1579. // assertInBody(t, body, from);
  1580. // }
  1581. // }
  1582. // ----
  1583. boolean isAdviceMethod() {
  1584. if (memberView == null) {
  1585. return false;
  1586. }
  1587. return memberView.getAssociatedShadowMunger() != null;
  1588. }
  1589. boolean isAjSynthetic() {
  1590. if (memberView == null) {
  1591. return true;
  1592. }
  1593. return memberView.isAjSynthetic();
  1594. }
  1595. boolean isSynthetic() {
  1596. if (memberView == null) {
  1597. return false;
  1598. }
  1599. return memberView.isSynthetic();
  1600. }
  1601. public ISourceLocation getSourceLocation() {
  1602. if (memberView != null) {
  1603. return memberView.getSourceLocation();
  1604. }
  1605. return null;
  1606. }
  1607. public AjAttribute.EffectiveSignatureAttribute getEffectiveSignature() {
  1608. // if (memberView == null) return null;
  1609. if (effectiveSignature != null) {
  1610. return effectiveSignature;
  1611. }
  1612. return memberView.getEffectiveSignature();
  1613. }
  1614. public void setEffectiveSignature(ResolvedMember member, Shadow.Kind kind, boolean shouldWeave) {
  1615. this.effectiveSignature = new AjAttribute.EffectiveSignatureAttribute(member, kind, shouldWeave);
  1616. }
  1617. public String getSignature() {
  1618. if (memberView != null) {
  1619. return memberView.getSignature();
  1620. }
  1621. return MemberImpl.typesToSignature(BcelWorld.fromBcel(getReturnType()), BcelWorld.fromBcel(getArgumentTypes()), false);
  1622. }
  1623. public String getParameterSignature() {
  1624. if (memberView != null) {
  1625. return memberView.getParameterSignature();
  1626. }
  1627. return MemberImpl.typesToSignature(BcelWorld.fromBcel(getArgumentTypes()));
  1628. }
  1629. public BcelMethod getMemberView() {
  1630. return memberView;
  1631. }
  1632. public void forcePublic() {
  1633. markAsChanged();
  1634. modifiers = Utility.makePublic(modifiers);
  1635. }
  1636. public boolean getCanInline() {
  1637. return canInline;
  1638. }
  1639. public void setCanInline(boolean canInline) {
  1640. this.canInline = canInline;
  1641. }
  1642. public void addAttribute(Attribute attribute) {
  1643. attributes.add(attribute);
  1644. }
  1645. public String toTraceString() {
  1646. return toShortString();
  1647. }
  1648. public ConstantPool getConstantPool() {
  1649. return enclosingClass.getConstantPool();
  1650. }
  1651. public static boolean isConstructor(LazyMethodGen aMethod) {
  1652. return aMethod.getName().equals("<init>");
  1653. }
  1654. }