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.lookup;
15 import java.util.Collection;
16 import java.util.HashMap;
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;
39 public class PrivilegedHandler implements IPrivilegedHandler {
40 private AspectDeclaration inAspect;
41 private Map<ResolvedMember, Object> accessors = new HashMap<>();
43 public PrivilegedHandler(AspectDeclaration inAspect) {
44 this.inAspect = inAspect;
48 public boolean definesPrivilegedAccessToField(FieldBinding field) {
49 if (field instanceof ParameterizedFieldBinding) {
50 field = ((ParameterizedFieldBinding) field).originalField;
52 ResolvedMember key = inAspect.factory.makeResolvedMember(field);
53 return (accessors.containsKey(key));
57 public FieldBinding getPrivilegedAccessField(FieldBinding baseField, ASTNode location) {
58 if (baseField instanceof ParameterizedFieldBinding) {
59 baseField = ((ParameterizedFieldBinding) baseField).originalField;
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);
69 ResolvedType rt = inAspect.factory.fromEclipse(baseField.declaringClass);
71 // ReferenceTypeDelegate rtd = ((ReferenceType) rt).getDelegate();
72 // if (rtd instanceof EclipseSourceType) {
73 rt.addInterTypeMunger(new EclipseTypeMunger(inAspect.factory, new PrivilegedAccessMunger(key, true), inAspect.typeX,
81 public MethodBinding getPrivilegedAccessMethod(MethodBinding baseMethod, ASTNode location) {
82 if (baseMethod.alwaysNeedsAccessMethod())
85 ResolvedMember key = null;
87 if (baseMethod instanceof ParameterizedMethodBinding) {
88 key = inAspect.factory.makeResolvedMember(((ParameterizedMethodBinding) baseMethod).original());
90 key = inAspect.factory.makeResolvedMember(baseMethod);
92 if (accessors.containsKey(key))
93 return (MethodBinding) accessors.get(key);
96 if (baseMethod.isConstructor()) {
97 ret = new MethodBinding(baseMethod, baseMethod.declaringClass);
98 ret.modifiers = AstUtil.makePublic(ret.modifiers);
99 baseMethod.modifiers = ret.modifiers;
101 ret = inAspect.factory.makeMethodBinding(AjcMemberMaker.privilegedAccessMethodForMethod(inAspect.typeX, key));
103 checkWeaveAccess(key.getDeclaringType(), location);
104 accessors.put(key, ret);
105 // if (!baseMethod.isConstructor()) {
106 // ResolvedType rt = inAspect.factory.fromEclipse(baseMethod.declaringClass);
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),
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);
123 checkWeaveAccess(key.getDeclaringType(), location);
124 accessors.put(key, key);
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);
136 check.signal(typeX.getName() + " (needed for privileged access)", loc);
141 public ResolvedMember[] getMembers() {
142 Collection<ResolvedMember> m = accessors.keySet();
144 ResolvedMember[] ret = new ResolvedMember[len];
147 ret[index++] = (ResolvedMember) o;