01: /*
02: * ReflectionUtil.java
03: *
04: * Created on August 13, 2007, 2:33 PM
05: *
06: * To change this template, choose Tools | Template Manager
07: * and open the template in the editor.
08: */
09:
10: package com.sun.xml.wss.impl.misc;
11:
12: import com.sun.xml.wss.impl.XWSSecurityRuntimeException;
13: import com.sun.xml.wss.logging.LogDomainConstants;
14: import java.lang.reflect.InvocationTargetException;
15: import java.lang.reflect.Method;
16: import java.util.logging.Level;
17: import java.util.logging.Logger;
18:
19: /**
20: * Reflection utilities wrapper
21: */
22: public class ReflectionUtil {
23: private static final Logger log = Logger.getLogger(
24: LogDomainConstants.WSS_API_DOMAIN,
25: LogDomainConstants.WSS_API_DOMAIN_BUNDLE);
26:
27: /**
28: * Reflectively invokes specified method on the specified target
29: */
30: public static <T> T invoke(final Object target,
31: final String methodName, final Class<T> resultClass,
32: final Object... parameters)
33: throws XWSSecurityRuntimeException {
34: Class[] parameterTypes;
35: if (parameters != null && parameters.length > 0) {
36: parameterTypes = new Class[parameters.length];
37: int i = 0;
38: for (Object parameter : parameters) {
39: parameterTypes[i++] = parameter.getClass();
40: }
41: } else {
42: parameterTypes = null;
43: }
44:
45: return invoke(target, methodName, resultClass, parameters,
46: parameterTypes);
47: }
48:
49: /**
50: * Reflectively invokes specified method on the specified target
51: */
52: public static <T> T invoke(final Object target,
53: final String methodName, final Class<T> resultClass,
54: final Object[] parameters, final Class[] parameterTypes)
55: throws XWSSecurityRuntimeException {
56: try {
57: final Method method = target.getClass().getMethod(
58: methodName, parameterTypes);
59: final Object result = method.invoke(target, parameters);
60:
61: return resultClass.cast(result);
62: } catch (IllegalArgumentException e) {
63: log
64: .log(Level.SEVERE,
65: "WSS0810.method.invocation.failed", e);
66: throw e;
67: } catch (InvocationTargetException e) {
68: log
69: .log(Level.SEVERE,
70: "WSS0810.method.invocation.failed", e);
71: throw new XWSSecurityRuntimeException(e);
72: } catch (IllegalAccessException e) {
73: log
74: .log(Level.SEVERE,
75: "WSS0810.method.invocation.failed", e);
76: throw new XWSSecurityRuntimeException(e);
77: } catch (SecurityException e) {
78: log
79: .log(Level.SEVERE,
80: "WSS0810.method.invocation.failed", e);
81: throw e;
82: } catch (NoSuchMethodException e) {
83: log
84: .log(Level.SEVERE,
85: "WSS0810.method.invocation.failed", e);
86: throw new XWSSecurityRuntimeException(e);
87: }
88: }
89:
90: }
|