01: /* ====================================================================
02: The Jicarilla Software License
03:
04: Copyright (c) 2003 Leo Simons.
05: All rights reserved.
06:
07: Permission is hereby granted, free of charge, to any person obtaining
08: a copy of this software and associated documentation files (the
09: "Software"), to deal in the Software without restriction, including
10: without limitation the rights to use, copy, modify, merge, publish,
11: distribute, sublicense, and/or sell copies of the Software, and to
12: permit persons to whom the Software is furnished to do so, subject to
13: the following conditions:
14:
15: The above copyright notice and this permission notice shall be
16: included in all copies or substantial portions of the Software.
17:
18: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25: ==================================================================== */
26: package org.jicarilla.container.util;
27:
28: import org.jicarilla.lang.Assert;
29:
30: import java.util.ArrayList;
31: import java.util.List;
32:
33: /**
34: * Utility methods that encapsulate reflection magic.
35: *
36: * @author <a href="mail at leosimons dot com">Leo Simons</a>
37: * @version $Id: ReflectionUtil.java,v 1.3 2004/03/23 13:37:52 lsimons Exp $
38: */
39: public class ReflectionUtil {
40: public static Class loadClass(final String name)
41: throws ClassNotFoundException {
42: Assert.assertNotNull("name argument may not be null", name);
43:
44: return Thread.currentThread().getContextClassLoader()
45: .loadClass(name);
46: }
47:
48: public static Class[] getAllInterfaces(final Class clazz) {
49: Assert.assertNotNull("clazz argument may not be null", clazz);
50:
51: final List alreadyFound = new ArrayList();
52: if (clazz.isInterface())
53: alreadyFound.add(clazz);
54:
55: return (Class[]) getAllSuperInterfaces(clazz, alreadyFound)
56: .toArray(new Class[0]);
57: }
58:
59: protected static List getAllSuperInterfaces(final Class clazz,
60: final List alreadyFound) {
61: // done
62: if (clazz.equals(Object.class))
63: return alreadyFound;
64:
65: // add all interfaces implemented
66: final Class[] interfaces = clazz.getInterfaces();
67: for (int i = 0; i < interfaces.length; i++) {
68: final Class anInterface = interfaces[i];
69: if (alreadyFound.contains(anInterface))
70: continue;
71:
72: alreadyFound.add(anInterface);
73:
74: // add all superinterfaces implemented
75: getAllSuperInterfaces(anInterface, alreadyFound);
76: }
77:
78: // add all interfaces implemented by superclasses
79: final Class superClass = clazz.getSuperclass();
80: if (superClass != null && !superClass.isInterface())
81: getAllSuperInterfaces(superClass, alreadyFound);
82:
83: return alreadyFound;
84: }
85: }
|