| 1 24 25 package org.aspectj.compiler.base.cst; 26 27 import org.aspectj.compiler.base.ast.*; 28 import org.aspectj.compiler.base.*; 29 import java.util.*; 30 31 34 public class CUScope extends Scope { 35 private CompilationUnit cu; 36 37 public CUScope(JavaCompiler compiler, Scope parent, CompilationUnit cu) { 38 super(compiler, parent); 39 this.cu = cu; 40 } 41 public String shortToString() { return "CU(" + cu.getSourceFileName() + ")"; } 42 43 public Expr bindUnqualifiedName(String name, ASTObject fromWhere) { 44 return null; 45 } 46 47 public Type findMethodLookupType(String name, ASTObject fromWhere) { 48 return null; 49 } 50 51 HashMap nameToType = new HashMap(); 52 public Type findType(String name, ASTObject fromWhere) { 53 Type type = (Type)nameToType.get(name); 54 if (type != null) return type; 55 56 type = internalFindType(name, fromWhere); 57 if (type != null) { 58 nameToType.put(name, type); 59 } 60 return type; 61 } 62 63 protected boolean isPackageAccessible(TypeDec typeDec) { 64 String p1 = typeDec.getPackageName(); 65 String p2 = cu.getPackageName(); 66 if (p1 == null) return p2 == null; 67 else return p1.equals(p2); 68 } 69 70 protected boolean checkAccessibility(TypeDec typeDec, ASTObject fromWhere) { 71 if (typeDec.isPublic()) return true; 72 if (typeDec.isPrivate()) return false; 73 if (typeDec.isProtected()) { 74 if (isPackageAccessible(typeDec)) return true; 75 return typeDec.isAccessible(fromWhere); 76 } 77 return isPackageAccessible(typeDec); 78 } 79 80 81 82 protected Type checkFoundType(String id, Type oldType, Type foundType, ASTObject fromWhere) { 83 if (foundType == null) return oldType; 84 if (foundType == getTypeManager().TYPE_NOT_FOUND) return oldType; 85 86 if (!checkAccessibility(foundType.getTypeDec(), fromWhere)) return oldType; 87 88 if (oldType != null && foundType != oldType) { 89 return handleAmbiguousImport(id, foundType, oldType, fromWhere); 90 } else { 91 return foundType; 92 } 93 } 94 95 96 protected Type internalFindType(String id, ASTObject fromWhere) { 97 Type type = null; 98 99 101 Imports imports = cu.getImports(); 103 if (imports != null) { 104 for (int i=0; i<imports.size(); i++) { 105 Import _import = imports.get(i); 106 if (!_import.getStar()) { 107 if (_import.capturesId(id)) { 108 return _import.getType(); 109 } 110 } 111 } 112 } 113 114 String packageName = cu.getPackageName(); 116 type = getTypeManager().findType(packageName, id); 117 if (type != null) return type; 118 119 120 123 if (imports != null) { 126 for (int i=0; i<imports.size(); i++) { 127 Import _import = imports.get(i); 128 if (_import.getStar()) { 129 Object o = _import.getTypeName().resolve(_import.makeTypeNameScope(), false); 130 Type tryType = null; 131 if (o == null) continue; 132 133 if (o instanceof String ) { 134 tryType = getTypeManager().findType((String )o, id); 135 } else if (o instanceof Type) { 136 tryType = ((Type)o).getInnerType(id, _import, false); 137 } 138 type = checkFoundType(id, type, tryType, fromWhere); 139 } 140 } 141 } 142 143 145 Type javaLangType = getTypeManager().findType("java.lang", id); 147 type = checkFoundType(id, type, javaLangType, fromWhere); 148 149 153 return type; 154 } 155 156 protected Type handleAmbiguousImport(String id, Type type1, Type type2, ASTObject showWhere) { 157 getCompiler().showError(showWhere, "ambiguous type reference, could be either " + 158 type1.getString() + " or " + type2.getString()); 159 return null; 160 } 161 } 162 | Popular Tags |