1 package org.opentrafficsim.core;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.lang.reflect.Field;
6 import java.lang.reflect.Modifier;
7 import java.util.ArrayList;
8 import java.util.Collection;
9 import java.util.Enumeration;
10 import java.util.regex.Pattern;
11 import java.util.zip.ZipEntry;
12 import java.util.zip.ZipException;
13 import java.util.zip.ZipFile;
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 public final class ClassList
30 {
31
32
33
34 private ClassList()
35 {
36
37 }
38
39
40
41
42
43
44
45 public static Collection<String> getResources(final Pattern pattern)
46 {
47 final ArrayList<String> retval = new ArrayList<String>();
48 final String classPath = System.getProperty("java.class.path", ".");
49 final String[] classPathElements = classPath.split(System.getProperty("path.separator"));
50 for (final String element : classPathElements)
51 {
52 retval.addAll(getResources(element, pattern));
53 }
54 return retval;
55 }
56
57
58
59
60
61
62
63 private static Collection<String> getResources(final String element, final Pattern pattern)
64 {
65 final ArrayList<String> retval = new ArrayList<String>();
66 final File file = new File(element);
67 if (file.isDirectory())
68 {
69 retval.addAll(getResourcesFromDirectory(file, pattern));
70 }
71 else
72 {
73 retval.addAll(getResourcesFromJarFile(file, pattern));
74 }
75 return retval;
76 }
77
78
79
80
81
82
83
84 private static Collection<String> getResourcesFromJarFile(final File file, final Pattern pattern)
85 {
86 final ArrayList<String> retval = new ArrayList<String>();
87 ZipFile zf;
88 try
89 {
90 zf = new ZipFile(file);
91 }
92 catch (final ZipException e)
93 {
94 throw new Error(e);
95 }
96 catch (final IOException e)
97 {
98 throw new Error(e);
99 }
100 final Enumeration<?> e = zf.entries();
101 while (e.hasMoreElements())
102 {
103 final ZipEntry ze = (ZipEntry) e.nextElement();
104 final String fileName = ze.getName();
105 final boolean accept = pattern.matcher(fileName).matches();
106 if (accept)
107 {
108 retval.add(fileName);
109 }
110 }
111 try
112 {
113 zf.close();
114 }
115 catch (final IOException e1)
116 {
117 throw new Error(e1);
118 }
119 return retval;
120 }
121
122
123
124
125
126
127
128 private static Collection<String> getResourcesFromDirectory(final File directory, final Pattern pattern)
129 {
130 final ArrayList<String> retval = new ArrayList<String>();
131 final File[] fileList = directory.listFiles();
132 if (null == fileList)
133 {
134 throw new Error("Could not list files");
135 }
136 for (final File file : fileList)
137 {
138 if (file.isDirectory())
139 {
140 retval.addAll(getResourcesFromDirectory(file, pattern));
141 }
142 else
143 {
144 try
145 {
146 final String fileName = file.getCanonicalPath();
147 final boolean accept = pattern.matcher(fileName).matches();
148 if (accept)
149 {
150 retval.add(fileName);
151 }
152 }
153 catch (final IOException e)
154 {
155 throw new Error(e);
156 }
157 }
158 }
159 return retval;
160 }
161
162
163
164
165
166
167
168 public static Collection<Class<?>> classList(final String packageRoot, final boolean excludeInterfaces)
169 {
170 Collection<String> classList = ClassList.getResources(Pattern.compile(".*[^-]classes." + packageRoot + ".*\\.class"));
171 Collection<Class<?>> result = new ArrayList<Class<?>>();
172 for (String className : classList)
173 {
174 int pos = className.indexOf("\\org\\");
175 if (pos >= 0)
176 {
177 className = className.substring(pos + 1);
178 }
179 className = className.replaceAll("\\\\", ".");
180 pos = className.lastIndexOf(".class");
181 if (pos >= 0)
182 {
183 className = className.substring(0, pos);
184 }
185 if (className.endsWith("package-info"))
186 {
187 continue;
188 }
189
190 try
191 {
192 Class<?> c = Class.forName(className);
193
194 boolean exclude = false;
195 if (excludeInterfaces)
196 {
197 for (String modifierString : Modifier.toString(c.getModifiers()).split(" "))
198 {
199 if (modifierString.equals("interface"))
200 {
201
202 exclude = true;
203 continue;
204 }
205 }
206 }
207 if (!exclude)
208 {
209 result.add(c);
210 }
211 }
212 catch (ClassNotFoundException exception)
213 {
214 exception.printStackTrace();
215 }
216 }
217 return result;
218 }
219
220
221
222
223
224
225 public static boolean isAnonymousInnerClass(final Class<?> c)
226 {
227 String className = c.getName();
228 int pos = className.lastIndexOf("$");
229 if (pos > 0)
230 {
231 while (++pos < className.length())
232 {
233 if (!Character.isDigit(className.charAt(pos)))
234 {
235 break;
236 }
237 }
238 if (pos >= className.length())
239 {
240 return true;
241 }
242 }
243 return false;
244 }
245
246
247
248
249
250
251 public static boolean hasNonStaticFields(final Class<?> c)
252 {
253 for (Field f : c.getDeclaredFields())
254 {
255
256 if (!Modifier.isStatic(f.getModifiers()))
257 {
258 return true;
259 }
260 }
261 if (c.equals(Object.class))
262 {
263 return false;
264 }
265 return hasNonStaticFields(c.getSuperclass());
266 }
267
268
269
270
271
272 public static void main(final String[] args)
273 {
274 Pattern pattern;
275 if (args.length < 1)
276 {
277 pattern = Pattern.compile(".*[^-]classes.org.opentrafficsim.*\\.class");
278 }
279 else
280 {
281 pattern = Pattern.compile(args[0]);
282 }
283 final Collection<String> list = ClassList.getResources(pattern);
284 for (final String name : list)
285 {
286 System.out.println(name);
287 }
288 }
289
290 }