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.

AccessForInlineVisitor.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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.ajdt.internal.compiler.ast;
  13. //import java.util.Arrays;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. import org.aspectj.ajdt.internal.compiler.lookup.EclipseFactory;
  17. import org.aspectj.ajdt.internal.compiler.lookup.InlineAccessFieldBinding;
  18. import org.aspectj.ajdt.internal.compiler.lookup.InterTypeFieldBinding;
  19. import org.aspectj.ajdt.internal.compiler.lookup.InterTypeMethodBinding;
  20. import org.aspectj.ajdt.internal.compiler.lookup.PrivilegedFieldBinding;
  21. import org.aspectj.ajdt.internal.compiler.lookup.PrivilegedHandler;
  22. import org.aspectj.org.eclipse.jdt.internal.compiler.ASTVisitor;
  23. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.AllocationExpression;
  24. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.AssertStatement;
  25. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess;
  26. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.FieldReference;
  27. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.MessageSend;
  28. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference;
  29. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference;
  30. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.SingleNameReference;
  31. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.SingleTypeReference;
  32. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.ThisReference;
  33. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
  34. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ArrayBinding;
  35. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BlockScope;
  36. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
  37. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
  38. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ParameterizedMethodBinding;
  39. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding;
  40. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ProblemFieldBinding;
  41. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
  42. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
  43. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.VariableBinding;
  44. import org.aspectj.weaver.AjcMemberMaker;
  45. import org.aspectj.weaver.ResolvedMember;
  46. /**
  47. * Walks the body of around advice
  48. *
  49. * Makes sure that all member accesses are to public members. Will convert to use access methods when needed to ensure that. This
  50. * makes it much simpler (and more modular) to inline the body of an around.
  51. *
  52. * ??? constructors are handled different and require access to the target type. changes to
  53. * org.eclipse.jdt.internal.compiler.ast.AllocationExpression would be required to fix this issue.
  54. *
  55. * @author Jim Hugunin
  56. */
  57. public class AccessForInlineVisitor extends ASTVisitor {
  58. PrivilegedHandler handler;
  59. AspectDeclaration inAspect;
  60. EclipseFactory world; // alias for inAspect.world
  61. private Map<TypeBinding, Map<FieldBinding, ResolvedMember>> alreadyProcessedReceivers = new HashMap<TypeBinding, Map<FieldBinding, ResolvedMember>>();
  62. // set to true for ClassLiteralAccess and AssertStatement
  63. // ??? A better answer would be to transform these into inlinable forms
  64. public boolean isInlinable = true;
  65. public AccessForInlineVisitor(AspectDeclaration inAspect, PrivilegedHandler handler) {
  66. this.inAspect = inAspect;
  67. this.world = inAspect.factory;
  68. this.handler = handler;
  69. }
  70. public void endVisit(SingleNameReference ref, BlockScope scope) {
  71. if (ref.binding instanceof FieldBinding) {
  72. ref.binding = getAccessibleField((FieldBinding) ref.binding, ref.actualReceiverType);
  73. }
  74. }
  75. public void endVisit(QualifiedNameReference ref, BlockScope scope) {
  76. if (ref.binding instanceof FieldBinding) {
  77. ref.binding = getAccessibleField((FieldBinding) ref.binding, ref.actualReceiverType);
  78. }
  79. if (ref.otherBindings != null && ref.otherBindings.length > 0) {
  80. TypeBinding receiverType;
  81. if (ref.binding instanceof FieldBinding) {
  82. receiverType = ((FieldBinding) ref.binding).type;
  83. } else if (ref.binding instanceof VariableBinding) {
  84. receiverType = ((VariableBinding) ref.binding).type;
  85. } else {
  86. // !!! understand and fix this case later
  87. receiverType = ref.otherBindings[0].declaringClass;
  88. }
  89. boolean cont = true; // don't continue if we come across a problem
  90. for (int i = 0, len = ref.otherBindings.length; i < len && cont; i++) {
  91. FieldBinding binding = ref.otherBindings[i];
  92. ref.otherBindings[i] = getAccessibleField(binding, receiverType);
  93. if (!(binding instanceof ProblemFieldBinding) && binding != null)
  94. receiverType = binding.type; // TODO Why is this sometimes null?
  95. else
  96. cont = false;
  97. }
  98. }
  99. }
  100. public void endVisit(FieldReference ref, BlockScope scope) {
  101. ref.binding = getAccessibleField(ref.binding, ref.actualReceiverType);
  102. }
  103. public void endVisit(MessageSend send, BlockScope scope) {
  104. if (send instanceof Proceed)
  105. return;
  106. if (send.binding == null || !send.binding.isValidBinding())
  107. return;
  108. if (send.isSuperAccess() && !send.binding.isStatic()) {
  109. send.receiver = new ThisReference(send.sourceStart, send.sourceEnd);
  110. // send.arguments = AstUtil.insert(new ThisReference(send.sourceStart, send.sourceEnd), send.arguments);
  111. MethodBinding superAccessBinding = getSuperAccessMethod(send.binding);
  112. AstUtil.replaceMethodBinding(send, superAccessBinding);
  113. } else if (!isPublic(send.binding)) {
  114. send.syntheticAccessor = getAccessibleMethod(send.binding, send.actualReceiverType);
  115. }
  116. }
  117. public void endVisit(AllocationExpression send, BlockScope scope) {
  118. if (send.binding == null || !send.binding.isValidBinding())
  119. return;
  120. // XXX TBD
  121. if (isPublic(send.binding))
  122. return;
  123. makePublic(send.binding.declaringClass);
  124. send.binding = handler.getPrivilegedAccessMethod(send.binding, send);
  125. }
  126. public void endVisit(QualifiedTypeReference ref, BlockScope scope) {
  127. makePublic(ref.resolvedType); // getTypeBinding(scope)); //??? might be trouble
  128. }
  129. public void endVisit(SingleTypeReference ref, BlockScope scope) {
  130. makePublic(ref.resolvedType); // getTypeBinding(scope)); //??? might be trouble
  131. }
  132. private FieldBinding getAccessibleField(FieldBinding binding, TypeBinding receiverType) {
  133. // System.err.println("checking field: " + binding);
  134. if (binding == null || !binding.isValidBinding())
  135. return binding;
  136. makePublic(receiverType);
  137. if (isPublic(binding))
  138. return binding;
  139. if (binding instanceof PrivilegedFieldBinding)
  140. return binding;
  141. if (binding instanceof InterTypeFieldBinding)
  142. return binding;
  143. if (binding.isPrivate() && binding.declaringClass != inAspect.binding) {
  144. binding.modifiers = AstUtil.makePackageVisible(binding.modifiers);
  145. }
  146. // Avoid repeatedly building ResolvedMembers by using info on any done previously in this visitor
  147. Map<FieldBinding, ResolvedMember> alreadyResolvedMembers = alreadyProcessedReceivers.get(receiverType);
  148. if (alreadyResolvedMembers == null) {
  149. alreadyResolvedMembers = new HashMap<FieldBinding, ResolvedMember>();
  150. alreadyProcessedReceivers.put(receiverType, alreadyResolvedMembers);
  151. }
  152. ResolvedMember m = alreadyResolvedMembers.get(binding);
  153. if (m == null) {
  154. m = world.makeResolvedMember(binding, receiverType);
  155. alreadyResolvedMembers.put(binding, m);
  156. }
  157. if (inAspect.accessForInline.containsKey(m)) {
  158. return (FieldBinding) inAspect.accessForInline.get(m);
  159. }
  160. FieldBinding ret = new InlineAccessFieldBinding(inAspect, binding, m);
  161. inAspect.accessForInline.put(m, ret);
  162. return ret;
  163. }
  164. private MethodBinding getAccessibleMethod(MethodBinding binding, TypeBinding receiverType) {
  165. if (!binding.isValidBinding())
  166. return binding;
  167. makePublic(receiverType); // ???
  168. if (isPublic(binding))
  169. return binding;
  170. if (binding instanceof InterTypeMethodBinding)
  171. return binding;
  172. if (binding instanceof ParameterizedMethodBinding) { // pr124999
  173. binding = binding.original();
  174. }
  175. ResolvedMember m = null;
  176. if (binding.isPrivate() && binding.declaringClass != inAspect.binding) {
  177. // does this always mean that the aspect is an inner aspect of the bindings
  178. // declaring class? After all, the field is private but we can see it from
  179. // where we are.
  180. binding.modifiers = AstUtil.makePackageVisible(binding.modifiers);
  181. m = world.makeResolvedMember(binding);
  182. } else {
  183. // Sometimes receiverType and binding.declaringClass are *not* the same.
  184. // Sometimes receiverType is a subclass of binding.declaringClass. In these situations
  185. // we want the generated inline accessor to call the method on the subclass (at
  186. // runtime this will be satisfied by the super).
  187. m = world.makeResolvedMember(binding, receiverType);
  188. }
  189. if (inAspect.accessForInline.containsKey(m))
  190. return (MethodBinding) inAspect.accessForInline.get(m);
  191. MethodBinding ret = world.makeMethodBinding(AjcMemberMaker.inlineAccessMethodForMethod(inAspect.typeX, m));
  192. inAspect.accessForInline.put(m, ret);
  193. return ret;
  194. }
  195. static class SuperAccessMethodPair {
  196. public ResolvedMember originalMethod;
  197. public MethodBinding accessMethod;
  198. public SuperAccessMethodPair(ResolvedMember originalMethod, MethodBinding accessMethod) {
  199. this.originalMethod = originalMethod;
  200. this.accessMethod = accessMethod;
  201. }
  202. }
  203. private MethodBinding getSuperAccessMethod(MethodBinding binding) {
  204. ResolvedMember m = world.makeResolvedMember(binding);
  205. ResolvedMember superAccessMember = AjcMemberMaker.superAccessMethod(inAspect.typeX, m);
  206. if (inAspect.superAccessForInline.containsKey(superAccessMember)) {
  207. return ((SuperAccessMethodPair) inAspect.superAccessForInline.get(superAccessMember)).accessMethod;
  208. }
  209. MethodBinding ret = world.makeMethodBinding(superAccessMember);
  210. inAspect.superAccessForInline.put(superAccessMember, new SuperAccessMethodPair(m, ret));
  211. return ret;
  212. }
  213. private boolean isPublic(FieldBinding fieldBinding) {
  214. // these are always effectively public to the inliner
  215. if (fieldBinding instanceof InterTypeFieldBinding)
  216. return true;
  217. return fieldBinding.isPublic();
  218. }
  219. private boolean isPublic(MethodBinding methodBinding) {
  220. // these are always effectively public to the inliner
  221. if (methodBinding instanceof InterTypeMethodBinding)
  222. return true;
  223. return methodBinding.isPublic();
  224. }
  225. private void makePublic(TypeBinding binding) {
  226. if (binding == null || !binding.isValidBinding())
  227. return; // has already produced an error
  228. if (binding instanceof ReferenceBinding) {
  229. ReferenceBinding rb = (ReferenceBinding) binding;
  230. if (!rb.isPublic()) {
  231. try {
  232. if (rb instanceof ParameterizedTypeBinding) {
  233. rb = (ReferenceBinding) rb.erasure();
  234. }
  235. } catch (Throwable t) { // TODO remove post 1.7.0
  236. t.printStackTrace();
  237. }
  238. handler.notePrivilegedTypeAccess(rb, null); // ???
  239. }
  240. } else if (binding instanceof ArrayBinding) {
  241. makePublic(((ArrayBinding) binding).leafComponentType);
  242. } else {
  243. return;
  244. }
  245. }
  246. public void endVisit(AssertStatement assertStatement, BlockScope scope) {
  247. isInlinable = false;
  248. }
  249. public void endVisit(ClassLiteralAccess classLiteral, BlockScope scope) {
  250. isInlinable = false;
  251. }
  252. public boolean visit(TypeDeclaration localTypeDeclaration, BlockScope scope) {
  253. // we don't want to transform any local anonymous classes as they won't be inlined
  254. return false;
  255. }
  256. }