Here you can find the source of invokeInstanceMethod(Object target, String methodName, Class> type, Object arg)
Parameter | Description |
---|---|
target | the target |
methodName | the method name |
type | the type |
arg | the arg |
public static Object invokeInstanceMethod(Object target, String methodName, Class<?> type, Object arg)
//package com.java2s; /*/*from w w w . j av a2s.c o m*/ * JBoss, Home of Professional Open Source. * Copyright 2011, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ import java.lang.reflect.Method; public class Main { private static final Class[] EMPTY_CLASSES = new Class[0]; private static final Object[] EMPTY_ARGS = new Object[0]; /** * Invoke no-param method. * * @param target the object on which to invoke method * @param methodName the name of the method * @return value returned by invoked method */ public static Object invokeInstanceMethod(Object target, String methodName) { return invokeInstanceMethod(target, methodName, EMPTY_CLASSES, EMPTY_ARGS); } /** * Invoke method. * * @param target the target * @param methodName the method name * @param type the type * @param arg the arg * @return method's return value */ public static Object invokeInstanceMethod(Object target, String methodName, Class<?> type, Object arg) { return invokeInstanceMethod(target, methodName, new Class[] { type }, new Object[] { arg }); } /** * Invoke method. * * @param target the target * @param methodName the method name * @param types the types * @param args the args * @return method's return value */ public static Object invokeInstanceMethod(Object target, String methodName, Class[] types, Object[] args) { if (target == null) throw new IllegalArgumentException("Null target"); return invokeMethod(target, target.getClass(), methodName, types, args); } /** * Invoke method. * * @param target the target * @param clazz the class * @param methodName the method name * @param types the types * @param args the args * @return method's return value */ private static Object invokeMethod(Object target, Class<?> clazz, String methodName, Class[] types, Object[] args) { final Method m = findMethod(clazz, methodName, types); try { return m.invoke(target, args); } catch (Exception e) { throw new RuntimeException(e); } } /** * Find method. * * @param clazz the class * @param methodName the method name * @param types the types * @return method's return value */ private static Method findMethod(Class<?> clazz, String methodName, Class[] types) { if (clazz == null) throw new IllegalArgumentException("Null class"); if (methodName == null) throw new IllegalArgumentException("Null method name"); try { Class<?> current = clazz; while (current != null) { try { final Method m = current.getDeclaredMethod(methodName, types); m.setAccessible(true); return m; } catch (NoSuchMethodException ignored) { } current = current.getSuperclass(); } } catch (Throwable t) { throw new RuntimeException(t); } throw new IllegalStateException("Couldn't find method: " + clazz.getName() + " / " + methodName); } }