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.

LazyMethodGen.java 61KB

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