Просмотр исходного кода

简化代码:利用反射获取指定对象的指定字段的值

sunyj 8 лет назад
Родитель
Сommit
f142fa4dd5
1 измененных файлов с 27 добавлено и 36 удалено
  1. 27 36
      kanban-common/src/main/java/com/uas/kanban/base/BaseDao.java

+ 27 - 36
kanban-common/src/main/java/com/uas/kanban/base/BaseDao.java

@@ -171,23 +171,12 @@ public class BaseDao<T extends BaseEntity> {
 				continue;
 			}
 
-			Object value = null;
 			int modifiers = field.getModifiers();
 			// 不处理static、final、transient修饰的变量(只处理普通成员变量)
 			if (Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers) || Modifier.isTransient(modifiers)) {
 				continue;
 			}
-			try {
-				if (!field.isAccessible()) {
-					field.setAccessible(true);
-					value = field.get(t);
-					field.setAccessible(false);
-				} else {
-					value = field.get(t);
-				}
-			} catch (IllegalArgumentException | IllegalAccessException e) {
-				throw new IllegalStateException("通过反射自动构造 UpdateOperations 时出错", e);
-			}
+			Object value = getValue(field, t);
 			// 如果值为空,则移除文档中的该字段
 			if (ObjectUtils.isEmpty(value)) {
 				operations.unset(name);
@@ -198,6 +187,30 @@ public class BaseDao<T extends BaseEntity> {
 		return operations;
 	}
 
+	/**
+	 * 利用反射获取指定对象的指定字段的值
+	 *
+	 * @param field 指定字段
+	 * @param k     指定对象
+	 * @return 指定字段的值
+	 * @throws IllegalStateException
+	 */
+	private <K> Object getValue(Field field, @NotEmpty("k") K k) throws IllegalStateException {
+		Object value;
+		try {
+			if (!field.isAccessible()) {
+				field.setAccessible(true);
+				value = field.get(k);
+				field.setAccessible(false);
+			} else {
+				value = field.get(k);
+			}
+		} catch (SecurityException | IllegalArgumentException | IllegalAccessException e) {
+			throw new IllegalStateException("通过反射取值失败", e);
+		}
+		return value;
+	}
+
 	/**
 	 * Saves an entity (Object) and updates the @Id field
 	 *
@@ -298,18 +311,7 @@ public class BaseDao<T extends BaseEntity> {
 			if ((fieldProperty == null || fieldProperty.nullable()) && embedded == null && reference == null) {
 				continue;
 			}
-			Object value = null;
-			try {
-				if (!field.isAccessible()) {
-					field.setAccessible(true);
-					value = field.get(k);
-					field.setAccessible(false);
-				} else {
-					value = field.get(k);
-				}
-			} catch (SecurityException | IllegalArgumentException | IllegalAccessException e) {
-				throw new IllegalStateException("通过反射检查字段是否为空时出错", e);
-			}
+			Object value = getValue(field, k);
 
 			// 如果使用 {@link FieldProperty} 指定不可为空,但是值为空,则抛出异常
 			if (fieldProperty != null && !fieldProperty.nullable() && ObjectUtils.isEmpty(value)) {
@@ -456,23 +458,12 @@ public class BaseDao<T extends BaseEntity> {
 			} catch (NoSuchFieldException | SecurityException e1) {
 				throw new IllegalArgumentException("表中不存在该字段:" + key);
 			}
-			Object value = null;
 			int modifiers = field.getModifiers();
 			// 不处理static、final、transient修饰的变量(只处理普通成员变量)
 			if (Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers) || Modifier.isTransient(modifiers)) {
 				continue;
 			}
-			try {
-				if (!field.isAccessible()) {
-					field.setAccessible(true);
-					value = field.get(t);
-					field.setAccessible(false);
-				} else {
-					value = field.get(t);
-				}
-			} catch (IllegalArgumentException | IllegalAccessException e) {
-				throw new IllegalStateException("通过反射自动构造 UpdateOperations 时出错", e);
-			}
+			Object value = getValue(field, t);
 			// 如果值为空,则移除文档中的该字段
 			if (ObjectUtils.isEmpty(value)) {
 				operations.unset(key);