]> source.dussan.org Git - aspectj.git/blob
b71eddae9fad0b8f59d383456d70f3a10c932c19
[aspectj.git] /
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 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
8  *
9  * Contributors:
10  *     PARC     initial implementation
11  * ******************************************************************/
12
13 package org.aspectj.ajdt.internal.compiler.ast;
14
15 //import java.util.Arrays;
16
17 import java.util.HashMap;
18 import java.util.Map;
19
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;
52
53 /**
54  * Walks the body of around advice
55  *
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.
58  *
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.
61  *
62  * @author Jim Hugunin
63  */
64
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<>();
70
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;
74
75         public AccessForInlineVisitor(AspectDeclaration inAspect, PrivilegedHandler handler) {
76                 this.inAspect = inAspect;
77                 this.world = inAspect.factory;
78                 this.handler = handler;
79         }
80
81         public void endVisit(SingleNameReference ref, BlockScope scope) {
82                 if (ref.binding instanceof FieldBinding) {
83                         ref.binding = getAccessibleField((FieldBinding) ref.binding, ref.actualReceiverType);
84                 }
85         }
86
87         public void endVisit(QualifiedNameReference ref, BlockScope scope) {
88                 if (ref.binding instanceof FieldBinding) {
89                         ref.binding = getAccessibleField((FieldBinding) ref.binding, ref.actualReceiverType);
90                 }
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;
97                         } else {
98                                 // !!! understand and fix this case later
99                                 receiverType = ref.otherBindings[0].declaringClass;
100                         }
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?
107                                 else
108                                         cont = false;
109                         }
110                 }
111         }
112
113         public void endVisit(FieldReference ref, BlockScope scope) {
114                 ref.binding = getAccessibleField(ref.binding, ref.actualReceiverType);
115         }
116
117         public void endVisit(MessageSend send, BlockScope scope) {
118                 if (send instanceof Proceed)
119                         return;
120                 if (send.binding == null || !send.binding.isValidBinding())
121                         return;
122
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);
130                 }
131
132         }
133
134         private boolean isCloneMethod(MethodBinding binding) {
135                 return (CharOperation.equals(binding.selector, TypeConstants.CLONE)) &&
136                                 (CharOperation.equals(binding.declaringClass.compoundName, TypeConstants.JAVA_LANG_OBJECT));
137         }
138
139         public void endVisit(AllocationExpression send, BlockScope scope) {
140                 if (send.binding == null || !send.binding.isValidBinding())
141                         return;
142                 // XXX TBD
143                 if (isPublic(send.binding))
144                         return;
145                 makePublic(send.binding.declaringClass);
146                 send.binding = handler.getPrivilegedAccessMethod(send.binding, send);
147         }
148
149         public void endVisit(QualifiedTypeReference ref, BlockScope scope) {
150                 makePublic(ref.resolvedType); // getTypeBinding(scope)); //??? might be trouble
151         }
152
153         public void endVisit(SingleTypeReference ref, BlockScope scope) {
154                 makePublic(ref.resolvedType); // getTypeBinding(scope)); //??? might be trouble
155         }
156
157         private FieldBinding getAccessibleField(FieldBinding binding, TypeBinding receiverType) {
158                 // System.err.println("checking field: " + binding);
159                 if (binding == null || !binding.isValidBinding())
160                         return binding;
161
162                 makePublic(receiverType);
163                 if (isPublic(binding))
164                         return binding;
165                 if (binding instanceof PrivilegedFieldBinding)
166                         return binding;
167                 if (binding instanceof InterTypeFieldBinding)
168                         return binding;
169
170                 if (binding.isPrivate() && binding.declaringClass != inAspect.binding) {
171                         binding.modifiers = AstUtil.makePackageVisible(binding.modifiers);
172                 }
173
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);
177                 if (m == null) {
178                         m = world.makeResolvedMember(binding, receiverType);
179                         alreadyResolvedMembers.put(binding, m);
180                 }
181
182                 if (inAspect.accessForInline.containsKey(m)) {
183                         return (FieldBinding) inAspect.accessForInline.get(m);
184                 }
185                 FieldBinding ret = new InlineAccessFieldBinding(inAspect, binding, m);
186                 inAspect.accessForInline.put(m, ret);
187                 return ret;
188         }
189
190         private MethodBinding getAccessibleMethod(MethodBinding binding, TypeBinding receiverType) {
191                 if (!binding.isValidBinding())
192                         return binding;
193
194                 makePublic(receiverType); // ???
195                 if (isPublic(binding))
196                         return binding;
197                 if (binding instanceof InterTypeMethodBinding)
198                         return binding;
199
200                 if (binding instanceof ParameterizedMethodBinding) { // pr124999
201                         binding = binding.original();
202                 }
203
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
208                         // where we are.
209                         binding.modifiers = AstUtil.makePackageVisible(binding.modifiers);
210                         m = world.makeResolvedMember(binding);
211                 } else {
212                         // Sometimes receiverType and binding.declaringClass are *not* the same.
213
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);
218                 }
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);
223                 return ret;
224         }
225
226         static class SuperAccessMethodPair {
227                 public ResolvedMember originalMethod;
228                 public MethodBinding accessMethod;
229
230                 public SuperAccessMethodPair(ResolvedMember originalMethod, MethodBinding accessMethod) {
231                         this.originalMethod = originalMethod;
232                         this.accessMethod = accessMethod;
233                 }
234         }
235
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;
241                 }
242                 MethodBinding ret = world.makeMethodBinding(superAccessMember);
243                 inAspect.superAccessForInline.put(superAccessMember, new SuperAccessMethodPair(m, ret));
244                 return ret;
245         }
246
247         private boolean isPublic(FieldBinding fieldBinding) {
248                 // these are always effectively public to the inliner
249                 if (fieldBinding instanceof InterTypeFieldBinding)
250                         return true;
251                 return fieldBinding.isPublic();
252         }
253
254         private boolean isPublic(MethodBinding methodBinding) {
255                 // these are always effectively public to the inliner
256                 if (methodBinding instanceof InterTypeMethodBinding)
257                         return true;
258                 return methodBinding.isPublic();
259         }
260
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()) {
267                                 try {
268                                         if (rb instanceof ParameterizedTypeBinding) {
269                                                 rb = (ReferenceBinding) rb.erasure();
270                                         }
271                                 } catch (Throwable t) { // TODO remove post 1.7.0
272                                         t.printStackTrace();
273                                 }
274                                 handler.notePrivilegedTypeAccess(rb, null); // ???
275                         }
276                 } else if (binding instanceof ArrayBinding) {
277                         makePublic(((ArrayBinding) binding).leafComponentType);
278                 } else {
279                         return;
280                 }
281         }
282
283         public void endVisit(AssertStatement assertStatement, BlockScope scope) {
284                 isInlinable = false;
285         }
286
287         public void endVisit(ClassLiteralAccess classLiteral, BlockScope scope) {
288                 isInlinable = false;
289         }
290
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
293                 return false;
294         }
295
296 }