Here you can find the source of invoke(Object _o, String _method, Object... _args)
Parameter | Description |
---|---|
NoSuchMethodException | if the method is not found |
InvocationTargetException | target is not defined |
IllegalAccessException | if the method is not private |
IllegalArgumentException | an exception |
@SuppressWarnings("unchecked") public static <T extends Object> T invoke(Object _o, String _method, Object... _args) throws NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
//package com.java2s; /******************************************************************************* * Copyright (c) 2013 <a href="mailto:daniel.williams@gmail.com">Daniel Williams</a>. * All rights reserved. This program, solace.common, and file, ReflectionUtil.java, and the accompanying materials * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl.html//from www . java2 s . c o m * * Contributors: * <a href="mailto:daniel.williams@gmail.com">Daniel Williams</a> - initial API and implementation ******************************************************************************/ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; public class Main { /** * Invokes a method. Pass in java.lang.Void if it is not to return anything. * * @throws NoSuchMethodException * if the method is not found * @throws InvocationTargetException * target is not defined * @throws IllegalAccessException * if the method is not private * @throws IllegalArgumentException */ @SuppressWarnings("unchecked") public static <T extends Object> T invoke(Object _o, String _method, Object... _args) throws NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { ArrayList<Class<?>> inputs = new ArrayList<Class<?>>(); for (Object o : _args) inputs.add(o.getClass()); Method m = _o.getClass().getMethod(_method, inputs.toArray(new Class<?>[0])); return (T) m.invoke(_o, _args); } }