01: /*
02: * User: Michael Rettig
03: * Date: Aug 14, 2002
04: * Time: 3:05:57 PM
05: */
06: package net.sourceforge.jaxor.tests;
07:
08: import net.sourceforge.jaxor.util.SystemException;
09:
10: import java.lang.reflect.Field;
11: import java.util.ArrayList;
12: import java.util.Iterator;
13: import java.util.List;
14: import java.util.Vector;
15:
16: /**
17: * Hackity Hack Hack Hack. This groovy little number rips the class Vector out of
18: * Class Loader so we can see what classes are loaded. Fun. Fun. Fun.
19: */
20: public class ClassLoaderHack {
21:
22: public static List findLoadedClasses(String pack) {
23: return findLoadedClasses(new String[] { pack });
24: }
25:
26: public static List findLoadedClasses(String[] packagePrefix) {
27: Field field = getField();
28: List allClasses = getValue(field);
29: List filtered = new ArrayList();
30: for (Iterator iterator = allClasses.iterator(); iterator
31: .hasNext();) {
32: Class aClass = (Class) iterator.next();
33: addByPackage(packagePrefix, aClass, filtered);
34: }
35: return filtered;
36: }
37:
38: private static void addByPackage(String[] packagePrefix,
39: Class aClass, List filtered) {
40: for (int i = 0; i < packagePrefix.length; i++) {
41: String s = packagePrefix[i];
42: if (aClass.getName().startsWith(s)) {
43: filtered.add(aClass.getName());
44: return;
45: }
46: }
47: }
48:
49: private static Vector getValue(Field field) {
50: try {
51: return (Vector) field.get(ClassLoaderHack.class
52: .getClassLoader());
53: } catch (IllegalArgumentException e) {
54: throw new SystemException("Failed to get field:classes", e);
55: } catch (IllegalAccessException e) {
56: throw new SystemException("Failed to get field:classes", e);
57: }
58: }
59:
60: private static Field getField() {
61: try {
62: Field field = ClassLoader.class.getDeclaredField("classes");
63: field.setAccessible(true);
64: return field;
65: } catch (NoSuchFieldException e) {
66: throw new SystemException("Failed to get field:classes", e);
67: } catch (SecurityException e) {
68: throw new SystemException("Failed to get field:classes", e);
69: }
70: }
71: }
|