Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

AccessForInlineVisitor.java 11KB

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