2
|
1 |
/*
|
|
2 |
* Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
package java.beans;
|
|
26 |
|
|
27 |
import java.lang.reflect.Constructor;
|
|
28 |
import java.lang.reflect.Field;
|
|
29 |
import java.lang.reflect.Method;
|
|
30 |
import java.lang.reflect.Modifier;
|
|
31 |
|
|
32 |
import java.lang.ref.Reference;
|
|
33 |
import java.lang.ref.SoftReference;
|
|
34 |
|
|
35 |
import java.util.*;
|
|
36 |
|
|
37 |
import com.sun.beans.ObjectHandler;
|
|
38 |
import sun.reflect.misc.MethodUtil;
|
|
39 |
import sun.reflect.misc.ConstructorUtil;
|
|
40 |
import sun.reflect.misc.ReflectUtil;
|
|
41 |
|
|
42 |
/**
|
|
43 |
* A utility class for reflectively finding methods, constuctors and fields
|
|
44 |
* using reflection.
|
|
45 |
*/
|
|
46 |
class ReflectionUtils {
|
|
47 |
|
|
48 |
private static Reference methodCacheRef;
|
|
49 |
|
|
50 |
public static Class typeToClass(Class type) {
|
|
51 |
return type.isPrimitive() ? ObjectHandler.typeNameToClass(type.getName()) : type;
|
|
52 |
}
|
|
53 |
|
|
54 |
public static boolean isPrimitive(Class type) {
|
|
55 |
return primitiveTypeFor(type) != null;
|
|
56 |
}
|
|
57 |
|
|
58 |
public static Class primitiveTypeFor(Class wrapper) {
|
|
59 |
if (wrapper == Boolean.class) return Boolean.TYPE;
|
|
60 |
if (wrapper == Byte.class) return Byte.TYPE;
|
|
61 |
if (wrapper == Character.class) return Character.TYPE;
|
|
62 |
if (wrapper == Short.class) return Short.TYPE;
|
|
63 |
if (wrapper == Integer.class) return Integer.TYPE;
|
|
64 |
if (wrapper == Long.class) return Long.TYPE;
|
|
65 |
if (wrapper == Float.class) return Float.TYPE;
|
|
66 |
if (wrapper == Double.class) return Double.TYPE;
|
|
67 |
if (wrapper == Void.class) return Void.TYPE;
|
|
68 |
return null;
|
|
69 |
}
|
|
70 |
|
|
71 |
/**
|
|
72 |
* Tests each element on the class arrays for assignability.
|
|
73 |
*
|
|
74 |
* @param argClasses arguments to be tested
|
|
75 |
* @param argTypes arguments from Method
|
|
76 |
* @return true if each class in argTypes is assignable from the
|
|
77 |
* corresponding class in argClasses.
|
|
78 |
*/
|
|
79 |
private static boolean matchArguments(Class[] argClasses, Class[] argTypes) {
|
|
80 |
return matchArguments(argClasses, argTypes, false);
|
|
81 |
}
|
|
82 |
|
|
83 |
/**
|
|
84 |
* Tests each element on the class arrays for equality.
|
|
85 |
*
|
|
86 |
* @param argClasses arguments to be tested
|
|
87 |
* @param argTypes arguments from Method
|
|
88 |
* @return true if each class in argTypes is equal to the
|
|
89 |
* corresponding class in argClasses.
|
|
90 |
*/
|
|
91 |
private static boolean matchExplicitArguments(Class[] argClasses, Class[] argTypes) {
|
|
92 |
return matchArguments(argClasses, argTypes, true);
|
|
93 |
}
|
|
94 |
|
|
95 |
private static boolean matchArguments(Class[] argClasses,
|
|
96 |
Class[] argTypes, boolean explicit) {
|
|
97 |
|
|
98 |
boolean match = (argClasses.length == argTypes.length);
|
|
99 |
for(int j = 0; j < argClasses.length && match; j++) {
|
|
100 |
Class argType = argTypes[j];
|
|
101 |
if (argType.isPrimitive()) {
|
|
102 |
argType = typeToClass(argType);
|
|
103 |
}
|
|
104 |
if (explicit) {
|
|
105 |
// Test each element for equality
|
|
106 |
if (argClasses[j] != argType) {
|
|
107 |
match = false;
|
|
108 |
}
|
|
109 |
} else {
|
|
110 |
// Consider null an instance of all classes.
|
|
111 |
if (argClasses[j] != null &&
|
|
112 |
!(argType.isAssignableFrom(argClasses[j]))) {
|
|
113 |
match = false;
|
|
114 |
}
|
|
115 |
}
|
|
116 |
}
|
|
117 |
return match;
|
|
118 |
}
|
|
119 |
|
|
120 |
/**
|
|
121 |
* @return the method which best matches the signature or throw an exception
|
|
122 |
* if it can't be found or the method is ambiguous.
|
|
123 |
*/
|
|
124 |
static Method getPublicMethod(Class declaringClass, String methodName,
|
|
125 |
Class[] argClasses) throws NoSuchMethodException {
|
|
126 |
Method m;
|
|
127 |
|
|
128 |
m = findPublicMethod(declaringClass, methodName, argClasses);
|
|
129 |
if (m == null)
|
|
130 |
throw new NoSuchMethodException(declaringClass.getName() + "." + methodName);
|
|
131 |
return m;
|
|
132 |
}
|
|
133 |
|
|
134 |
/**
|
|
135 |
* @return the method which best matches the signature or null if it cant be found or
|
|
136 |
* the method is ambiguous.
|
|
137 |
*/
|
|
138 |
public static Method findPublicMethod(Class declaringClass, String methodName,
|
|
139 |
Class[] argClasses) {
|
|
140 |
// Many methods are "getters" which take no arguments.
|
|
141 |
// This permits the following optimisation which
|
|
142 |
// avoids the expensive call to getMethods().
|
|
143 |
if (argClasses.length == 0) {
|
|
144 |
try {
|
|
145 |
return MethodUtil.getMethod(declaringClass, methodName, argClasses);
|
|
146 |
}
|
|
147 |
catch (NoSuchMethodException e) {
|
|
148 |
return null;
|
|
149 |
} catch (SecurityException se) {
|
|
150 |
// fall through
|
|
151 |
}
|
|
152 |
}
|
|
153 |
Method[] methods = MethodUtil.getPublicMethods(declaringClass);
|
|
154 |
List list = new ArrayList();
|
|
155 |
for(int i = 0; i < methods.length; i++) {
|
|
156 |
// Collect all the methods which match the signature.
|
|
157 |
Method method = methods[i];
|
|
158 |
if (method.getName().equals(methodName)) {
|
|
159 |
if (matchArguments(argClasses, method.getParameterTypes())) {
|
|
160 |
list.add(method);
|
|
161 |
}
|
|
162 |
}
|
|
163 |
}
|
|
164 |
if (list.size() > 0) {
|
|
165 |
if (list.size() == 1) {
|
|
166 |
return (Method)list.get(0);
|
|
167 |
}
|
|
168 |
else {
|
|
169 |
ListIterator iterator = list.listIterator();
|
|
170 |
Method method;
|
|
171 |
while (iterator.hasNext()) {
|
|
172 |
method = (Method)iterator.next();
|
|
173 |
if (matchExplicitArguments(argClasses, method.getParameterTypes())) {
|
|
174 |
return method;
|
|
175 |
}
|
|
176 |
}
|
|
177 |
// There are more than one method which matches this signature.
|
|
178 |
// try to return the most specific method.
|
|
179 |
return getMostSpecificMethod(list, argClasses);
|
|
180 |
}
|
|
181 |
}
|
|
182 |
return null;
|
|
183 |
}
|
|
184 |
|
|
185 |
/**
|
|
186 |
* Return the most specific method from the list of methods which
|
|
187 |
* matches the args. The most specific method will have the most
|
|
188 |
* number of equal parameters or will be closest in the inheritance
|
|
189 |
* heirarchy to the runtime execution arguments.
|
|
190 |
* <p>
|
|
191 |
* See the JLS section 15.12
|
|
192 |
* http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#20448
|
|
193 |
*
|
|
194 |
* @param methods List of methods which already have the same param length
|
|
195 |
* and arg types are assignable to param types
|
|
196 |
* @param args an array of param types to match
|
|
197 |
* @return method or null if a specific method cannot be determined
|
|
198 |
*/
|
|
199 |
private static Method getMostSpecificMethod(List methods, Class[] args) {
|
|
200 |
Method method = null;
|
|
201 |
|
|
202 |
int matches = 0;
|
|
203 |
int lastMatch = matches;
|
|
204 |
|
|
205 |
ListIterator iterator = methods.listIterator();
|
|
206 |
while (iterator.hasNext()) {
|
|
207 |
Method m = (Method)iterator.next();
|
|
208 |
Class[] mArgs = m.getParameterTypes();
|
|
209 |
matches = 0;
|
|
210 |
for (int i = 0; i < args.length; i++) {
|
|
211 |
Class mArg = mArgs[i];
|
|
212 |
if (mArg.isPrimitive()) {
|
|
213 |
mArg = typeToClass(mArg);
|
|
214 |
}
|
|
215 |
if (args[i] == mArg) {
|
|
216 |
matches++;
|
|
217 |
}
|
|
218 |
}
|
|
219 |
if (matches == 0 && lastMatch == 0) {
|
|
220 |
if (method == null) {
|
|
221 |
method = m;
|
|
222 |
} else {
|
|
223 |
// Test existing method. We already know that the args can
|
|
224 |
// be assigned to all the method params. However, if the
|
|
225 |
// current method parameters is higher in the inheritance
|
|
226 |
// hierarchy then replace it.
|
|
227 |
if (!matchArguments(method.getParameterTypes(),
|
|
228 |
m.getParameterTypes())) {
|
|
229 |
method = m;
|
|
230 |
}
|
|
231 |
}
|
|
232 |
} else if (matches > lastMatch) {
|
|
233 |
lastMatch = matches;
|
|
234 |
method = m;
|
|
235 |
} else if (matches == lastMatch) {
|
|
236 |
// ambiguous method selection.
|
|
237 |
method = null;
|
|
238 |
}
|
|
239 |
}
|
|
240 |
return method;
|
|
241 |
}
|
|
242 |
|
|
243 |
/**
|
|
244 |
* @return the method or null if it can't be found or is ambiguous.
|
|
245 |
*/
|
|
246 |
public static Method findMethod(Class targetClass, String methodName,
|
|
247 |
Class[] argClasses) {
|
|
248 |
Method m = findPublicMethod(targetClass, methodName, argClasses);
|
|
249 |
if (m != null && Modifier.isPublic(m.getDeclaringClass().getModifiers())) {
|
|
250 |
return m;
|
|
251 |
}
|
|
252 |
|
|
253 |
/*
|
|
254 |
Search the interfaces for a public version of this method.
|
|
255 |
|
|
256 |
Example: the getKeymap() method of a JTextField
|
|
257 |
returns a package private implementation of the
|
|
258 |
of the public Keymap interface. In the Keymap
|
|
259 |
interface there are a number of "properties" one
|
|
260 |
being the "resolveParent" property implied by the
|
|
261 |
getResolveParent() method. This getResolveParent()
|
|
262 |
cannot be called reflectively because the class
|
|
263 |
itself is not public. Instead we search the class's
|
|
264 |
interfaces and find the getResolveParent()
|
|
265 |
method of the Keymap interface - on which invoke
|
|
266 |
may be applied without error.
|
|
267 |
|
|
268 |
So in :-
|
|
269 |
|
|
270 |
JTextField o = new JTextField("Hello, world");
|
|
271 |
Keymap km = o.getKeymap();
|
|
272 |
Method m1 = km.getClass().getMethod("getResolveParent", new Class[0]);
|
|
273 |
Method m2 = Keymap.class.getMethod("getResolveParent", new Class[0]);
|
|
274 |
|
|
275 |
Methods m1 and m2 are different. The invocation of method
|
|
276 |
m1 unconditionally throws an IllegalAccessException where
|
|
277 |
the invocation of m2 will invoke the implementation of the
|
|
278 |
method. Note that (ignoring the overloading of arguments)
|
|
279 |
there is only one implementation of the named method which
|
|
280 |
may be applied to this target.
|
|
281 |
*/
|
|
282 |
for(Class type = targetClass; type != null; type = type.getSuperclass()) {
|
|
283 |
Class[] interfaces = type.getInterfaces();
|
|
284 |
for(int i = 0; i < interfaces.length; i++) {
|
|
285 |
m = findPublicMethod(interfaces[i], methodName, argClasses);
|
|
286 |
if (m != null) {
|
|
287 |
return m;
|
|
288 |
}
|
|
289 |
}
|
|
290 |
}
|
|
291 |
return null;
|
|
292 |
}
|
|
293 |
|
|
294 |
/**
|
|
295 |
* A class that represents the unique elements of a method that will be a
|
|
296 |
* key in the method cache.
|
|
297 |
*/
|
|
298 |
private static class Signature {
|
|
299 |
private Class targetClass;
|
|
300 |
private String methodName;
|
|
301 |
private Class[] argClasses;
|
|
302 |
|
|
303 |
private volatile int hashCode = 0;
|
|
304 |
|
|
305 |
public Signature(Class targetClass, String methodName, Class[] argClasses) {
|
|
306 |
this.targetClass = targetClass;
|
|
307 |
this.methodName = methodName;
|
|
308 |
this.argClasses = argClasses;
|
|
309 |
}
|
|
310 |
|
|
311 |
public boolean equals(Object o2) {
|
|
312 |
if (this == o2) {
|
|
313 |
return true;
|
|
314 |
}
|
|
315 |
Signature that = (Signature)o2;
|
|
316 |
if (!(targetClass == that.targetClass)) {
|
|
317 |
return false;
|
|
318 |
}
|
|
319 |
if (!(methodName.equals(that.methodName))) {
|
|
320 |
return false;
|
|
321 |
}
|
|
322 |
if (argClasses.length != that.argClasses.length) {
|
|
323 |
return false;
|
|
324 |
}
|
|
325 |
for (int i = 0; i < argClasses.length; i++) {
|
|
326 |
if (!(argClasses[i] == that.argClasses[i])) {
|
|
327 |
return false;
|
|
328 |
}
|
|
329 |
}
|
|
330 |
return true;
|
|
331 |
}
|
|
332 |
|
|
333 |
/**
|
|
334 |
* Hash code computed using algorithm suggested in
|
|
335 |
* Effective Java, Item 8.
|
|
336 |
*/
|
|
337 |
public int hashCode() {
|
|
338 |
if (hashCode == 0) {
|
|
339 |
int result = 17;
|
|
340 |
result = 37 * result + targetClass.hashCode();
|
|
341 |
result = 37 * result + methodName.hashCode();
|
|
342 |
if (argClasses != null) {
|
|
343 |
for (int i = 0; i < argClasses.length; i++) {
|
|
344 |
result = 37 * result + ((argClasses[i] == null) ? 0 :
|
|
345 |
argClasses[i].hashCode());
|
|
346 |
}
|
|
347 |
}
|
|
348 |
hashCode = result;
|
|
349 |
}
|
|
350 |
return hashCode;
|
|
351 |
}
|
|
352 |
}
|
|
353 |
|
|
354 |
/**
|
|
355 |
* A wrapper to findMethod(), which will search or populate the method
|
|
356 |
* in a cache.
|
|
357 |
* @throws exception if the method is ambiguios.
|
|
358 |
*/
|
|
359 |
public static synchronized Method getMethod(Class targetClass,
|
|
360 |
String methodName,
|
|
361 |
Class[] argClasses) {
|
|
362 |
Object signature = new Signature(targetClass, methodName, argClasses);
|
|
363 |
|
|
364 |
Method method = null;
|
|
365 |
Map methodCache = null;
|
|
366 |
boolean cache = false;
|
|
367 |
if (ReflectUtil.isPackageAccessible(targetClass)) {
|
|
368 |
cache = true;
|
|
369 |
}
|
|
370 |
|
|
371 |
if (cache && methodCacheRef != null &&
|
|
372 |
(methodCache = (Map)methodCacheRef.get()) != null) {
|
|
373 |
method = (Method)methodCache.get(signature);
|
|
374 |
if (method != null) {
|
|
375 |
return method;
|
|
376 |
}
|
|
377 |
}
|
|
378 |
method = findMethod(targetClass, methodName, argClasses);
|
|
379 |
if (cache && method != null) {
|
|
380 |
if (methodCache == null) {
|
|
381 |
methodCache = new HashMap();
|
|
382 |
methodCacheRef = new SoftReference(methodCache);
|
|
383 |
}
|
|
384 |
methodCache.put(signature, method);
|
|
385 |
}
|
|
386 |
return method;
|
|
387 |
}
|
|
388 |
|
|
389 |
/**
|
|
390 |
* Return a constructor on the class with the arguments.
|
|
391 |
*
|
|
392 |
* @throws exception if the method is ambiguios.
|
|
393 |
*/
|
|
394 |
public static Constructor getConstructor(Class cls, Class[] args) {
|
|
395 |
Constructor constructor = null;
|
|
396 |
|
|
397 |
// PENDING: Implement the resolutuion of ambiguities properly.
|
|
398 |
Constructor[] ctors = ConstructorUtil.getConstructors(cls);
|
|
399 |
for(int i = 0; i < ctors.length; i++) {
|
|
400 |
if (matchArguments(args, ctors[i].getParameterTypes())) {
|
|
401 |
constructor = ctors[i];
|
|
402 |
}
|
|
403 |
}
|
|
404 |
return constructor;
|
|
405 |
}
|
|
406 |
|
|
407 |
public static Object getPrivateField(Object instance, Class cls, String name) {
|
|
408 |
return getPrivateField(instance, cls, name, null);
|
|
409 |
}
|
|
410 |
|
|
411 |
/**
|
|
412 |
* Returns the value of a private field.
|
|
413 |
*
|
|
414 |
* @param instance object instance
|
|
415 |
* @param cls class
|
|
416 |
* @param name name of the field
|
|
417 |
* @param el an exception listener to handle exceptions; or null
|
|
418 |
* @return value of the field; null if not found or an error is encountered
|
|
419 |
*/
|
|
420 |
public static Object getPrivateField(Object instance, Class cls,
|
|
421 |
String name, ExceptionListener el) {
|
|
422 |
try {
|
|
423 |
Field f = cls.getDeclaredField(name);
|
|
424 |
f.setAccessible(true);
|
|
425 |
return f.get(instance);
|
|
426 |
}
|
|
427 |
catch (Exception e) {
|
|
428 |
if (el != null) {
|
|
429 |
el.exceptionThrown(e);
|
|
430 |
}
|
|
431 |
}
|
|
432 |
return null;
|
|
433 |
}
|
|
434 |
}
|