]> source.dussan.org Git - aspectj.git/blob
fd36f61fb35b5b33c16bd7f213f7707a1c0a6a19
[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.lookup;
14
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import org.aspectj.ajdt.internal.compiler.ast.AspectDeclaration;
20 import org.aspectj.ajdt.internal.compiler.ast.AstUtil;
21 import org.aspectj.bridge.ISourceLocation;
22 import org.aspectj.org.eclipse.jdt.internal.compiler.ast.ASTNode;
23 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
24 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.IPrivilegedHandler;
25 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
26 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ParameterizedFieldBinding;
27 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ParameterizedMethodBinding;
28 import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
29 import org.aspectj.weaver.AjcMemberMaker;
30 import org.aspectj.weaver.Lint;
31 import org.aspectj.weaver.Member;
32 import org.aspectj.weaver.PrivilegedAccessMunger;
33 import org.aspectj.weaver.ResolvedMember;
34 import org.aspectj.weaver.ResolvedMemberImpl;
35 import org.aspectj.weaver.ResolvedType;
36 import org.aspectj.weaver.UnresolvedType;
37 import org.aspectj.weaver.World;
38
39 public class PrivilegedHandler implements IPrivilegedHandler {
40         private AspectDeclaration inAspect;
41         private Map<ResolvedMember, Object> accessors = new HashMap<>();
42
43         public PrivilegedHandler(AspectDeclaration inAspect) {
44                 this.inAspect = inAspect;
45         }
46
47         @Override
48         public boolean definesPrivilegedAccessToField(FieldBinding field) {
49                 if (field instanceof ParameterizedFieldBinding) {
50                         field = ((ParameterizedFieldBinding) field).originalField;
51                 }
52                 ResolvedMember key = inAspect.factory.makeResolvedMember(field);
53                 return (accessors.containsKey(key));
54         }
55
56         @Override
57         public FieldBinding getPrivilegedAccessField(FieldBinding baseField, ASTNode location) {
58                 if (baseField instanceof ParameterizedFieldBinding) {
59                         baseField = ((ParameterizedFieldBinding) baseField).originalField;
60                 }
61                 ResolvedMember key = inAspect.factory.makeResolvedMember(baseField);
62                 if (accessors.containsKey(key))
63                         return (FieldBinding) accessors.get(key);
64                 FieldBinding ret = new PrivilegedFieldBinding(inAspect, baseField);
65                 checkWeaveAccess(key.getDeclaringType(), location);
66                 if (!baseField.alwaysNeedsAccessMethod(true))
67                         accessors.put(key, ret);
68                 // 307120
69                 ResolvedType rt = inAspect.factory.fromEclipse(baseField.declaringClass);
70                 if (rt != null) {
71                         // ReferenceTypeDelegate rtd = ((ReferenceType) rt).getDelegate();
72                         // if (rtd instanceof EclipseSourceType) {
73                         rt.addInterTypeMunger(new EclipseTypeMunger(inAspect.factory, new PrivilegedAccessMunger(key, true), inAspect.typeX,
74                                         null), true);
75                         // }
76                 }
77                 return ret;
78         }
79
80         @Override
81         public MethodBinding getPrivilegedAccessMethod(MethodBinding baseMethod, ASTNode location) {
82                 if (baseMethod.alwaysNeedsAccessMethod())
83                         return baseMethod;
84
85                 ResolvedMember key = null;
86
87                 if (baseMethod instanceof ParameterizedMethodBinding) {
88                         key = inAspect.factory.makeResolvedMember(((ParameterizedMethodBinding) baseMethod).original());
89                 } else {
90                         key = inAspect.factory.makeResolvedMember(baseMethod);
91                 }
92                 if (accessors.containsKey(key))
93                         return (MethodBinding) accessors.get(key);
94
95                 MethodBinding ret;
96                 if (baseMethod.isConstructor()) {
97                         ret = new MethodBinding(baseMethod, baseMethod.declaringClass);
98                         ret.modifiers = AstUtil.makePublic(ret.modifiers);
99                         baseMethod.modifiers = ret.modifiers;
100                 } else {
101                         ret = inAspect.factory.makeMethodBinding(AjcMemberMaker.privilegedAccessMethodForMethod(inAspect.typeX, key));
102                 }
103                 checkWeaveAccess(key.getDeclaringType(), location);
104                 accessors.put(key, ret);
105                 // if (!baseMethod.isConstructor()) {
106                 // ResolvedType rt = inAspect.factory.fromEclipse(baseMethod.declaringClass);
107                 // if (rt!=null) {
108                 // ReferenceTypeDelegate rtd = ((ReferenceType)rt).getDelegate();
109                 // if (rtd instanceof EclipseSourceType) {
110                 // rt.addInterTypeMunger(new EclipseTypeMunger(inAspect.factory,new PrivilegedAccessMunger(key, true),inAspect.typeX,null),
111                 // true);
112                 // }
113                 // }
114                 // }
115                 return ret;
116         }
117
118         @Override
119         public void notePrivilegedTypeAccess(ReferenceBinding type, ASTNode location) {
120                 ResolvedMember key = new ResolvedMemberImpl(Member.STATIC_INITIALIZATION, inAspect.factory.fromEclipse(type), 0,
121                                 UnresolvedType.VOID, "", UnresolvedType.NONE);
122
123                 checkWeaveAccess(key.getDeclaringType(), location);
124                 accessors.put(key, key);
125         }
126
127         private void checkWeaveAccess(UnresolvedType typeX, ASTNode location) {
128                 World world = inAspect.factory.getWorld();
129                 Lint.Kind check = world.getLint().typeNotExposedToWeaver;
130                 if (check.isEnabled()) {
131                         if (!world.resolve(typeX).isExposedToWeaver()) {
132                                 ISourceLocation loc = null;
133                                 if (location != null) {
134                                         loc = new EclipseSourceLocation(inAspect.compilationResult, location.sourceStart, location.sourceEnd);
135                                 }
136                                 check.signal(typeX.getName() + " (needed for privileged access)", loc);
137                         }
138                 }
139         }
140
141         public ResolvedMember[] getMembers() {
142                 Collection<ResolvedMember> m = accessors.keySet();
143                 int len = m.size();
144                 ResolvedMember[] ret = new ResolvedMember[len];
145                 int index = 0;
146                 for (Object o : m) {
147                         ret[index++] = (ResolvedMember) o;
148                 }
149                 return ret;
150         }
151 }