Here you can find the source of invokeMethod(Object caller, String methodName, Object[] params)
Parameter | Description |
---|---|
NoSuchMethodException | an exception |
SecurityException | an exception |
InvocationTargetException | an exception |
IllegalAccessException | an exception |
IllegalArgumentException | an exception |
public static final Object invokeMethod(Object caller, String methodName, Object[] params) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException
//package com.java2s; /**//from w w w . j av a 2 s.c o m * File Name : BeanUtil.java * Author : Administrator:Adelwin * Create Date : Mar 13, 2006:1:22:45 AM * * Copyright (c) 2006 Adelwin. All Rights Reserved. <BR> * <BR> * This software contains confidential and proprietary information of * Adelwin. ("Confidential Information").<BR> * <BR> * Such Confidential Information shall not be disclosed and it shall * only be used in accordance with the terms of the license agreement * entered into with Solveware Independent; other than in accordance with the written * permission of Solveware Independent. <BR> * * */ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main { /** * invoke method and returns it result(s) * @throws NoSuchMethodException * @throws SecurityException * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException */ public static final Object invokeMethod(Object caller, String methodName, Object[] params) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException { // Class clazz = caller.getClass(); Method method = null; Class<?>[] paramTypes = (params == null ? null : getObjectTypes(params)); method = findMethod(caller.getClass(), methodName, paramTypes); return (method == null ? null : method.invoke(caller, params)); } public static Class<?>[] getObjectTypes(Object[] objs) { Class<?>[] objTypes = new Class[objs.length]; for (int i = 0; i < objs.length; i++) { objTypes[i] = objs[i].getClass(); } return objTypes; } public static final Method findMethod(Class<?> clazz, String methodName, Class<?>[] paramTypes) { Method method = null; try { method = clazz.getDeclaredMethod(methodName, paramTypes); return method; } catch (NoSuchMethodException e) { return findMethod(clazz.getSuperclass(), methodName, paramTypes); } catch (NullPointerException e) { return null; } } }