1 24 25 package org.aspectj.compiler.base; 26 27 import org.aspectj.compiler.base.ast.*; 28 29 import java.util.*; 30 31 108 109 public class InnerAccessFixer extends WalkerPass { 110 111 public InnerAccessFixer(JavaCompiler compiler) { 112 super(compiler); 113 } 114 115 public String getDisplayName() { 116 return "adding inner access backdoors"; 117 } 118 119 public ASTObject process(ASTObject object) { 120 object.walk(this); 121 return object.postInnerAccess(this); 122 } 123 124 private Map accessMethods = new HashMap(); 125 126 public MethodDec getAccessMethod(Type qType, Dec dec, String op, Expr src) { 127 Triple key = new Triple(qType, dec, op); 128 MethodDec methodDec = (MethodDec) accessMethods.get(key); 129 if (methodDec == null) { 130 methodDec = src.buildAccessMethod(this); 131 ((NameType)qType).getTypeDec().addToBody(methodDec); 132 accessMethods.put(key, methodDec); 133 } 134 return methodDec; 135 } 136 137 private int accessCounter = 0; 138 public MethodDec makeAccessMethod(Type returnType, Formals formals, Expr bodyExpr) { 139 final AST ast = getAST(); 140 return 141 ast.makeMethod(ast.makeModifiers(Modifiers.STATIC), 142 returnType, 143 "access$" + accessCounter++, 144 formals, 145 returnType.isVoid() 146 ? ast.makeCodeBody(ast.makeStmt(bodyExpr), ast.makeReturn()) 147 : ast.makeCodeBody(ast.makeReturn(bodyExpr))); 148 } 149 150 165 public Expr makeOutsidePrimary(boolean isStatic, Exprs args, Expr e) { 166 if (e.getType() instanceof PrimitiveType) throw new RuntimeException (); 167 if (isStatic) { 168 return e; 169 } else { 170 args.add(0, e); 171 return getAST().makeTypeExpr(e.getType()); 172 } 173 } 174 175 187 public Expr makeInsidePrimary(boolean isStatic, Formals f, Type ty) { 188 final AST ast = getAST(); 189 if (isStatic) { 190 return ast.makeTypeExpr(ty); 191 } else { 192 FormalDec obj = ast.makeFormal(ty, "obj$"); 193 f.add(0, obj); 194 return ast.makeVar(obj); 195 } 196 } 197 198 202 public boolean isAccessible(Dec dec, Expr q) { 203 if (dec.isPrivate()) { 204 return q.getBytecodeTypeDec() == dec.getBytecodeTypeDec(); 205 } else if (dec.isProtected()) { 206 TypeDec target = dec.getBytecodeTypeDec(); 207 TypeDec src = q.getBytecodeTypeDec(); 208 if (src.getType().isSubtypeOf(target.getType())) return true; 209 String pkg1 = src.getPackageName(); 210 String pkg2 = target.getPackageName(); 211 return (pkg1 == null) ? (pkg2 == null) : pkg1.equals(pkg2); 212 } else { 213 return true; 214 } 215 } 216 217 219 220 static class Triple { 221 Object one, two, three; 222 Triple(Object one, Object two, Object three) { 223 this.one = one; this.two = two; this.three = three; 224 } 225 public int hashCode() { 226 return one.hashCode() + two.hashCode() * 2 + three.hashCode() * 3; 227 } 228 public boolean equals(Object other) { 229 if (other instanceof Triple) { 230 Triple otherTriple = (Triple) other; 231 return one.equals(otherTriple.one) 232 && two.equals(otherTriple.two) 233 && three.equals(otherTriple.three); 234 } else { 235 return false; 236 } 237 } 238 } 239 } 240 | Popular Tags |