KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dynaop > util > Classes


1 package dynaop.util;
2
3 import java.util.*;
4 import java.lang.reflect.*;
5
6 /**
7  * Class utilities.
8  *
9  * @author Bob Lee (crazybob@crazybob.org)
10  */

11 public class Classes {
12
13     public static Method EQUALS_METHOD;
14     public static Method HASHCODE_METHOD;
15     public static Method TOSTRING_METHOD;
16
17     static {
18         try {
19             EQUALS_METHOD =
20                 Object JavaDoc.class.getMethod("equals", new Class JavaDoc[] { Object JavaDoc.class });
21             HASHCODE_METHOD =
22                 Object JavaDoc.class.getMethod("hashCode", null);
23             TOSTRING_METHOD =
24                 Object JavaDoc.class.getMethod("toString", null);
25         }
26         catch (Exception JavaDoc e) {
27             throw NestedException.wrap(e);
28         }
29     }
30     
31     public static Method[] OBJECT_METHODS = new Method[] { EQUALS_METHOD,
32             HASHCODE_METHOD, TOSTRING_METHOD };
33     public static Set OBJECT_METHODS_SET =
34             new HashSet(Arrays.asList(OBJECT_METHODS));
35     
36     private Classes() {}
37
38     /**
39      * Creates a new instance of clazz using default constructor and
40      * converting exceptions.
41      */

42     public static Object JavaDoc newInstance(Class JavaDoc clazz) {
43         try {
44             return clazz.newInstance();
45         }
46         catch (Exception JavaDoc e) {
47             throw NestedException.wrap(e);
48         }
49     }
50
51     /**
52      * Creates a new instance of clazz converting exceptions.
53      */

54     public static Object JavaDoc newInstance(Constructor constructor,
55             Object JavaDoc[] arguments) {
56         try {
57             return constructor.newInstance(arguments);
58         }
59         catch (Exception JavaDoc e) {
60             throw NestedException.wrap(e);
61         }
62     }
63
64     /**
65      * Returns true if clazz implements all of the interfaces.
66      */

67     public static boolean implementsInterfaces(Class JavaDoc clazz,
68             Class JavaDoc[] interfaces) {
69         for (int i = 0; i < interfaces.length; i++)
70             if (!interfaces[i].isAssignableFrom(clazz))
71                 return false;
72         return true;
73     }
74
75     /**
76      * Invokes method on target object with arguments. If target object is
77      * a dynamic proxy, this invokes the proxy's invocation handler directly.
78      */

79     public static Object JavaDoc invoke(Object JavaDoc target, Method method,
80             Object JavaDoc[] args) throws Throwable JavaDoc {
81         if (target instanceof Proxy) {
82             return Proxy.getInvocationHandler(target).
83                 invoke(target, method, args);
84         }
85         else {
86             try {
87                 return method.invoke(target, args);
88             }
89             catch (IllegalAccessException JavaDoc e) {
90                 throw NestedException.wrap(e);
91             }
92             catch (InvocationTargetException e) {
93                 throw NestedException.wrap(e.getTargetException());
94             }
95         }
96     }
97     
98     /**
99      * Gets the class's loader. If the class was loaded in the system loader,
100      * returns the system loader rather than <code>null</code>.
101      */

102     public static ClassLoader JavaDoc getClassLoader(Class JavaDoc clazz) {
103         ClassLoader JavaDoc loader = clazz.getClassLoader();
104         return (loader == null) ? ClassLoader.getSystemClassLoader() :
105             loader;
106     }
107
108     /**
109      * Gets the context class loader or the system class loader if the context
110      * if null.
111      */

112     public static ClassLoader JavaDoc getClassLoader() {
113         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
114         return (loader == null) ? ClassLoader.getSystemClassLoader() :
115             loader;
116     }
117     
118     /**
119      * Finds a ClassLoader for which all of the classes are visible.
120      */

121     public static ClassLoader JavaDoc commonLoader(Class JavaDoc[] classes) {
122         if (classes.length == 0) throw new IllegalArgumentException JavaDoc(
123             "No classes provided.");
124         if (classes.length == 1) return getClassLoader(classes[0]);
125         
126         // ick, I know. but, it does the trick...
127
for (int i = 1; i < classes.length; i++) {
128             try {
129                 ClassLoader JavaDoc loader = getClassLoader(classes[i]);
130                 for (int i2 = 0; i2 < classes.length; i2++) {
131                     if (i == i2)
132                         continue;
133                     loader.loadClass(classes[i2].getName());
134                 }
135                 return loader;
136             }
137             catch (ClassNotFoundException JavaDoc e) {}
138         }
139         
140         throw new RuntimeException JavaDoc("No common class loader: " +
141             Arrays.asList(classes));
142     }
143     
144     /**
145      * Finds a ClassLoader for which all of the classes are visible.
146      */

147     public static ClassLoader JavaDoc commonLoader(Collection classes) {
148         return commonLoader(
149             (Class JavaDoc[]) classes.toArray(new Class JavaDoc[classes.size()]));
150     }
151
152     /**
153      * Gets all interfaces implemented by a given type and its super types.
154      */

155     public static List getAllInterfacesAsList(Class JavaDoc clazz) {
156         List interfaces = null;
157         if (clazz.isInterface())
158             return Collections.singletonList(clazz);
159         else {
160             interfaces = new ArrayList();
161             addAllInterfaces(clazz, interfaces);
162             Collections.reverse(interfaces);
163             return interfaces;
164         }
165     }
166
167     /**
168      * Gets all interfaces implemented by a given type and its super types.
169      */

170     public static Class JavaDoc[] getAllInterfaces(Class JavaDoc clazz) {
171         return (Class JavaDoc[]) getAllInterfacesAsList(clazz).
172             toArray(new Class JavaDoc[0]);
173     }
174     
175     /**
176      * Adds all interfaces implemented by given type and its super types
177      * to list.
178      */

179     static void addAllInterfaces(Class JavaDoc clazz, List list) {
180         while (clazz != null && clazz != Object JavaDoc.class) {
181             Class JavaDoc[] interfaces = clazz.getInterfaces();
182             
183             // add in reverse order.
184
for (int i = interfaces.length - 1; i >= 0; i--)
185                 if (!list.contains(interfaces[i]))
186                     list.add(interfaces[i]);
187             clazz = clazz.getSuperclass();
188         }
189     }
190
191     static final Map primitiveMap = new HashMap();
192
193     static {
194         // populate primitive map.
195
primitiveMap.put("boolean", boolean.class);
196         primitiveMap.put("byte", byte.class);
197         primitiveMap.put("char", char.class);
198         primitiveMap.put("short", short.class);
199         primitiveMap.put("int", int.class);
200         primitiveMap.put("long", long.class);
201         primitiveMap.put("float", float.class);
202         primitiveMap.put("double", double.class);
203         primitiveMap.put("void", void.class);
204     }
205
206     /**
207     * Same as <code>Class.forName()</code>, except that
208     * it works for primitive types.
209     */

210     public static Class JavaDoc forName(String JavaDoc name) throws ClassNotFoundException JavaDoc {
211         Class JavaDoc clazz = (Class JavaDoc) primitiveMap.get(name);
212         if (clazz != null)
213             return clazz;
214         return getClassLoader().loadClass(name);
215     }
216 }
217
Popular Tags