|
@@ -15,10 +15,7 @@
|
|
|
*/
|
|
|
package com.baomidou.mybatisplus.toolkit;
|
|
|
|
|
|
-import java.lang.reflect.InvocationTargetException;
|
|
|
-import java.lang.reflect.Method;
|
|
|
-import java.lang.reflect.ParameterizedType;
|
|
|
-import java.lang.reflect.Type;
|
|
|
+import java.lang.reflect.*;
|
|
|
import java.util.List;
|
|
|
|
|
|
import org.apache.ibatis.logging.Log;
|
|
@@ -44,12 +41,21 @@ public class ReflectionKit {
|
|
|
* 反射 method 方法名,例如 getId
|
|
|
* </p>
|
|
|
*
|
|
|
- * @param str
|
|
|
- * 属性字符串内容
|
|
|
- * @return
|
|
|
+ *
|
|
|
+ * @param field
|
|
|
+ * @param str
|
|
|
+ * 属性字符串内容
|
|
|
+ * @return
|
|
|
*/
|
|
|
- public static String getMethodCapitalize(final String str) {
|
|
|
- return StringUtils.concatCapitalize("get", str);
|
|
|
+ public static String getMethodCapitalize(Field field, final String str) {
|
|
|
+ Class<?> fieldType = field.getType();
|
|
|
+ String concatstr;
|
|
|
+ if (Boolean.class.equals(fieldType) || boolean.class.equals(fieldType)) {
|
|
|
+ concatstr = "is";
|
|
|
+ }else{
|
|
|
+ concatstr ="get";
|
|
|
+ }
|
|
|
+ return StringUtils.concatCapitalize(concatstr, str);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -65,13 +71,14 @@ public class ReflectionKit {
|
|
|
public static Object getMethodValue(Class<?> cls, Object entity, String str) {
|
|
|
Object obj = null;
|
|
|
try {
|
|
|
- Method method = cls.getMethod(getMethodCapitalize(str));
|
|
|
+ Field field = cls.getDeclaredField(str);
|
|
|
+ Method method = cls.getMethod(getMethodCapitalize(field,str));
|
|
|
obj = method.invoke(entity);
|
|
|
} catch (NoSuchMethodException e) {
|
|
|
logger.warn(String.format("Warn: No such method. in %s. Cause:", cls.getSimpleName()) + e);
|
|
|
} catch (IllegalAccessException e) {
|
|
|
logger.warn(String.format("Warn: Cannot execute a private method. in %s. Cause:", cls.getSimpleName()) + e);
|
|
|
- } catch (InvocationTargetException e) {
|
|
|
+ } catch (Exception e) {
|
|
|
logger.warn("Warn: Unexpected exception on getMethodValue. Cause:" + e);
|
|
|
}
|
|
|
return obj;
|