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.

BcelRenderer.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 org.aspectj.apache.bcel.Constants;
  14. import org.aspectj.apache.bcel.generic.InstructionFactory;
  15. import org.aspectj.apache.bcel.generic.InstructionHandle;
  16. import org.aspectj.apache.bcel.generic.InstructionList;
  17. import org.aspectj.apache.bcel.generic.ObjectType;
  18. import org.aspectj.apache.bcel.generic.ReferenceType;
  19. import org.aspectj.apache.bcel.generic.Type;
  20. import org.aspectj.weaver.BCException;
  21. import org.aspectj.weaver.Member;
  22. import org.aspectj.weaver.MemberImpl;
  23. import org.aspectj.weaver.UnresolvedType;
  24. import org.aspectj.weaver.ast.And;
  25. import org.aspectj.weaver.ast.Call;
  26. import org.aspectj.weaver.ast.CallExpr;
  27. import org.aspectj.weaver.ast.Expr;
  28. import org.aspectj.weaver.ast.FieldGet;
  29. import org.aspectj.weaver.ast.FieldGetCall;
  30. import org.aspectj.weaver.ast.HasAnnotation;
  31. import org.aspectj.weaver.ast.IExprVisitor;
  32. import org.aspectj.weaver.ast.ITestVisitor;
  33. import org.aspectj.weaver.ast.Instanceof;
  34. import org.aspectj.weaver.ast.Literal;
  35. import org.aspectj.weaver.ast.Not;
  36. import org.aspectj.weaver.ast.Or;
  37. import org.aspectj.weaver.ast.Test;
  38. import org.aspectj.weaver.ast.Var;
  39. import org.aspectj.weaver.internal.tools.MatchingContextBasedTest;
  40. // we generate right to left, btw.
  41. public final class BcelRenderer implements ITestVisitor, IExprVisitor {
  42. private InstructionList instructions;
  43. private InstructionFactory fact;
  44. private BcelWorld world;
  45. InstructionHandle sk, fk, next = null;
  46. private BcelRenderer(InstructionFactory fact, BcelWorld world) {
  47. super();
  48. this.fact = fact;
  49. this.world = world;
  50. this.instructions = new InstructionList();
  51. }
  52. // ---- renderers
  53. public static InstructionList renderExpr(InstructionFactory fact, BcelWorld world, Expr e) {
  54. BcelRenderer renderer = new BcelRenderer(fact, world);
  55. e.accept(renderer);
  56. return renderer.instructions;
  57. }
  58. public static InstructionList renderExpr(InstructionFactory fact, BcelWorld world, Expr e, Type desiredType) {
  59. BcelRenderer renderer = new BcelRenderer(fact, world);
  60. e.accept(renderer);
  61. InstructionList il = renderer.instructions;
  62. il.append(Utility.createConversion(fact, BcelWorld.makeBcelType(e.getType()), desiredType));
  63. return il;
  64. }
  65. public static InstructionList renderExprs(InstructionFactory fact, BcelWorld world, Expr[] es) {
  66. BcelRenderer renderer = new BcelRenderer(fact, world);
  67. for (int i = es.length - 1; i >= 0; i--) {
  68. es[i].accept(renderer);
  69. }
  70. return renderer.instructions;
  71. }
  72. /*
  73. * Get the instructions representing this test.
  74. *
  75. * @param e test to render
  76. *
  77. * @param sk instructionHandle to jump to if our rendered check succeeds (typically start of advice)
  78. *
  79. * @param fk instructionHandle to jump to if our rendered check fails (typically after end of advice)
  80. *
  81. * @param next instructionHandle that will follow this generated code. Passing in null will generate one unnecessary GOTO
  82. * instruction.
  83. *
  84. * @returns the instruction list representing this expression
  85. */
  86. public static InstructionList renderTest(InstructionFactory fact, BcelWorld world, Test e, InstructionHandle sk,
  87. InstructionHandle fk, InstructionHandle next) {
  88. BcelRenderer renderer = new BcelRenderer(fact, world);
  89. renderer.recur(e, sk, fk, next);
  90. return renderer.instructions;
  91. }
  92. // ---- recurrers
  93. private void recur(Test e, InstructionHandle sk, InstructionHandle fk, InstructionHandle next) {
  94. this.sk = sk;
  95. this.fk = fk;
  96. this.next = next;
  97. e.accept(this);
  98. }
  99. // ---- test visitors
  100. public void visit(And e) {
  101. InstructionHandle savedFk = fk;
  102. recur(e.getRight(), sk, fk, next);
  103. InstructionHandle ning = instructions.getStart();
  104. recur(e.getLeft(), ning, savedFk, ning);
  105. }
  106. public void visit(Or e) {
  107. InstructionHandle savedSk = sk;
  108. recur(e.getRight(), sk, fk, next);
  109. recur(e.getLeft(), savedSk, instructions.getStart(), instructions.getStart());
  110. }
  111. public void visit(Not e) {
  112. recur(e.getBody(), fk, sk, next);
  113. }
  114. public void visit(Instanceof i) {
  115. instructions.insert(createJumpBasedOnBooleanOnStack());
  116. instructions.insert(Utility.createInstanceof(fact, (ReferenceType) BcelWorld.makeBcelType(i.getType())));
  117. i.getVar().accept(this);
  118. }
  119. public void visit(HasAnnotation hasAnnotation) {
  120. // in Java:
  121. // foo.class.isAnnotationPresent(annotationClass);
  122. // in bytecode:
  123. // ifnull? skip to the end if it is as getClass() will fail (see pr 257833)
  124. // load var onto the stack (done for us later)
  125. // invokevirtual java/lang/Object.getClass:()Ljava/lang/Class
  126. // ldc_w annotationClass
  127. // invokevirtual java/lang/Class.isAnnotationPresent:(Ljava/lang/Class;)Z
  128. InstructionList il = new InstructionList();
  129. // If it is null jump past the advice call
  130. il.append(InstructionFactory.createBranchInstruction(Constants.IFNULL, fk));
  131. // Load up the var again
  132. il.append(((BcelVar) hasAnnotation.getVar()).createLoad(fact));
  133. Member getClass = MemberImpl.method(UnresolvedType.OBJECT, 0, UnresolvedType.JL_CLASS, "getClass", UnresolvedType.NONE);
  134. il.append(Utility.createInvoke(fact, world, getClass));
  135. // aload annotationClass
  136. il.append(fact.createConstant(new ObjectType(hasAnnotation.getAnnotationType().getName())));
  137. // int annClassIndex = fact.getConstantPool().addClass(hasAnnotation.getAnnotationType().getSignature());
  138. // il.append(new LDC_W(annClassIndex));
  139. Member isAnnotationPresent = MemberImpl.method(UnresolvedType.JL_CLASS, 0, UnresolvedType.BOOLEAN, "isAnnotationPresent",
  140. new UnresolvedType[] { UnresolvedType.JL_CLASS });
  141. il.append(Utility.createInvoke(fact, world, isAnnotationPresent));
  142. il.append(createJumpBasedOnBooleanOnStack());
  143. instructions.insert(il);
  144. hasAnnotation.getVar().accept(this);
  145. }
  146. /*
  147. * (non-Javadoc)
  148. *
  149. * @see org.aspectj.weaver.ast.ITestVisitor#visit(org.aspectj.weaver.internal.tools.MatchingContextBasedTest)
  150. */
  151. public void visit(MatchingContextBasedTest matchingContextTest) {
  152. throw new UnsupportedOperationException("matching context extension not supported in bytecode weaving");
  153. }
  154. private InstructionList createJumpBasedOnBooleanOnStack() {
  155. InstructionList il = new InstructionList();
  156. if (sk == fk) {
  157. // don't bother generating if it doesn't matter
  158. if (sk != next) {
  159. il.insert(InstructionFactory.createBranchInstruction(Constants.GOTO, sk));
  160. }
  161. return il;
  162. }
  163. if (fk == next) {
  164. il.insert(InstructionFactory.createBranchInstruction(Constants.IFNE, sk));
  165. } else if (sk == next) {
  166. il.insert(InstructionFactory.createBranchInstruction(Constants.IFEQ, fk));
  167. } else {
  168. il.insert(InstructionFactory.createBranchInstruction(Constants.GOTO, sk));
  169. il.insert(InstructionFactory.createBranchInstruction(Constants.IFEQ, fk));
  170. }
  171. return il;
  172. }
  173. public void visit(Literal literal) {
  174. if (literal == Literal.FALSE) {
  175. throw new BCException("visiting a false expression");
  176. }
  177. }
  178. public void visit(Call call) {
  179. Member method = call.getMethod();
  180. // assert method.isStatic()
  181. Expr[] args = call.getArgs();
  182. InstructionList callIl = new InstructionList();
  183. for (int i = 0, len = args.length; i < len; i++) {
  184. // XXX only correct for static method calls
  185. Type desiredType = BcelWorld.makeBcelType(method.getParameterTypes()[i]);
  186. Expr arg = args[i];
  187. // if arg is null it is because we couldn't bind it properly, for example see 162135
  188. if (arg == null) {
  189. InstructionList iList = new InstructionList();
  190. iList.append(InstructionFactory.createNull(desiredType));
  191. callIl.append(iList);
  192. } else {
  193. callIl.append(renderExpr(fact, world, arg, desiredType));
  194. }
  195. }
  196. // System.out.println("rendered args: " + callIl);
  197. callIl.append(Utility.createInvoke(fact, world, method));
  198. callIl.append(createJumpBasedOnBooleanOnStack());
  199. instructions.insert(callIl);
  200. }
  201. public void visit(FieldGetCall fieldGetCall) {
  202. Member field = fieldGetCall.getField();
  203. Member method = fieldGetCall.getMethod();
  204. InstructionList il = new InstructionList();
  205. il.append(Utility.createGet(fact, field));
  206. // assert !method.isStatic()
  207. Expr[] args = fieldGetCall.getArgs();
  208. // System.out.println("args: " + Arrays.asList(args));
  209. il.append(renderExprs(fact, world, args));
  210. // System.out.println("rendered args: " + callIl);
  211. il.append(Utility.createInvoke(fact, world, method));
  212. il.append(createJumpBasedOnBooleanOnStack());
  213. instructions.insert(il);
  214. }
  215. // ---- expr visitors
  216. public void visit(Var var) {
  217. BcelVar bvar = (BcelVar) var;
  218. bvar.insertLoad(instructions, fact);
  219. }
  220. public void visit(FieldGet fieldGet) {
  221. Member field = fieldGet.getField();
  222. // assert field.isStatic()
  223. instructions.insert(Utility.createGet(fact, field));
  224. }
  225. public void visit(CallExpr call) {
  226. Member method = call.getMethod();
  227. // assert method.isStatic()
  228. Expr[] args = call.getArgs();
  229. InstructionList callIl = renderExprs(fact, world, args);
  230. callIl.append(Utility.createInvoke(fact, world, method));
  231. instructions.insert(callIl);
  232. }
  233. }