invoke spring proxy bean`s method
2008年08月14日
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.springframework.aop.support.AopUtils;
import org.springframework.util.MethodInvoker;
/**
*
* @author wangliang_gz@hotmail.com
* @version 1.1.0
*/
public class MethodUtils {
static public Object invokeMethod(Object obj, String methodName,
Object[] args) {
if (AopUtils.isJdkDynamicProxy(obj)) {
if (args == null) {
args = new Object[0];
}
int arguments = args.length;
Class parameterTypes[] = new Class[arguments];
for (int i = 0; i < arguments; i++) {
parameterTypes[i] = args[i].getClass();
}
Class _targetClass = AopUtils.getTargetClass(obj);
Class[] _targetInterfaces = _targetClass.getInterfaces();
Method method = null;
for (int i = 0; i < _targetInterfaces.length; i++) {
method = org.apache.commons.beanutils.MethodUtils
.getMatchingAccessibleMethod(_targetInterfaces[i], methodName,
parameterTypes);
if (method != null) {
break;
}
}
if (method == null) {
method = org.apache.commons.beanutils.MethodUtils
.getMatchingAccessibleMethod(_targetClass, methodName,
parameterTypes);
}
try {
// AopUtils.invokeJoinpointUsingReflection(obj, method, args);
return java.lang.reflect.Proxy.getInvocationHandler(obj)
.invoke(obj, method, args);
} catch (Throwable e) {
throw new RuntimeException(e);
}
} else {
try {
MethodInvoker invoker = new MethodInvoker();
invoker.setTargetObject(obj);
invoker.setArguments(args);
invoker.setTargetMethod(methodName);
invoker.prepare();
return invoker.invoke();
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
import java.lang.reflect.Method;
import org.springframework.aop.support.AopUtils;
import org.springframework.util.MethodInvoker;
/**
*
* @author wangliang_gz@hotmail.com
* @version 1.1.0
*/
public class MethodUtils {
static public Object invokeMethod(Object obj, String methodName,
Object[] args) {
if (AopUtils.isJdkDynamicProxy(obj)) {
if (args == null) {
args = new Object[0];
}
int arguments = args.length;
Class parameterTypes[] = new Class[arguments];
for (int i = 0; i < arguments; i++) {
parameterTypes[i] = args[i].getClass();
}
Class _targetClass = AopUtils.getTargetClass(obj);
Class[] _targetInterfaces = _targetClass.getInterfaces();
Method method = null;
for (int i = 0; i < _targetInterfaces.length; i++) {
method = org.apache.commons.beanutils.MethodUtils
.getMatchingAccessibleMethod(_targetInterfaces[i], methodName,
parameterTypes);
if (method != null) {
break;
}
}
if (method == null) {
method = org.apache.commons.beanutils.MethodUtils
.getMatchingAccessibleMethod(_targetClass, methodName,
parameterTypes);
}
try {
// AopUtils.invokeJoinpointUsingReflection(obj, method, args);
return java.lang.reflect.Proxy.getInvocationHandler(obj)
.invoke(obj, method, args);
} catch (Throwable e) {
throw new RuntimeException(e);
}
} else {
try {
MethodInvoker invoker = new MethodInvoker();
invoker.setTargetObject(obj);
invoker.setArguments(args);
invoker.setTargetMethod(methodName);
invoker.prepare();
return invoker.invoke();
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
