ReflectUtils.invokeGetter支持静态类及方法调用

This commit is contained in:
thinkgem
2019-05-08 16:58:08 +08:00
parent e123d96465
commit b0d66960a8

View File

@@ -40,8 +40,9 @@ public class ReflectUtils {
private static Logger logger = LoggerFactory.getLogger(ReflectUtils.class); private static Logger logger = LoggerFactory.getLogger(ReflectUtils.class);
/** /**
* 调用Getter方法. * 调用Getter方法
* 支持多级,如:对象名.对象名.方法 * 支持多级,如:对象名.对象名.方法
* 支持静态类及方法调用
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <E> E invokeGetter(Object obj, String propertyName) { public static <E> E invokeGetter(Object obj, String propertyName) {
@@ -54,8 +55,9 @@ public class ReflectUtils {
} }
/** /**
* 调用Setter方法, 仅匹配方法名 * 调用Setter方法仅匹配方法名
* 支持多级,如:对象名.对象名.方法 * 支持多级,如:对象名.对象名.方法
* 支持静态类及方法调用
*/ */
public static <E> void invokeSetter(Object obj, String propertyName, E value) { public static <E> void invokeSetter(Object obj, String propertyName, E value) {
Object object = obj; Object object = obj;
@@ -72,7 +74,7 @@ public class ReflectUtils {
} }
/** /**
* 直接读取对象属性值, 无视private/protected修饰符, 不经过getter函数. * 直接读取对象属性值无视private/protected修饰符不经过getter函数
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <E> E getFieldValue(final Object obj, final String fieldName) { public static <E> E getFieldValue(final Object obj, final String fieldName) {
@@ -92,7 +94,7 @@ public class ReflectUtils {
} }
/** /**
* 直接设置对象属性值, 无视private/protected修饰符, 不经过setter函数. * 直接设置对象属性值无视private/protected修饰符不经过setter函数
*/ */
public static <E> void setFieldValue(final Object obj, final String fieldName, final E value) { public static <E> void setFieldValue(final Object obj, final String fieldName, final E value) {
Field field = getAccessibleField(obj, fieldName); Field field = getAccessibleField(obj, fieldName);
@@ -109,9 +111,10 @@ public class ReflectUtils {
} }
/** /**
* 直接调用对象方法, 无视private/protected修饰符. * 直接调用对象方法无视private/protected修饰符
* 用于一次性调用的情况否则应使用getAccessibleMethod()函数获得Method后反复调用. * 用于一次性调用的情况否则应使用getAccessibleMethod()函数获得Method后反复调用
* 同时匹配方法名+参数类型, * 同时匹配方法名+参数类型,
* 支持静态类及方法调用
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <E> E invokeMethod(final Object obj, final String methodName, final Class<?>[] parameterTypes, public static <E> E invokeMethod(final Object obj, final String methodName, final Class<?>[] parameterTypes,
@@ -122,11 +125,11 @@ public class ReflectUtils {
Method method = getAccessibleMethod(obj, methodName, parameterTypes); Method method = getAccessibleMethod(obj, methodName, parameterTypes);
if (method == null) { if (method == null) {
//throw new IllegalArgumentException("在 [" + obj.getClass() + "] 中,没有找到 [" + methodName + "] 方法 "); //throw new IllegalArgumentException("在 [" + obj.getClass() + "] 中,没有找到 [" + methodName + "] 方法 ");
logger.debug("在 [" + obj.getClass() + "] 中,没有找到 [" + methodName + "] 方法 "); logger.debug("在 [" + (obj.getClass() == Class.class ? obj : obj.getClass()) + "] 中,没有找到 [" + methodName + "] 方法 ");
return null; return null;
} }
try { try {
return (E)method.invoke(obj, args); return (E)method.invoke(obj.getClass() == Class.class ? null : obj, args);
} catch (Exception e) { } catch (Exception e) {
String msg = "method: "+method+", obj: "+obj+", args: "+args+""; String msg = "method: "+method+", obj: "+obj+", args: "+args+"";
throw convertReflectionExceptionToUnchecked(msg, e); throw convertReflectionExceptionToUnchecked(msg, e);
@@ -134,9 +137,10 @@ public class ReflectUtils {
} }
/** /**
* 直接调用对象方法, 无视private/protected修饰符 * 直接调用对象方法无视private/protected修饰符
* 用于一次性调用的情况否则应使用getAccessibleMethodByName()函数获得Method后反复调用. * 用于一次性调用的情况否则应使用getAccessibleMethodByName()函数获得Method后反复调用
* 只匹配函数名,如果有多个同名函数调用第一个 * 只匹配函数名,如果有多个同名函数调用第一个
* 支持静态类及方法调用
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <E> E invokeMethodByName(final Object obj, final String methodName, final Object[] args) { public static <E> E invokeMethodByName(final Object obj, final String methodName, final Object[] args) {
@@ -144,7 +148,7 @@ public class ReflectUtils {
if (method == null) { if (method == null) {
// 如果为空不报错,直接返回空。 // 如果为空不报错,直接返回空。
// throw new IllegalArgumentException("在 [" + obj.getClass() + "] 中,没有找到 [" + methodName + "] 方法 "); // throw new IllegalArgumentException("在 [" + obj.getClass() + "] 中,没有找到 [" + methodName + "] 方法 ");
logger.debug("在 [" + obj.getClass() + "] 中,没有找到 [" + methodName + "] 方法 "); logger.debug("在 [" + (obj.getClass() == Class.class ? obj : obj.getClass()) + "] 中,没有找到 [" + methodName + "] 方法 ");
return null; return null;
} }
try { try {
@@ -175,7 +179,7 @@ public class ReflectUtils {
} }
} }
} }
return (E)method.invoke(obj, args); return (E)method.invoke(obj.getClass() == Class.class ? null : obj, args);
} catch (Exception e) { } catch (Exception e) {
String msg = "method: "+method+", obj: "+obj+", args: "+args+""; String msg = "method: "+method+", obj: "+obj+", args: "+args+"";
throw convertReflectionExceptionToUnchecked(msg, e); throw convertReflectionExceptionToUnchecked(msg, e);
@@ -183,8 +187,8 @@ public class ReflectUtils {
} }
/** /**
* 循环向上转型, 获取对象的DeclaredField, 并强制设置为可访问. * 循环向上转型获取对象的DeclaredField并强制设置为可访问
* 如向上转型到Object仍无法找到, 返回null. * 如向上转型到Object仍无法找到返回null
*/ */
public static Field getAccessibleField(final Object obj, final String fieldName) { public static Field getAccessibleField(final Object obj, final String fieldName) {
// 为空不报错。直接返回 null // 为空不报错。直接返回 null
@@ -207,10 +211,10 @@ public class ReflectUtils {
} }
/** /**
* 循环向上转型, 获取对象的DeclaredMethod,并强制设置为可访问. * 循环向上转型获取对象的DeclaredMethod,并强制设置为可访问
* 如向上转型到Object仍无法找到, 返回null. * 如向上转型到Object仍无法找到返回null
* 匹配函数名+参数类型。 * 匹配函数名+参数类型。
* 用于方法需要被多次调用的情况. 先使用本函数先取得Method,然后调用Method.invoke(Object obj, Object... args) * 用于方法需要被多次调用的情况先使用本函数先取得Method然后调用Method.invoke(Object obj, Object... args)
*/ */
public static Method getAccessibleMethod(final Object obj, final String methodName, public static Method getAccessibleMethod(final Object obj, final String methodName,
final Class<?>... parameterTypes) { final Class<?>... parameterTypes) {
@@ -219,8 +223,12 @@ public class ReflectUtils {
if (obj == null){ if (obj == null){
return null; return null;
} }
Class<?> clazz = obj.getClass();
if (clazz == Class.class){
clazz = (Class) obj;
}
Validate.notBlank(methodName, "methodName can't be blank"); Validate.notBlank(methodName, "methodName can't be blank");
for (Class<?> searchType = obj.getClass(); searchType != Object.class; searchType = searchType.getSuperclass()) { for (Class<?> searchType = clazz; searchType != Object.class; searchType = searchType.getSuperclass()) {
try { try {
Method method = searchType.getDeclaredMethod(methodName, parameterTypes); Method method = searchType.getDeclaredMethod(methodName, parameterTypes);
makeAccessible(method); makeAccessible(method);
@@ -234,10 +242,10 @@ public class ReflectUtils {
} }
/** /**
* 循环向上转型, 获取对象的DeclaredMethod,并强制设置为可访问. * 循环向上转型获取对象的DeclaredMethod并强制设置为可访问
* 如向上转型到Object仍无法找到, 返回null. * 如向上转型到Object仍无法找到返回null
* 只匹配函数名。 * 只匹配函数名。
* 用于方法需要被多次调用的情况. 先使用本函数先取得Method,然后调用Method.invoke(Object obj, Object... args) * 用于方法需要被多次调用的情况先使用本函数先取得Method然后调用Method.invoke(Object obj, Object... args)
*/ */
public static Method getAccessibleMethodByName(final Object obj, final String methodName, int argsNum) { public static Method getAccessibleMethodByName(final Object obj, final String methodName, int argsNum) {
// 为空不报错。直接返回 null // 为空不报错。直接返回 null
@@ -245,8 +253,12 @@ public class ReflectUtils {
if (obj == null){ if (obj == null){
return null; return null;
} }
Class<?> clazz = obj.getClass();
if (clazz == Class.class){
clazz = (Class) obj;
}
Validate.notBlank(methodName, "methodName can't be blank"); Validate.notBlank(methodName, "methodName can't be blank");
for (Class<?> searchType = obj.getClass(); searchType != Object.class; searchType = searchType.getSuperclass()) { for (Class<?> searchType = clazz; searchType != Object.class; searchType = searchType.getSuperclass()) {
Method[] methods = searchType.getDeclaredMethods(); Method[] methods = searchType.getDeclaredMethods();
for (Method method : methods) { for (Method method : methods) {
if (method.getName().equals(methodName) && method.getParameterTypes().length == argsNum) { if (method.getName().equals(methodName) && method.getParameterTypes().length == argsNum) {
@@ -279,10 +291,9 @@ public class ReflectUtils {
} }
/** /**
* 通过反射, 获得Class定义中声明的泛型参数的类型, 注意泛型必须定义在父类处 * 通过反射获得Class定义中声明的泛型参数的类型注意泛型必须定义在父类处
* 如无法找到, 返回Object.class. * 如无法找到返回Object.class
* eg. * 如 public UserDao extends CrudDao<User>
* public UserDao extends HibernateDao<User>
* @param clazz The class to introspect * @param clazz The class to introspect
* @return the first generic declaration, or Object.class if cannot be determined * @return the first generic declaration, or Object.class if cannot be determined
*/ */
@@ -292,9 +303,9 @@ public class ReflectUtils {
} }
/** /**
* 通过反射, 获得Class定义中声明的父类的泛型参数的类型. * 通过反射获得Class定义中声明的父类的泛型参数的类型
* 如无法找到, 返回Object.class. * 如无法找到返回Object.class
* 如public UserDao extends HibernateDao<User,Long> * 如 public UserDao extends CrudDao<User, Long>
* @param clazz clazz The class to introspect * @param clazz clazz The class to introspect
* @param index the Index of the generic ddeclaration,start from 0. * @param index the Index of the generic ddeclaration,start from 0.
* @return the index generic declaration, or Object.class if cannot be determined * @return the index generic declaration, or Object.class if cannot be determined
@@ -323,6 +334,9 @@ public class ReflectUtils {
return (Class) params[index]; return (Class) params[index];
} }
/**
* 获取类的Class如果为内部类则返回上级类Class
*/
public static Class<?> getUserClass(Object instance) { public static Class<?> getUserClass(Object instance) {
if (instance == null){ if (instance == null){
throw new RuntimeException("Instance must not be null"); throw new RuntimeException("Instance must not be null");
@@ -339,7 +353,7 @@ public class ReflectUtils {
} }
/** /**
* 将反射时的checked exception转换为unchecked exception. * 将反射时的checked exception转换为unchecked exception
*/ */
public static RuntimeException convertReflectionExceptionToUnchecked(String msg, Exception e) { public static RuntimeException convertReflectionExceptionToUnchecked(String msg, Exception e) {
if (e instanceof IllegalAccessException || e instanceof IllegalArgumentException if (e instanceof IllegalAccessException || e instanceof IllegalArgumentException