1 24 25 package org.aspectj.compiler.base.bytecode; 26 27 import org.aspectj.compiler.base.JavaCompiler; 28 import org.aspectj.compiler.base.CompilerObject; 29 import org.aspectj.compiler.base.parser.SourceInfo; 30 import org.aspectj.compiler.base.ast.*; 31 import org.aspectj.compiler.base.*; 32 33 34 import java.util.*; 35 import java.lang.reflect.Modifier ; 36 37 public class ASTConnection extends CompilerObject { 38 39 public ASTConnection(JavaCompiler compiler) { 40 super(compiler); 41 } 42 43 protected SourceLocation dummySource = new DummySourceLocation(getCompiler()); 44 45 47 private TypeDs classesToTypeDs(ConstantPool.Class[] classes) { 48 if (classes == null || classes.length == 0) { 49 return null; 50 } 51 52 TypeDs names = new TypeDs(dummySource); 53 54 final int N = classes.length; 55 for(int i=0; i<N; i++) { 56 names.add(makeTypeD(classes[i])); 58 } 59 return names; 60 } 61 62 public TypeDec makeTypeDec(ClassFile classFile) { 64 int modifiers = classFile.modifiers; 65 boolean isInterface = Modifier.isInterface(modifiers); 66 modifiers = modifiers & ~Modifier.INTERFACE; 67 Decs body = new Decs(dummySource); 68 69 String fullName = classFile.thisClass.getName(); 70 String name = fullName; 71 String packageName = null; 72 int dot = fullName.lastIndexOf('.'); 73 if (dot != -1) { 74 packageName = fullName.substring(0, dot); 75 name = fullName.substring(dot+1); 76 } 77 78 String id = name; 79 String enclosingName = null; 80 int dollar = id.lastIndexOf('$'); 81 if (dollar != -1) { 82 name = id.substring(dollar+1); 83 enclosingName = id.substring(0, dollar); 84 } 85 86 CompilationUnit cu = new CompilationUnit(dummySource, 87 packageName, null, new Decs(dummySource), 88 new SourceInfo(null, null),true); 89 SourceLocation loc = new TextSourceLocation(cu, -1, -1); 90 92 94 TypeD superClass; 95 if (classFile.superClass == null) { 96 superClass = null; 97 } else { 98 superClass = makeTypeD(classFile.superClass); 99 } 100 101 TypeDs _implements = classesToTypeDs(classFile.interfaces); 102 103 TypeDec typeDec; 104 if (isInterface) { 105 typeDec = getAST().makeInterface(getAST().makeModifiers(modifiers), 106 name, _implements); 107 } else { 108 typeDec = getAST().makeClass(getAST().makeModifiers(modifiers), 109 name, superClass, _implements); 110 } 111 typeDec.setBody(body); 112 113 typeDec.setSourceLocation(loc); 114 if (enclosingName != null) { 115 Type t = getTypeManager().getType(packageName, enclosingName); 116 t.getTypeDec().getBody().add(typeDec); 117 typeDec.setEnclosingTypeDec(t.getTypeDec()); 118 } else { 119 cu.getDecs().add(typeDec); 120 } 121 123 128 for(int i=0; i<classFile.fields.length; i++) { 129 FieldDec dec = makeFieldDec(classFile.fields[i]); 130 dec.setSourceLocation(loc); 131 body.add(dec); 133 } 136 137 for(int i=0; i<classFile.methods.length; i++) { 138 CodeDec dec = makeCodeDec(classFile.methods[i]); 139 if (dec == null) { 140 continue; 142 } 143 dec.setSourceLocation(loc); 144 body.add(dec); 146 } 149 150 Attributes.InnerClassesAttribute attr = 151 (Attributes.InnerClassesAttribute) 152 classFile.attributes.findAttribute(Attributes.InnerClassesAttribute.class); 153 if (attr != null) { 154 typeDec.memberTypeNames = attr.getContainedTypeNames(fullName); 155 if (! attr.isPackageLevel(fullName)) { 159 Modifiers m = new Modifiers(dummySource, attr.getModifiers(fullName)); 160 typeDec.setModifiers(m); 162 if (! m.isStatic() && ! (typeDec instanceof InterfaceDec)) { 163 for (Iterator i = typeDec.getBody().iterator(); i.hasNext(); ) { 164 Object dec = i.next(); 165 if (dec instanceof ConstructorDec) { 166 ConstructorDec cdec = (ConstructorDec) dec; 167 Formals f = cdec.getFormals(); 168 cdec.setEnclosingInstanceFormal(f.get(0)); 169 f.remove(0); 170 } 171 } 172 typeDec.setInnerDiscoveries(false, true, false); 173 } else { 174 typeDec.setInnerDiscoveries(false, false, false); 175 } 176 } else { 177 typeDec.setInnerDiscoveries(true, false, false); 178 } 179 } else { 180 typeDec.setInnerDiscoveries(true, false, false); 181 } 182 183 186 188 190 return typeDec; 191 } 192 193 public TypeD makeTypeD(ConstantPool.Class cpClass) { 194 String name = cpClass.getName(); 195 int lastDot = name.lastIndexOf('.'); 196 if (lastDot == -1) { 197 return new LazyNameTypeD(dummySource,null, name); 198 } else { 199 return new LazyNameTypeD(dummySource,name.substring(0, lastDot), name.substring(lastDot+1)); 200 } 201 } 202 203 public FieldDec makeFieldDec(ClassFile.FieldInfo field) { 204 int modifiers = field.modifiers; 205 TypeD typeD = makeTypeD(field.descriptor.value); 206 String name = field.name.value; 207 Expr initializer = null; 208 209 Attributes.ConstantValueAttribute attr = 210 (Attributes.ConstantValueAttribute) 211 field.attributes.findAttribute(Attributes.ConstantValueAttribute.class); 212 if (attr != null) { 213 ConstantPool.Entry entry = attr.value; 214 if (entry instanceof ConstantPool.String) { 215 ConstantPool.String stringEntry = (ConstantPool.String)entry; 216 initializer = new StringLiteralExpr(dummySource,stringEntry.value.value); 217 } else if (entry instanceof ConstantPool.Float) { 218 ConstantPool.Float floatEntry = (ConstantPool.Float)entry; 219 initializer = new FloatLiteralExpr(dummySource,floatEntry.value); 220 } else if (entry instanceof ConstantPool.Long) { 221 ConstantPool.Long longEntry = (ConstantPool.Long)entry; 222 initializer = new LongLiteralExpr(dummySource,longEntry.value); 223 } else if (entry instanceof ConstantPool.Double) { 224 ConstantPool.Double doubleEntry = (ConstantPool.Double)entry; 225 initializer = new DoubleLiteralExpr(dummySource,doubleEntry.value); 226 } else { 227 ConstantPool.Integer intEntry = (ConstantPool.Integer)entry; 228 int init = intEntry.value; 229 if (typeD.getType().isBoolean()) { 230 initializer = new BooleanLiteralExpr(dummySource,init != 0); 231 } else { 232 initializer = new IntLiteralExpr(dummySource,init); 233 } 234 } 235 } 236 return new FieldDec(dummySource,new Modifiers(dummySource,modifiers), typeD, name, initializer); 237 } 238 239 240 public CodeDec makeCodeDec(ClassFile.MethodInfo method) { 241 int modifiers = method.modifiers; 242 String name = method.name.value; 243 244 TypeDs _throws = classesToTypeDs(method.getExceptions()); 245 Formals formals = getFormals(method.descriptor.value); 248 249 if (name.equals("<init>")) { 250 CodeDec ret = new ConstructorDec(dummySource,new Modifiers(dummySource,modifiers), formals, _throws, null); 251 if (method.isDeprecated()) ret.setDeprecated(true); 252 return ret; 253 } else if (name.equals("<clinit>")) { 254 return null; 256 } 257 258 TypeD typeD = getReturnTypeD(method.descriptor.value); 260 261 262 CodeDec ret = new MethodDec(dummySource,new Modifiers(dummySource,modifiers), typeD, name, 263 formals, _throws, null); 264 if (method.isDeprecated()) { 265 ret.setDeprecated(true); 266 } 268 269 return ret; 270 } 271 272 TypeD getReturnTypeD(String descriptor) { 273 int lastParen = descriptor.lastIndexOf(')'); 274 return makeTypeD(descriptor.substring(lastParen+1)); 275 } 276 277 class IndexedChars { 278 char[] chars; 279 int index; 280 public IndexedChars(char[] chars) { 281 this.chars = chars; 282 index = 0; 283 } 284 285 public char read() { 286 return chars[index++]; 287 } 288 289 public String readTo(char end) { 290 for(int j=index; j<chars.length; j++) { 291 if (chars[j] == end) { 292 String ret = new String (chars, index, j-index); 293 index = j+1; 294 return ret; 295 } 296 } 297 String ret = new String (chars, index, chars.length-index); 298 index = chars.length; 299 return ret; 300 } 301 } 302 303 TypeD readNameTypeD(IndexedChars ic) { 304 char[] chars = ic.chars; 305 int startIndex = ic.index; 306 int endIndex = startIndex; 307 int idStart = startIndex; 308 while (endIndex < chars.length) { 309 if (chars[endIndex] == '/') { 310 chars[endIndex] = '.'; 311 idStart = endIndex+1; 312 } 313 if (chars[endIndex] == ';') break; 314 endIndex++; 315 } 316 317 String id = new String (chars, idStart, endIndex-idStart); 318 String pname; 319 if (idStart == startIndex) pname = null; 320 else pname = new String (chars, startIndex, idStart-startIndex-1); 321 ic.index = endIndex+1; 323 return new LazyNameTypeD(dummySource, pname, id); 324 } 325 326 327 TypeD readTypeD(IndexedChars ic) { 328 switch(ic.read()) { 329 case 'B': return getTypeManager().byteType.makeTypeD(); 330 case 'C': return getTypeManager().charType.makeTypeD(); 331 case 'D': return getTypeManager().doubleType.makeTypeD(); 332 case 'F': return getTypeManager().floatType.makeTypeD(); 333 case 'I': return getTypeManager().intType.makeTypeD(); 334 case 'J': return getTypeManager().longType.makeTypeD(); 335 case 'S': return getTypeManager().shortType.makeTypeD(); 336 case 'Z': return getTypeManager().booleanType.makeTypeD(); 337 case 'V': return getTypeManager().voidType.makeTypeD(); 338 case '[': return new ArrayTypeD(dummySource,readTypeD(ic)); 339 case 'L': return readNameTypeD(ic); 340 } 341 throw new RuntimeException ("unknown descriptor: "+ic.chars[ic.index-1]+" in "+new String (ic.chars)); 342 } 343 344 345 Formals getFormals(String descriptor) { 346 IndexedChars ic = new IndexedChars(descriptor.toCharArray()); 347 ic.index++; 348 List formals = new ArrayList(); 349 int n = 0; 350 while (ic.chars[ic.index] != ')') { 351 FormalDec formal = new FormalDec(dummySource,new Modifiers(dummySource,0), readTypeD(ic), "__" + n, null); 352 n += 1; 353 formals.add(formal); 354 } 355 return new Formals(dummySource,(FormalDec[])formals.toArray(new FormalDec[formals.size()])); 356 } 357 358 public TypeD makeTypeD(String descriptor) { 359 return readTypeD(new IndexedChars(descriptor.toCharArray())); 360 } 361 } 362 | Popular Tags |