KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > util > ReflectionUtils


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.webflow.util;
17
18 import java.lang.reflect.InvocationTargetException JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20
21 /**
22  * Simple utility class for working with the java reflection API. Only intended for
23  * internal use. Will likely disappear in a future release of Spring Web Flow and
24  * simply rely on {@link org.springframework.util.ReflectionUtils} if necessary.
25  *
26  * @author Keith Donald
27  */

28 public class ReflectionUtils {
29
30     /**
31      * Invoke the specified {@link Method} against the supplied target object
32      * with no arguments. The target object can be <code>null</code> when
33      * invoking a static {@link Method}. All exceptions are treated as fatal and will be
34      * converted to unchecked exceptions.
35      * @see #invokeMethod(java.lang.reflect.Method, Object, Object[])
36      * @throws RuntimeException when something goes wrong invoking the method or
37      * when the method itself throws an exception
38      */

39     public static Object JavaDoc invokeMethod(Method JavaDoc method, Object JavaDoc target) throws RuntimeException JavaDoc {
40         return invokeMethod(method, target, null);
41     }
42
43     /**
44      * Invoke the specified {@link Method} against the supplied target object
45      * with the supplied arguments. The target object can be null when invoking a
46      * static {@link Method}. All exceptions are treated as fatal and will be
47      * converted to unchecked exceptions.
48      * @see #invokeMethod(java.lang.reflect.Method, Object, Object[])
49      * @throws RuntimeException when something goes wrong invoking the method or
50      * when the method itself throws an exception
51      */

52     public static Object JavaDoc invokeMethod(Method JavaDoc method, Object JavaDoc target, Object JavaDoc[] args) throws RuntimeException JavaDoc {
53         try {
54             return method.invoke(target, args);
55         }
56         catch (IllegalAccessException JavaDoc ex) {
57             handleReflectionException(ex);
58             throw new IllegalStateException JavaDoc("Unexpected reflection exception - " + ex.getClass().getName() + ": "
59                     + ex.getMessage());
60         }
61         catch (InvocationTargetException JavaDoc ex) {
62             handleReflectionException(ex);
63             throw new IllegalStateException JavaDoc("Unexpected reflection exception - " + ex.getClass().getName() + ": "
64                     + ex.getMessage());
65         }
66     }
67     
68     /**
69      * Handle the given reflection exception.
70      * Should only be called if no checked exception is expected to
71      * be thrown by the target method.
72      * <p>
73      * Throws the underlying RuntimeException or Error in case
74      * of an InvocationTargetException with such a root cause. Throws
75      * an IllegalStateException with an appropriate message else.
76      * @param ex the reflection exception to handle
77      */

78     private static void handleReflectionException(Exception JavaDoc ex) {
79         if (ex instanceof NoSuchMethodException JavaDoc) {
80             throw new IllegalStateException JavaDoc("Method not found: " + ex.getMessage());
81         }
82         if (ex instanceof IllegalAccessException JavaDoc) {
83             throw new IllegalStateException JavaDoc("Could not access method: " + ex.getMessage());
84         }
85         if (ex instanceof InvocationTargetException JavaDoc) {
86             handleInvocationTargetException((InvocationTargetException JavaDoc) ex);
87         }
88         throw new IllegalStateException JavaDoc(
89                 "Unexpected reflection exception - " + ex.getClass().getName() + ": " + ex.getMessage());
90     }
91     
92     /**
93      * Handle the given invocation target exception.
94      * Should only be called if no checked exception is expected to
95      * be thrown by the target method.
96      * <p>
97      * Throws the underlying RuntimeException or Error in case
98      * of such a root cause. Throws an IllegalStateException else.
99      * @param ex the invocation target exception to handle
100      */

101     private static void handleInvocationTargetException(InvocationTargetException JavaDoc ex) {
102         if (ex.getTargetException() instanceof RuntimeException JavaDoc) {
103             throw (RuntimeException JavaDoc) ex.getTargetException();
104         }
105         if (ex.getTargetException() instanceof Error JavaDoc) {
106             throw (Error JavaDoc) ex.getTargetException();
107         }
108         throw new IllegalStateException JavaDoc(
109                 "Unexpected exception thrown by method - " + ex.getTargetException().getClass().getName() +
110                 ": " + ex.getTargetException().getMessage());
111     }
112 }
Popular Tags