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