Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AccessForInlineVisitor.java 10KB

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