降低ReflectUtils的错误级别,如果null不抛错,打印日志警告即可。
This commit is contained in:
@@ -76,7 +76,9 @@ public class ReflectUtils {
|
|||||||
public static Object getFieldValue(final Object obj, final String fieldName) {
|
public static Object getFieldValue(final Object obj, final String fieldName) {
|
||||||
Field field = getAccessibleField(obj, fieldName);
|
Field field = getAccessibleField(obj, fieldName);
|
||||||
if (field == null) {
|
if (field == null) {
|
||||||
throw new IllegalArgumentException("在 [" + obj.getClass() + "] 中,没有找到 [" + fieldName + "] 字段 ");
|
//throw new IllegalArgumentException("在 [" + obj.getClass() + "] 中,没有找到 [" + fieldName + "] 字段 ");
|
||||||
|
logger.warn("在 [" + obj.getClass() + "] 中,没有找到 [" + fieldName + "] 字段 ");
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
Object result = null;
|
Object result = null;
|
||||||
try {
|
try {
|
||||||
@@ -93,7 +95,9 @@ public class ReflectUtils {
|
|||||||
public static void setFieldValue(final Object obj, final String fieldName, final Object value) {
|
public static void setFieldValue(final Object obj, final String fieldName, final Object value) {
|
||||||
Field field = getAccessibleField(obj, fieldName);
|
Field field = getAccessibleField(obj, fieldName);
|
||||||
if (field == null) {
|
if (field == null) {
|
||||||
throw new IllegalArgumentException("在 [" + obj.getClass() + "] 中,没有找到 [" + fieldName + "] 字段 ");
|
//throw new IllegalArgumentException("在 [" + obj.getClass() + "] 中,没有找到 [" + fieldName + "] 字段 ");
|
||||||
|
logger.warn("在 [" + obj.getClass() + "] 中,没有找到 [" + fieldName + "] 字段 ");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
field.set(obj, value);
|
field.set(obj, value);
|
||||||
@@ -114,7 +118,9 @@ 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.warn("在 [" + obj.getClass() + "] 中,没有找到 [" + methodName + "] 方法 ");
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return method.invoke(obj, args);
|
return method.invoke(obj, args);
|
||||||
@@ -132,7 +138,10 @@ public class ReflectUtils {
|
|||||||
public static Object invokeMethodByName(final Object obj, final String methodName, final Object[] args) {
|
public static Object invokeMethodByName(final Object obj, final String methodName, final Object[] args) {
|
||||||
Method method = getAccessibleMethodByName(obj, methodName, args.length);
|
Method method = getAccessibleMethodByName(obj, methodName, args.length);
|
||||||
if (method == null) {
|
if (method == null) {
|
||||||
throw new IllegalArgumentException("在 [" + obj.getClass() + "] 中,没有找到 [" + methodName + "] 方法 ");
|
// 如果为空不报错,直接返回空。
|
||||||
|
// throw new IllegalArgumentException("在 [" + obj.getClass() + "] 中,没有找到 [" + methodName + "] 方法 ");
|
||||||
|
logger.warn("在 [" + obj.getClass() + "] 中,没有找到 [" + methodName + "] 方法 ");
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
// 类型转换(将参数数据类型转换为目标方法参数类型)
|
// 类型转换(将参数数据类型转换为目标方法参数类型)
|
||||||
@@ -174,7 +183,11 @@ public class ReflectUtils {
|
|||||||
* 如向上转型到Object仍无法找到, 返回null.
|
* 如向上转型到Object仍无法找到, 返回null.
|
||||||
*/
|
*/
|
||||||
public static Field getAccessibleField(final Object obj, final String fieldName) {
|
public static Field getAccessibleField(final Object obj, final String fieldName) {
|
||||||
Validate.notNull(obj, "object can't be null");
|
// 为空不报错。直接返回 null
|
||||||
|
// Validate.notNull(obj, "object can't be null");
|
||||||
|
if (obj == null){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
Validate.notBlank(fieldName, "fieldName can't be blank");
|
Validate.notBlank(fieldName, "fieldName can't be blank");
|
||||||
for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) {
|
for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) {
|
||||||
try {
|
try {
|
||||||
@@ -197,7 +210,11 @@ public class ReflectUtils {
|
|||||||
*/
|
*/
|
||||||
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) {
|
||||||
Validate.notNull(obj, "object can't be null");
|
// 为空不报错。直接返回 null
|
||||||
|
// Validate.notNull(obj, "object can't be null");
|
||||||
|
if (obj == null){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
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 = obj.getClass(); searchType != Object.class; searchType = searchType.getSuperclass()) {
|
||||||
try {
|
try {
|
||||||
@@ -219,7 +236,11 @@ public class ReflectUtils {
|
|||||||
* 用于方法需要被多次调用的情况. 先使用本函数先取得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) {
|
||||||
Validate.notNull(obj, "object can't be null");
|
// 为空不报错。直接返回 null
|
||||||
|
// Validate.notNull(obj, "object can't be null");
|
||||||
|
if (obj == null){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
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 = obj.getClass(); searchType != Object.class; searchType = searchType.getSuperclass()) {
|
||||||
Method[] methods = searchType.getDeclaredMethods();
|
Method[] methods = searchType.getDeclaredMethods();
|
||||||
|
|||||||
Reference in New Issue
Block a user