1 package org.apache.fulcrum.yaafi.framework.reflection;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.lang.reflect.Constructor;
23 import java.lang.reflect.InvocationTargetException;
24 import java.lang.reflect.Method;
25 import java.util.ArrayList;
26 import java.util.Iterator;
27 import java.util.List;
28
29 /**
30 * Helper clazz to do a little bit of reflection magic.
31 *
32 * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
33 */
34
35 public class Clazz
36 {
37 /**
38 * Determine if the class can be loaded.
39 *
40 * @param classLoader the classloader to be used
41 * @param clazzName the name of the class to be loaded
42 * @return true if the class was found
43 */
44 public static boolean hasClazz( ClassLoader classLoader, String clazzName )
45 {
46 try
47 {
48 classLoader.loadClass( clazzName );
49 return true;
50 }
51 catch (ClassNotFoundException e)
52 {
53 return false;
54 }
55 }
56
57 /**
58 * Loads a class with the given name.
59 * @param classLoader the class loader to be used
60 * @param clazzName the name of the clazz to be loaded
61 * @return the loaded class
62 * @throws ClassNotFoundException the class was nout found
63 */
64 public static Class getClazz( ClassLoader classLoader, String clazzName )
65 throws ClassNotFoundException
66 {
67 return classLoader.loadClass( clazzName );
68 }
69
70 /**
71 * Creates a new instance of the class
72 * @param clazz the class to be instantiated
73 * @param signature the signature of the constructor
74 * @param args the arguments to be passed
75 * @return the newly created instance
76 * @throws NoSuchMethodException the method was not found
77 * @throws InvocationTargetException an exception was thrown in the constructor
78 * @throws InstantiationException the target class could not be instantiated
79 * @throws IllegalAccessException an field couldn't be accessed
80 */
81 public static Object newInstance( Class clazz, Class[] signature, Object[] args )
82 throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException
83 {
84 Object result = null;
85 Constructor constructor = clazz.getConstructor( signature );
86 result = constructor.newInstance( args );
87 return result;
88 }
89
90 /**
91 * Invokes a given method on the instance.
92 * @param instance the instance
93 * @param methodName the name of the method to be invoked
94 * @param signature the signature of the method
95 * @param args the arguments for the method invocation
96 * @return the result of the method invocation
97 * @throws NoSuchMethodException the method was not found
98 * @throws InvocationTargetException an exception was thrown in the constructor
99 * @throws IllegalAccessException an field couldn't be accessed
100 */
101 public static Object invoke( Object instance, String methodName, Class[] signature, Object[] args )
102 throws NoSuchMethodException, InvocationTargetException, IllegalAccessException
103 {
104 Object result = null;
105 Method method = instance.getClass().getMethod( methodName, signature );
106 result = method.invoke( instance, args );
107 return result;
108 }
109
110 /**
111 * Invokes a static method on a class.
112 * @param clazz the class instance to work on
113 * @param methodName the name of the method to be invoked
114 * @param signature the signature of the method
115 * @param args the arguments for the method invocation
116 * @return the result of the method invocation
117 * @throws NoSuchMethodException the method was not found
118 * @throws InvocationTargetException an exception was thrown in the constructor
119 * @throws IllegalAccessException an field couldn't be accessed
120 */
121
122 public static Object invoke( Class clazz, String methodName, Class[] signature, Object[] args )
123 throws NoSuchMethodException, InvocationTargetException, IllegalAccessException
124 {
125 Object result = null;
126 Method method = clazz.getMethod( methodName, signature );
127 result = method.invoke( null, args );
128 return result;
129 }
130
131 /**
132 * <p>Gets a <code>List</code> of all interfaces implemented by the given
133 * class and its superclasses.</p>
134 *
135 * <p>The order is determined by looking through each interface in turn as
136 * declared in the source file and following its hierarchy up. Then each
137 * superclass is considered in the same way. Later duplicates are ignored,
138 * so the order is maintained.</p>
139 *
140 * @param cls the class to look up, may be <code>null</code>
141 * @return the <code>List</code> of interfaces in order,
142 * <code>null</code> if null input
143 */
144 public static List getAllInterfaces(Class cls)
145 {
146 if (cls == null)
147 {
148 return null;
149 }
150 List list = new ArrayList();
151 while (cls != null)
152 {
153 Class [] interfaces = cls.getInterfaces();
154 for (int i = 0; i < interfaces.length; i++)
155 {
156 if (list.contains( interfaces[i] ) == false)
157 {
158 list.add( interfaces[i] );
159 }
160 List superInterfaces = getAllInterfaces( interfaces[i] );
161 for (Iterator it = superInterfaces.iterator(); it.hasNext();)
162 {
163 Class intface = (Class) it.next();
164 if (list.contains( intface ) == false)
165 {
166 list.add( intface );
167 }
168 }
169 }
170 cls = cls.getSuperclass();
171 }
172 return list;
173 }
174 }