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
33 package org.sat4j.sat;
34
35 import java.io.File;
36 import java.io.FilenameFilter;
37 import java.io.IOException;
38 import java.lang.reflect.Modifier;
39 import java.net.JarURLConnection;
40 import java.net.URL;
41 import java.util.Enumeration;
42 import java.util.HashSet;
43 import java.util.Set;
44 import java.util.Vector;
45 import java.util.jar.JarEntry;
46 import java.util.jar.JarFile;
47 import java.util.zip.ZipEntry;
48
49
50
51
52
53
54
55
56 public class RTSI {
57
58 public static Vector<String> alreadySeenPckges;
59
60 public static Vector<String> find(String tosubclassname) {
61 alreadySeenPckges = new Vector<String>();
62 Set<String> v = new HashSet<String>();
63 Set<String> tmp;
64 try {
65
66
67 Class<?> tosubclass = Class.forName(tosubclassname);
68 Package[] pcks = Package.getPackages();
69 for (Package pck : pcks) {
70 tmp = find(pck.getName(), tosubclass);
71 if (tmp != null) {
72 v.addAll(tmp);
73 }
74 }
75 } catch (ClassNotFoundException ex) {
76 System.err.println("Class " + tosubclassname + " not found!");
77 }
78 return new Vector<String>(v);
79 }
80
81 public static Set<String> find(String pckname, String tosubclassname) {
82 Set<String> v = new HashSet<String>();
83 try {
84 Class<?> tosubclass = Class.forName(tosubclassname);
85 v = find(pckname, tosubclass);
86 } catch (ClassNotFoundException ex) {
87 System.err.println("Class " + tosubclassname + " not found!");
88 }
89 return v;
90 }
91
92 public static Set<String> find(String pckgname, Class<?> tosubclass) {
93 if (alreadySeenPckges.contains(pckgname)) {
94 return new HashSet<String>();
95 } else {
96 alreadySeenPckges.add(pckgname);
97 return findnames(pckgname, tosubclass);
98 }
99 }
100
101 public static Set<String> findnames(String pckgname, Class<?> tosubclass) {
102 Set<String> v = new HashSet<String>();
103
104
105
106 String name = new String(pckgname);
107 if (!name.startsWith("/")) {
108 name = "/" + name;
109 }
110 name = name.replace('.', '/');
111
112
113 URL url = RTSI.class.getResource(name);
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132 if (url == null) {
133 return null;
134 }
135
136 File directory = new File(url.getFile());
137
138
139
140 if (directory.exists()) {
141
142 String[] files = directory.list();
143 for (String file : files) {
144
145
146 if (file.endsWith(".class")) {
147
148 String classname = file.substring(0, file.length() - 6);
149 try {
150
151 Class<?> o = Class.forName(pckgname + "." + classname);
152
153 if (tosubclass.isAssignableFrom(o) && !o.isInterface()
154 && !Modifier.isAbstract(o.getModifiers())) {
155
156 v.add(classname);
157 }
158 } catch (NoClassDefFoundError cnfex) {
159
160
161 } catch (ClassNotFoundException cnfex) {
162 System.err.println(cnfex);
163 }
164
165
166
167
168
169
170
171 }
172 }
173 File[] dirs = directory.listFiles(new FilenameFilter() {
174 public boolean accept(File dir, String name) {
175 return new File(dir.getAbsolutePath() + "/" + name)
176 .isDirectory();
177 }
178 });
179 Set<String> tmp;
180 for (File dir : dirs) {
181 String newName = pckgname + "." + dir.getName();
182 tmp = find(newName, tosubclass);
183 if (tmp != null) {
184 v.addAll(tmp);
185 }
186
187 }
188
189 } else {
190 try {
191
192
193 JarURLConnection conn = (JarURLConnection) url.openConnection();
194 String starts = conn.getEntryName();
195 JarFile jfile = conn.getJarFile();
196 Enumeration<JarEntry> e = jfile.entries();
197 while (e.hasMoreElements()) {
198 ZipEntry entry = e.nextElement();
199 String entryname = entry.getName();
200 if (entryname.startsWith(starts)
201
202 && entryname.endsWith(".class")) {
203 String classname = entryname.substring(0,
204 entryname.length() - 6);
205
206 if (classname.startsWith("/")) {
207 classname = classname.substring(1);
208 }
209 classname = classname.replace('/', '.');
210 try {
211
212
213
214
215
216 Class<?> o = Class.forName(classname);
217
218 if (tosubclass.isAssignableFrom(o)
219 && !o.isInterface()
220 && !Modifier.isAbstract(o.getModifiers())) {
221
222 v.add(classname.substring(classname
223 .lastIndexOf('.') + 1));
224 }
225 } catch (NoClassDefFoundError cnfex) {
226
227
228 } catch (ClassNotFoundException cnfex) {
229 System.err.print(cnfex);
230 }
231
232
233
234
235
236
237
238 }
239 }
240 } catch (IOException ioex) {
241 System.err.println(ioex);
242 }
243 }
244
245 return v;
246 }
247
248 public static void displayResultOfFind(String tosubclassname) {
249 System.out.println(find(tosubclassname));
250 }
251
252 public static void displayResultOfFind(String pckname, String tosubclassname) {
253 System.out.println(find(pckname, tosubclassname));
254 }
255
256 public static void displayResultOfFind(String pckgname, Class<?> tosubclass) {
257 System.out.println(findnames(pckgname, tosubclass));
258
259 }
260
261 public static void main(String[] args) {
262 if (args.length == 2) {
263 displayResultOfFind(args[0], args[1]);
264 } else {
265 if (args.length == 1) {
266 displayResultOfFind(args[0]);
267 } else {
268 System.out.println("Usage: java RTSI [<package>] <subclass>");
269 }
270 }
271 }
272 }