KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.*;
31 import java.util.zip.*;
32 import java.util.StringTokenizer JavaDoc;
33
34 import java.util.*;
35
36 public class ClassPathManager extends ClassManager {
37     List/*ClassManager*/ classManagers;
38
39     private ClassPathManager(JavaCompiler compiler, List classManagers) {
40         super(compiler);
41         this.classManagers = classManagers;
42     }
43
44     public ClassPathManager(JavaCompiler compiler, String JavaDoc classpath,
45                             String JavaDoc bootclasspath, String JavaDoc extdirs) {
46         super(compiler);
47         //this.astConnection = new ASTConnection(compiler);
48
// Build up the appropriate ClassManagers for this classpath
49
List managers = new LinkedList();
50
51         if (bootclasspath == null) {
52             bootclasspath = System.getProperty("sun.boot.class.path", "");
53         }
54         addClasspathManagers(managers, bootclasspath);
55
56         if (extdirs == null) {
57             extdirs = System.getProperty("java.ext.dirs", "");
58         }
59         addExtensionManagers(managers, extdirs);
60
61         if (classpath == null) {
62             classpath = System.getProperty("java.class.path", "");
63         }
64         addClasspathManagers(managers, classpath);
65
66         classManagers = managers;
67     }
68
69     protected void addClasspathManagers(List managers, String JavaDoc classpath) {
70         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(classpath, java.io.File.pathSeparator);
71         while (tok.hasMoreTokens()) {
72             String JavaDoc entry = tok.nextToken().trim();
73             //System.out.println("entry: " + entry);
74

75             if (entry.endsWith(".jar") || entry.endsWith(".zip")) {
76                 managers.add(new JarClassManager(getCompiler(), new File(entry)));
77             } else {
78                 managers.add(new DirectoryClassManager(getCompiler(), new File(entry)));
79             }
80         }
81     }
82
83
84     protected void addExtensionManagers(List managers, String JavaDoc extdirs) {
85         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(extdirs, java.io.File.pathSeparator);
86         while (tok.hasMoreTokens()) {
87             String JavaDoc entry = tok.nextToken().trim();
88             addExtensionDirManagers(managers, new File(entry));
89         }
90     }
91
92     protected void addExtensionDirManagers(List managers, File extdir) {
93         if (!extdir.isDirectory()) return;
94
95         String JavaDoc[] names = extdir.list();
96         if (names == null) return;
97
98         for(int i=0; i<names.length; i++) {
99             // choose to skip all files that don't end in .jar or .zip
100
if (!names[i].endsWith(".jar") && !names[i].endsWith(".zip")) {
101                 continue;
102             }
103
104             File file = new File(extdir, names[i]);
105             if (file.isFile()) {
106                 managers.add(new JarClassManager(getCompiler(), file));
107             }
108         }
109     }
110
111     private String JavaDoc join(String JavaDoc[] names) {
112         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
113         buf.append(names[0]);
114         for(int i=1; i<names.length; i++) {
115             buf.append('.');
116             buf.append(names[i]);
117         }
118         return buf.toString();
119     }
120
121     public TypeDec findTypeDec(String JavaDoc name) {
122         for (Iterator i = classManagers.iterator(); i.hasNext(); ) {
123             ClassManager cm = (ClassManager)i.next();
124             TypeDec ret = cm.findTypeDec(name);
125             if (ret != null) return ret;
126         }
127         return null;
128     }
129
130     public ClassManager makeSubPackageManager(String JavaDoc subPackageName) {
131         List ret = new LinkedList();
132 // System.out.println("total: " + Runtime.getRuntime().totalMemory() +
133
// ", free: " + Runtime.getRuntime().freeMemory());
134
for (Iterator i = classManagers.iterator(); i.hasNext(); ) {
135             ClassManager cm = (ClassManager)i.next();
136             ClassManager newCM = cm.makeSubPackageManager(subPackageName);
137             if (newCM != null) ret.add(newCM);
138         }
139 // System.out.println("total: " + Runtime.getRuntime().totalMemory() +
140
// ", free: " + Runtime.getRuntime().freeMemory());
141
// getCompiler().showMessage("new cpm: " + ret);
142
return new ClassPathManager(getCompiler(), ret);
143     }
144
145     public Collection/*List<String>*/ getAllPossibleTypeNames() {
146         Collection ret = new ArrayList();
147         for (Iterator i = classManagers.iterator(); i.hasNext(); ) {
148             ClassManager cm = (ClassManager)i.next();
149             ret.addAll(cm.getAllPossibleTypeNames());
150         }
151         return ret;
152     }
153     
154     public Collection/*String*/ getPathStrings() {
155         Collection ret = new ArrayList();
156         for (Iterator i = classManagers.iterator(); i.hasNext(); ) {
157             ClassManager cm = (ClassManager)i.next();
158             ret.addAll(cm.getPathStrings());
159         }
160         return ret;
161     }
162         
163 }
164
165
166 class DirectoryClassManager extends ClassManager {
167     File directory;
168     public DirectoryClassManager(JavaCompiler compiler, File directory) {
169         super(compiler);
170         this.directory = directory;
171     }
172
173     public ClassManager makeSubPackageManager(String JavaDoc subPackageName) {
174         File dir = new File(directory, subPackageName);
175         if (!dir.isDirectory()) return null;
176         return new DirectoryClassManager(getCompiler(), dir);
177     }
178
179     public TypeDec findTypeDec(String JavaDoc name) {
180         File testFile = new File(directory, name+".class");
181         // special work-around for the strange handling of 'nul.class'
182
// on Windows
183
if (name.equals("nul")) return null;
184         if (testFile.isFile()) {
185             try {
186                 //XXX add SourceLocation to this
187
return makeTypeDec(name, new FileInputStream(testFile));
188             } catch (FileNotFoundException e) {
189                 return null;
190             }
191         } else {
192             return null;
193         }
194     }
195     public Collection/*List<String>*/ getAllPossibleTypeNames() {
196         Collection temp = new ArrayList();
197         collectNames(directory, Collections.EMPTY_LIST, temp);
198         //System.err.println("XXXX: classPathManager");
199
//System.err.println(temp);
200
return temp;
201     }
202     
203     private void collectNames(File directory, List prefixName, Collection names) {
204         File[] files = directory.listFiles();
205         if (files == null) return;
206         for (int i = files.length - 1; i >= 0; i--) {
207             File f = files[i];
208             if (f.isDirectory()) {
209                 collectNames(f, newList(prefixName, f.getName()), names);
210             } else if (isClassFile(f)) {
211                 names.add(newList(prefixName, filenameToClassName(f.getName())));
212             }
213         }
214     }
215     
216     private List newList(List left, Object JavaDoc right) {
217         ArrayList temp = new ArrayList(left);
218         temp.add(right);
219         return temp;
220     }
221     
222     private List newList(List left, List right) {
223         ArrayList temp = new ArrayList(left);
224         temp.addAll(right);
225         return temp;
226     }
227     
228     private boolean isClassFile(File f) {
229         return f.isFile() && f.getName().toLowerCase().endsWith(".class");
230     }
231     
232     public String JavaDoc toString() { return "dir(" + directory + ")"; }
233     
234     protected String JavaDoc getPathString() {
235         return directory.toString();
236     }
237 }
238     
239
240
241 class JarClassManager extends ClassManager {
242     File origFile;
243     ZipFile zipFile;
244     String JavaDoc prefix = null;
245
246     private JarClassManager(JavaCompiler compiler, ZipFile zipFile, String JavaDoc prefix) {
247         super(compiler);
248         this.zipFile = zipFile;
249         this.prefix = prefix;
250     }
251
252     public JarClassManager(JavaCompiler compiler, File jarFile) {
253         super(compiler);
254         origFile = jarFile;
255         //System.out.println("checking: "+jarFile);
256
if (jarFile.isFile()) {
257             //System.out.println("is file");
258
try {
259                 zipFile = new ZipFile(jarFile);
260             } catch (IOException e) {
261                 zipFile = null;
262             }
263         } else {
264             zipFile = null;
265         }
266     }
267
268     public ClassManager makeSubPackageManager(String JavaDoc subPackageName) {
269         if (zipFile == null) return null;
270
271         String JavaDoc newPrefix;
272         if (prefix == null) newPrefix = subPackageName + '/';
273         else newPrefix = prefix + subPackageName + '/';
274         
275         //XXX for performance, we'd really like a test of "possibility" here
276

277         JarClassManager ret = new JarClassManager(getCompiler(), zipFile, newPrefix);
278         return ret;
279     }
280
281     public TypeDec findTypeDec(String JavaDoc name) {
282         if (zipFile == null) return null;
283
284         String JavaDoc fileName = prefix + name + ".class";
285
286         ZipEntry entry = zipFile.getEntry(fileName);
287         if (entry == null) return null;
288
289         try {
290             return makeTypeDec(name, zipFile.getInputStream(entry));
291         } catch (IOException e) {
292             return null;
293         }
294     }
295     
296     public Collection/*List<String>*/ getAllPossibleTypeNames() {
297         Collection ret = new ArrayList();
298         if (zipFile == null) return ret;
299         
300         for (Enumeration i = zipFile.entries(); i.hasMoreElements(); ) {
301             ZipEntry entry = (ZipEntry)i.nextElement();
302             if (entry.isDirectory()) continue;
303             String JavaDoc name = entry.getName();
304             if (name.toLowerCase().endsWith(".class")) {
305                 ret.add(filenameToClassName(name));
306             }
307         }
308         return ret;
309     }
310     
311     public String JavaDoc toString() { return "jar(" + zipFile.getName() + ")"; }
312     
313     protected String JavaDoc getPathString() {
314         if (zipFile == null) {
315             return origFile.toString() + "<not found>";
316         } else {
317             return origFile.toString();
318         }
319     }
320 }
321
Popular Tags