1 24 25 package org.aspectj.compiler.base.ast; 26 27 import org.aspectj.compiler.base.parser.SourceInfo; 28 29 import org.aspectj.compiler.base.*; 30 import org.aspectj.compiler.base.cst.CUScope; 31 32 import java.util.*; 33 import java.io.File ; 34 import java.io.IOException ; 35 36 44 public class CompilationUnit extends ASTObject { 45 46 private CUScope scope = new CUScope(getCompiler(), null, this); 47 48 private ArrayList ownDecs = new ArrayList(); 49 50 61 public CompilationUnit(JavaCompiler compiler, File _file) { 63 this(new DummySourceLocation(compiler), null, null, null, new SourceInfo(_file, null), false); 64 sourceLocation = new TextSourceLocation(this, 0, 0); 65 setDecs(new Decs(getSourceLocation())); 66 } 67 68 public List getDefinedTypes() { 69 return ownDecs; 70 } 71 72 public void addDefinedType(TypeDec dec) { 73 ownDecs.add(dec); 75 } 76 77 78 public Type getDeclaringType() { 79 return null; 80 } 81 public Type getLexicalType() { 82 return null; 83 } 84 85 public TypeDec getBytecodeTypeDec() { 86 return null; 87 } 88 89 public CodeDec getEnclosingCodeDec() { 90 return null; 91 } 92 93 public CUScope getScope() { 94 return scope; 95 } 96 97 public void preScope(ScopeWalker walker) { walker.enterCU(this); } 99 101 public String toString() { return getSourceFile().toString(); } 127 128 public void cleanup() { 130 } 133 public void cleanupDefinedTypes() { 134 ownDecs = new ArrayList(); 135 } 136 137 private String sourceCanonicalPath = null; 138 public String getSourceCanonicalPath() { 139 if (sourceCanonicalPath != null) return sourceCanonicalPath; 140 File file = getSourceFile(); 141 if (file == null) return null; 142 143 try { 144 sourceCanonicalPath = file.getCanonicalPath(); 145 } catch (IOException ioe) {} 146 147 return sourceCanonicalPath; 148 } 149 150 private String sourceDirectory = null; 151 public String getSourceDirectory() { 152 if (sourceDirectory != null) return sourceDirectory; 153 154 sourceDirectory = new File (getSourceCanonicalPath()).getParent(); 155 156 return sourceDirectory; 157 } 158 159 160 public File getSourceFile() { return sourceInfo.getFile(); } 161 public int getLine(int position) { if (sourceInfo == null) return -1; return sourceInfo.getLine(position); } 162 public int getColumn(int position) { return sourceInfo.getColumn(position); } 163 164 public CompilationUnit getCompilationUnit() { 165 return this; 166 } 167 168 public void addTypesToTypeGraph() { 169 ScopeWalker walker = new ScopeWalker(getCompiler(), getScope()); 170 Decs decs = getDecs(); 171 for (int i=0, N=decs.size(); i<N; i++) { 172 TypeDec typeDec = (TypeDec)decs.get(i); 173 typeDec.addToTypeGraph(walker); 174 } 175 } 176 177 public void buildSignatures() { 178 ScopeWalker walker = new ScopeWalker(getCompiler(), getScope()); 179 Decs decs = getDecs(); 180 for (int i=0, N=decs.size(); i<N; i++) { 181 TypeDec typeDec = (TypeDec)decs.get(i); 182 typeDec.buildSignatures(walker); 183 } 184 } 185 186 187 public void checkSpec() { 188 Imports imports = getImports(); 189 Map importedTypeNames = new HashMap(); 190 for (int i=0,N=imports.size(); i<N; i++) { 191 Import imp = imports.get(i); 192 if (!imp.getStar()) { 193 Type t = imp.getType(); 194 Decs decs = getDecs(); 196 for (int j=0, M=decs.size(); j<M; j++) { 197 TypeDec typeDec = (TypeDec)decs.get(j); 198 String id = typeDec.getId(); 199 if (t.getId().equals(id) && !(t == typeDec.getType())) { 200 imp.showError("import conflicts with definition of " + 201 typeDec.getPrettyString()); 202 } 203 } 204 Type iType = (Type)importedTypeNames.get(t.getId()); 205 if (iType != null && iType != t) { 206 imp.showError("import conflicts with previous import of " + 207 iType.getPrettyString()); 208 } 209 importedTypeNames.put(t.getId(), t); 210 } 211 } 212 213 214 if (getOptions().strict && sourceInfo != null) { 216 Decs decs = getDecs(); 217 for (int i=0, N=decs.size(); i<N; i++) { 218 TypeDec typeDec = (TypeDec)decs.get(i); 219 if (typeDec.isPublic()) { 220 String id = typeDec.getId(); 221 String filename = sourceInfo.getFile().getName(); 222 if (!filename.startsWith(id)) { 224 this.showError("public class must be in file of same name, i.e " + 225 id + ".java"); 226 } 227 } 228 } 229 } 230 } 231 232 233 237 public void unparse(CodeWriter writer) { 238 if (getPackageName() != null) { 239 writer.writeKeyword("package"); 240 writer.requiredSpace(); 241 writer.write(getPackageName()); 242 writer.closeStmt(); 243 } 244 245 writer.writeChildren(imports); 246 writer.write(decs); 247 } 248 249 public final void generateBytecode(File outputDir) throws IOException { 252 for (Iterator i = getDefinedTypes().iterator(); i.hasNext(); ) { 253 TypeDec typeDec = (TypeDec) i.next(); 254 typeDec.generateBytecode(outputDir); 255 } 256 } 257 258 protected String packageName; 260 public String getPackageName() { return packageName; } 261 public void setPackageName(String _packageName) { packageName = _packageName; } 262 263 protected Imports imports; 264 public Imports getImports() { return imports; } 265 public void setImports(Imports _imports) { 266 if (_imports != null) _imports.setParent(this); 267 imports = _imports; 268 } 269 270 protected Decs decs; 271 public Decs getDecs() { return decs; } 272 public void setDecs(Decs _decs) { 273 if (_decs != null) _decs.setParent(this); 274 decs = _decs; 275 } 276 277 protected SourceInfo sourceInfo; 278 public SourceInfo getSourceInfo() { return sourceInfo; } 279 public void setSourceInfo(SourceInfo _sourceInfo) { sourceInfo = _sourceInfo; } 280 281 protected boolean scanned; 282 public boolean getScanned() { return scanned; } 283 public void setScanned(boolean _scanned) { scanned = _scanned; } 284 285 public CompilationUnit(SourceLocation location, String _packageName, Imports _imports, Decs _decs, SourceInfo _sourceInfo, boolean _scanned) { 286 super(location); 287 setPackageName(_packageName); 288 setImports(_imports); 289 setDecs(_decs); 290 setSourceInfo(_sourceInfo); 291 setScanned(_scanned); 292 } 293 protected CompilationUnit(SourceLocation source) { 294 super(source); 295 } 296 297 public ASTObject copyWalk(CopyWalker walker) { 298 CompilationUnit ret = new CompilationUnit(getSourceLocation()); 299 ret.preCopy(walker, this); 300 ret.packageName = packageName; 301 if (imports != null) ret.setImports( (Imports)walker.process(imports) ); 302 if (decs != null) ret.setDecs( (Decs)walker.process(decs) ); 303 ret.sourceInfo = sourceInfo; 304 ret.scanned = scanned; 305 return ret; 306 } 307 308 public ASTObject getChildAt(int childIndex) { 309 switch(childIndex) { 310 case 0: return imports; 311 case 1: return decs; 312 default: return super.getChildAt(childIndex); 313 } 314 } 315 public String getChildNameAt(int childIndex) { 316 switch(childIndex) { 317 case 0: return "imports"; 318 case 1: return "decs"; 319 default: return super.getChildNameAt(childIndex); 320 } 321 } 322 public void setChildAt(int childIndex, ASTObject child) { 323 switch(childIndex) { 324 case 0: setImports((Imports)child); return; 325 case 1: setDecs((Decs)child); return; 326 default: super.setChildAt(childIndex, child); return; 327 } 328 } 329 public int getChildCount() { 330 return 2; 331 } 332 333 public String getDefaultDisplayName() { 334 return "CompilationUnit(packageName: "+packageName+", "+"sourceInfo: "+sourceInfo+", "+"scanned: "+scanned+")"; 335 } 336 337 } 339 340 341 342 | Popular Tags |