1 24 25 package org.aspectj.compiler.base; 26 27 import org.aspectj.compiler.base.ast.*; 28 import org.aspectj.compiler.base.bytecode.*; 29 30 import java.util.*; 31 import java.io.*; 32 33 public final class TypeManager extends CompilerObject { 34 protected SourceLocation dummySource = new DummySourceLocation(getCompiler()); 35 public final TypeDec TYPE_DEC_NOT_FOUND = new ClassDec(dummySource, 36 new Modifiers(dummySource,0), 37 "*NF*",null, null, new Decs(dummySource)); 38 39 public final NameType TYPE_NOT_FOUND = new NotFoundType(TYPE_DEC_NOT_FOUND); 40 { 41 TYPE_DEC_NOT_FOUND.setType(TYPE_NOT_FOUND); 42 } 45 46 private ClassPathManager classPathManager; 47 private Map packages = new HashMap(); 48 private PackageSO defaultPackage; 49 private Collection loadedTypes = new HashSet(); 50 51 public TypeManager(JavaCompiler compiler) { 52 super(compiler); 53 Options options = compiler.getOptions(); 54 if (options.classpath == null) options.classpath = options.defaultClasspath; 55 classPathManager = 56 new ClassPathManager(compiler, 57 options.classpath, options.bootclasspath, 58 options.extdirs); 59 defaultPackage = new PackageSO(compiler, null, classPathManager); 60 } 62 63 private Collection allVisibleTypeNames = null; 64 65 70 public Collection getAllVisibleTypeNames() { 71 if (allVisibleTypeNames == null) { 72 allVisibleTypeNames = classPathManager.getAllPossibleTypeNames(); 73 for (Iterator i = getWorld().getTypes().iterator(); i.hasNext();) { 74 TypeDec t = (TypeDec) i.next(); 75 allVisibleTypeNames.add(t.getType().getNamePieces()); 76 } 77 for (Iterator i = getPrimitiveTypeMap().values().iterator(); i.hasNext(); ) { 78 Type t = (Type)i.next(); 79 allVisibleTypeNames.add(t.getNamePieces()); 80 } 81 } 82 return allVisibleTypeNames; 83 } 84 85 88 public Collection getClassPathStrings() { 89 return classPathManager.getPathStrings(); 90 } 91 92 95 public Collection getLoadedTypes() { 96 return loadedTypes; 97 } 98 99 100 public Type getType(String packageName, String className) { 101 Type type = findType(packageName, className); 102 if (type == null) { 103 getCompiler().showError("missing type " + packageName + '.' + className); 104 return TYPE_NOT_FOUND; 105 } else { 106 return type; 107 } 108 } 109 110 public synchronized Type findType(String packageName, String className) { 111 PackageSO p = findPackage(packageName); 113 Type ret = p.findType(className); 114 if (ret != null) loadedTypes.add(ret); 115 return ret; 117 } 118 119 public void addType(Type type) { 120 122 PackageSO p = findPackage(type.getPackageName()); 123 p.addType(type); 124 loadedTypes.add(type); 125 } 126 127 PackageSO findTopLevelPackage(String name) { 128 PackageSO p = (PackageSO)packages.get(name); 129 if (p != null) return p; 130 131 p = new PackageSO(getCompiler(), name, 132 classPathManager.makeSubPackageManager(name)); 133 packages.put(name, p); 137 return p; 138 } 139 140 public PackageSO findPackage(String packageName) { 141 if (packageName == null) return defaultPackage; 142 143 PackageSO p = (PackageSO)packages.get(packageName); 144 if (p != null) return p; 145 146 String [] names = splitDottedName(packageName); 147 p = findTopLevelPackage(names[0]); 148 for (int i=1; i<names.length; i++) { 149 p = p.findPackage(names[i]); 151 } 152 154 packages.put(packageName, p); 155 return p; 156 } 157 158 String [] splitDottedName(String name) { 159 int nameCount = 0; 161 int lastDot=-1; 162 do { 163 lastDot = name.indexOf('.', lastDot+1); 164 nameCount++; 165 } while (lastDot != -1); 166 167 String [] names = new String [nameCount]; 168 lastDot = 0; 169 int index=0; 170 while (true) { 171 int nextDot = name.indexOf('.', lastDot); 172 if (nextDot == -1) { 173 names[index] = name.substring(lastDot); 174 break; 175 } else { 176 names[index++] = name.substring(lastDot, nextDot); 177 lastDot = nextDot+1; 178 } 179 } 180 181 return names; 182 } 183 184 185 public boolean checkLoadable(String packageName, String className) { 186 PackageSO p = findPackage(packageName); 187 return p.findTypeOnClassPath(className) != null; 188 } 189 190 191 private final Map primitiveTypeMap = new HashMap(); 192 193 public final Type nullType = new NullType(getCompiler()); 194 public final Type voidType = new VoidType(getCompiler()); 195 { primitiveTypeMap.put("void", voidType); } 196 public final Type anyType = TYPE_NOT_FOUND; 198 public final PrimitiveType intType = new IntType(getCompiler()); 199 { primitiveTypeMap.put("int", intType); } 200 public final PrimitiveType byteType = new ByteType(getCompiler()); 201 { primitiveTypeMap.put("byte", byteType); } 202 public final PrimitiveType shortType = new ShortType(getCompiler()); 203 { primitiveTypeMap.put("short", shortType); } 204 public final PrimitiveType longType = new LongType(getCompiler()); 205 { primitiveTypeMap.put("long", longType); } 206 public final PrimitiveType doubleType = new DoubleType(getCompiler()); 207 { primitiveTypeMap.put("double", doubleType); } 208 public final PrimitiveType floatType = new FloatType(getCompiler()); 209 { primitiveTypeMap.put("float", floatType); } 210 public final PrimitiveType charType = new CharType(getCompiler()); 211 { primitiveTypeMap.put("char", charType); } 212 public final PrimitiveType booleanType = new BooleanType(getCompiler()); 213 { primitiveTypeMap.put("boolean", booleanType); } 214 215 216 public Type findPrimitiveType(String id) { 217 return (Type)getPrimitiveTypeMap().get(id); 218 } 219 220 public Map getPrimitiveTypeMap() { 221 return primitiveTypeMap; 222 } 223 224 225 public final NameType getObjectType() { 226 return (NameType)getType("java.lang", "Object"); 227 } 228 public final NameType getStringType() { 229 return (NameType)getType("java.lang", "String"); 230 } 231 public final NameType getClassType() { 232 return (NameType)getType("java.lang", "Class"); 233 } 234 public final NameType getCloneableType() { 235 return (NameType)getType("java.lang", "Cloneable"); 236 } 237 public final NameType getSerializableType() { 238 return (NameType)getType("java.io", "Serializable"); 239 } 240 public final NameType getRuntimeExceptionType() { 241 return (NameType)getType("java.lang", "RuntimeException"); 242 } 243 public final NameType getErrorType() { 244 return (NameType)getType("java.lang", "Error"); 245 } 246 public final NameType getThrowableType() { 247 return (NameType)getType("java.lang", "Throwable"); 248 } 249 public final NameType getExceptionType() { 250 return (NameType)getType("java.lang", "Exception"); 251 } 252 public final NameType getStringBufferType() { 253 return (NameType)getType("java.lang", "StringBuffer"); 254 } 255 256 public final Type getJoinPointType() { 258 return getType("org.aspectj.lang", "JoinPoint"); 259 } 260 public final Type getJoinPointStaticPartType() { 261 return getType("org.aspectj.lang", "JoinPoint$StaticPart"); 262 } 263 public final Type getAroundClosureType() { 264 return getType("org.aspectj.runtime.internal", "AroundClosure"); 265 } 266 public final Type getConversionsType() { 267 return getType("org.aspectj.runtime.internal", "Conversions"); 268 } 269 270 276 277 280 300 public Type unaryNumericPromotion(Type ty) { 301 if (ty == doubleType || ty == floatType || ty == longType) { 302 return ty; 303 } 304 return intType; 305 } 306 307 public Type binaryNumericPromotion(Type ty1, Type ty2) { 308 if (ty1 == doubleType || ty2 == doubleType) { 309 return doubleType; 310 } 311 if (ty1 == floatType || ty2 == floatType) { 312 return floatType; 313 } 314 if (ty1 == longType || ty2 == longType) { 315 return longType; 316 } 317 return intType; 318 } 319 } 320 321 | Popular Tags |