1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package com.generationjava.util;
33
34 import java.io.File;
35
36 import java.util.Collection;
37 import java.util.HashMap;
38
39 import com.generationjava.collections.FQMap;
40
41 import com.generationjava.io.find.Finder;
42 import com.generationjava.io.find.FileFinder;
43 import com.generationjava.io.find.ZipFinder;
44 import com.generationjava.io.find.FindListener;
45 import com.generationjava.io.find.FindEvent;
46
47 /***
48 * Indexes a class path.
49 */
50 public class ClassIndex implements FindListener {
51
52 private FQMap map = null;
53
54 /***
55 * Create an index of the runtime classpath.
56 */
57 public ClassIndex() {
58 this(System.getProperty("java.class.path"));
59 }
60
61 /***
62 * Creates an index of the given classpath.
63 */
64 public ClassIndex(String classpath) {
65 map = new FQMap();
66 int idx = -1;
67 int current = 0;
68 while( (idx = classpath.indexOf(File.pathSeparatorChar, idx)) != -1 ) {
69 String path = classpath.substring(current, idx);
70
71 File file = new File(path);
72 Finder finder = null;
73 if(file.isDirectory()) {
74
75 finder = new FileFinder();
76 } else {
77
78
79 finder = new ZipFinder();
80 }
81 finder.addFindListener(this);
82 HashMap options = new HashMap();
83 options.put(Finder.NAME, "class");
84 finder.find(file, options);
85
86 idx++;
87 current = idx;
88 }
89
90 }
91
92 /***
93 * Get the package that a given classname is in.
94 */
95 public String getPackage(String classname) {
96 return (String)map.get(classname);
97 }
98
99 public Collection getRootClasses() {
100 return getClassesIn("");
101 }
102 public Collection getClassesIn(String packagename) {/package-summary.html">ong> Collection getClassesIn(String packagename) {
103 FQMap tmp = map;
104 if(!"".equals(packagename)) {
105 tmp = (FQMap)map.get(packagename);
106 }
107 Collection[] colls = tmp.getSeparatedValues();
108 return colls[1];
109 }
110
111 public Collection getRootPackages() {
112 return getPackagesIn("");
113 }
114 public Collection getPackagesIn(String packagename) {/package-summary.html">ong> Collection getPackagesIn(String packagename) {
115 FQMap tmp = map;
116 if(!"".equals(packagename)) {
117 tmp = (FQMap)map.get(packagename);
118 }
119 Collection[] colls = tmp.getSeparatedValues();
120 return colls[0];
121 }
122
123 public void directoryStarted(FindEvent findEvent) {
124
125 }
126
127 public void directoryFinished(FindEvent findEvent) {
128
129 }
130
131 public void fileFound(FindEvent findEvent) {
132 File file = findEvent.getFile();
133 String filename = file.getName();
134
135
136 if(filename.indexOf("$") != -1) {
137 return;
138 }
139
140 filename = filename.substring(0,filename.length()-6);
141
142 if(map.get(filename) != null) {
143
144 return;
145 }
146
147 String pck = findEvent.getDirectory().getPath();
148 pck = pck.replace('/','.');
149 while(pck.startsWith(".")) {
150 pck = pck.substring(1);
151 }
152
153 map.put(filename, pck);
154 }
155
156 }
157