1 24 25 package org.aspectj.compiler.base; 26 27 import java.util.*; 28 import org.aspectj.compiler.base.ast.*; 29 30 public class ClassExprPass extends WalkerPass { 31 public ClassExprPass(JavaCompiler jc) { super(jc); } 32 public String getDisplayName() { return "desugaring T.class exprs"; } 33 34 TypeDec dec = null; 35 HashMap fields = null; 36 MethodDec method = null; 37 TypeDec holder = null; 39 public ASTObject process(ASTObject object) { 40 if (object instanceof TypeDec) { 41 TypeDec savedDec = this.dec; 42 HashMap savedFields = this.fields; 43 MethodDec savedMethod = this.method; 44 TypeDec savedHolder = this.holder; 45 dec = (TypeDec) object; 46 fields = new HashMap(); 47 if (object instanceof ClassDec) { 48 holder = dec; 49 } 50 object.walk(this); 51 this.dec = savedDec; 52 this.fields = savedFields; 53 this.method = savedMethod; 54 this.holder = savedHolder; 55 return object; 56 } 57 if (object instanceof ClassExpr) { 58 object = handleClassExpr((ClassExpr) object); 59 } 60 object.walk(this); 61 return object; 62 } 63 64 ASTObject handleClassExpr(ClassExpr object) { 65 final AST ast = getAST(); 66 Type ty = object.getTypeD().getType(); 67 if (ty instanceof PrimitiveType) return ty.getClassExpr(); 68 FieldDec field = genField(ty); 69 MethodDec maker = genMethod(); 70 return ast.makeTriTest(ast.makeBinop("==", 71 ast.makeGet(field), 72 ast.makeNull()), 73 ast.makeSet(field, 74 ast.makeStaticCall(method, 75 ast.makeString(ty.getExternalName()))), 76 ast.makeGet(field)); 77 } 78 79 FieldDec genField(Type ty) { 80 final AST ast = getAST(); 81 final TypeManager tm = getTypeManager(); 82 FieldDec field = (FieldDec) fields.get(ty); 83 if (field == null) { 84 if (holder == null) genHolder(); 85 field = ast.makeField(ast.makeModifiers(Modifiers.STATIC), 86 tm.getClassType(), 87 genFieldName(ty)); 88 holder.getBody().add(field); 89 fields.put(ty, field); 90 } 91 return field; 92 } 93 94 String genFieldName(Type ty) { 95 String str = ty.getExternalName(); 96 if (ty instanceof ArrayType) { 97 str = "array" + str; 98 } else { 99 str = "class$" + str; 100 } 101 str = str.replace('[','$').replace('.','$'); 102 if (str.charAt(str.length() - 1) == ';') { 103 str = str.substring(0, str.length() - 1); 104 } 105 return str; 106 } 107 108 MethodDec genMethod() { 109 final AST ast = getAST(); 110 final TypeManager tm = getTypeManager(); 111 if (method == null) { 112 if (holder == null) genHolder(); 113 FormalDec formal = ast.makeFormal(tm.getStringType(), "name$"); 114 FormalDec exnFormal = ast.makeFormal(tm.getType("java.lang", "ClassNotFoundException"), 115 "exn$"); 116 method = ast.makeMethod( 117 ast.makeModifiers(Modifiers.STATIC), 118 tm.getClassType(), 119 "class$", 120 ast.makeFormals(formal), 121 ast.makeCodeBody( 122 ast.makeTryCatch( 123 ast.makeBlock( 124 ast.makeReturn( 125 ast.makeStaticCall(tm.getClassType(), 126 "forName", 127 ast.makeExprs(ast.makeVar(formal))))), 128 exnFormal, 129 ast.makeBlock( 130 ast.makeThrow( 131 ast.makeNew( 132 tm.getType("java.lang", "NoClassDefFoundError"), 133 ast.makeCall(ast.makeVar(exnFormal), 134 "getMessage", 135 ast.makeExprs()))))))); 136 holder.getBody().add(method); 137 } 138 return method; 139 } 140 141 void genHolder() { 142 final AST ast = getAST(); 143 final TypeManager tm = getTypeManager(); 144 145 holder = ast.makeClass(ast.makeModifiers(Modifiers.STATIC | Modifiers.PRIVATE), 146 "00", 147 tm.getObjectType().makeTypeD(), 148 ast.makeTypeDs()); 149 holder.setIsInner(false); 150 dec.addMemberTypeDec(holder); 151 } 152 } 153 | Popular Tags |