Here you can find the source of invoke(String name, Object object, Object... args)
public static Object invoke(String name, Object object, Object... args)
//package com.java2s; /*/*from w w w . ja v a2 s .c o m*/ * Copyright (C) 2006-2007 Mindquarry GmbH, All Rights Reserved * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (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.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the * License for the specific language governing rights and limitations * under the License. */ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main { public static Object invoke(Method method, Object object, Object... args) { try { return method.invoke(object, args); } catch (IllegalArgumentException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e.getTargetException()); } } public static Object invoke(String name, Object object, Object... args) { try { Class<?>[] parameterTypes = new Class<?>[args.length]; for (int i = 0; i < args.length; i++) parameterTypes[i] = args[i].getClass(); Method method = findMethod(object.getClass(), name, parameterTypes); return method.invoke(object, args); } catch (IllegalArgumentException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e.getTargetException()); } } public static Method findMethod(Class<?> clazz, String name, Class<?>[] parameterTypes) { try { return clazz.getMethod(name, parameterTypes); } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } } }