mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
enhance aop
This commit is contained in:
parent
5b5645cbbb
commit
45609bc22b
@ -9,14 +9,14 @@ import java.lang.reflect.Method;
|
|||||||
* @author ted.L
|
* @author ted.L
|
||||||
* @since 4.18
|
* @since 4.18
|
||||||
*/
|
*/
|
||||||
public interface Aspect{
|
public interface Aspect {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 目标方法执行前的操作
|
* 目标方法执行前的操作
|
||||||
*
|
*
|
||||||
* @param target 目标对象
|
* @param target 目标对象
|
||||||
* @param method 目标方法
|
* @param method 目标方法
|
||||||
* @param args 参数
|
* @param args 参数
|
||||||
* @return 是否继续执行接下来的操作
|
* @return 是否继续执行接下来的操作
|
||||||
*/
|
*/
|
||||||
boolean before(Object target, Method method, Object[] args);
|
boolean before(Object target, Method method, Object[] args);
|
||||||
@ -24,15 +24,15 @@ public interface Aspect{
|
|||||||
/**
|
/**
|
||||||
* 目标方法执行后的操作
|
* 目标方法执行后的操作
|
||||||
* 如果 target.method 抛出异常且
|
* 如果 target.method 抛出异常且
|
||||||
|
*
|
||||||
|
* @param target 目标对象
|
||||||
|
* @param method 目标方法
|
||||||
|
* @param args 参数
|
||||||
|
* @param returnVal 目标方法执行返回值
|
||||||
|
* @return 是否允许返回值(接下来的操作)
|
||||||
* @see Aspect#afterException 返回true,则不会执行此操作
|
* @see Aspect#afterException 返回true,则不会执行此操作
|
||||||
* 如果
|
* 如果
|
||||||
* @see Aspect#afterException 返回false,则无论target.method是否抛出异常,均会执行此操作
|
* @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);
|
boolean after(Object target, Method method, Object[] args, Object returnVal);
|
||||||
|
|
||||||
@ -41,8 +41,8 @@ public interface Aspect{
|
|||||||
*
|
*
|
||||||
* @param target 目标对象
|
* @param target 目标对象
|
||||||
* @param method 目标方法
|
* @param method 目标方法
|
||||||
* @param args 参数
|
* @param args 参数
|
||||||
* @param e 异常
|
* @param e 异常
|
||||||
* @return 是否允许抛出异常
|
* @return 是否允许抛出异常
|
||||||
*/
|
*/
|
||||||
boolean afterException(Object target, Method method, Object[] args, Throwable e);
|
boolean afterException(Object target, Method method, Object[] args, Throwable e);
|
||||||
|
@ -7,37 +7,41 @@ import java.lang.reflect.Method;
|
|||||||
* 简单切面类,不做任何操作<br>
|
* 简单切面类,不做任何操作<br>
|
||||||
* 可以继承此类实现自己需要的方法即可
|
* 可以继承此类实现自己需要的方法即可
|
||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly, ted.L
|
||||||
* @author ted.L
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public abstract class SimpleAspect implements Aspect, Serializable{
|
public class SimpleAspect implements Aspect, Serializable {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
/**
|
|
||||||
* @see Aspect#before(Object, Method, Object[])
|
|
||||||
* @return 是否继续执行接下来的操作 默认值true
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public boolean before(Object target, Method method, Object[] args) {
|
public boolean before(Object target, Method method, Object[] args) {
|
||||||
//继承此类后实现此方法
|
//继承此类后实现此方法
|
||||||
return true;
|
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
|
@Override
|
||||||
public boolean after(Object target, Method method, Object[] args, Object returnVal) {
|
public boolean after(Object target, Method method, Object[] args, Object returnVal) {
|
||||||
//继承此类后实现此方法
|
//继承此类后实现此方法
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @see Aspect#afterException(Object, Method, Object[], Throwable)
|
|
||||||
* @return 是否允许抛出异常 默认值true
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public boolean afterException(Object target, Method method, Object[] args, Throwable e) {
|
public boolean afterException(Object target, Method method, Object[] args, Throwable e) {
|
||||||
//继承此类后实现此方法
|
//继承此类后实现此方法
|
||||||
|
@ -8,23 +8,27 @@ import cn.hutool.core.util.StrUtil;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过日志打印方法的执行时间的切面
|
* 通过日志打印方法的执行时间的切面
|
||||||
* @author Looly
|
|
||||||
*
|
*
|
||||||
|
* @author Looly
|
||||||
*/
|
*/
|
||||||
public class TimeIntervalAspect extends SimpleAspect {
|
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
|
@Override
|
||||||
public boolean before(Object target, Method method, Object[] args) {
|
public boolean before(Object target, Method method, Object[] args) {
|
||||||
interval.start();
|
interval.start();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean after(Object target, Method method, Object[] args, Object returnVal) {
|
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));
|
Console.log("Method [{}.{}] execute spend [{}]ms return value [{}]",
|
||||||
return true;
|
target.getClass().getName(), //
|
||||||
}
|
method.getName(), //
|
||||||
|
interval.intervalMs(), //
|
||||||
|
returnVal);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,17 @@
|
|||||||
package cn.hutool.aop.interceptor;
|
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.io.Serializable;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
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实现的动态代理切面
|
* Cglib实现的动态代理切面
|
||||||
*
|
*
|
||||||
* @author looly
|
* @author looly, ted.L
|
||||||
* @author ted.L
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class CglibInterceptor implements MethodInterceptor, Serializable {
|
public class CglibInterceptor implements MethodInterceptor, Serializable {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
@ -40,22 +37,20 @@ public class CglibInterceptor implements MethodInterceptor, Serializable {
|
|||||||
@Override
|
@Override
|
||||||
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
|
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
|
||||||
Object result = null;
|
Object result = null;
|
||||||
|
|
||||||
|
// 开始前回调
|
||||||
if (aspect.before(target, method, args)) {
|
if (aspect.before(target, method, args)) {
|
||||||
try {
|
try {
|
||||||
// result = ReflectUtil.invoke(target, method, args);
|
|
||||||
result = proxy.invokeSuper(obj, args);
|
result = proxy.invokeSuper(obj, args);
|
||||||
} catch (UtilException e) {
|
} catch (InvocationTargetException e) {
|
||||||
final Throwable cause = e.getCause();
|
// 异常回调(只捕获业务代码导致的异常,而非反射导致的异常)
|
||||||
if (!(e.getCause() instanceof InvocationTargetException)) {
|
if (aspect.afterException(target, method, args, e.getTargetException())) {
|
||||||
// 其它异常属于代理的异常,直接抛出
|
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
if(aspect.afterException(target, method, args, ((InvocationTargetException) cause).getTargetException())){
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 结束执行回调
|
||||||
if (aspect.after(target, method, args, result)) {
|
if (aspect.after(target, method, args, result)) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ import java.lang.reflect.Method;
|
|||||||
|
|
||||||
import cn.hutool.aop.aspects.Aspect;
|
import cn.hutool.aop.aspects.Aspect;
|
||||||
import cn.hutool.core.exceptions.UtilException;
|
import cn.hutool.core.exceptions.UtilException;
|
||||||
|
import cn.hutool.core.util.ClassUtil;
|
||||||
import cn.hutool.core.util.ReflectUtil;
|
import cn.hutool.core.util.ReflectUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -14,9 +15,8 @@ import cn.hutool.core.util.ReflectUtil;
|
|||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
* @author ted.L
|
* @author ted.L
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class JdkInterceptor implements InvocationHandler, Serializable{
|
public class JdkInterceptor implements InvocationHandler, Serializable {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private Object target;
|
private Object target;
|
||||||
@ -42,20 +42,22 @@ public class JdkInterceptor implements InvocationHandler, Serializable{
|
|||||||
final Object target = this.target;
|
final Object target = this.target;
|
||||||
final Aspect aspect = this.aspect;
|
final Aspect aspect = this.aspect;
|
||||||
Object result = null;
|
Object result = null;
|
||||||
|
|
||||||
|
// 开始前回调
|
||||||
if (aspect.before(target, method, args)) {
|
if (aspect.before(target, method, args)) {
|
||||||
|
ReflectUtil.setAccessible(method);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
result = ReflectUtil.invoke(target, method, args);
|
result = method.invoke(ClassUtil.isStatic(method) ? null : target, args);
|
||||||
} catch (UtilException e) {
|
} catch (InvocationTargetException e) {
|
||||||
final Throwable cause = e.getCause();
|
// 异常回调(只捕获业务代码导致的异常,而非反射导致的异常)
|
||||||
if (!(e.getCause() instanceof InvocationTargetException)) {
|
if (aspect.afterException(target, method, args, e.getTargetException())) {
|
||||||
// 其它异常属于代理的异常,直接抛出
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
if(aspect.afterException(target, method, args, ((InvocationTargetException) cause).getTargetException())){
|
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 结束执行回调
|
||||||
if (aspect.after(target, method, args, result)) {
|
if (aspect.after(target, method, args, result)) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -8,14 +8,16 @@ import cn.hutool.aop.interceptor.JdkInterceptor;
|
|||||||
* JDK实现的切面代理
|
* JDK实现的切面代理
|
||||||
*
|
*
|
||||||
* @author looly
|
* @author looly
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class JdkProxyFactory extends ProxyFactory{
|
public class JdkProxyFactory extends ProxyFactory {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <T> T proxy(T target, Aspect aspect) {
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package cn.hutool.aop.test;
|
package cn.hutool.aop.test;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.Console;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@ -10,64 +11,61 @@ import cn.hutool.aop.aspects.TimeIntervalAspect;
|
|||||||
* AOP模块单元测试
|
* AOP模块单元测试
|
||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class AopTest {
|
public class AopTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void aopTest() {
|
public void aopTest() {
|
||||||
Animal cat = ProxyUtil.proxy(new Cat(), TimeIntervalAspect.class);
|
Animal cat = ProxyUtil.proxy(new Cat(), TimeIntervalAspect.class);
|
||||||
String result = cat.eat();
|
String result = cat.eat();
|
||||||
Assert.assertEquals("猫吃鱼", result);
|
Assert.assertEquals("猫吃鱼", result);
|
||||||
cat.seize();
|
cat.seize();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void aopByCglibTest() {
|
public void aopByCglibTest() {
|
||||||
Dog dog = ProxyUtil.proxy(new Dog(), TimeIntervalAspect.class);
|
Dog dog = ProxyUtil.proxy(new Dog(), TimeIntervalAspect.class);
|
||||||
String result = dog.eat();
|
String result = dog.eat();
|
||||||
Assert.assertEquals("狗吃肉", result);
|
Assert.assertEquals("狗吃肉", result);
|
||||||
dog.seize();
|
dog.seize();
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Animal {
|
interface Animal {
|
||||||
String eat();
|
String eat();
|
||||||
|
|
||||||
void seize();
|
void seize();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 有接口
|
* 有接口
|
||||||
*
|
*
|
||||||
* @author looly
|
* @author looly
|
||||||
*
|
*/
|
||||||
*/
|
static class Cat implements Animal {
|
||||||
static class Cat implements Animal {
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String eat() {
|
public String eat() {
|
||||||
return "猫吃鱼";
|
return "猫吃鱼";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void seize() {
|
public void seize() {
|
||||||
System.out.println("抓了条鱼");
|
Console.log("抓了条鱼");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 无接口
|
* 无接口
|
||||||
*
|
*
|
||||||
* @author looly
|
* @author looly
|
||||||
*
|
*/
|
||||||
*/
|
static class Dog {
|
||||||
static class Dog {
|
public String eat() {
|
||||||
public String eat() {
|
return "狗吃肉";
|
||||||
return "狗吃肉";
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void seize() {
|
public void seize() {
|
||||||
System.out.println("抓了只鸡");
|
Console.log("抓了只鸡");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,5 @@
|
|||||||
package cn.hutool.core.util;
|
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.collection.CollectionUtil;
|
||||||
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.convert.Convert;
|
||||||
import cn.hutool.core.exceptions.UtilException;
|
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.Filter;
|
||||||
import cn.hutool.core.lang.SimpleCache;
|
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 {
|
public class ReflectUtil {
|
||||||
|
|
||||||
/** 构造对象缓存 */
|
/**
|
||||||
|
* 构造对象缓存
|
||||||
|
*/
|
||||||
private static final SimpleCache<Class<?>, Constructor<?>[]> CONSTRUCTORS_CACHE = new SimpleCache<>();
|
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<?>, Field[]> FIELDS_CACHE = new SimpleCache<>();
|
||||||
/** 方法缓存 */
|
/**
|
||||||
|
* 方法缓存
|
||||||
|
*/
|
||||||
private static final SimpleCache<Class<?>, Method[]> METHODS_CACHE = new SimpleCache<>();
|
private static final SimpleCache<Class<?>, Method[]> METHODS_CACHE = new SimpleCache<>();
|
||||||
|
|
||||||
// --------------------------------------------------------------------------------------------------------- Constructor
|
// --------------------------------------------------------------------------------------------------------- Constructor
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查找类中的指定参数的构造方法,如果找到构造方法,会自动设置可访问为true
|
* 查找类中的指定参数的构造方法,如果找到构造方法,会自动设置可访问为true
|
||||||
*
|
*
|
||||||
* @param <T> 对象类型
|
* @param <T> 对象类型
|
||||||
* @param clazz 类
|
* @param clazz 类
|
||||||
* @param parameterTypes 参数类型,只要任何一个参数是指定参数的父类或接口或相等即可,此参数可以不传
|
* @param parameterTypes 参数类型,只要任何一个参数是指定参数的父类或接口或相等即可,此参数可以不传
|
||||||
* @return 构造方法,如果未找到返回null
|
* @return 构造方法,如果未找到返回null
|
||||||
*/
|
*/
|
||||||
@ -51,7 +59,7 @@ public class ReflectUtil {
|
|||||||
pts = constructor.getParameterTypes();
|
pts = constructor.getParameterTypes();
|
||||||
if (ClassUtil.isAllAssignableFrom(pts, parameterTypes)) {
|
if (ClassUtil.isAllAssignableFrom(pts, parameterTypes)) {
|
||||||
// 构造可访问
|
// 构造可访问
|
||||||
constructor.setAccessible(true);
|
setAccessible(constructor);
|
||||||
return (Constructor<T>) constructor;
|
return (Constructor<T>) constructor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -61,7 +69,7 @@ public class ReflectUtil {
|
|||||||
/**
|
/**
|
||||||
* 获得一个类中所有构造列表
|
* 获得一个类中所有构造列表
|
||||||
*
|
*
|
||||||
* @param <T> 构造的对象类型
|
* @param <T> 构造的对象类型
|
||||||
* @param beanClass 类
|
* @param beanClass 类
|
||||||
* @return 字段列表
|
* @return 字段列表
|
||||||
* @throws SecurityException 安全检查异常
|
* @throws SecurityException 安全检查异常
|
||||||
@ -91,11 +99,12 @@ public class ReflectUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------------------------------------------------------- Field
|
// --------------------------------------------------------------------------------------------------------- Field
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查找指定类中是否包含指定名称对应的字段,包括所有字段(包括非public字段),也包括父类和Object类的字段
|
* 查找指定类中是否包含指定名称对应的字段,包括所有字段(包括非public字段),也包括父类和Object类的字段
|
||||||
*
|
*
|
||||||
* @param beanClass 被查找字段的类,不能为null
|
* @param beanClass 被查找字段的类,不能为null
|
||||||
* @param name 字段名
|
* @param name 字段名
|
||||||
* @return 是否包含字段
|
* @return 是否包含字段
|
||||||
* @throws SecurityException 安全异常
|
* @throws SecurityException 安全异常
|
||||||
* @since 4.1.21
|
* @since 4.1.21
|
||||||
@ -108,7 +117,7 @@ public class ReflectUtil {
|
|||||||
* 查找指定类中的所有字段(包括非public字段),也包括父类和Object类的字段, 字段不存在则返回<code>null</code>
|
* 查找指定类中的所有字段(包括非public字段),也包括父类和Object类的字段, 字段不存在则返回<code>null</code>
|
||||||
*
|
*
|
||||||
* @param beanClass 被查找字段的类,不能为null
|
* @param beanClass 被查找字段的类,不能为null
|
||||||
* @param name 字段名
|
* @param name 字段名
|
||||||
* @return 字段
|
* @return 字段
|
||||||
* @throws SecurityException 安全异常
|
* @throws SecurityException 安全异常
|
||||||
*/
|
*/
|
||||||
@ -144,7 +153,7 @@ public class ReflectUtil {
|
|||||||
/**
|
/**
|
||||||
* 获得一个类中所有字段列表,直接反射获取,无缓存
|
* 获得一个类中所有字段列表,直接反射获取,无缓存
|
||||||
*
|
*
|
||||||
* @param beanClass 类
|
* @param beanClass 类
|
||||||
* @param withSuperClassFieds 是否包括父类的字段列表
|
* @param withSuperClassFieds 是否包括父类的字段列表
|
||||||
* @return 字段列表
|
* @return 字段列表
|
||||||
* @throws SecurityException 安全检查异常
|
* @throws SecurityException 安全检查异常
|
||||||
@ -171,7 +180,7 @@ public class ReflectUtil {
|
|||||||
/**
|
/**
|
||||||
* 获取字段值
|
* 获取字段值
|
||||||
*
|
*
|
||||||
* @param obj 对象
|
* @param obj 对象
|
||||||
* @param fieldName 字段名
|
* @param fieldName 字段名
|
||||||
* @return 字段值
|
* @return 字段值
|
||||||
* @throws UtilException 包装IllegalAccessException异常
|
* @throws UtilException 包装IllegalAccessException异常
|
||||||
@ -186,7 +195,7 @@ public class ReflectUtil {
|
|||||||
/**
|
/**
|
||||||
* 获取字段值
|
* 获取字段值
|
||||||
*
|
*
|
||||||
* @param obj 对象
|
* @param obj 对象
|
||||||
* @param field 字段
|
* @param field 字段
|
||||||
* @return 字段值
|
* @return 字段值
|
||||||
* @throws UtilException 包装IllegalAccessException异常
|
* @throws UtilException 包装IllegalAccessException异常
|
||||||
@ -195,8 +204,8 @@ public class ReflectUtil {
|
|||||||
if (null == obj || null == field) {
|
if (null == obj || null == field) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
field.setAccessible(true);
|
setAccessible(field);
|
||||||
Object result = null;
|
Object result;
|
||||||
try {
|
try {
|
||||||
result = field.get(obj);
|
result = field.get(obj);
|
||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
@ -207,6 +216,7 @@ public class ReflectUtil {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取所有字段的值
|
* 获取所有字段的值
|
||||||
|
*
|
||||||
* @param obj bean对象
|
* @param obj bean对象
|
||||||
* @return 字段值数组
|
* @return 字段值数组
|
||||||
* @since 4.1.17
|
* @since 4.1.17
|
||||||
@ -228,9 +238,9 @@ public class ReflectUtil {
|
|||||||
/**
|
/**
|
||||||
* 设置字段值
|
* 设置字段值
|
||||||
*
|
*
|
||||||
* @param obj 对象
|
* @param obj 对象
|
||||||
* @param fieldName 字段名
|
* @param fieldName 字段名
|
||||||
* @param value 值,值类型必须与字段类型匹配,不会自动转换对象类型
|
* @param value 值,值类型必须与字段类型匹配,不会自动转换对象类型
|
||||||
* @throws UtilException 包装IllegalAccessException异常
|
* @throws UtilException 包装IllegalAccessException异常
|
||||||
*/
|
*/
|
||||||
public static void setFieldValue(Object obj, String fieldName, Object value) throws UtilException {
|
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 field 字段
|
||||||
* @param value 值,值类型必须与字段类型匹配,不会自动转换对象类型
|
* @param value 值,值类型必须与字段类型匹配,不会自动转换对象类型
|
||||||
* @throws UtilException UtilException 包装IllegalAccessException异常
|
* @throws UtilException UtilException 包装IllegalAccessException异常
|
||||||
@ -253,14 +263,15 @@ public class ReflectUtil {
|
|||||||
public static void setFieldValue(Object obj, Field field, Object value) throws UtilException {
|
public static void setFieldValue(Object obj, Field field, Object value) throws UtilException {
|
||||||
Assert.notNull(obj);
|
Assert.notNull(obj);
|
||||||
Assert.notNull(field, "Field in [{}] not exist !", obj.getClass().getName());
|
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();
|
Class<?> fieldType = field.getType();
|
||||||
if(false == fieldType.isAssignableFrom(value.getClass())) {
|
if (false == fieldType.isAssignableFrom(value.getClass())) {
|
||||||
//对于类型不同的字段,尝试转换,转换失败则使用原对象类型
|
//对于类型不同的字段,尝试转换,转换失败则使用原对象类型
|
||||||
final Object targetValue = Convert.convert(fieldType, value);
|
final Object targetValue = Convert.convert(fieldType, value);
|
||||||
if(null != targetValue) {
|
if (null != targetValue) {
|
||||||
value = targetValue;
|
value = targetValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -274,6 +285,7 @@ public class ReflectUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------------------------------------------------------- method
|
// --------------------------------------------------------------------------------------------------------- method
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得指定类本类及其父类中的Public方法名<br>
|
* 获得指定类本类及其父类中的Public方法名<br>
|
||||||
* 去重重载的方法
|
* 去重重载的方法
|
||||||
@ -282,9 +294,9 @@ public class ReflectUtil {
|
|||||||
* @return 方法名Set
|
* @return 方法名Set
|
||||||
*/
|
*/
|
||||||
public static Set<String> getPublicMethodNames(Class<?> clazz) {
|
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);
|
final Method[] methodArray = getPublicMethods(clazz);
|
||||||
if(ArrayUtil.isNotEmpty(methodArray)) {
|
if (ArrayUtil.isNotEmpty(methodArray)) {
|
||||||
for (Method method : methodArray) {
|
for (Method method : methodArray) {
|
||||||
methodSet.add(method.getName());
|
methodSet.add(method.getName());
|
||||||
}
|
}
|
||||||
@ -305,7 +317,7 @@ public class ReflectUtil {
|
|||||||
/**
|
/**
|
||||||
* 获得指定类过滤后的Public方法列表
|
* 获得指定类过滤后的Public方法列表
|
||||||
*
|
*
|
||||||
* @param clazz 查找方法的类
|
* @param clazz 查找方法的类
|
||||||
* @param filter 过滤器
|
* @param filter 过滤器
|
||||||
* @return 过滤后的方法列表
|
* @return 过滤后的方法列表
|
||||||
*/
|
*/
|
||||||
@ -332,7 +344,7 @@ public class ReflectUtil {
|
|||||||
/**
|
/**
|
||||||
* 获得指定类过滤后的Public方法列表
|
* 获得指定类过滤后的Public方法列表
|
||||||
*
|
*
|
||||||
* @param clazz 查找方法的类
|
* @param clazz 查找方法的类
|
||||||
* @param excludeMethods 不包括的方法
|
* @param excludeMethods 不包括的方法
|
||||||
* @return 过滤后的方法列表
|
* @return 过滤后的方法列表
|
||||||
*/
|
*/
|
||||||
@ -349,7 +361,7 @@ public class ReflectUtil {
|
|||||||
/**
|
/**
|
||||||
* 获得指定类过滤后的Public方法列表
|
* 获得指定类过滤后的Public方法列表
|
||||||
*
|
*
|
||||||
* @param clazz 查找方法的类
|
* @param clazz 查找方法的类
|
||||||
* @param excludeMethodNames 不包括的方法名列表
|
* @param excludeMethodNames 不包括的方法名列表
|
||||||
* @return 过滤后的方法列表
|
* @return 过滤后的方法列表
|
||||||
*/
|
*/
|
||||||
@ -366,7 +378,7 @@ public class ReflectUtil {
|
|||||||
/**
|
/**
|
||||||
* 查找指定Public方法 如果找不到对应的方法或方法不为public的则返回<code>null</code>
|
* 查找指定Public方法 如果找不到对应的方法或方法不为public的则返回<code>null</code>
|
||||||
*
|
*
|
||||||
* @param clazz 类
|
* @param clazz 类
|
||||||
* @param methodName 方法名
|
* @param methodName 方法名
|
||||||
* @param paramTypes 参数类型
|
* @param paramTypes 参数类型
|
||||||
* @return 方法
|
* @return 方法
|
||||||
@ -387,9 +399,9 @@ public class ReflectUtil {
|
|||||||
* 此方法为精准获取方法名,即方法名和参数数量和类型必须一致,否则返回<code>null</code>。
|
* 此方法为精准获取方法名,即方法名和参数数量和类型必须一致,否则返回<code>null</code>。
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param obj 被查找的对象,如果为{@code null}返回{@code null}
|
* @param obj 被查找的对象,如果为{@code null}返回{@code null}
|
||||||
* @param methodName 方法名,如果为空字符串返回{@code null}
|
* @param methodName 方法名,如果为空字符串返回{@code null}
|
||||||
* @param args 参数
|
* @param args 参数
|
||||||
* @return 方法
|
* @return 方法
|
||||||
* @throws SecurityException 无访问权限抛出异常
|
* @throws SecurityException 无访问权限抛出异常
|
||||||
*/
|
*/
|
||||||
@ -407,7 +419,7 @@ public class ReflectUtil {
|
|||||||
* 此方法为精准获取方法名,即方法名和参数数量和类型必须一致,否则返回<code>null</code>。
|
* 此方法为精准获取方法名,即方法名和参数数量和类型必须一致,否则返回<code>null</code>。
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param clazz 类,如果为{@code null}返回{@code null}
|
* @param clazz 类,如果为{@code null}返回{@code null}
|
||||||
* @param methodName 方法名,如果为空字符串返回{@code null}
|
* @param methodName 方法名,如果为空字符串返回{@code null}
|
||||||
* @param paramTypes 参数类型,指定参数类型如果是方法的子类也算
|
* @param paramTypes 参数类型,指定参数类型如果是方法的子类也算
|
||||||
* @return 方法
|
* @return 方法
|
||||||
@ -425,7 +437,7 @@ public class ReflectUtil {
|
|||||||
* 此方法为精准获取方法名,即方法名和参数数量和类型必须一致,否则返回<code>null</code>。
|
* 此方法为精准获取方法名,即方法名和参数数量和类型必须一致,否则返回<code>null</code>。
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param clazz 类,如果为{@code null}返回{@code null}
|
* @param clazz 类,如果为{@code null}返回{@code null}
|
||||||
* @param methodName 方法名,如果为空字符串返回{@code null}
|
* @param methodName 方法名,如果为空字符串返回{@code null}
|
||||||
* @param paramTypes 参数类型,指定参数类型如果是方法的子类也算
|
* @param paramTypes 参数类型,指定参数类型如果是方法的子类也算
|
||||||
* @return 方法
|
* @return 方法
|
||||||
@ -442,7 +454,7 @@ public class ReflectUtil {
|
|||||||
* 此方法为精准获取方法名,即方法名和参数数量和类型必须一致,否则返回<code>null</code>。
|
* 此方法为精准获取方法名,即方法名和参数数量和类型必须一致,否则返回<code>null</code>。
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param clazz 类,如果为{@code null}返回{@code null}
|
* @param clazz 类,如果为{@code null}返回{@code null}
|
||||||
* @param ignoreCase 是否忽略大小写
|
* @param ignoreCase 是否忽略大小写
|
||||||
* @param methodName 方法名,如果为空字符串返回{@code null}
|
* @param methodName 方法名,如果为空字符串返回{@code null}
|
||||||
* @param paramTypes 参数类型,指定参数类型如果是方法的子类也算
|
* @param paramTypes 参数类型,指定参数类型如果是方法的子类也算
|
||||||
@ -475,7 +487,7 @@ public class ReflectUtil {
|
|||||||
* 此方法只检查方法名是否一致,并不检查参数的一致性。
|
* 此方法只检查方法名是否一致,并不检查参数的一致性。
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param clazz 类,如果为{@code null}返回{@code null}
|
* @param clazz 类,如果为{@code null}返回{@code null}
|
||||||
* @param methodName 方法名,如果为空字符串返回{@code null}
|
* @param methodName 方法名,如果为空字符串返回{@code null}
|
||||||
* @return 方法
|
* @return 方法
|
||||||
* @throws SecurityException 无权访问抛出异常
|
* @throws SecurityException 无权访问抛出异常
|
||||||
@ -492,7 +504,7 @@ public class ReflectUtil {
|
|||||||
* 此方法只检查方法名是否一致(忽略大小写),并不检查参数的一致性。
|
* 此方法只检查方法名是否一致(忽略大小写),并不检查参数的一致性。
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param clazz 类,如果为{@code null}返回{@code null}
|
* @param clazz 类,如果为{@code null}返回{@code null}
|
||||||
* @param methodName 方法名,如果为空字符串返回{@code null}
|
* @param methodName 方法名,如果为空字符串返回{@code null}
|
||||||
* @return 方法
|
* @return 方法
|
||||||
* @throws SecurityException 无权访问抛出异常
|
* @throws SecurityException 无权访问抛出异常
|
||||||
@ -509,7 +521,7 @@ public class ReflectUtil {
|
|||||||
* 此方法只检查方法名是否一致,并不检查参数的一致性。
|
* 此方法只检查方法名是否一致,并不检查参数的一致性。
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param clazz 类,如果为{@code null}返回{@code null}
|
* @param clazz 类,如果为{@code null}返回{@code null}
|
||||||
* @param ignoreCase 是否忽略大小写
|
* @param ignoreCase 是否忽略大小写
|
||||||
* @param methodName 方法名,如果为空字符串返回{@code null}
|
* @param methodName 方法名,如果为空字符串返回{@code null}
|
||||||
* @return 方法
|
* @return 方法
|
||||||
@ -541,7 +553,7 @@ public class ReflectUtil {
|
|||||||
* @throws SecurityException 安全异常
|
* @throws SecurityException 安全异常
|
||||||
*/
|
*/
|
||||||
public static Set<String> getMethodNames(Class<?> clazz) 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);
|
final Method[] methods = getMethods(clazz);
|
||||||
for (Method method : methods) {
|
for (Method method : methods) {
|
||||||
methodSet.add(method.getName());
|
methodSet.add(method.getName());
|
||||||
@ -552,7 +564,7 @@ public class ReflectUtil {
|
|||||||
/**
|
/**
|
||||||
* 获得指定类过滤后的Public方法列表
|
* 获得指定类过滤后的Public方法列表
|
||||||
*
|
*
|
||||||
* @param clazz 查找方法的类
|
* @param clazz 查找方法的类
|
||||||
* @param filter 过滤器
|
* @param filter 过滤器
|
||||||
* @return 过滤后的方法列表
|
* @return 过滤后的方法列表
|
||||||
* @throws SecurityException 安全异常
|
* @throws SecurityException 安全异常
|
||||||
@ -584,7 +596,7 @@ public class ReflectUtil {
|
|||||||
/**
|
/**
|
||||||
* 获得一个类中所有方法列表,直接反射获取,无缓存
|
* 获得一个类中所有方法列表,直接反射获取,无缓存
|
||||||
*
|
*
|
||||||
* @param beanClass 类
|
* @param beanClass 类
|
||||||
* @param withSuperClassMethods 是否包括父类的方法列表
|
* @param withSuperClassMethods 是否包括父类的方法列表
|
||||||
* @return 方法列表
|
* @return 方法列表
|
||||||
* @throws SecurityException 安全检查异常
|
* @throws SecurityException 安全检查异常
|
||||||
@ -643,10 +655,11 @@ public class ReflectUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------------------------------------------------------- newInstance
|
// --------------------------------------------------------------------------------------------------------- newInstance
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 实例化对象
|
* 实例化对象
|
||||||
*
|
*
|
||||||
* @param <T> 对象类型
|
* @param <T> 对象类型
|
||||||
* @param clazz 类名
|
* @param clazz 类名
|
||||||
* @return 对象
|
* @return 对象
|
||||||
* @throws UtilException 包装各类异常
|
* @throws UtilException 包装各类异常
|
||||||
@ -663,8 +676,8 @@ public class ReflectUtil {
|
|||||||
/**
|
/**
|
||||||
* 实例化对象
|
* 实例化对象
|
||||||
*
|
*
|
||||||
* @param <T> 对象类型
|
* @param <T> 对象类型
|
||||||
* @param clazz 类
|
* @param clazz 类
|
||||||
* @param params 构造函数参数
|
* @param params 构造函数参数
|
||||||
* @return 对象
|
* @return 对象
|
||||||
* @throws UtilException 包装各类异常
|
* @throws UtilException 包装各类异常
|
||||||
@ -682,7 +695,7 @@ public class ReflectUtil {
|
|||||||
final Class<?>[] paramTypes = ClassUtil.getClasses(params);
|
final Class<?>[] paramTypes = ClassUtil.getClasses(params);
|
||||||
final Constructor<T> constructor = getConstructor(clazz, paramTypes);
|
final Constructor<T> constructor = getConstructor(clazz, paramTypes);
|
||||||
if (null == constructor) {
|
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 {
|
try {
|
||||||
return constructor.newInstance(params);
|
return constructor.newInstance(params);
|
||||||
@ -694,7 +707,7 @@ public class ReflectUtil {
|
|||||||
/**
|
/**
|
||||||
* 尝试遍历并调用此类的所有构造方法,直到构造成功并返回
|
* 尝试遍历并调用此类的所有构造方法,直到构造成功并返回
|
||||||
*
|
*
|
||||||
* @param <T> 对象类型
|
* @param <T> 对象类型
|
||||||
* @param beanClass 被构造的类
|
* @param beanClass 被构造的类
|
||||||
* @return 构造后的对象
|
* @return 构造后的对象
|
||||||
*/
|
*/
|
||||||
@ -714,24 +727,24 @@ public class ReflectUtil {
|
|||||||
if (0 == parameterTypes.length) {
|
if (0 == parameterTypes.length) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
constructor.setAccessible(true);
|
setAccessible(constructor);
|
||||||
try {
|
try {
|
||||||
return constructor.newInstance(ClassUtil.getDefaultValues(parameterTypes));
|
return constructor.newInstance(ClassUtil.getDefaultValues(parameterTypes));
|
||||||
} catch (Exception e) {
|
} catch (Exception ignore) {
|
||||||
// 构造出错时继续尝试下一种构造方式
|
// 构造出错时继续尝试下一种构造方式
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------------------------------------------------------- invoke
|
// --------------------------------------------------------------------------------------------------------- invoke
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行静态方法
|
* 执行静态方法
|
||||||
*
|
*
|
||||||
* @param <T> 对象类型
|
* @param <T> 对象类型
|
||||||
* @param method 方法(对象方法或static方法都可)
|
* @param method 方法(对象方法或static方法都可)
|
||||||
* @param args 参数对象
|
* @param args 参数对象
|
||||||
* @return 结果
|
* @return 结果
|
||||||
* @throws UtilException 多种异常包装
|
* @throws UtilException 多种异常包装
|
||||||
*/
|
*/
|
||||||
@ -748,10 +761,10 @@ public class ReflectUtil {
|
|||||||
* 2. 如果某个参数为null但是方法这个位置的参数为原始类型,则赋予原始类型默认值
|
* 2. 如果某个参数为null但是方法这个位置的参数为原始类型,则赋予原始类型默认值
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @param <T> 返回对象类型
|
* @param <T> 返回对象类型
|
||||||
* @param obj 对象,如果执行静态方法,此值为<code>null</code>
|
* @param obj 对象,如果执行静态方法,此值为<code>null</code>
|
||||||
* @param method 方法(对象方法或static方法都可)
|
* @param method 方法(对象方法或static方法都可)
|
||||||
* @param args 参数对象
|
* @param args 参数对象
|
||||||
* @return 结果
|
* @return 结果
|
||||||
* @throws UtilException 一些列异常的包装
|
* @throws UtilException 一些列异常的包装
|
||||||
*/
|
*/
|
||||||
@ -775,18 +788,16 @@ public class ReflectUtil {
|
|||||||
/**
|
/**
|
||||||
* 执行方法
|
* 执行方法
|
||||||
*
|
*
|
||||||
* @param <T> 返回对象类型
|
* @param <T> 返回对象类型
|
||||||
* @param obj 对象,如果执行静态方法,此值为<code>null</code>
|
* @param obj 对象,如果执行静态方法,此值为<code>null</code>
|
||||||
* @param method 方法(对象方法或static方法都可)
|
* @param method 方法(对象方法或static方法都可)
|
||||||
* @param args 参数对象
|
* @param args 参数对象
|
||||||
* @return 结果
|
* @return 结果
|
||||||
* @throws UtilException 一些列异常的包装
|
* @throws UtilException 一些列异常的包装
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static <T> T invoke(Object obj, Method method, Object... args) throws UtilException {
|
public static <T> T invoke(Object obj, Method method, Object... args) throws UtilException {
|
||||||
if (false == method.isAccessible()) {
|
setAccessible(method);
|
||||||
method.setAccessible(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return (T) method.invoke(ClassUtil.isStatic(method) ? null : obj, args);
|
return (T) method.invoke(ClassUtil.isStatic(method) ? null : obj, args);
|
||||||
@ -798,10 +809,10 @@ public class ReflectUtil {
|
|||||||
/**
|
/**
|
||||||
* 执行对象中指定方法
|
* 执行对象中指定方法
|
||||||
*
|
*
|
||||||
* @param <T> 返回对象类型
|
* @param <T> 返回对象类型
|
||||||
* @param obj 方法所在对象
|
* @param obj 方法所在对象
|
||||||
* @param methodName 方法名
|
* @param methodName 方法名
|
||||||
* @param args 参数列表
|
* @param args 参数列表
|
||||||
* @return 执行结果
|
* @return 执行结果
|
||||||
* @throws UtilException IllegalAccessException包装
|
* @throws UtilException IllegalAccessException包装
|
||||||
* @since 3.1.2
|
* @since 3.1.2
|
||||||
@ -813,4 +824,18 @@ public class ReflectUtil {
|
|||||||
}
|
}
|
||||||
return invoke(obj, method, args);
|
return invoke(obj, method, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置方法为可访问(私有方法可以被外部调用)
|
||||||
|
*
|
||||||
|
* @param <T> AccessibleObject的子类,比如Class、Method、Field等
|
||||||
|
* @param accessibleObject 可设置访问权限的对象,比如Class、Method、Field等
|
||||||
|
* @since 4.6.8
|
||||||
|
*/
|
||||||
|
public static <T extends AccessibleObject> T setAccessible(T accessibleObject) {
|
||||||
|
if (null != accessibleObject && false == accessibleObject.isAccessible()) {
|
||||||
|
accessibleObject.setAccessible(true);
|
||||||
|
}
|
||||||
|
return accessibleObject;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user