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 v1.0
6 * which accompanies this distribution and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * PARC initial implementation
11 * ******************************************************************/
13 package org.aspectj.ajdt.internal.compiler.lookup;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.Iterator;
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;
40 public class PrivilegedHandler implements IPrivilegedHandler {
41 private AspectDeclaration inAspect;
42 private Map accessors = new HashMap();
44 public PrivilegedHandler(AspectDeclaration inAspect) {
45 this.inAspect = inAspect;
49 public boolean definesPrivilegedAccessToField(FieldBinding field) {
50 if (field instanceof ParameterizedFieldBinding) {
51 field = ((ParameterizedFieldBinding) field).originalField;
53 ResolvedMember key = inAspect.factory.makeResolvedMember(field);
54 return (accessors.containsKey(key));
58 public FieldBinding getPrivilegedAccessField(FieldBinding baseField, ASTNode location) {
59 if (baseField instanceof ParameterizedFieldBinding) {
60 baseField = ((ParameterizedFieldBinding) baseField).originalField;
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);
70 ResolvedType rt = inAspect.factory.fromEclipse(baseField.declaringClass);
72 // ReferenceTypeDelegate rtd = ((ReferenceType) rt).getDelegate();
73 // if (rtd instanceof EclipseSourceType) {
74 rt.addInterTypeMunger(new EclipseTypeMunger(inAspect.factory, new PrivilegedAccessMunger(key, true), inAspect.typeX,
82 public MethodBinding getPrivilegedAccessMethod(MethodBinding baseMethod, ASTNode location) {
83 if (baseMethod.alwaysNeedsAccessMethod())
86 ResolvedMember key = null;
88 if (baseMethod instanceof ParameterizedMethodBinding) {
89 key = inAspect.factory.makeResolvedMember(((ParameterizedMethodBinding) baseMethod).original());
91 key = inAspect.factory.makeResolvedMember(baseMethod);
93 if (accessors.containsKey(key))
94 return (MethodBinding) accessors.get(key);
97 if (baseMethod.isConstructor()) {
98 ret = new MethodBinding(baseMethod, baseMethod.declaringClass);
99 ret.modifiers = AstUtil.makePublic(ret.modifiers);
100 baseMethod.modifiers = ret.modifiers;
102 ret = inAspect.factory.makeMethodBinding(AjcMemberMaker.privilegedAccessMethodForMethod(inAspect.typeX, key));
104 checkWeaveAccess(key.getDeclaringType(), location);
105 accessors.put(key, ret);
106 // if (!baseMethod.isConstructor()) {
107 // ResolvedType rt = inAspect.factory.fromEclipse(baseMethod.declaringClass);
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),
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);
124 checkWeaveAccess(key.getDeclaringType(), location);
125 accessors.put(key, key);
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);
137 check.signal(typeX.getName() + " (needed for privileged access)", loc);
142 public ResolvedMember[] getMembers() {
143 Collection m = accessors.keySet();
145 ResolvedMember[] ret = new ResolvedMember[len];
147 for (Iterator i = m.iterator(); i.hasNext();) {
148 ret[index++] = (ResolvedMember) i.next();