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.

BcelPerClauseAspectAdder.java 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*******************************************************************************
  2. * Copyright (c) 2005 Contributors.
  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://eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * initial implementation Alexandre Vasseur
  11. *******************************************************************************/
  12. package org.aspectj.weaver.bcel;
  13. import java.util.List;
  14. import org.aspectj.apache.bcel.Constants;
  15. import org.aspectj.apache.bcel.generic.InstructionBranch;
  16. import org.aspectj.apache.bcel.generic.InstructionConstants;
  17. import org.aspectj.apache.bcel.generic.InstructionFactory;
  18. import org.aspectj.apache.bcel.generic.InstructionHandle;
  19. import org.aspectj.apache.bcel.generic.InstructionList;
  20. import org.aspectj.apache.bcel.generic.ObjectType;
  21. import org.aspectj.apache.bcel.generic.ReferenceType;
  22. import org.aspectj.apache.bcel.generic.Type;
  23. import org.aspectj.weaver.AjAttribute;
  24. import org.aspectj.weaver.AjcMemberMaker;
  25. import org.aspectj.weaver.Member;
  26. import org.aspectj.weaver.NameMangler;
  27. import org.aspectj.weaver.ResolvedMember;
  28. import org.aspectj.weaver.ResolvedType;
  29. import org.aspectj.weaver.UnresolvedType;
  30. import org.aspectj.weaver.patterns.PerClause;
  31. /**
  32. * Adds aspectOf(), hasAspect() etc to the annotation defined aspects
  33. *
  34. * @author Alexandre Vasseur
  35. * @author Andy Clement
  36. */
  37. public class BcelPerClauseAspectAdder extends BcelTypeMunger {
  38. private final PerClause.Kind kind;
  39. private boolean hasGeneratedInner = false;
  40. public BcelPerClauseAspectAdder(ResolvedType aspect, PerClause.Kind kind) {
  41. super(null, aspect);
  42. this.kind = kind;
  43. if (kind == PerClause.SINGLETON || kind == PerClause.PERTYPEWITHIN || kind == PerClause.PERCFLOW) {
  44. // no inner needed
  45. hasGeneratedInner = true;
  46. }
  47. }
  48. public boolean munge(BcelClassWeaver weaver) {
  49. LazyClassGen gen = weaver.getLazyClassGen();
  50. doAggressiveInner(gen);
  51. // Only munge the aspect type
  52. if (!gen.getType().equals(aspectType)) {
  53. return false;
  54. }
  55. return doMunge(gen, true);
  56. }
  57. public boolean forceMunge(LazyClassGen gen, boolean checkAlreadyThere) {
  58. doAggressiveInner(gen);
  59. return doMunge(gen, checkAlreadyThere);
  60. }
  61. private void doAggressiveInner(LazyClassGen gen) {
  62. // agressively generate the inner interface if any
  63. // Note: we do so because of the bug #75442 that leads to have this interface implemented by all classes and not
  64. // only those matched by the per clause, which fails under LTW since the very first class
  65. // gets weaved and impl this interface that is still not defined.
  66. if (!hasGeneratedInner) {
  67. if (kind == PerClause.PEROBJECT) {// redundant test - see constructor, but safer
  68. // inner class
  69. UnresolvedType interfaceTypeX = AjcMemberMaker.perObjectInterfaceType(aspectType);
  70. LazyClassGen interfaceGen = new LazyClassGen(interfaceTypeX.getName(), "java.lang.Object", null,
  71. Constants.ACC_INTERFACE + Constants.ACC_PUBLIC + Constants.ACC_ABSTRACT, new String[0], getWorld());
  72. interfaceGen.addMethodGen(makeMethodGen(interfaceGen, AjcMemberMaker.perObjectInterfaceGet(aspectType)));
  73. interfaceGen.addMethodGen(makeMethodGen(interfaceGen, AjcMemberMaker.perObjectInterfaceSet(aspectType)));
  74. // not really an inner class of it but that does not matter, we pass back to the LTW
  75. gen.addGeneratedInner(interfaceGen);
  76. }
  77. hasGeneratedInner = true;
  78. }
  79. }
  80. private boolean doMunge(LazyClassGen gen, boolean checkAlreadyThere) {
  81. if (checkAlreadyThere && hasPerClauseMembersAlready(gen)) {
  82. return false;
  83. }
  84. generatePerClauseMembers(gen);
  85. if (kind == PerClause.SINGLETON) {
  86. generatePerSingletonAspectOfMethod(gen);
  87. generatePerSingletonHasAspectMethod(gen);
  88. generatePerSingletonAjcClinitMethod(gen);
  89. } else if (kind == PerClause.PEROBJECT) {
  90. generatePerObjectAspectOfMethod(gen);
  91. generatePerObjectHasAspectMethod(gen);
  92. generatePerObjectBindMethod(gen);
  93. // these will be added by the PerObjectInterface munger that affects the type - pr144602
  94. // generatePerObjectGetSetMethods(gen);
  95. } else if (kind == PerClause.PERCFLOW) {
  96. generatePerCflowAspectOfMethod(gen);
  97. generatePerCflowHasAspectMethod(gen);
  98. generatePerCflowPushMethod(gen);
  99. generatePerCflowAjcClinitMethod(gen);
  100. } else if (kind == PerClause.PERTYPEWITHIN) {
  101. generatePerTWAspectOfMethod(gen);
  102. generatePerTWHasAspectMethod(gen);
  103. generatePerTWGetInstanceMethod(gen);
  104. generatePerTWCreateAspectInstanceMethod(gen);
  105. generatePerTWGetWithinTypeNameMethod(gen);
  106. } else {
  107. throw new Error("should not happen - not such kind " + kind.getName());
  108. }
  109. return true;
  110. }
  111. public ResolvedMember getMatchingSyntheticMember(Member member) {
  112. return null;
  113. }
  114. public ResolvedMember getSignature() {
  115. return null;
  116. }
  117. public boolean matches(ResolvedType onType) {
  118. // cannot always do the right thing because may need to eagerly generate ajcMightHaveAspect interface for LTW (says Alex)
  119. if (hasGeneratedInner) { // pr237419 - not always going to generate the marker interface
  120. return aspectType.equals(onType);
  121. } else {
  122. return true;
  123. }
  124. }
  125. private boolean hasPerClauseMembersAlready(LazyClassGen classGen) {
  126. List<LazyMethodGen> methodGens = classGen.getMethodGens();
  127. for (LazyMethodGen method: methodGens) {
  128. if ("aspectOf".equals(method.getName())) {
  129. if ("()".equals(method.getParameterSignature()) && (kind == PerClause.SINGLETON || kind == PerClause.PERCFLOW)) {
  130. return true;
  131. } else if ("(Ljava/lang/Object;)".equals(method.getParameterSignature()) && kind == PerClause.PEROBJECT) {
  132. return true;
  133. } else if ("(Ljava/lang/Class;)".equals(method.getParameterSignature()) && kind == PerClause.PERTYPEWITHIN) {
  134. return true;
  135. }
  136. }
  137. }
  138. return false;
  139. }
  140. private void generatePerClauseMembers(LazyClassGen classGen) {
  141. // FIXME Alex handle when field already there - or handle it with / similar to isAnnotationDefinedAspect()
  142. // for that use aspectType and iterate on the fields.
  143. // FIXME Alex percflowX is not using this one but AJ code style does generate it so..
  144. ResolvedMember failureFieldInfo = AjcMemberMaker.initFailureCauseField(aspectType);
  145. if (kind == PerClause.SINGLETON) {
  146. classGen.addField(makeFieldGen(classGen, failureFieldInfo), null);
  147. }
  148. if (kind == PerClause.SINGLETON) {
  149. ResolvedMember perSingletonFieldInfo = AjcMemberMaker.perSingletonField(aspectType);
  150. classGen.addField(makeFieldGen(classGen, perSingletonFieldInfo), null);
  151. // pr144602 - don't need to do this, PerObjectInterface munger will do it
  152. // } else if (kind == PerClause.PEROBJECT) {
  153. // ResolvedMember perObjectFieldInfo = AjcMemberMaker.perObjectField(aspectType, aspectType);
  154. // classGen.addField(makeFieldGen(classGen, perObjectFieldInfo).(), null);
  155. // // if lazy generation of the inner interface MayHaveAspect works on LTW (see previous note)
  156. // // it should be done here.
  157. } else if (kind == PerClause.PERCFLOW) {
  158. ResolvedMember perCflowFieldInfo = AjcMemberMaker.perCflowField(aspectType);
  159. classGen.addField(makeFieldGen(classGen, perCflowFieldInfo), null);
  160. } else if (kind == PerClause.PERTYPEWITHIN) {
  161. ResolvedMember perTypeWithinForField = AjcMemberMaker.perTypeWithinWithinTypeField(aspectType, aspectType);
  162. classGen.addField(makeFieldGen(classGen, perTypeWithinForField), null);
  163. }
  164. }
  165. private void generatePerSingletonAspectOfMethod(LazyClassGen classGen) {
  166. InstructionFactory factory = classGen.getFactory();
  167. LazyMethodGen method = makeMethodGen(classGen, AjcMemberMaker.perSingletonAspectOfMethod(aspectType));
  168. flagAsSynthetic(method, false);
  169. classGen.addMethodGen(method);
  170. InstructionList il = method.getBody();
  171. il.append(Utility.createGet(factory, AjcMemberMaker.perSingletonField(aspectType)));
  172. InstructionBranch ifNotNull = InstructionFactory.createBranchInstruction(Constants.IFNONNULL, null);
  173. il.append(ifNotNull);
  174. il.append(factory.createNew(AjcMemberMaker.NO_ASPECT_BOUND_EXCEPTION.getName()));
  175. il.append(InstructionConstants.DUP);
  176. il.append(InstructionFactory.PUSH(classGen.getConstantPool(), aspectType.getName()));
  177. il.append(Utility.createGet(factory, AjcMemberMaker.initFailureCauseField(aspectType)));
  178. il.append(factory.createInvoke(AjcMemberMaker.NO_ASPECT_BOUND_EXCEPTION.getName(), "<init>", Type.VOID, new Type[] {
  179. Type.STRING, new ObjectType("java.lang.Throwable") }, Constants.INVOKESPECIAL));
  180. il.append(InstructionConstants.ATHROW);
  181. InstructionHandle ifElse = il.append(Utility.createGet(factory, AjcMemberMaker.perSingletonField(aspectType)));
  182. il.append(InstructionFactory.createReturn(Type.OBJECT));
  183. ifNotNull.setTarget(ifElse);
  184. }
  185. private void generatePerSingletonHasAspectMethod(LazyClassGen classGen) {
  186. InstructionFactory factory = classGen.getFactory();
  187. LazyMethodGen method = makeMethodGen(classGen, AjcMemberMaker.perSingletonHasAspectMethod(aspectType));
  188. flagAsSynthetic(method, false);
  189. classGen.addMethodGen(method);
  190. InstructionList il = method.getBody();
  191. il.append(Utility.createGet(factory, AjcMemberMaker.perSingletonField(aspectType)));
  192. InstructionBranch ifNull = InstructionFactory.createBranchInstruction(Constants.IFNULL, null);
  193. il.append(ifNull);
  194. il.append(InstructionFactory.PUSH(classGen.getConstantPool(), true));
  195. il.append(InstructionFactory.createReturn(Type.INT));
  196. InstructionHandle ifElse = il.append(InstructionFactory.PUSH(classGen.getConstantPool(), false));
  197. il.append(InstructionFactory.createReturn(Type.INT));
  198. ifNull.setTarget(ifElse);
  199. }
  200. private void generatePerSingletonAjcClinitMethod(LazyClassGen classGen) {
  201. InstructionFactory factory = classGen.getFactory();
  202. LazyMethodGen method = makeMethodGen(classGen, AjcMemberMaker.ajcPostClinitMethod(aspectType));
  203. flagAsSynthetic(method, true);
  204. classGen.addMethodGen(method);
  205. InstructionList il = method.getBody();
  206. il.append(factory.createNew(aspectType.getName()));
  207. il.append(InstructionConstants.DUP);
  208. il.append(factory.createInvoke(aspectType.getName(), "<init>", Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));
  209. il.append(Utility.createSet(factory, AjcMemberMaker.perSingletonField(aspectType)));
  210. il.append(InstructionFactory.createReturn(Type.VOID));
  211. // patch <clinit> to delegate to ajc$postClinit at the end
  212. LazyMethodGen clinit = classGen.getStaticInitializer();
  213. il = new InstructionList();
  214. InstructionHandle tryStart = il.append(factory.createInvoke(aspectType.getName(), NameMangler.AJC_POST_CLINIT_NAME,
  215. Type.VOID, Type.NO_ARGS, Constants.INVOKESTATIC));
  216. InstructionBranch tryEnd = InstructionFactory.createBranchInstruction(Constants.GOTO, null);
  217. il.append(tryEnd);
  218. InstructionHandle handler = il.append(InstructionConstants.ASTORE_0);
  219. il.append(InstructionConstants.ALOAD_0);
  220. il.append(Utility.createSet(factory, AjcMemberMaker.initFailureCauseField(aspectType)));
  221. il.append(InstructionFactory.createReturn(Type.VOID));
  222. tryEnd.setTarget(il.getEnd());
  223. // replace the original "return" with a "nop"
  224. // TODO AV - a bit odd, looks like Bcel alters bytecode and has a IMPDEP1 in its representation
  225. if (clinit.getBody().getEnd().getInstruction().opcode == Constants.IMPDEP1) {
  226. clinit.getBody().getEnd().getPrev().setInstruction(InstructionConstants.NOP);
  227. }
  228. clinit.getBody().getEnd().setInstruction(InstructionConstants.NOP);
  229. clinit.getBody().append(il);
  230. clinit.addExceptionHandler(tryStart, handler.getPrev(), handler, new ObjectType("java.lang.Throwable"), false);
  231. }
  232. private void generatePerObjectAspectOfMethod(LazyClassGen classGen) {
  233. InstructionFactory factory = classGen.getFactory();
  234. ReferenceType interfaceType = (ReferenceType) BcelWorld.makeBcelType(AjcMemberMaker.perObjectInterfaceType(aspectType));
  235. LazyMethodGen method = makeMethodGen(classGen, AjcMemberMaker.perObjectAspectOfMethod(aspectType));
  236. flagAsSynthetic(method, false);
  237. classGen.addMethodGen(method);
  238. InstructionList il = method.getBody();
  239. il.append(InstructionConstants.ALOAD_0);
  240. il.append(factory.createInstanceOf(interfaceType));
  241. InstructionBranch ifEq = InstructionFactory.createBranchInstruction(Constants.IFEQ, null);
  242. il.append(ifEq);
  243. il.append(InstructionConstants.ALOAD_0);
  244. il.append(factory.createCheckCast(interfaceType));
  245. il.append(Utility.createInvoke(factory, Constants.INVOKEINTERFACE, AjcMemberMaker.perObjectInterfaceGet(aspectType)));
  246. il.append(InstructionConstants.DUP);
  247. InstructionBranch ifNull = InstructionFactory.createBranchInstruction(Constants.IFNULL, null);
  248. il.append(ifNull);
  249. il.append(InstructionFactory.createReturn(BcelWorld.makeBcelType(aspectType)));
  250. InstructionHandle ifNullElse = il.append(InstructionConstants.POP);
  251. ifNull.setTarget(ifNullElse);
  252. InstructionHandle ifEqElse = il.append(factory.createNew(AjcMemberMaker.NO_ASPECT_BOUND_EXCEPTION.getName()));
  253. ifEq.setTarget(ifEqElse);
  254. il.append(InstructionConstants.DUP);
  255. il.append(factory.createInvoke(AjcMemberMaker.NO_ASPECT_BOUND_EXCEPTION.getName(), "<init>", Type.VOID, Type.NO_ARGS,
  256. Constants.INVOKESPECIAL));
  257. il.append(InstructionConstants.ATHROW);
  258. }
  259. private void generatePerObjectHasAspectMethod(LazyClassGen classGen) {
  260. InstructionFactory factory = classGen.getFactory();
  261. ReferenceType interfaceType = (ReferenceType) BcelWorld.makeBcelType(AjcMemberMaker.perObjectInterfaceType(aspectType));
  262. LazyMethodGen method = makeMethodGen(classGen, AjcMemberMaker.perObjectHasAspectMethod(aspectType));
  263. flagAsSynthetic(method, false);
  264. classGen.addMethodGen(method);
  265. InstructionList il = method.getBody();
  266. il.append(InstructionConstants.ALOAD_0);
  267. il.append(factory.createInstanceOf(interfaceType));
  268. InstructionBranch ifEq = InstructionFactory.createBranchInstruction(Constants.IFEQ, null);
  269. il.append(ifEq);
  270. il.append(InstructionConstants.ALOAD_0);
  271. il.append(factory.createCheckCast(interfaceType));
  272. il.append(Utility.createInvoke(factory, Constants.INVOKEINTERFACE, AjcMemberMaker.perObjectInterfaceGet(aspectType)));
  273. InstructionBranch ifNull = InstructionFactory.createBranchInstruction(Constants.IFNULL, null);
  274. il.append(ifNull);
  275. il.append(InstructionConstants.ICONST_1);
  276. il.append(InstructionFactory.createReturn(Type.INT));
  277. InstructionHandle ifEqElse = il.append(InstructionConstants.ICONST_0);
  278. ifEq.setTarget(ifEqElse);
  279. ifNull.setTarget(ifEqElse);
  280. il.append(InstructionFactory.createReturn(Type.INT));
  281. }
  282. private void generatePerObjectBindMethod(LazyClassGen classGen) {
  283. InstructionFactory factory = classGen.getFactory();
  284. ReferenceType interfaceType = (ReferenceType) BcelWorld.makeBcelType(AjcMemberMaker.perObjectInterfaceType(aspectType));
  285. LazyMethodGen method = makeMethodGen(classGen, AjcMemberMaker.perObjectBind(aspectType));
  286. flagAsSynthetic(method, true);
  287. classGen.addMethodGen(method);
  288. InstructionList il = method.getBody();
  289. il.append(InstructionConstants.ALOAD_0);
  290. il.append(factory.createInstanceOf(interfaceType));
  291. InstructionBranch ifEq = InstructionFactory.createBranchInstruction(Constants.IFEQ, null);
  292. il.append(ifEq);
  293. il.append(InstructionConstants.ALOAD_0);
  294. il.append(factory.createCheckCast(interfaceType));
  295. il.append(Utility.createInvoke(factory, Constants.INVOKEINTERFACE, AjcMemberMaker.perObjectInterfaceGet(aspectType)));
  296. InstructionBranch ifNonNull = InstructionFactory.createBranchInstruction(Constants.IFNONNULL, null);
  297. il.append(ifNonNull);
  298. il.append(InstructionConstants.ALOAD_0);
  299. il.append(factory.createCheckCast(interfaceType));
  300. il.append(factory.createNew(aspectType.getName()));
  301. il.append(InstructionConstants.DUP);
  302. il.append(factory.createInvoke(aspectType.getName(), "<init>", Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));
  303. il.append(Utility.createInvoke(factory, Constants.INVOKEINTERFACE, AjcMemberMaker.perObjectInterfaceSet(aspectType)));
  304. InstructionHandle end = il.append(InstructionFactory.createReturn(Type.VOID));
  305. ifEq.setTarget(end);
  306. ifNonNull.setTarget(end);
  307. }
  308. // private void generatePerObjectGetSetMethods(LazyClassGen classGen) {
  309. // InstructionFactory factory = classGen.getFactory();
  310. //
  311. // LazyMethodGen methodGet = makeMethodGen(classGen, AjcMemberMaker.perObjectInterfaceGet(aspectType));
  312. // flagAsSynthetic(methodGet, true);
  313. // classGen.addMethodGen(methodGet);
  314. // InstructionList ilGet = methodGet.getBody();
  315. // ilGet = new InstructionList();
  316. // ilGet.append(InstructionConstants.ALOAD_0);
  317. // ilGet.append(Utility.createGet(factory, AjcMemberMaker.perObjectField(aspectType, aspectType)));
  318. // ilGet.append(InstructionFactory.createReturn(Type.OBJECT));
  319. //
  320. // LazyMethodGen methodSet = makeMethodGen(classGen, AjcMemberMaker.perObjectInterfaceSet(aspectType));
  321. // flagAsSynthetic(methodSet, true);
  322. // classGen.addMethodGen(methodSet);
  323. // InstructionList ilSet = methodSet.getBody();
  324. // ilSet = new InstructionList();
  325. // ilSet.append(InstructionConstants.ALOAD_0);
  326. // ilSet.append(InstructionConstants.ALOAD_1);
  327. // ilSet.append(Utility.createSet(factory, AjcMemberMaker.perObjectField(aspectType, aspectType)));
  328. // ilSet.append(InstructionFactory.createReturn(Type.VOID));
  329. // }
  330. private void generatePerCflowAspectOfMethod(LazyClassGen classGen) {
  331. InstructionFactory factory = classGen.getFactory();
  332. LazyMethodGen method = makeMethodGen(classGen, AjcMemberMaker.perCflowAspectOfMethod(aspectType));
  333. flagAsSynthetic(method, false);
  334. classGen.addMethodGen(method);
  335. InstructionList il = method.getBody();
  336. il.append(Utility.createGet(factory, AjcMemberMaker.perCflowField(aspectType)));
  337. il.append(Utility.createInvoke(factory, Constants.INVOKEVIRTUAL, AjcMemberMaker.cflowStackPeekInstance()));
  338. il.append(factory.createCheckCast((ReferenceType) BcelWorld.makeBcelType(aspectType)));
  339. il.append(InstructionFactory.createReturn(Type.OBJECT));
  340. }
  341. private void generatePerCflowHasAspectMethod(LazyClassGen classGen) {
  342. InstructionFactory factory = classGen.getFactory();
  343. LazyMethodGen method = makeMethodGen(classGen, AjcMemberMaker.perCflowHasAspectMethod(aspectType));
  344. flagAsSynthetic(method, false);
  345. classGen.addMethodGen(method);
  346. InstructionList il = method.getBody();
  347. il.append(Utility.createGet(factory, AjcMemberMaker.perCflowField(aspectType)));
  348. il.append(Utility.createInvoke(factory, Constants.INVOKEVIRTUAL, AjcMemberMaker.cflowStackIsValid()));
  349. il.append(InstructionFactory.createReturn(Type.INT));
  350. }
  351. private void generatePerCflowPushMethod(LazyClassGen classGen) {
  352. InstructionFactory factory = classGen.getFactory();
  353. LazyMethodGen method = makeMethodGen(classGen, AjcMemberMaker.perCflowPush(aspectType));
  354. flagAsSynthetic(method, true);
  355. classGen.addMethodGen(method);
  356. InstructionList il = method.getBody();
  357. il.append(Utility.createGet(factory, AjcMemberMaker.perCflowField(aspectType)));
  358. il.append(factory.createNew(aspectType.getName()));
  359. il.append(InstructionConstants.DUP);
  360. il.append(factory.createInvoke(aspectType.getName(), "<init>", Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));
  361. il.append(Utility.createInvoke(factory, Constants.INVOKEVIRTUAL, AjcMemberMaker.cflowStackPushInstance()));
  362. il.append(InstructionFactory.createReturn(Type.VOID));
  363. }
  364. private void generatePerCflowAjcClinitMethod(LazyClassGen classGen) {
  365. InstructionFactory factory = classGen.getFactory();
  366. LazyMethodGen method = classGen.getAjcPreClinit(); // Creates a clinit if there isn't one
  367. InstructionList il = new InstructionList();
  368. il.append(factory.createNew(AjcMemberMaker.CFLOW_STACK_TYPE.getName()));
  369. il.append(InstructionConstants.DUP);
  370. il.append(factory.createInvoke(AjcMemberMaker.CFLOW_STACK_TYPE.getName(), "<init>", Type.VOID, Type.NO_ARGS,
  371. Constants.INVOKESPECIAL));
  372. il.append(Utility.createSet(factory, AjcMemberMaker.perCflowField(aspectType)));
  373. method.getBody().insert(il);
  374. }
  375. private void generatePerTWAspectOfMethod(LazyClassGen classGen) {
  376. InstructionFactory factory = classGen.getFactory();
  377. LazyMethodGen method = makeMethodGen(classGen, AjcMemberMaker.perTypeWithinAspectOfMethod(aspectType, classGen.getWorld()
  378. .isInJava5Mode()));
  379. flagAsSynthetic(method, false);
  380. classGen.addMethodGen(method);
  381. InstructionList il = method.getBody();
  382. InstructionHandle tryStart = il.append(InstructionConstants.ALOAD_0);
  383. il.append(Utility.createInvoke(factory, Constants.INVOKESTATIC, AjcMemberMaker.perTypeWithinGetInstance(aspectType)));
  384. il.append(InstructionConstants.ASTORE_1);
  385. il.append(InstructionConstants.ALOAD_1);
  386. InstructionBranch ifNonNull = InstructionFactory.createBranchInstruction(Constants.IFNONNULL, null);
  387. il.append(ifNonNull);
  388. il.append(factory.createNew(AjcMemberMaker.NO_ASPECT_BOUND_EXCEPTION.getName()));
  389. il.append(InstructionConstants.DUP);
  390. il.append(InstructionFactory.PUSH(classGen.getConstantPool(), aspectType.getName()));
  391. il.append(InstructionConstants.ACONST_NULL);
  392. il.append(factory.createInvoke(AjcMemberMaker.NO_ASPECT_BOUND_EXCEPTION.getName(), "<init>", Type.VOID, new Type[] {
  393. Type.STRING, new ObjectType("java.lang.Throwable") }, Constants.INVOKESPECIAL));
  394. il.append(InstructionConstants.ATHROW);
  395. InstructionHandle ifElse = il.append(InstructionConstants.ALOAD_1);
  396. ifNonNull.setTarget(ifElse);
  397. il.append(InstructionFactory.createReturn(Type.OBJECT));
  398. InstructionHandle handler = il.append(InstructionConstants.ASTORE_1);
  399. il.append(factory.createNew(AjcMemberMaker.NO_ASPECT_BOUND_EXCEPTION.getName()));
  400. il.append(InstructionConstants.DUP);
  401. il.append(factory.createInvoke(AjcMemberMaker.NO_ASPECT_BOUND_EXCEPTION.getName(), "<init>", Type.VOID, Type.NO_ARGS,
  402. Constants.INVOKESPECIAL));
  403. il.append(InstructionConstants.ATHROW);
  404. method.addExceptionHandler(tryStart, handler.getPrev(), handler, new ObjectType("java.lang.Exception"), false);
  405. }
  406. // Create 'public String getWithinTypeName() { return ajc$withinType;}'
  407. private void generatePerTWGetWithinTypeNameMethod(LazyClassGen classGen) {
  408. InstructionFactory factory = classGen.getFactory();
  409. LazyMethodGen method = makeMethodGen(classGen, AjcMemberMaker.perTypeWithinGetWithinTypeNameMethod(aspectType, classGen
  410. .getWorld().isInJava5Mode()));
  411. flagAsSynthetic(method, false);
  412. classGen.addMethodGen(method);
  413. // 0: aload_0
  414. // 1: getfield #14; //Field ajc$withinType:Ljava/lang/String;
  415. // 4: areturn
  416. InstructionList il = method.getBody();
  417. il.append(InstructionConstants.ALOAD_0);
  418. il.append(Utility.createGet(factory, AjcMemberMaker.perTypeWithinWithinTypeField(aspectType, aspectType)));
  419. il.append(InstructionConstants.ARETURN);
  420. }
  421. private void generatePerTWHasAspectMethod(LazyClassGen classGen) {
  422. InstructionFactory factory = classGen.getFactory();
  423. LazyMethodGen method = makeMethodGen(classGen, AjcMemberMaker.perTypeWithinHasAspectMethod(aspectType, classGen.getWorld()
  424. .isInJava5Mode()));
  425. flagAsSynthetic(method, false);
  426. classGen.addMethodGen(method);
  427. InstructionList il = method.getBody();
  428. InstructionHandle tryStart = il.append(InstructionConstants.ALOAD_0);
  429. il.append(Utility.createInvoke(factory, Constants.INVOKESTATIC, AjcMemberMaker.perTypeWithinGetInstance(aspectType)));
  430. InstructionBranch ifNull = InstructionFactory.createBranchInstruction(Constants.IFNULL, null);
  431. il.append(ifNull);
  432. il.append(InstructionConstants.ICONST_1);
  433. il.append(InstructionConstants.IRETURN);
  434. InstructionHandle ifElse = il.append(InstructionConstants.ICONST_0);
  435. ifNull.setTarget(ifElse);
  436. il.append(InstructionConstants.IRETURN);
  437. InstructionHandle handler = il.append(InstructionConstants.ASTORE_1);
  438. il.append(InstructionConstants.ICONST_0);
  439. il.append(InstructionConstants.IRETURN);
  440. method.addExceptionHandler(tryStart, handler.getPrev(), handler, new ObjectType("java.lang.Exception"), false);
  441. }
  442. private void generatePerTWGetInstanceMethod(LazyClassGen classGen) {
  443. InstructionFactory factory = classGen.getFactory();
  444. LazyMethodGen method = makeMethodGen(classGen, AjcMemberMaker.perTypeWithinGetInstance(aspectType));
  445. flagAsSynthetic(method, true);
  446. classGen.addMethodGen(method);
  447. InstructionList il = method.getBody();
  448. InstructionHandle tryStart = il.append(InstructionConstants.ALOAD_0);
  449. il.append(InstructionFactory.PUSH(factory.getConstantPool(), NameMangler.perTypeWithinLocalAspectOf(aspectType)));
  450. il.append(InstructionConstants.ACONST_NULL);// Class[] for "getDeclaredMethod"
  451. il.append(factory.createInvoke("java/lang/Class", "getDeclaredMethod", Type.getType("Ljava/lang/reflect/Method;"),
  452. new Type[] { Type.getType("Ljava/lang/String;"), Type.getType("[Ljava/lang/Class;") }, Constants.INVOKEVIRTUAL));
  453. il.append(InstructionConstants.ACONST_NULL);// Object for "invoke", static method
  454. il.append(InstructionConstants.ACONST_NULL);// Object[] for "invoke", no arg
  455. il.append(factory.createInvoke("java/lang/reflect/Method", "invoke", Type.OBJECT, new Type[] {
  456. Type.getType("Ljava/lang/Object;"), Type.getType("[Ljava/lang/Object;") }, Constants.INVOKEVIRTUAL));
  457. il.append(factory.createCheckCast((ReferenceType) BcelWorld.makeBcelType(aspectType)));
  458. il.append(InstructionConstants.ARETURN);
  459. InstructionHandle handler = il.append(InstructionConstants.ASTORE_1);
  460. il.append(InstructionConstants.ACONST_NULL);
  461. il.append(InstructionConstants.ARETURN);
  462. method.addExceptionHandler(tryStart, handler.getPrev(), handler, new ObjectType("java.lang.Exception"), false);
  463. }
  464. private void generatePerTWCreateAspectInstanceMethod(LazyClassGen classGen) {
  465. InstructionFactory factory = classGen.getFactory();
  466. LazyMethodGen method = makeMethodGen(classGen, AjcMemberMaker.perTypeWithinCreateAspectInstance(aspectType));
  467. flagAsSynthetic(method, true);
  468. classGen.addMethodGen(method);
  469. InstructionList il = method.getBody();
  470. il.append(factory.createNew(aspectType.getName()));
  471. il.append(InstructionConstants.DUP);
  472. il.append(factory.createInvoke(aspectType.getName(), "<init>", Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));
  473. il.append(InstructionConstants.ASTORE_1);
  474. il.append(InstructionConstants.ALOAD_1);
  475. il.append(InstructionConstants.ALOAD_0);
  476. il.append(Utility.createSet(factory, AjcMemberMaker.perTypeWithinWithinTypeField(aspectType, aspectType)));
  477. il.append(InstructionConstants.ALOAD_1);
  478. il.append(InstructionConstants.ARETURN);
  479. }
  480. /**
  481. * Add standard Synthetic (if wished) and AjSynthetic (always) attributes
  482. *
  483. * @param methodGen
  484. * @param makeJavaSynthetic true if standard Synthetic attribute must be set as well (invisible to user)
  485. */
  486. private static void flagAsSynthetic(LazyMethodGen methodGen, boolean makeJavaSynthetic) {
  487. if (makeJavaSynthetic) {
  488. methodGen.makeSynthetic();
  489. }
  490. methodGen.addAttribute(Utility
  491. .bcelAttribute(new AjAttribute.AjSynthetic(), methodGen.getEnclosingClass().getConstantPool()));
  492. }
  493. // public boolean isLateTypeMunger() {
  494. // return true;
  495. // }
  496. }