Here you can find the source of invokeMethodByName(@Nonnull T object, @Nonnull String name, Class typeArg, Object arg)
Parameter | Description |
---|---|
object | object from super class which need to invoke method |
name | name of method which need to invoke |
typeArg | type of method argument |
arg | argument of method |
Parameter | Description |
---|---|
Exception | an exception |
public static <T> void invokeMethodByName(@Nonnull T object, @Nonnull String name, Class typeArg, Object arg) throws Exception
//package com.java2s; /******************************************************************************* * Copyright (c) 2012-2015 Codenvy, S.A. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors://from w w w .j a v a2 s . co m * Codenvy, S.A. - initial API and implementation *******************************************************************************/ import javax.annotation.Nonnull; import java.lang.reflect.Method; public class Main { /** * Do method from superclass by name. * * @param object * object from super class which need to invoke method * @param name * name of method which need to invoke * @param arg * argument of method * @throws Exception */ public static <T> void invokeMethodByName(@Nonnull T object, @Nonnull String name, Object arg) throws Exception { Method method = object.getClass().getDeclaredMethod(name, Object.class); method.setAccessible(true); method.invoke(object, arg); } /** * Do method from superclass by name. * * @param object * object from super class which need to invoke method * @param name * name of method which need to invoke * @param typeArg * type of method argument * @param arg * argument of method * @throws Exception */ public static <T> void invokeMethodByName(@Nonnull T object, @Nonnull String name, Class typeArg, Object arg) throws Exception { Method method = object.getClass().getDeclaredMethod(name, typeArg); method.setAccessible(true); method.invoke(object, arg); } }