001 package com.mockrunner.util.common; 002 003 import java.lang.reflect.Method; 004 005 import com.mockrunner.base.NestedApplicationException; 006 007 public class MethodUtil 008 { 009 /** 010 * Invokes the method with the specified name on the specified object 011 * and throws a {@link com.mockrunner.base.NestedApplicationException}, 012 * if the invocation fails. The method must be public and must not 013 * have any parameters. 014 * @param object the object the method is invoked from 015 * @param methodName the name of the method 016 * @return the result of the method invocation 017 */ 018 public static Object invoke(Object object, String methodName) 019 { 020 try 021 { 022 Method method = object.getClass().getMethod(methodName, null); 023 return method.invoke(object, null); 024 } 025 catch(Exception exc) 026 { 027 throw new NestedApplicationException(exc); 028 } 029 } 030 031 /** 032 * Invokes the method with the specified name on the specified object 033 * and throws a {@link com.mockrunner.base.NestedApplicationException}, 034 * if the invocation fails. The method must be public and must have 035 * exactly one paremeter of the type specified by the given 036 * <code>parameter</code>. 037 * @param object the object the method is invoked from 038 * @param methodName the name of the method 039 * @param parameter the parameter, must not be <code>null</code> 040 * @return the result of the method invocation 041 */ 042 public static Object invoke(Object object, String methodName, Object parameter) 043 { 044 try 045 { 046 Method method = object.getClass().getMethod(methodName, new Class[] {parameter.getClass()}); 047 return method.invoke(object, new Object[] {parameter}); 048 } 049 catch(Exception exc) 050 { 051 throw new NestedApplicationException(exc); 052 } 053 } 054 }