KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > types > reflect > ClassPathLoader


1 package polyglot.types.reflect;
2
3 import polyglot.main.Report;
4
5 import java.io.*;
6 import java.util.*;
7
8 /**
9  * We implement our own class loader. All this pain is so
10  * we can define the classpath on the command line.
11  */

12 public class ClassPathLoader
13 {
14     List classpath;
15     ClassFileLoader loader;
16
17     public ClassPathLoader(List classpath, ClassFileLoader loader) {
18         this.classpath = new ArrayList(classpath);
19         this.loader = loader;
20     }
21
22     public ClassPathLoader(String JavaDoc classpath, ClassFileLoader loader) {
23         this.classpath = new ArrayList();
24
25         StringTokenizer st = new StringTokenizer(classpath, File.pathSeparator);
26
27         while (st.hasMoreTokens()) {
28             String JavaDoc s = st.nextToken();
29             this.classpath.add(new File(s));
30         }
31
32         this.loader = loader;
33     }
34
35     public String JavaDoc classpath() {
36         return classpath.toString();
37     }
38
39     public boolean packageExists(String JavaDoc name) {
40     for (Iterator i = classpath.iterator(); i.hasNext(); ) {
41         File dir = (File) i.next();
42             if (loader.packageExists(dir, name)) {
43                 return true;
44             }
45         }
46
47         return false;
48     }
49
50     /**
51      * Load a class from the classpath. If the class is not found, then
52      * <code>null</code> is returned.
53      */

54     public ClassFile loadClass(String JavaDoc name) {
55         if (Report.should_report(verbose, 2)) {
56         Report.report(2, "attempting to load class " + name);
57         Report.report(2, "classpath = " + classpath);
58     }
59
60     for (Iterator i = classpath.iterator(); i.hasNext(); ) {
61         File dir = (File) i.next();
62             ClassFile cf = loader.loadClass(dir, name);
63             if (cf != null) {
64                 return cf;
65             }
66         }
67
68         return null;
69     }
70
71     static Collection verbose;
72
73     static {
74         verbose = new HashSet();
75         verbose.add("loader");
76     }
77 }
78
Popular Tags