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.

BcelAccessForInlineMunger.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. * Alexandre Vasseur initial implementation
  11. *******************************************************************************/
  12. package org.aspectj.weaver.bcel;
  13. import java.lang.reflect.Modifier;
  14. import java.util.ArrayList;
  15. import java.util.HashMap;
  16. import java.util.HashSet;
  17. import java.util.List;
  18. import java.util.Map;
  19. import java.util.Set;
  20. import org.aspectj.apache.bcel.Constants;
  21. import org.aspectj.apache.bcel.classfile.ConstantPool;
  22. import org.aspectj.apache.bcel.generic.FieldInstruction;
  23. import org.aspectj.apache.bcel.generic.Instruction;
  24. import org.aspectj.apache.bcel.generic.InstructionConstants;
  25. import org.aspectj.apache.bcel.generic.InstructionFactory;
  26. import org.aspectj.apache.bcel.generic.InstructionHandle;
  27. import org.aspectj.apache.bcel.generic.InstructionList;
  28. import org.aspectj.apache.bcel.generic.InvokeInstruction;
  29. import org.aspectj.apache.bcel.generic.Type;
  30. import org.aspectj.weaver.AjAttribute;
  31. import org.aspectj.weaver.AjcMemberMaker;
  32. import org.aspectj.weaver.Member;
  33. import org.aspectj.weaver.NameMangler;
  34. import org.aspectj.weaver.ResolvedMember;
  35. import org.aspectj.weaver.ResolvedType;
  36. import org.aspectj.weaver.Shadow;
  37. import org.aspectj.weaver.UnresolvedType;
  38. /**
  39. * Looks for all access to method or field that are not public within the body of the around advices and replace the invocations to
  40. * a wrapper call so that the around advice can further be inlined.
  41. * <p/>
  42. * This munger is used for @AJ aspects for which inlining wrapper is not done at compile time.
  43. * <p/>
  44. * Specific state and logic is kept in the munger ala ITD so that call/get/set pointcuts can still be matched on the wrapped member
  45. * thanks to the EffectiveSignature attribute.
  46. *
  47. * @author Alexandre Vasseur
  48. * @author Andy Clement
  49. */
  50. public class BcelAccessForInlineMunger extends BcelTypeMunger {
  51. private Map<String, ResolvedMember> inlineAccessors;
  52. private LazyClassGen aspectGen;
  53. /**
  54. * The wrapper methods representing any created inlineAccessors
  55. */
  56. private Set<LazyMethodGen> inlineAccessorMethodGens;
  57. public BcelAccessForInlineMunger(ResolvedType aspectType) {
  58. super(null, aspectType);
  59. if (aspectType.getWorld().isXnoInline()) {
  60. throw new Error("This should not happen");
  61. }
  62. }
  63. @Override
  64. public boolean munge(BcelClassWeaver weaver) {
  65. aspectGen = weaver.getLazyClassGen();
  66. inlineAccessors = new HashMap<String, ResolvedMember>(0);
  67. inlineAccessorMethodGens = new HashSet<LazyMethodGen>();
  68. // look for all @Around advices
  69. for (LazyMethodGen methodGen : aspectGen.getMethodGens()) {
  70. if (methodGen.hasAnnotation(UnresolvedType.forName("org/aspectj/lang/annotation/Around"))) {
  71. openAroundAdvice(methodGen);
  72. }
  73. }
  74. // add the accessors
  75. for (LazyMethodGen lazyMethodGen : inlineAccessorMethodGens) {
  76. aspectGen.addMethodGen(lazyMethodGen);
  77. }
  78. // flush some
  79. inlineAccessorMethodGens = null;
  80. // we keep m_inlineAccessorsResolvedMembers for shadow matching
  81. return true;
  82. }
  83. /**
  84. * Looks in the wrapper we have added so that we can find their effective signature if needed
  85. */
  86. @Override
  87. public ResolvedMember getMatchingSyntheticMember(Member member) {
  88. ResolvedMember rm = inlineAccessors.get(member.getName());// + member.getSignature());
  89. // System.err.println("lookup for " + member.getName() + ":" + member.getSignature() + " = "
  90. // + (rm == null ? "" : rm.getName()));
  91. return rm;
  92. }
  93. @Override
  94. public ResolvedMember getSignature() {
  95. return null;
  96. }
  97. /**
  98. * Match only the aspect for which we act
  99. */
  100. @Override
  101. public boolean matches(ResolvedType onType) {
  102. return aspectType.equals(onType);
  103. }
  104. /**
  105. * Prepare the around advice, flag it as cannot be inlined if it can't be
  106. */
  107. private void openAroundAdvice(LazyMethodGen aroundAdvice) {
  108. InstructionHandle curr = aroundAdvice.getBody().getStart();
  109. InstructionHandle end = aroundAdvice.getBody().getEnd();
  110. ConstantPool cpg = aroundAdvice.getEnclosingClass().getConstantPool();
  111. InstructionFactory factory = aroundAdvice.getEnclosingClass().getFactory();
  112. boolean realizedCannotInline = false;
  113. while (curr != end) {
  114. if (realizedCannotInline) {
  115. // we know we cannot inline this advice so no need for futher handling
  116. break;
  117. }
  118. InstructionHandle next = curr.getNext();
  119. Instruction inst = curr.getInstruction();
  120. // open-up method call
  121. if ((inst instanceof InvokeInstruction)) {
  122. InvokeInstruction invoke = (InvokeInstruction) inst;
  123. ResolvedType callee = aspectGen.getWorld().resolve(UnresolvedType.forName(invoke.getClassName(cpg)));
  124. // look in the whole method list and not just declared for super calls and alike
  125. List<ResolvedMember> methods = callee.getMethodsWithoutIterator(false, true, false);
  126. for (ResolvedMember resolvedMember : methods) {
  127. if (invoke.getName(cpg).equals(resolvedMember.getName())
  128. && invoke.getSignature(cpg).equals(resolvedMember.getSignature()) && !resolvedMember.isPublic()) {
  129. if ("<init>".equals(invoke.getName(cpg))) {
  130. // skipping open up for private constructor
  131. // can occur when aspect new a private inner type
  132. // too complex to handle new + dup + .. + invokespecial here.
  133. aroundAdvice.setCanInline(false);
  134. realizedCannotInline = true;
  135. } else {
  136. // specific handling for super.foo() calls, where foo is non public
  137. ResolvedType memberType = aspectGen.getWorld().resolve(resolvedMember.getDeclaringType());
  138. if (!aspectType.equals(memberType) && memberType.isAssignableFrom(aspectType)) {
  139. // old test was...
  140. // if (aspectType.getSuperclass() != null
  141. // && aspectType.getSuperclass().getName().equals(resolvedMember.getDeclaringType().getName())) {
  142. ResolvedMember accessor = createOrGetInlineAccessorForSuperDispatch(resolvedMember);
  143. InvokeInstruction newInst = factory.createInvoke(aspectType.getName(), accessor.getName(),
  144. BcelWorld.makeBcelType(accessor.getReturnType()),
  145. BcelWorld.makeBcelTypes(accessor.getParameterTypes()), Constants.INVOKEVIRTUAL);
  146. curr.setInstruction(newInst);
  147. } else {
  148. ResolvedMember accessor = createOrGetInlineAccessorForMethod(resolvedMember);
  149. InvokeInstruction newInst = factory.createInvoke(aspectType.getName(), accessor.getName(),
  150. BcelWorld.makeBcelType(accessor.getReturnType()),
  151. BcelWorld.makeBcelTypes(accessor.getParameterTypes()), Constants.INVOKESTATIC);
  152. curr.setInstruction(newInst);
  153. }
  154. }
  155. break;// ok we found a matching callee member and swapped the instruction with the accessor
  156. }
  157. }
  158. } else if (inst instanceof FieldInstruction) {
  159. FieldInstruction invoke = (FieldInstruction) inst;
  160. ResolvedType callee = aspectGen.getWorld().resolve(UnresolvedType.forName(invoke.getClassName(cpg)));
  161. for (int i = 0; i < callee.getDeclaredJavaFields().length; i++) {
  162. ResolvedMember resolvedMember = callee.getDeclaredJavaFields()[i];
  163. if (invoke.getName(cpg).equals(resolvedMember.getName())
  164. && invoke.getSignature(cpg).equals(resolvedMember.getSignature()) && !resolvedMember.isPublic()) {
  165. final ResolvedMember accessor;
  166. if ((inst.opcode == Constants.GETFIELD) || (inst.opcode == Constants.GETSTATIC)) {
  167. accessor = createOrGetInlineAccessorForFieldGet(resolvedMember);
  168. } else {
  169. accessor = createOrGetInlineAccessorForFieldSet(resolvedMember);
  170. }
  171. InvokeInstruction newInst = factory.createInvoke(aspectType.getName(), accessor.getName(),
  172. BcelWorld.makeBcelType(accessor.getReturnType()),
  173. BcelWorld.makeBcelTypes(accessor.getParameterTypes()), Constants.INVOKESTATIC);
  174. curr.setInstruction(newInst);
  175. break;// ok we found a matching callee member and swapped the instruction with the accessor
  176. }
  177. }
  178. }
  179. curr = next;
  180. }
  181. // no reason for not inlining this advice
  182. // since it is used for @AJ advice that cannot be inlined by defauilt
  183. // make sure we set inline to true since we have done this analysis
  184. if (!realizedCannotInline) {
  185. aroundAdvice.setCanInline(true);
  186. }
  187. }
  188. /**
  189. * Find (or add if not yet created) an inline wrapper for a non public method call
  190. */
  191. private ResolvedMember createOrGetInlineAccessorForMethod(ResolvedMember resolvedMember) {
  192. String accessorName = NameMangler.inlineAccessMethodForMethod(resolvedMember.getName(), resolvedMember.getDeclaringType(),
  193. aspectType);
  194. String key = accessorName;// new StringBuilder(accessorName).append(resolvedMember.getSignature()).toString();
  195. ResolvedMember inlineAccessor = inlineAccessors.get(key);
  196. // System.err.println(key + " accessor=" + inlineAccessor);
  197. if (inlineAccessor == null) {
  198. // add static method to aspect
  199. inlineAccessor = AjcMemberMaker.inlineAccessMethodForMethod(aspectType, resolvedMember);
  200. // add new accessor method to aspect bytecode
  201. InstructionFactory factory = aspectGen.getFactory();
  202. LazyMethodGen method = makeMethodGen(aspectGen, inlineAccessor);
  203. method.makeSynthetic();
  204. List<AjAttribute> methodAttributes = new ArrayList<AjAttribute>();
  205. methodAttributes.add(new AjAttribute.AjSynthetic());
  206. methodAttributes.add(new AjAttribute.EffectiveSignatureAttribute(resolvedMember, Shadow.MethodCall, false));
  207. method.addAttribute(Utility.bcelAttribute(methodAttributes.get(0), aspectGen.getConstantPool()));
  208. // flag the effective signature, so that we can deobfuscate the signature to apply method call pointcut
  209. method.addAttribute(Utility.bcelAttribute(methodAttributes.get(1), aspectGen.getConstantPool()));
  210. inlineAccessorMethodGens.add(method);
  211. InstructionList il = method.getBody();
  212. int register = 0;
  213. for (int i = 0, max = inlineAccessor.getParameterTypes().length; i < max; i++) {
  214. UnresolvedType ptype = inlineAccessor.getParameterTypes()[i];
  215. Type type = BcelWorld.makeBcelType(ptype);
  216. il.append(InstructionFactory.createLoad(type, register));
  217. register += type.getSize();
  218. }
  219. il.append(Utility.createInvoke(factory, Modifier.isStatic(resolvedMember.getModifiers()) ? Constants.INVOKESTATIC
  220. : Constants.INVOKEVIRTUAL, resolvedMember));
  221. il.append(InstructionFactory.createReturn(BcelWorld.makeBcelType(inlineAccessor.getReturnType())));
  222. inlineAccessors.put(key, new BcelMethod(aspectGen.getBcelObjectType(), method.getMethod(), methodAttributes));
  223. }
  224. return inlineAccessor;
  225. }
  226. /**
  227. * Add an inline wrapper for a non public super.method call
  228. */
  229. private ResolvedMember createOrGetInlineAccessorForSuperDispatch(ResolvedMember resolvedMember) {
  230. String accessor = NameMangler.superDispatchMethod(aspectType, resolvedMember.getName());
  231. String key = accessor;
  232. ResolvedMember inlineAccessor = inlineAccessors.get(key);
  233. if (inlineAccessor == null) {
  234. // add super accessor method to class:
  235. inlineAccessor = AjcMemberMaker.superAccessMethod(aspectType, resolvedMember);
  236. // add new accessor method to aspect bytecode
  237. InstructionFactory factory = aspectGen.getFactory();
  238. LazyMethodGen method = makeMethodGen(aspectGen, inlineAccessor);
  239. // flag it synthetic, AjSynthetic
  240. method.makeSynthetic();
  241. List<AjAttribute> methodAttributes = new ArrayList<AjAttribute>();
  242. methodAttributes.add(new AjAttribute.AjSynthetic());
  243. methodAttributes.add(new AjAttribute.EffectiveSignatureAttribute(resolvedMember, Shadow.MethodCall, false));
  244. method.addAttribute(Utility.bcelAttribute(methodAttributes.get(0), aspectGen.getConstantPool()));
  245. // flag the effective signature, so that we can deobfuscate the signature to apply method call pointcut
  246. method.addAttribute(Utility.bcelAttribute(methodAttributes.get(1), aspectGen.getConstantPool()));
  247. inlineAccessorMethodGens.add(method);
  248. InstructionList il = method.getBody();
  249. il.append(InstructionConstants.ALOAD_0);
  250. int register = 1;
  251. for (int i = 0; i < inlineAccessor.getParameterTypes().length; i++) {
  252. UnresolvedType typeX = inlineAccessor.getParameterTypes()[i];
  253. Type type = BcelWorld.makeBcelType(typeX);
  254. il.append(InstructionFactory.createLoad(type, register));
  255. register += type.getSize();
  256. }
  257. il.append(Utility.createInvoke(factory, Constants.INVOKESPECIAL, resolvedMember));
  258. il.append(InstructionFactory.createReturn(BcelWorld.makeBcelType(inlineAccessor.getReturnType())));
  259. inlineAccessors.put(key, new BcelMethod(aspectGen.getBcelObjectType(), method.getMethod(), methodAttributes));
  260. }
  261. return inlineAccessor;
  262. }
  263. /**
  264. * Add an inline wrapper for a non public field get
  265. */
  266. private ResolvedMember createOrGetInlineAccessorForFieldGet(ResolvedMember resolvedMember) {
  267. String accessor = NameMangler.inlineAccessMethodForFieldGet(resolvedMember.getName(), resolvedMember.getDeclaringType(),
  268. aspectType);
  269. String key = accessor;
  270. ResolvedMember inlineAccessor = inlineAccessors.get(key);
  271. if (inlineAccessor == null) {
  272. // add static method to aspect
  273. inlineAccessor = AjcMemberMaker.inlineAccessMethodForFieldGet(aspectType, resolvedMember);
  274. // add new accessor method to aspect bytecode
  275. InstructionFactory factory = aspectGen.getFactory();
  276. LazyMethodGen method = makeMethodGen(aspectGen, inlineAccessor);
  277. // flag it synthetic, AjSynthetic
  278. method.makeSynthetic();
  279. List<AjAttribute> methodAttributes = new ArrayList<AjAttribute>();
  280. methodAttributes.add(new AjAttribute.AjSynthetic());
  281. methodAttributes.add(new AjAttribute.EffectiveSignatureAttribute(resolvedMember, Shadow.FieldGet, false));
  282. // flag the effective signature, so that we can deobfuscate the signature to apply method call pointcut
  283. method.addAttribute(Utility.bcelAttribute(methodAttributes.get(0), aspectGen.getConstantPool()));
  284. method.addAttribute(Utility.bcelAttribute(methodAttributes.get(1), aspectGen.getConstantPool()));
  285. inlineAccessorMethodGens.add(method);
  286. InstructionList il = method.getBody();
  287. if (Modifier.isStatic(resolvedMember.getModifiers())) {
  288. // field accessed is static so no "this" as accessor sole parameter
  289. } else {
  290. il.append(InstructionConstants.ALOAD_0);
  291. }
  292. il.append(Utility.createGet(factory, resolvedMember));
  293. il.append(InstructionFactory.createReturn(BcelWorld.makeBcelType(inlineAccessor.getReturnType())));
  294. inlineAccessors.put(key, new BcelMethod(aspectGen.getBcelObjectType(), method.getMethod(), methodAttributes));
  295. }
  296. return inlineAccessor;
  297. }
  298. /**
  299. * Add an inline wrapper for a non public field set
  300. */
  301. private ResolvedMember createOrGetInlineAccessorForFieldSet(ResolvedMember resolvedMember) {
  302. String accessor = NameMangler.inlineAccessMethodForFieldSet(resolvedMember.getName(), resolvedMember.getDeclaringType(),
  303. aspectType);
  304. String key = accessor;
  305. ResolvedMember inlineAccessor = inlineAccessors.get(key);
  306. if (inlineAccessor == null) {
  307. // add static method to aspect
  308. inlineAccessor = AjcMemberMaker.inlineAccessMethodForFieldSet(aspectType, resolvedMember);
  309. // add new accessor method to aspect bytecode
  310. InstructionFactory factory = aspectGen.getFactory();
  311. LazyMethodGen method = makeMethodGen(aspectGen, inlineAccessor);
  312. // flag it synthetic, AjSynthetic
  313. method.makeSynthetic();
  314. List<AjAttribute> methodAttributes = new ArrayList<AjAttribute>();
  315. methodAttributes.add(new AjAttribute.AjSynthetic());
  316. methodAttributes.add(new AjAttribute.EffectiveSignatureAttribute(resolvedMember, Shadow.FieldSet, false));
  317. method.addAttribute(Utility.bcelAttribute(methodAttributes.get(0), aspectGen.getConstantPool()));
  318. // flag the effective signature, so that we can deobfuscate the signature to apply method call pointcut
  319. method.addAttribute(Utility.bcelAttribute(methodAttributes.get(1), aspectGen.getConstantPool()));
  320. inlineAccessorMethodGens.add(method);
  321. InstructionList il = method.getBody();
  322. if (Modifier.isStatic(resolvedMember.getModifiers())) {
  323. // field accessed is static so sole parameter is field value to be set
  324. il.append(InstructionFactory.createLoad(BcelWorld.makeBcelType(resolvedMember.getReturnType()), 0));
  325. } else {
  326. il.append(InstructionConstants.ALOAD_0);
  327. il.append(InstructionFactory.createLoad(BcelWorld.makeBcelType(resolvedMember.getReturnType()), 1));
  328. }
  329. il.append(Utility.createSet(factory, resolvedMember));
  330. il.append(InstructionConstants.RETURN);
  331. inlineAccessors.put(key, new BcelMethod(aspectGen.getBcelObjectType(), method.getMethod(), methodAttributes));
  332. }
  333. return inlineAccessor;
  334. }
  335. }