KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > bytecode > ClassManager


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25 package org.aspectj.compiler.base.bytecode;
26
27 import org.aspectj.compiler.base.*;
28 import org.aspectj.compiler.base.ast.*;
29
30
31 import java.io.*;
32 import java.util.zip.*;
33 import java.util.StringTokenizer JavaDoc;
34
35 import java.util.*;
36
37 abstract public class ClassManager extends CompilerObject {
38     private ASTConnection astConnection = null;
39
40     public ClassManager(JavaCompiler compiler) {
41         super(compiler);
42     }
43
44     private ASTConnection getASTConnection() {
45         if (astConnection == null) {
46             astConnection = new ASTConnection(getCompiler());
47         }
48         return astConnection;
49     }
50
51     public abstract TypeDec findTypeDec(String JavaDoc name);
52     public abstract ClassManager makeSubPackageManager(String JavaDoc subPackageName);
53     
54     /**
55      * returns all possible type names visible to this class manager
56      *
57      * they are encoded as a List of elements (broken on '.' and '$')
58      */

59     public abstract Collection/*List<String>*/ getAllPossibleTypeNames();
60
61     public TypeDec makeTypeDec(String JavaDoc name, InputStream inputStream) {
62         //InputStream inputStream = classPathManager.findClass(name);
63
if (inputStream == null) return null;
64
65         try {
66             ClassFile classFile = new ClassFile();
67             try {
68                 DataInputStream dataStream =
69                         new DataInputStream(prereadInputStream(inputStream));
70                 classFile.readFrom(dataStream);
71             } catch (IOException e) {
72                 System.err.println("ioexception for: "+name+", "+inputStream);
73                 e.printStackTrace();
74                 return null;
75             } catch (InvalidClassFileException icfe) {
76                 System.err.println("bad class file for: "+name+", "+inputStream);
77                 icfe.printStackTrace();
78                 return null;
79             }
80
81             if (getCompiler().getOptionVerbose()) {
82                 getCompiler().showMessage("loading: "+name);
83             }
84
85             TypeDec typeDec = getASTConnection().makeTypeDec(classFile);
86             
87             return typeDec;
88         } finally {
89             try {
90                 inputStream.close();
91             } catch (IOException e) {}
92         }
93     }
94     
95     public Collection/*String*/ getPathStrings() {
96         Collection ret = new LinkedList();
97         ret.add(getPathString());
98         return ret;
99     }
100     
101     protected String JavaDoc getPathString() {
102         throw new UnsupportedOperationException JavaDoc("no simple string");
103     }
104         
105     
106     //!!! assume that classfiles are never larger than 500K here
107
//!!! should degrade gracefully if this is violated
108
private static byte[] bytes = new byte[500*1024];
109     private InputStream prereadInputStream(InputStream stream) throws IOException {
110         int index = 0;
111         while (true) {
112             int nread = stream.read(bytes, index, bytes.length-index);
113             if (nread == -1) break;
114             index = index+nread;
115         }
116         stream.close();
117
118         return new ByteArrayInputStream(bytes, 0, index);
119     }
120     
121     public List filenameToClassName(String JavaDoc filename) {
122         int last = 0;
123         List ret = new ArrayList();
124         for (int i=0, len = filename.length(); i < len; i++) {
125             char ch = filename.charAt(i);
126             if (ch == '.' || ch == '/' || ch == '\\' || ch == '$') {
127                 ret.add(filename.substring(last, i));
128                 last = i+1;
129             }
130         }
131         // The last .class doesn't get added to the list, and this is good
132

133         return ret;
134     }
135 }
136
Popular Tags