소스 검색

去除方法推测,直接访问属性字段.

https://gitee.com/baomidou/mybatis-plus/issues/I1AEZV
聂秋秋 5 년 전
부모
커밋
5d62a4dd89

+ 4 - 4
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/Wrapper.java

@@ -131,7 +131,7 @@ public abstract class Wrapper<T> implements ISqlSegment {
         if (tableInfo.getFieldList().stream().anyMatch(e -> fieldStrategyMatch(entity, e))) {
             return true;
         }
-        return StringUtils.isNotBlank(tableInfo.getKeyProperty()) ? Objects.nonNull(ReflectionKit.getMethodValue(entity, tableInfo.getKeyProperty())) : false;
+        return StringUtils.isNotBlank(tableInfo.getKeyProperty()) ? Objects.nonNull(ReflectionKit.getFieldValue(entity, tableInfo.getKeyProperty())) : false;
     }
 
     /**
@@ -140,15 +140,15 @@ public abstract class Wrapper<T> implements ISqlSegment {
     private boolean fieldStrategyMatch(T entity, TableFieldInfo e) {
         switch (e.getWhereStrategy()) {
             case NOT_NULL:
-                return Objects.nonNull(ReflectionKit.getMethodValue(entity, e.getProperty()));
+                return Objects.nonNull(ReflectionKit.getFieldValue(entity, e.getProperty()));
             case IGNORED:
                 return true;
             case NOT_EMPTY:
-                return StringUtils.checkValNotNull(ReflectionKit.getMethodValue(entity, e.getProperty()));
+                return StringUtils.checkValNotNull(ReflectionKit.getFieldValue(entity, e.getProperty()));
             case NEVER:
                 return false;
             default:
-                return Objects.nonNull(ReflectionKit.getMethodValue(entity, e.getProperty()));
+                return Objects.nonNull(ReflectionKit.getFieldValue(entity, e.getProperty()));
         }
     }
 

+ 32 - 4
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/ReflectionKit.java

@@ -84,7 +84,7 @@ public final class ReflectionKit {
     public static String setMethodCapitalize(Field field, final String str) {
         return StringUtils.concatCapitalize("set", str);
     }
-
+    
     /**
      * <p>
      * 获取 public get方法的值
@@ -94,7 +94,9 @@ public final class ReflectionKit {
      * @param entity 实体
      * @param str    属性字符串内容
      * @return Object
+     * @deprecated 3.3.2
      */
+    @Deprecated
     public static Object getMethodValue(Class<?> cls, Object entity, String str) {
         Map<String, Field> fieldMaps = getFieldMap(cls);
         try {
@@ -109,17 +111,39 @@ public final class ReflectionKit {
             throw ExceptionUtils.mpe("Error: InvocationTargetException on getMethodValue.  Cause:" + e);
         }
     }
-
+    
+    /**
+     * 获取字段值
+     *
+     * @param entity    实体
+     * @param fieldName 字段名称
+     * @return 属性值
+     */
+    public static Object getFieldValue(Object entity, String fieldName) {
+        Class cls = entity.getClass();
+        Map<String, Field> fieldMaps = getFieldMap(cls);
+        try {
+            Assert.notEmpty(fieldMaps, "Error: NoSuchField in %s for %s.  Cause:", cls.getSimpleName(), fieldName);
+            Field field = fieldMaps.get(fieldName);
+            field.setAccessible(true);
+            return field.get(entity);
+        } catch (IllegalAccessException e) {
+            throw ExceptionUtils.mpe("Error: Cannot read field in %s.  Cause:", e, cls.getSimpleName());
+        }
+    }
+    
     /**
      * 猜测方法名
      *
      * @param field 字段
      * @param str   属性字符串内容
+     * @deprecated 3.3.2
      */
+    @Deprecated
     private static String guessGetterName(Field field, final String str) {
         return StringUtils.guessGetterName(str, field.getType());
     }
-
+    
     /**
      * <p>
      * 获取 public get方法的值
@@ -128,7 +152,9 @@ public final class ReflectionKit {
      * @param entity 实体
      * @param str    属性字符串内容
      * @return Object
+     * @deprecated 3.3.2
      */
+    @Deprecated
     public static Object getMethodValue(Object entity, String str) {
         if (null == entity) {
             return null;
@@ -246,14 +272,16 @@ public final class ReflectionKit {
                 .forEach(f -> fieldMap.put(f.getName(), f));
         return fieldMap;
     }
-
+    
     /**
      * 获取字段get方法
      *
      * @param cls   class
      * @param field 字段
      * @return Get方法
+     * @deprecated 3.3.2
      */
+    @Deprecated
     public static Method getMethod(Class<?> cls, Field field) {
         try {
             return cls.getDeclaredMethod(ReflectionKit.guessGetterName(field, field.getName()));

+ 3 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/StringUtils.java

@@ -137,14 +137,16 @@ public final class StringUtils {
     public static boolean isNotBlank(final CharSequence cs) {
         return !isBlank(cs);
     }
-
+    
     /**
      * 猜测方法属性对应的 Getter 名称,具体规则请参考 JavaBeans 规范
      *
      * @param name 属性名称
      * @param type 属性类型
      * @return 返回猜测的名称
+     * @deprecated 3.3.2
      */
+    @Deprecated
     public static String guessGetterName(String name, Class<?> type) {
         return boolean.class == type ? name.startsWith("is") ? name : "is" + upperFirst(name) : "get" + upperFirst(name);
     }

+ 1 - 1
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/activerecord/Model.java

@@ -271,7 +271,7 @@ public abstract class Model<T extends Model<?>> implements Serializable {
      * 主键值
      */
     protected Serializable pkVal() {
-        return (Serializable) ReflectionKit.getMethodValue(this, TableInfoHelper.getTableInfo(getClass()).getKeyProperty());
+        return (Serializable) ReflectionKit.getFieldValue(this, TableInfoHelper.getTableInfo(getClass()).getKeyProperty());
     }
 
     /**

+ 2 - 2
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/service/impl/ServiceImpl.java

@@ -142,7 +142,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
             Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");
             String keyProperty = tableInfo.getKeyProperty();
             Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!");
-            Object idVal = ReflectionKit.getMethodValue(cls, entity, tableInfo.getKeyProperty());
+            Object idVal = ReflectionKit.getFieldValue(entity, tableInfo.getKeyProperty());
             return StringUtils.checkValNull(idVal) || Objects.isNull(getById((Serializable) idVal)) ? save(entity) : updateById(entity);
         }
         return false;
@@ -156,7 +156,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
         String keyProperty = tableInfo.getKeyProperty();
         Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!");
         return executeBatch(entityList, batchSize, (sqlSession, entity) -> {
-            Object idVal = ReflectionKit.getMethodValue(entityClass, entity, keyProperty);
+            Object idVal = ReflectionKit.getFieldValue(entity, keyProperty);
             if (StringUtils.checkValNull(idVal) || Objects.isNull(getById((Serializable) idVal))) {
                 sqlSession.insert(tableInfo.getSqlStatement(SqlMethod.INSERT_ONE.getMethod()), entity);
             } else {