View Javadoc
1 /*** 2 * Redistribution and use of this software and associated documentation 3 * ("Software"), with or without modification, are permitted provided 4 * that the following conditions are met: 5 * 6 * 1. Redistributions of source code must retain copyright 7 * statements and notices. Redistributions must also contain a 8 * copy of this document. 9 * 10 * 2. Redistributions in binary form must reproduce the 11 * above copyright notice, this list of conditions and the 12 * following disclaimer in the documentation and/or other 13 * materials provided with the distribution. 14 * 15 * 3. The name "Exolab" must not be used to endorse or promote 16 * products derived from this Software without prior written 17 * permission of Exoffice Technologies. For written permission, 18 * please contact tma@netspace.net.au. 19 * 20 * 4. Products derived from this Software may not be called "Exolab" 21 * nor may "Exolab" appear in their names without prior written 22 * permission of Exoffice Technologies. Exolab is a registered 23 * trademark of Exoffice Technologies. 24 * 25 * 5. Due credit should be given to the Exolab Project 26 * (http://www.exolab.org/). 27 * 28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 39 * OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * Copyright 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved. 42 * 43 * $Id: MethodInvoker.java,v 1.2 2004/01/31 13:44:24 tanderson Exp $ 44 */ 45 package org.exolab.jmscts.core; 46 47 import java.lang.reflect.InvocationTargetException; 48 import java.lang.reflect.Method; 49 50 51 /*** 52 * A helper class for invoking methods, and verifying that 53 * any exception thrown matches that expected. 54 * 55 * @version $Revision: 1.2 $ $Date: 2004/01/31 13:44:24 $ 56 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a> 57 */ 58 public class MethodInvoker { 59 60 /*** 61 * The class to introspect. May be <code>null</code> 62 */ 63 private Class _clazz; 64 65 /*** 66 * The expected type of exception 67 */ 68 private final Class _exception; 69 70 /*** 71 * Construct an instance with that expects no exceptions when a method 72 * is invoked 73 */ 74 public MethodInvoker() { 75 this(null, null); 76 } 77 78 /*** 79 * Construct an instance that expects a single expected exception when 80 * a method is invoked. 81 * 82 * @param clazz the class to introspect. May be <code>null</code> 83 * @param exception the expected exception when methods are invoked, 84 * or <code>null</code> if no exceptions are expected. 85 */ 86 public MethodInvoker(Class clazz, Class exception) { 87 _clazz = clazz; 88 _exception = exception; 89 } 90 91 /*** 92 * Invoke a method taking no arguments 93 * <p> 94 * If the method completes normally, the value it returns is returned to 95 * the caller of invoke; if the value has a primitive type, it is wrapped 96 * in an object. If the underlying method return type is void, the 97 * invocation returns null. 98 * 99 * @param object the object to invoke the method on 100 * @param method the method name 101 * @return the result of invoking the method, or null, if it is void 102 * @throws Exception if an exception is thrown and it doesn't match 103 * the expected exception. If no exception is expected, then the exception 104 * will be propagated 105 * @throws NoSuchMethodException if the method doesn't exist 106 * 107 */ 108 public Object invoke(Object object, String method) 109 throws Exception, NoSuchMethodException { 110 return invoke(object, method, new Object[]{}); 111 } 112 113 /*** 114 * Invoke a method taking no arguments 115 * <p> 116 * If the method completes normally, the value it returns is returned to 117 * the caller of invoke; if the value has a primitive type, it is wrapped 118 * in an object. If the underlying method return type is void, the 119 * invocation returns null. 120 * 121 * @param object the object to invoke the method on 122 * @param method the method to invoke 123 * @return the result of invoking the method, or null, if it is void 124 * @throws Exception if an exception is thrown and it doesn't match 125 * the expected exception. If no exception is expected, then the exception 126 * will be propagated 127 */ 128 public Object invoke(Object object, Method method) 129 throws Exception { 130 return invoke(object, method, new Object[]{}); 131 } 132 133 /*** 134 * Invoke a method taking a single argument 135 * <p> 136 * If the method completes normally, the value it returns is returned to 137 * the caller of invoke; if the value has a primitive type, it is wrapped 138 * in an object. If the underlying method return type is void, the 139 * invocation returns null. 140 * 141 * @param object the object to invoke the method on 142 * @param method the method name 143 * @param argument the argument to pass to the method 144 * @return the result of invoking the method, or null, if it is void 145 * @throws Exception if an exception is thrown and it doesn't match 146 * the expected exception. If no exception is expected, then the exception 147 * will be propagated 148 * @throws NoSuchMethodException if the method doesn't exist 149 */ 150 public Object invoke(Object object, String method, Object argument) 151 throws Exception, NoSuchMethodException { 152 return invoke(object, method, new Object[]{argument}); 153 } 154 155 /*** 156 * Invoke a method taking a single argument 157 * <p> 158 * If the method completes normally, the value it returns is returned to 159 * the caller of invoke; if the value has a primitive type, it is wrapped 160 * in an object. If the underlying method return type is void, the 161 * invocation returns null. 162 * 163 * @param object the object to invoke the method on 164 * @param method the method to invoke 165 * @param argument the argument to pass to the method 166 * @return the result of invoking the method, or null, if it is void 167 * @throws Exception if an exception is thrown and it doesn't match 168 * the expected exception. If no exception is expected, then the exception 169 * will be propagated 170 */ 171 public Object invoke(Object object, Method method, Object argument) 172 throws Exception { 173 return invoke(object, method, new Object[]{argument}); 174 } 175 176 /*** 177 * Invoke a method taking a list of arguments 178 * <p> 179 * If the method completes normally, the value it returns is returned to 180 * the caller of invoke; if the value has a primitive type, it is wrapped 181 * in an object. If the underlying method return type is void, the 182 * invocation returns null. 183 * 184 * @param object the object to invoke the method on 185 * @param method the method name 186 * @param arguments the arguments to pass to the method 187 * @return the result of invoking the method, or null, if it is void 188 * @throws Exception if an exception is thrown and it doesn't match 189 * the expected exception. If no exception is expected, then the exception 190 * will be propagated 191 * @throws NoSuchMethodException if the method doesn't exist 192 */ 193 public Object invoke(Object object, String method, Object[] arguments) 194 throws Exception, NoSuchMethodException { 195 196 Class clazz = (_clazz != null) ? _clazz : object.getClass(); 197 Method invoker = ClassHelper.getMethod(clazz, method, arguments); 198 return invoke(object, invoker, arguments); 199 } 200 201 /*** 202 * Invoke a method taking a list of arguments 203 * <p> 204 * If the method completes normally, the value it returns is returned to 205 * the caller of invoke; if the value has a primitive type, it is wrapped 206 * in an object. If the underlying method return type is void, the 207 * invocation returns null. 208 * 209 * @param object the object to invoke the method on 210 * @param method the method to invoke 211 * @param arguments the arguments to pass to the method 212 * @return the result of invoking the method, or null, if it is void 213 * @throws Exception if an exception is thrown and it doesn't match 214 * the expected exception. If no exception is expected, then the exception 215 * will be propagated 216 */ 217 public Object invoke(Object object, Method method, Object[] arguments) 218 throws Exception { 219 Object result = null; 220 try { 221 result = method.invoke(object, arguments); 222 if (_exception != null) { 223 fail(method); 224 } 225 } catch (InvocationTargetException exception) { 226 if (_exception != null) { 227 verify(method, (Exception) exception.getTargetException()); 228 } else { 229 throw (Exception) exception.getTargetException(); 230 } 231 } 232 return result; 233 } 234 235 /*** 236 * Helper to raise an exception when a method doesn't throw the expected 237 * exception 238 * 239 * @param method the method 240 * @throws Exception details the expected exception 241 */ 242 protected void fail(Method method) throws Exception { 243 throw new Exception( 244 "Expected exception of type=" + _exception.getName() 245 + " to be thrown when invoking " + method); 246 } 247 248 /*** 249 * Helper to verify that an exception matches that expected 250 * 251 * @param method the method 252 * @param exception the exception thrown by the method 253 * @throws Exception if <code>exception</code> isn't of the expected type 254 */ 255 protected void verify(Method method, Exception exception) 256 throws Exception { 257 258 if (!_exception.isAssignableFrom(exception.getClass())) { 259 throw new Exception( 260 "Expected exception of type=" + _exception.getName() 261 + " to be thrown, when invoking " + method 262 + " but got exception type=" + exception.getClass().getName() 263 + ": " + exception); 264 } 265 } 266 267 }

This page was automatically generated by Maven