Description
call method by reflection this method allows to call protected and private methods; use it with care...
License
Open Source License
Parameter
Parameter | Description |
---|
targetClass | a parameter |
target | a parameter |
methodName | a parameter |
paramType0 | a parameter |
paramValue0 | a parameter |
Exception
Parameter | Description |
---|
NoSuchMethodException | an exception |
IllegalAccessException | an exception |
InvocationTargetException | an exception |
Declaration
public static Object invoke(Class targetClass, Object target, String methodName, Class paramType0,
Object paramValue0) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
Method Source Code
//package com.java2s;
/*//from w w w .java 2s. co m
* This file is part of the Jose Project
* see http://jose-chess.sourceforge.net/
* (c) 2002-2006 Peter Sch?fer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*/
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Main {
/**
* call method by reflection
* this method allows to call protected and private methods; use it with care...
*
* @param targetClass
* @param target
* @param methodName
* @param paramType0
* @param paramValue0
* @return
* @throws NoSuchMethodException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static Object invoke(Class targetClass, Object target, String methodName, Class paramType0,
Object paramValue0) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Class[] paramTypes = new Class[1];
Object[] paramValues = new Object[1];
paramTypes[0] = paramType0;
paramValues[0] = paramValue0;
return invoke(targetClass, target, methodName, paramTypes, paramValues);
}
public static Object invoke(String targetClassName, Object target, String methodName, Class paramType0,
Object paramValue0) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException,
ClassNotFoundException {
Class targetClass = Class.forName(targetClassName);
return invoke(targetClass, target, methodName, paramType0, paramValue0);
}
public static Object invoke(Class targetClass, Object target, String methodName, Class paramType0,
Object paramValue0, Class paramType1, Object paramValue1)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Class[] paramTypes = new Class[2];
Object[] paramValues = new Object[2];
paramTypes[0] = paramType0;
paramValues[0] = paramValue0;
paramTypes[1] = paramType1;
paramValues[1] = paramValue1;
return invoke(targetClass, target, methodName, paramTypes, paramValues);
}
public static Object invoke(Object target, String methodName, Class paramType0, Object paramValue0)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
return invoke(target.getClass(), target, methodName, paramType0, paramValue0);
}
public static Object invoke(String targetClassName, Object target, String methodName, Class paramType0,
Object paramValue0, Class paramType1, Object paramValue1) throws NoSuchMethodException,
IllegalAccessException, InvocationTargetException, ClassNotFoundException {
Class targetClass = Class.forName(targetClassName);
return invoke(targetClass, target, methodName, paramType0, paramValue0, paramType1, paramValue1);
}
public static Object invoke(Object target, String methodName, Class[] paramTypes, Object[] paramValues)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
return invoke(target.getClass(), target, methodName, paramTypes, paramValues);
}
public static Object invoke(Object target, String methodName)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
return invoke(target.getClass(), target, methodName, (Class[]) null, (Object[]) null);
}
public static Object invoke(Class targetClass, Object target, String methodName, Class[] paramTypes,
Object[] paramValues) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Method method = getMethod(targetClass, methodName, paramTypes);
method.setAccessible(true);
return method.invoke(target, paramValues);
}
public static Object invoke(String targetClassName, Object target, String methodName, Class[] paramTypes,
Object[] paramValues) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException,
ClassNotFoundException {
Class targetClass = Class.forName(targetClassName);
return invoke(targetClass, target, methodName, paramTypes, paramValues);
}
public static Method getMethod(Class targetClass, String methodName, Class[] paramTypes)
throws NoSuchMethodException {
Method method = null;
NoSuchMethodException nsmex = null;
for (Class clazz = targetClass; clazz != null; clazz = clazz.getSuperclass())
try {
method = clazz.getDeclaredMethod(methodName, paramTypes);
break;
} catch (NoSuchMethodException ex) {
if (nsmex == null)
nsmex = ex;
continue;
}
if (method == null && nsmex != null)
throw nsmex;
else
return method;
}
}
Related
- invoke(@Nonnull Object source, @Nonnull String name, Object... args)
- invoke(boolean makeAccessible, T object, Class super T> methodClass, String methodName, Class>[] paramTypes, Object... params)
- invoke(Class c, Object obj, String method, Class[] paramClasses, Object[] params)
- invoke(Class extends Annotation> annotationClass, Object instance, Object[] parameters)
- invoke(Class> clazz, Object obj, String methodName, Object... args)
- invoke(Class> clazz, String methodName, Class>[] parameterTypes, Object instance, Object[] args, Class returnType)
- invoke(Class> clazz, String methodName, Object instance, Class>[] signature, Object... args)