enhance aop

This commit is contained in:
Looly 2019-09-27 23:20:43 +08:00
parent 5b5645cbbb
commit 45609bc22b
8 changed files with 280 additions and 250 deletions

View File

@ -9,14 +9,14 @@ import java.lang.reflect.Method;
* @author ted.L
* @since 4.18
*/
public interface Aspect{
public interface Aspect {
/**
* 目标方法执行前的操作
*
* @param target 目标对象
* @param method 目标方法
* @param args 参数
* @param args 参数
* @return 是否继续执行接下来的操作
*/
boolean before(Object target, Method method, Object[] args);
@ -24,15 +24,15 @@ public interface Aspect{
/**
* 目标方法执行后的操作
* 如果 target.method 抛出异常且
*
* @param target 目标对象
* @param method 目标方法
* @param args 参数
* @param returnVal 目标方法执行返回值
* @return 是否允许返回值接下来的操作
* @see Aspect#afterException 返回true,则不会执行此操作
* 如果
* @see Aspect#afterException 返回false,则无论target.method是否抛出异常均会执行此操作
*
* @param target 目标对象
* @param method 目标方法
* @param args 参数
* @param returnVal 目标方法执行返回值
* @return 是否允许返回值接下来的操作
*/
boolean after(Object target, Method method, Object[] args, Object returnVal);
@ -41,8 +41,8 @@ public interface Aspect{
*
* @param target 目标对象
* @param method 目标方法
* @param args 参数
* @param e 异常
* @param args 参数
* @param e 异常
* @return 是否允许抛出异常
*/
boolean afterException(Object target, Method method, Object[] args, Throwable e);

View File

@ -7,37 +7,41 @@ import java.lang.reflect.Method;
* 简单切面类不做任何操作<br>
* 可以继承此类实现自己需要的方法即可
*
* @author Looly
* @author ted.L
*
* @author Looly, ted.L
*/
public abstract class SimpleAspect implements Aspect, Serializable{
public class SimpleAspect implements Aspect, Serializable {
private static final long serialVersionUID = 1L;
/**
* @see Aspect#before(Object, Method, Object[])
* @return 是否继续执行接下来的操作 默认值true
*/
@Override
public boolean before(Object target, Method method, Object[] args) {
//继承此类后实现此方法
return true;
}
/**
* @see Aspect#after(Object, Method, Object[], Object)
* @return 是否允许返回值接下来的操作 默认值true
*/
/**
* 目标方法执行后的操作
* 如果 target.method 抛出异常且
*
* @param target 目标对象
* @param method 目标方法
* @param args 参数
* @return 是否允许返回值接下来的操作
* @see Aspect#afterException 返回true,则不会执行此操作
* 如果
* @see Aspect#afterException 返回false,则无论target.method是否抛出异常均会执行此操作
*/
public boolean after(Object target, Method method, Object[] args) {
//继承此类后实现此方法
return after(target, method, args, null);
}
@Override
public boolean after(Object target, Method method, Object[] args, Object returnVal) {
//继承此类后实现此方法
return true;
}
/**
* @see Aspect#afterException(Object, Method, Object[], Throwable)
* @return 是否允许抛出异常 默认值true
*/
@Override
public boolean afterException(Object target, Method method, Object[] args, Throwable e) {
//继承此类后实现此方法

View File

@ -8,23 +8,27 @@ import cn.hutool.core.util.StrUtil;
/**
* 通过日志打印方法的执行时间的切面
* @author Looly
*
* @author Looly
*/
public class TimeIntervalAspect extends SimpleAspect {
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;
private TimeInterval interval = new TimeInterval();
private TimeInterval interval = new TimeInterval();
@Override
public boolean before(Object target, Method method, Object[] args) {
interval.start();
return true;
}
@Override
public boolean before(Object target, Method method, Object[] args) {
interval.start();
return true;
}
@Override
public boolean after(Object target, Method method, Object[] args, Object returnVal) {
Console.log("Method [{}.{}] execute spend [{}]ms return value [{}]", target.getClass().getName(), method.getName(), interval.intervalMs(), StrUtil.toString(returnVal));
return true;
}
@Override
public boolean after(Object target, Method method, Object[] args, Object returnVal) {
Console.log("Method [{}.{}] execute spend [{}]ms return value [{}]",
target.getClass().getName(), //
method.getName(), //
interval.intervalMs(), //
returnVal);
return true;
}
}

View File

@ -1,20 +1,17 @@
package cn.hutool.aop.interceptor;
import cn.hutool.aop.aspects.Aspect;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import cn.hutool.aop.aspects.Aspect;
import cn.hutool.core.exceptions.UtilException;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
/**
* Cglib实现的动态代理切面
*
* @author looly
* @author ted.L
*
* @author looly, ted.L
*/
public class CglibInterceptor implements MethodInterceptor, Serializable {
private static final long serialVersionUID = 1L;
@ -40,22 +37,20 @@ public class CglibInterceptor implements MethodInterceptor, Serializable {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
Object result = null;
// 开始前回调
if (aspect.before(target, method, args)) {
try {
// result = ReflectUtil.invoke(target, method, args);
result = proxy.invokeSuper(obj, args);
} catch (UtilException e) {
final Throwable cause = e.getCause();
if (!(e.getCause() instanceof InvocationTargetException)) {
// 其它异常属于代理的异常直接抛出
} catch (InvocationTargetException e) {
// 异常回调只捕获业务代码导致的异常而非反射导致的异常
if (aspect.afterException(target, method, args, e.getTargetException())) {
throw e;
}
if(aspect.afterException(target, method, args, ((InvocationTargetException) cause).getTargetException())){
throw e;
}
}
}
// 结束执行回调
if (aspect.after(target, method, args, result)) {
return result;
}

View File

@ -7,6 +7,7 @@ import java.lang.reflect.Method;
import cn.hutool.aop.aspects.Aspect;
import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ReflectUtil;
/**
@ -14,9 +15,8 @@ import cn.hutool.core.util.ReflectUtil;
*
* @author Looly
* @author ted.L
*
*/
public class JdkInterceptor implements InvocationHandler, Serializable{
public class JdkInterceptor implements InvocationHandler, Serializable {
private static final long serialVersionUID = 1L;
private Object target;
@ -42,20 +42,22 @@ public class JdkInterceptor implements InvocationHandler, Serializable{
final Object target = this.target;
final Aspect aspect = this.aspect;
Object result = null;
// 开始前回调
if (aspect.before(target, method, args)) {
ReflectUtil.setAccessible(method);
try {
result = ReflectUtil.invoke(target, method, args);
} catch (UtilException e) {
final Throwable cause = e.getCause();
if (!(e.getCause() instanceof InvocationTargetException)) {
// 其它异常属于代理的异常直接抛出
throw e;
}
if(aspect.afterException(target, method, args, ((InvocationTargetException) cause).getTargetException())){
result = method.invoke(ClassUtil.isStatic(method) ? null : target, args);
} catch (InvocationTargetException e) {
// 异常回调只捕获业务代码导致的异常而非反射导致的异常
if (aspect.afterException(target, method, args, e.getTargetException())) {
throw e;
}
}
}
// 结束执行回调
if (aspect.after(target, method, args, result)) {
return result;
}

View File

@ -8,14 +8,16 @@ import cn.hutool.aop.interceptor.JdkInterceptor;
* JDK实现的切面代理
*
* @author looly
*
*/
public class JdkProxyFactory extends ProxyFactory{
public class JdkProxyFactory extends ProxyFactory {
private static final long serialVersionUID = 1L;
@Override
@SuppressWarnings("unchecked")
public <T> T proxy(T target, Aspect aspect) {
return (T) ProxyUtil.newProxyInstance(target.getClass().getClassLoader(), new JdkInterceptor(target, aspect), target.getClass().getInterfaces());
return (T) ProxyUtil.newProxyInstance(//
target.getClass().getClassLoader(), //
new JdkInterceptor(target, aspect), //
target.getClass().getInterfaces());
}
}

View File

@ -1,5 +1,6 @@
package cn.hutool.aop.test;
import cn.hutool.core.lang.Console;
import org.junit.Assert;
import org.junit.Test;
@ -10,64 +11,61 @@ import cn.hutool.aop.aspects.TimeIntervalAspect;
* AOP模块单元测试
*
* @author Looly
*
*/
public class AopTest {
@Test
public void aopTest() {
Animal cat = ProxyUtil.proxy(new Cat(), TimeIntervalAspect.class);
String result = cat.eat();
Assert.assertEquals("猫吃鱼", result);
cat.seize();
}
@Test
public void aopTest() {
Animal cat = ProxyUtil.proxy(new Cat(), TimeIntervalAspect.class);
String result = cat.eat();
Assert.assertEquals("猫吃鱼", result);
cat.seize();
}
@Test
public void aopByCglibTest() {
Dog dog = ProxyUtil.proxy(new Dog(), TimeIntervalAspect.class);
String result = dog.eat();
Assert.assertEquals("狗吃肉", result);
@Test
public void aopByCglibTest() {
Dog dog = ProxyUtil.proxy(new Dog(), TimeIntervalAspect.class);
String result = dog.eat();
Assert.assertEquals("狗吃肉", result);
dog.seize();
}
}
interface Animal {
String eat();
interface Animal {
String eat();
void seize();
}
void seize();
}
/**
* 有接口
*
* @author looly
*
*/
static class Cat implements Animal {
/**
* 有接口
*
* @author looly
*/
static class Cat implements Animal {
@Override
public String eat() {
return "猫吃鱼";
}
@Override
public String eat() {
return "猫吃鱼";
}
@Override
public void seize() {
System.out.println("抓了条鱼");
}
}
@Override
public void seize() {
Console.log("抓了条鱼");
}
}
/**
* 无接口
*
* @author looly
*
*/
static class Dog {
public String eat() {
return "狗吃肉";
}
/**
* 无接口
*
* @author looly
*/
static class Dog {
public String eat() {
return "狗吃肉";
}
public void seize() {
System.out.println("抓了只鸡");
}
}
public void seize() {
Console.log("抓了只鸡");
}
}
}

View File

@ -1,13 +1,5 @@
package cn.hutool.core.util;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.exceptions.UtilException;
@ -15,6 +7,15 @@ import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Filter;
import cn.hutool.core.lang.SimpleCache;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* 反射工具类
*
@ -23,19 +24,26 @@ import cn.hutool.core.lang.SimpleCache;
*/
public class ReflectUtil {
/** 构造对象缓存 */
/**
* 构造对象缓存
*/
private static final SimpleCache<Class<?>, Constructor<?>[]> CONSTRUCTORS_CACHE = new SimpleCache<>();
/** 字段缓存 */
/**
* 字段缓存
*/
private static final SimpleCache<Class<?>, Field[]> FIELDS_CACHE = new SimpleCache<>();
/** 方法缓存 */
/**
* 方法缓存
*/
private static final SimpleCache<Class<?>, Method[]> METHODS_CACHE = new SimpleCache<>();
// --------------------------------------------------------------------------------------------------------- Constructor
/**
* 查找类中的指定参数的构造方法如果找到构造方法会自动设置可访问为true
*
* @param <T> 对象类型
* @param clazz
* @param <T> 对象类型
* @param clazz
* @param parameterTypes 参数类型只要任何一个参数是指定参数的父类或接口或相等即可此参数可以不传
* @return 构造方法如果未找到返回null
*/
@ -51,7 +59,7 @@ public class ReflectUtil {
pts = constructor.getParameterTypes();
if (ClassUtil.isAllAssignableFrom(pts, parameterTypes)) {
// 构造可访问
constructor.setAccessible(true);
setAccessible(constructor);
return (Constructor<T>) constructor;
}
}
@ -61,7 +69,7 @@ public class ReflectUtil {
/**
* 获得一个类中所有构造列表
*
* @param <T> 构造的对象类型
* @param <T> 构造的对象类型
* @param beanClass
* @return 字段列表
* @throws SecurityException 安全检查异常
@ -91,11 +99,12 @@ public class ReflectUtil {
}
// --------------------------------------------------------------------------------------------------------- Field
/**
* 查找指定类中是否包含指定名称对应的字段包括所有字段包括非public字段也包括父类和Object类的字段
*
* @param beanClass 被查找字段的类,不能为null
* @param name 字段名
* @param name 字段名
* @return 是否包含字段
* @throws SecurityException 安全异常
* @since 4.1.21
@ -108,7 +117,7 @@ public class ReflectUtil {
* 查找指定类中的所有字段包括非public字段也包括父类和Object类的字段 字段不存在则返回<code>null</code>
*
* @param beanClass 被查找字段的类,不能为null
* @param name 字段名
* @param name 字段名
* @return 字段
* @throws SecurityException 安全异常
*/
@ -144,7 +153,7 @@ public class ReflectUtil {
/**
* 获得一个类中所有字段列表直接反射获取无缓存
*
* @param beanClass
* @param beanClass
* @param withSuperClassFieds 是否包括父类的字段列表
* @return 字段列表
* @throws SecurityException 安全检查异常
@ -171,7 +180,7 @@ public class ReflectUtil {
/**
* 获取字段值
*
* @param obj 对象
* @param obj 对象
* @param fieldName 字段名
* @return 字段值
* @throws UtilException 包装IllegalAccessException异常
@ -186,7 +195,7 @@ public class ReflectUtil {
/**
* 获取字段值
*
* @param obj 对象
* @param obj 对象
* @param field 字段
* @return 字段值
* @throws UtilException 包装IllegalAccessException异常
@ -195,8 +204,8 @@ public class ReflectUtil {
if (null == obj || null == field) {
return null;
}
field.setAccessible(true);
Object result = null;
setAccessible(field);
Object result;
try {
result = field.get(obj);
} catch (IllegalAccessException e) {
@ -207,6 +216,7 @@ public class ReflectUtil {
/**
* 获取所有字段的值
*
* @param obj bean对象
* @return 字段值数组
* @since 4.1.17
@ -228,9 +238,9 @@ public class ReflectUtil {
/**
* 设置字段值
*
* @param obj 对象
* @param obj 对象
* @param fieldName 字段名
* @param value 值类型必须与字段类型匹配不会自动转换对象类型
* @param value 值类型必须与字段类型匹配不会自动转换对象类型
* @throws UtilException 包装IllegalAccessException异常
*/
public static void setFieldValue(Object obj, String fieldName, Object value) throws UtilException {
@ -245,7 +255,7 @@ public class ReflectUtil {
/**
* 设置字段值
*
* @param obj 对象
* @param obj 对象
* @param field 字段
* @param value 值类型必须与字段类型匹配不会自动转换对象类型
* @throws UtilException UtilException 包装IllegalAccessException异常
@ -253,14 +263,15 @@ public class ReflectUtil {
public static void setFieldValue(Object obj, Field field, Object value) throws UtilException {
Assert.notNull(obj);
Assert.notNull(field, "Field in [{}] not exist !", obj.getClass().getName());
field.setAccessible(true);
if(null != value) {
setAccessible(field);
if (null != value) {
Class<?> fieldType = field.getType();
if(false == fieldType.isAssignableFrom(value.getClass())) {
if (false == fieldType.isAssignableFrom(value.getClass())) {
//对于类型不同的字段尝试转换转换失败则使用原对象类型
final Object targetValue = Convert.convert(fieldType, value);
if(null != targetValue) {
if (null != targetValue) {
value = targetValue;
}
}
@ -274,6 +285,7 @@ public class ReflectUtil {
}
// --------------------------------------------------------------------------------------------------------- method
/**
* 获得指定类本类及其父类中的Public方法名<br>
* 去重重载的方法
@ -282,9 +294,9 @@ public class ReflectUtil {
* @return 方法名Set
*/
public static Set<String> getPublicMethodNames(Class<?> clazz) {
final HashSet<String> methodSet = new HashSet<String>();
final HashSet<String> methodSet = new HashSet<>();
final Method[] methodArray = getPublicMethods(clazz);
if(ArrayUtil.isNotEmpty(methodArray)) {
if (ArrayUtil.isNotEmpty(methodArray)) {
for (Method method : methodArray) {
methodSet.add(method.getName());
}
@ -305,7 +317,7 @@ public class ReflectUtil {
/**
* 获得指定类过滤后的Public方法列表
*
* @param clazz 查找方法的类
* @param clazz 查找方法的类
* @param filter 过滤器
* @return 过滤后的方法列表
*/
@ -332,7 +344,7 @@ public class ReflectUtil {
/**
* 获得指定类过滤后的Public方法列表
*
* @param clazz 查找方法的类
* @param clazz 查找方法的类
* @param excludeMethods 不包括的方法
* @return 过滤后的方法列表
*/
@ -349,7 +361,7 @@ public class ReflectUtil {
/**
* 获得指定类过滤后的Public方法列表
*
* @param clazz 查找方法的类
* @param clazz 查找方法的类
* @param excludeMethodNames 不包括的方法名列表
* @return 过滤后的方法列表
*/
@ -366,7 +378,7 @@ public class ReflectUtil {
/**
* 查找指定Public方法 如果找不到对应的方法或方法不为public的则返回<code>null</code>
*
* @param clazz
* @param clazz
* @param methodName 方法名
* @param paramTypes 参数类型
* @return 方法
@ -387,9 +399,9 @@ public class ReflectUtil {
* 此方法为精准获取方法名即方法名和参数数量和类型必须一致否则返回<code>null</code>
* </p>
*
* @param obj 被查找的对象如果为{@code null}返回{@code null}
* @param obj 被查找的对象如果为{@code null}返回{@code null}
* @param methodName 方法名如果为空字符串返回{@code null}
* @param args 参数
* @param args 参数
* @return 方法
* @throws SecurityException 无访问权限抛出异常
*/
@ -407,7 +419,7 @@ public class ReflectUtil {
* 此方法为精准获取方法名即方法名和参数数量和类型必须一致否则返回<code>null</code>
* </p>
*
* @param clazz 如果为{@code null}返回{@code null}
* @param clazz 如果为{@code null}返回{@code null}
* @param methodName 方法名如果为空字符串返回{@code null}
* @param paramTypes 参数类型指定参数类型如果是方法的子类也算
* @return 方法
@ -425,7 +437,7 @@ public class ReflectUtil {
* 此方法为精准获取方法名即方法名和参数数量和类型必须一致否则返回<code>null</code>
* </p>
*
* @param clazz 如果为{@code null}返回{@code null}
* @param clazz 如果为{@code null}返回{@code null}
* @param methodName 方法名如果为空字符串返回{@code null}
* @param paramTypes 参数类型指定参数类型如果是方法的子类也算
* @return 方法
@ -442,7 +454,7 @@ public class ReflectUtil {
* 此方法为精准获取方法名即方法名和参数数量和类型必须一致否则返回<code>null</code>
* </p>
*
* @param clazz 如果为{@code null}返回{@code null}
* @param clazz 如果为{@code null}返回{@code null}
* @param ignoreCase 是否忽略大小写
* @param methodName 方法名如果为空字符串返回{@code null}
* @param paramTypes 参数类型指定参数类型如果是方法的子类也算
@ -475,7 +487,7 @@ public class ReflectUtil {
* 此方法只检查方法名是否一致并不检查参数的一致性
* </p>
*
* @param clazz 如果为{@code null}返回{@code null}
* @param clazz 如果为{@code null}返回{@code null}
* @param methodName 方法名如果为空字符串返回{@code null}
* @return 方法
* @throws SecurityException 无权访问抛出异常
@ -492,7 +504,7 @@ public class ReflectUtil {
* 此方法只检查方法名是否一致忽略大小写并不检查参数的一致性
* </p>
*
* @param clazz 如果为{@code null}返回{@code null}
* @param clazz 如果为{@code null}返回{@code null}
* @param methodName 方法名如果为空字符串返回{@code null}
* @return 方法
* @throws SecurityException 无权访问抛出异常
@ -509,7 +521,7 @@ public class ReflectUtil {
* 此方法只检查方法名是否一致并不检查参数的一致性
* </p>
*
* @param clazz 如果为{@code null}返回{@code null}
* @param clazz 如果为{@code null}返回{@code null}
* @param ignoreCase 是否忽略大小写
* @param methodName 方法名如果为空字符串返回{@code null}
* @return 方法
@ -541,7 +553,7 @@ public class ReflectUtil {
* @throws SecurityException 安全异常
*/
public static Set<String> getMethodNames(Class<?> clazz) throws SecurityException {
final HashSet<String> methodSet = new HashSet<String>();
final HashSet<String> methodSet = new HashSet<>();
final Method[] methods = getMethods(clazz);
for (Method method : methods) {
methodSet.add(method.getName());
@ -552,7 +564,7 @@ public class ReflectUtil {
/**
* 获得指定类过滤后的Public方法列表
*
* @param clazz 查找方法的类
* @param clazz 查找方法的类
* @param filter 过滤器
* @return 过滤后的方法列表
* @throws SecurityException 安全异常
@ -584,7 +596,7 @@ public class ReflectUtil {
/**
* 获得一个类中所有方法列表直接反射获取无缓存
*
* @param beanClass
* @param beanClass
* @param withSuperClassMethods 是否包括父类的方法列表
* @return 方法列表
* @throws SecurityException 安全检查异常
@ -643,10 +655,11 @@ public class ReflectUtil {
}
// --------------------------------------------------------------------------------------------------------- newInstance
/**
* 实例化对象
*
* @param <T> 对象类型
* @param <T> 对象类型
* @param clazz 类名
* @return 对象
* @throws UtilException 包装各类异常
@ -663,8 +676,8 @@ public class ReflectUtil {
/**
* 实例化对象
*
* @param <T> 对象类型
* @param clazz
* @param <T> 对象类型
* @param clazz
* @param params 构造函数参数
* @return 对象
* @throws UtilException 包装各类异常
@ -682,7 +695,7 @@ public class ReflectUtil {
final Class<?>[] paramTypes = ClassUtil.getClasses(params);
final Constructor<T> constructor = getConstructor(clazz, paramTypes);
if (null == constructor) {
throw new UtilException("No Constructor matched for parameter types: [{}]", new Object[] { paramTypes });
throw new UtilException("No Constructor matched for parameter types: [{}]", new Object[]{paramTypes});
}
try {
return constructor.newInstance(params);
@ -694,7 +707,7 @@ public class ReflectUtil {
/**
* 尝试遍历并调用此类的所有构造方法直到构造成功并返回
*
* @param <T> 对象类型
* @param <T> 对象类型
* @param beanClass 被构造的类
* @return 构造后的对象
*/
@ -714,24 +727,24 @@ public class ReflectUtil {
if (0 == parameterTypes.length) {
continue;
}
constructor.setAccessible(true);
setAccessible(constructor);
try {
return constructor.newInstance(ClassUtil.getDefaultValues(parameterTypes));
} catch (Exception e) {
} catch (Exception ignore) {
// 构造出错时继续尝试下一种构造方式
continue;
}
}
return null;
}
// --------------------------------------------------------------------------------------------------------- invoke
/**
* 执行静态方法
*
* @param <T> 对象类型
* @param <T> 对象类型
* @param method 方法对象方法或static方法都可
* @param args 参数对象
* @param args 参数对象
* @return 结果
* @throws UtilException 多种异常包装
*/
@ -748,10 +761,10 @@ public class ReflectUtil {
* 2. 如果某个参数为null但是方法这个位置的参数为原始类型则赋予原始类型默认值
* </pre>
*
* @param <T> 返回对象类型
* @param obj 对象如果执行静态方法此值为<code>null</code>
* @param <T> 返回对象类型
* @param obj 对象如果执行静态方法此值为<code>null</code>
* @param method 方法对象方法或static方法都可
* @param args 参数对象
* @param args 参数对象
* @return 结果
* @throws UtilException 一些列异常的包装
*/
@ -775,18 +788,16 @@ public class ReflectUtil {
/**
* 执行方法
*
* @param <T> 返回对象类型
* @param obj 对象如果执行静态方法此值为<code>null</code>
* @param <T> 返回对象类型
* @param obj 对象如果执行静态方法此值为<code>null</code>
* @param method 方法对象方法或static方法都可
* @param args 参数对象
* @param args 参数对象
* @return 结果
* @throws UtilException 一些列异常的包装
*/
@SuppressWarnings("unchecked")
public static <T> T invoke(Object obj, Method method, Object... args) throws UtilException {
if (false == method.isAccessible()) {
method.setAccessible(true);
}
setAccessible(method);
try {
return (T) method.invoke(ClassUtil.isStatic(method) ? null : obj, args);
@ -798,10 +809,10 @@ public class ReflectUtil {
/**
* 执行对象中指定方法
*
* @param <T> 返回对象类型
* @param obj 方法所在对象
* @param <T> 返回对象类型
* @param obj 方法所在对象
* @param methodName 方法名
* @param args 参数列表
* @param args 参数列表
* @return 执行结果
* @throws UtilException IllegalAccessException包装
* @since 3.1.2
@ -813,4 +824,18 @@ public class ReflectUtil {
}
return invoke(obj, method, args);
}
/**
* 设置方法为可访问私有方法可以被外部调用
*
* @param <T> AccessibleObject的子类比如ClassMethodField等
* @param accessibleObject 可设置访问权限的对象比如ClassMethodField等
* @since 4.6.8
*/
public static <T extends AccessibleObject> T setAccessible(T accessibleObject) {
if (null != accessibleObject && false == accessibleObject.isAccessible()) {
accessibleObject.setAccessible(true);
}
return accessibleObject;
}
}