1 /* *******************************************************************
2 * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
4 * This program and the accompanying materials are made available
5 * under the terms of the Eclipse Public License v 2.0
6 * which accompanies this distribution and is available at
7 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
10 * PARC initial implementation
11 * ******************************************************************/
13 package org.aspectj.ajdt.internal.compiler.ast;
15 //import java.util.Arrays;
17 import java.util.HashMap;
20 import org.aspectj.ajdt.internal.compiler.lookup.EclipseFactory;
21 import org.aspectj.ajdt.internal.compiler.lookup.InlineAccessFieldBinding;
22 import org.aspectj.ajdt.internal.compiler.lookup.InterTypeFieldBinding;
23 import org.aspectj.ajdt.internal.compiler.lookup.InterTypeMethodBinding;
24 import org.aspectj.ajdt.internal.compiler.lookup.PrivilegedFieldBinding;
25 import org.aspectj.ajdt.internal.compiler.lookup.PrivilegedHandler;
26 import org.aspectj.org.eclipse.jdt.core.compiler.CharOperation;
27 import org.aspectj.org.eclipse.jdt.internal.compiler.ASTVisitor;
28 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.AllocationExpression;
29 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.AssertStatement;
30 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess;
31 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.FieldReference;
32 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.MessageSend;
33 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference;
34 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference;
35 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.SingleNameReference;
36 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.SingleTypeReference;
37 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.ThisReference;
38 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
39 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ArrayBinding;
40 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BlockScope;
41 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
42 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
43 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ParameterizedMethodBinding;
44 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding;
45 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ProblemFieldBinding;
46 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
47 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
48 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
49 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.VariableBinding;
50 import org.aspectj.weaver.AjcMemberMaker;
51 import org.aspectj.weaver.ResolvedMember;
54 * Walks the body of around advice
56 * Makes sure that all member accesses are to public members. Will convert to use access methods when needed to ensure that. This
57 * makes it much simpler (and more modular) to inline the body of an around.
59 * ??? constructors are handled different and require access to the target type. changes to
60 * org.eclipse.jdt.internal.compiler.ast.AllocationExpression would be required to fix this issue.
65 public class AccessForInlineVisitor extends ASTVisitor {
66 PrivilegedHandler handler;
67 AspectDeclaration inAspect;
68 EclipseFactory world; // alias for inAspect.world
69 private Map<TypeBinding, Map<FieldBinding, ResolvedMember>> alreadyProcessedReceivers = new HashMap<>();
71 // set to true for ClassLiteralAccess and AssertStatement
72 // ??? A better answer would be to transform these into inlinable forms
73 public boolean isInlinable = true;
75 public AccessForInlineVisitor(AspectDeclaration inAspect, PrivilegedHandler handler) {
76 this.inAspect = inAspect;
77 this.world = inAspect.factory;
78 this.handler = handler;
81 public void endVisit(SingleNameReference ref, BlockScope scope) {
82 if (ref.binding instanceof FieldBinding) {
83 ref.binding = getAccessibleField((FieldBinding) ref.binding, ref.actualReceiverType);
87 public void endVisit(QualifiedNameReference ref, BlockScope scope) {
88 if (ref.binding instanceof FieldBinding) {
89 ref.binding = getAccessibleField((FieldBinding) ref.binding, ref.actualReceiverType);
91 if (ref.otherBindings != null && ref.otherBindings.length > 0) {
92 TypeBinding receiverType;
93 if (ref.binding instanceof FieldBinding) {
94 receiverType = ((FieldBinding) ref.binding).type;
95 } else if (ref.binding instanceof VariableBinding) {
96 receiverType = ((VariableBinding) ref.binding).type;
98 // !!! understand and fix this case later
99 receiverType = ref.otherBindings[0].declaringClass;
101 boolean cont = true; // don't continue if we come across a problem
102 for (int i = 0, len = ref.otherBindings.length; i < len && cont; i++) {
103 FieldBinding binding = ref.otherBindings[i];
104 ref.otherBindings[i] = getAccessibleField(binding, receiverType);
105 if (!(binding instanceof ProblemFieldBinding) && binding != null)
106 receiverType = binding.type; // TODO Why is this sometimes null?
113 public void endVisit(FieldReference ref, BlockScope scope) {
114 ref.binding = getAccessibleField(ref.binding, ref.actualReceiverType);
117 public void endVisit(MessageSend send, BlockScope scope) {
118 if (send instanceof Proceed)
120 if (send.binding == null || !send.binding.isValidBinding())
123 if (send.isSuperAccess() && !send.binding.isStatic()) {
124 send.receiver = new ThisReference(send.sourceStart, send.sourceEnd);
125 // send.arguments = AstUtil.insert(new ThisReference(send.sourceStart, send.sourceEnd), send.arguments);
126 MethodBinding superAccessBinding = getSuperAccessMethod(send.binding);
127 AstUtil.replaceMethodBinding(send, superAccessBinding);
128 } else if (!isPublic(send.binding) && !isCloneMethod(send.binding)) {
129 send.syntheticAccessor = getAccessibleMethod(send.binding, send.actualReceiverType);
134 private boolean isCloneMethod(MethodBinding binding) {
135 return (CharOperation.equals(binding.selector, TypeConstants.CLONE)) &&
136 (CharOperation.equals(binding.declaringClass.compoundName, TypeConstants.JAVA_LANG_OBJECT));
139 public void endVisit(AllocationExpression send, BlockScope scope) {
140 if (send.binding == null || !send.binding.isValidBinding())
143 if (isPublic(send.binding))
145 makePublic(send.binding.declaringClass);
146 send.binding = handler.getPrivilegedAccessMethod(send.binding, send);
149 public void endVisit(QualifiedTypeReference ref, BlockScope scope) {
150 makePublic(ref.resolvedType); // getTypeBinding(scope)); //??? might be trouble
153 public void endVisit(SingleTypeReference ref, BlockScope scope) {
154 makePublic(ref.resolvedType); // getTypeBinding(scope)); //??? might be trouble
157 private FieldBinding getAccessibleField(FieldBinding binding, TypeBinding receiverType) {
158 // System.err.println("checking field: " + binding);
159 if (binding == null || !binding.isValidBinding())
162 makePublic(receiverType);
163 if (isPublic(binding))
165 if (binding instanceof PrivilegedFieldBinding)
167 if (binding instanceof InterTypeFieldBinding)
170 if (binding.isPrivate() && binding.declaringClass != inAspect.binding) {
171 binding.modifiers = AstUtil.makePackageVisible(binding.modifiers);
174 // Avoid repeatedly building ResolvedMembers by using info on any done previously in this visitor
175 Map<FieldBinding, ResolvedMember> alreadyResolvedMembers = alreadyProcessedReceivers.computeIfAbsent(receiverType, k -> new HashMap<>());
176 ResolvedMember m = alreadyResolvedMembers.get(binding);
178 m = world.makeResolvedMember(binding, receiverType);
179 alreadyResolvedMembers.put(binding, m);
182 if (inAspect.accessForInline.containsKey(m)) {
183 return (FieldBinding) inAspect.accessForInline.get(m);
185 FieldBinding ret = new InlineAccessFieldBinding(inAspect, binding, m);
186 inAspect.accessForInline.put(m, ret);
190 private MethodBinding getAccessibleMethod(MethodBinding binding, TypeBinding receiverType) {
191 if (!binding.isValidBinding())
194 makePublic(receiverType); // ???
195 if (isPublic(binding))
197 if (binding instanceof InterTypeMethodBinding)
200 if (binding instanceof ParameterizedMethodBinding) { // pr124999
201 binding = binding.original();
204 ResolvedMember m = null;
205 if (binding.isPrivate() && binding.declaringClass != inAspect.binding) {
206 // does this always mean that the aspect is an inner aspect of the bindings
207 // declaring class? After all, the field is private but we can see it from
209 binding.modifiers = AstUtil.makePackageVisible(binding.modifiers);
210 m = world.makeResolvedMember(binding);
212 // Sometimes receiverType and binding.declaringClass are *not* the same.
214 // Sometimes receiverType is a subclass of binding.declaringClass. In these situations
215 // we want the generated inline accessor to call the method on the subclass (at
216 // runtime this will be satisfied by the super).
217 m = world.makeResolvedMember(binding, receiverType);
219 if (inAspect.accessForInline.containsKey(m))
220 return (MethodBinding) inAspect.accessForInline.get(m);
221 MethodBinding ret = world.makeMethodBinding(AjcMemberMaker.inlineAccessMethodForMethod(inAspect.typeX, m));
222 inAspect.accessForInline.put(m, ret);
226 static class SuperAccessMethodPair {
227 public ResolvedMember originalMethod;
228 public MethodBinding accessMethod;
230 public SuperAccessMethodPair(ResolvedMember originalMethod, MethodBinding accessMethod) {
231 this.originalMethod = originalMethod;
232 this.accessMethod = accessMethod;
236 private MethodBinding getSuperAccessMethod(MethodBinding binding) {
237 ResolvedMember m = world.makeResolvedMember(binding);
238 ResolvedMember superAccessMember = AjcMemberMaker.superAccessMethod(inAspect.typeX, m);
239 if (inAspect.superAccessForInline.containsKey(superAccessMember)) {
240 return ((SuperAccessMethodPair) inAspect.superAccessForInline.get(superAccessMember)).accessMethod;
242 MethodBinding ret = world.makeMethodBinding(superAccessMember);
243 inAspect.superAccessForInline.put(superAccessMember, new SuperAccessMethodPair(m, ret));
247 private boolean isPublic(FieldBinding fieldBinding) {
248 // these are always effectively public to the inliner
249 if (fieldBinding instanceof InterTypeFieldBinding)
251 return fieldBinding.isPublic();
254 private boolean isPublic(MethodBinding methodBinding) {
255 // these are always effectively public to the inliner
256 if (methodBinding instanceof InterTypeMethodBinding)
258 return methodBinding.isPublic();
261 private void makePublic(TypeBinding binding) {
262 if (binding == null || !binding.isValidBinding())
263 return; // has already produced an error
264 if (binding instanceof ReferenceBinding) {
265 ReferenceBinding rb = (ReferenceBinding) binding;
266 if (!rb.isPublic()) {
268 if (rb instanceof ParameterizedTypeBinding) {
269 rb = (ReferenceBinding) rb.erasure();
271 } catch (Throwable t) { // TODO remove post 1.7.0
274 handler.notePrivilegedTypeAccess(rb, null); // ???
276 } else if (binding instanceof ArrayBinding) {
277 makePublic(((ArrayBinding) binding).leafComponentType);
283 public void endVisit(AssertStatement assertStatement, BlockScope scope) {
287 public void endVisit(ClassLiteralAccess classLiteral, BlockScope scope) {
291 public boolean visit(TypeDeclaration localTypeDeclaration, BlockScope scope) {
292 // we don't want to transform any local anonymous classes as they won't be inlined