Here you can find the source of invoke(Object obj, String method, Object param, Object paramType)
Parameter | Description |
---|---|
obj | the object upon which, to call the method |
method | the method you would like to call |
param | the parameter (or parameters if you pass an array) you wish to pass. The types of the parameters form part of the method signature so make sure types are right. |
public static Object invoke(Object obj, String method, Object param, Object paramType)
//package com.java2s; /*// www . j a va 2s . c o m * Copyright 2006 The National Library of New Zealand * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main { /** * Invoke a method if it can be found! Use this to remain backward * compatible with older JDK libraries that do not have the methods you * might want. This will allow your code to compile and run - as long as you * have a plan for what to do if this method returns null. * * @param obj * the object upon which, to call the method * @param method * the method you would like to call * @param param * the parameter (or parameters if you pass an array) you wish to * pass. The types of the parameters form part of the method * signature so make sure types are right. * @return the return value from the method - if there is one. This will be * null if the method was not avaialble and also null if the method * does not have a return value. */ public static Object invoke(Object obj, String method, Object param, Object paramType) { Object result = null; Object[] params = null; Class[] paramTypes = null; if (param.getClass().isArray()) { params = (Object[]) param; paramTypes = (Class[]) paramType; } else { params = new Object[1]; params[0] = param; paramTypes = new Class[1]; paramTypes[0] = (Class) paramType; } try { Method realMethod = obj.getClass().getMethod(method, paramTypes); realMethod.setAccessible(true); result = realMethod.invoke(obj, params); // sucessful invoke, if the result is still null, then the method // did not have a return value } catch (IllegalAccessException ex) { // just can't call it - bad luck. The desired behaviour is to // continue without throwing exceptions // modify this to send back an error } catch (InvocationTargetException ex) { // just can't call it - bad luck. The desired behaviour is to // continue without throwing exceptions // modify this to send back an error } catch (NoSuchMethodException ex) { // just can't call it - bad luck. The desired behaviour is to // continue without throwing exceptions // modify this to send back an error } return result; } }