From 6fc0b51a574434db9d21d1f254b3fce344c9f2f6 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Wed, 3 Jan 2024 21:56:45 +0800 Subject: [PATCH] =?UTF-8?q?perf(extension/crud):=20=E4=BC=98=E5=8C=96=20Ba?= =?UTF-8?q?seServiceImpl=20=E4=B8=AD=E8=8E=B7=E5=8F=96=E5=90=84=E6=B3=9B?= =?UTF-8?q?=E5=9E=8B=E5=8F=82=E6=95=B0=E7=B1=BB=E5=9E=8B=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../starter/core/util/ClassUtils.java | 49 +++++++++++++++++++ .../extension/crud/base/BaseServiceImpl.java | 49 ++++++++++++++----- 2 files changed, 85 insertions(+), 13 deletions(-) create mode 100644 continew-starter-core/src/main/java/top/charles7c/continew/starter/core/util/ClassUtils.java diff --git a/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/util/ClassUtils.java b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/util/ClassUtils.java new file mode 100644 index 00000000..816ac744 --- /dev/null +++ b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/util/ClassUtils.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.charles7c.continew.starter.core.util; + +import cn.hutool.core.util.ArrayUtil; +import cn.hutool.core.util.TypeUtil; + +import java.lang.reflect.Type; + +/** + * 类工具类 + * + * @author Charles7c + * @since 1.1.1 + */ +public class ClassUtils { + + /** + * 获得给定类的所有泛型参数 + * + * @param clazz 被检查的类,必须是已经确定泛型类型的类 + * @return {@link Class}[] + */ + public static Class[] getTypeArguments(Class clazz) { + final Type[] typeArguments = TypeUtil.getTypeArguments(clazz); + if (ArrayUtil.isEmpty(typeArguments)) { + return new Class[0]; + } + final Class[] classes = new Class[typeArguments.length]; + for (int i = 0; i < typeArguments.length; i++) { + classes[i] = TypeUtil.getClass(typeArguments[i]); + } + return classes; + } +} diff --git a/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/base/BaseServiceImpl.java b/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/base/BaseServiceImpl.java index b9c46d0b..c32e0b83 100644 --- a/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/base/BaseServiceImpl.java +++ b/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/base/BaseServiceImpl.java @@ -33,6 +33,7 @@ import jakarta.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.transaction.annotation.Transactional; +import top.charles7c.continew.starter.core.util.ClassUtils; import top.charles7c.continew.starter.core.util.ExceptionUtils; import top.charles7c.continew.starter.core.util.ReflectUtils; import top.charles7c.continew.starter.core.util.validate.CheckUtils; @@ -67,15 +68,10 @@ public abstract class BaseServiceImpl, T extends BaseDO, @Autowired protected M baseMapper; - private final Class entityClass; - private final Class listClass; - private final Class detailClass; - - protected BaseServiceImpl() { - this.entityClass = (Class) ClassUtil.getTypeArgument(this.getClass(), 1); - this.listClass = (Class) ClassUtil.getTypeArgument(this.getClass(), 2); - this.detailClass = (Class) ClassUtil.getTypeArgument(this.getClass(), 3); - } + private final Class[] typeArguments = ClassUtils.getTypeArguments(this.getClass()); + protected final Class entityClass = this.currentEntityClass(); + protected final Class listClass = this.currentListClass(); + protected final Class detailClass = this.currentDetailClass(); @Override public PageResp page(Q query, PageQuery pageQuery) { @@ -226,14 +222,41 @@ public abstract class BaseServiceImpl, T extends BaseDO, * @param detailObj 待填充详情信息 */ public void fillDetail(Object detailObj) { - if (detailObj instanceof BaseDetailResp detail) { - this.fill(detail); - Long updateUser = detail.getUpdateUser(); + if (detailObj instanceof BaseDetailResp detailResp) { + this.fill(detailResp); + Long updateUser = detailResp.getUpdateUser(); if (null == updateUser) { return; } CommonUserService userService = SpringUtil.getBean(CommonUserService.class); - detail.setUpdateUserString(ExceptionUtils.exToNull(() -> userService.getNicknameById(updateUser))); + detailResp.setUpdateUserString(ExceptionUtils.exToNull(() -> userService.getNicknameById(updateUser))); } } + + /** + * 获取当前实体类型 + * + * @return 当前实体类型 + */ + protected Class currentEntityClass() { + return (Class) this.typeArguments[1]; + } + + /** + * 获取当前列表信息类型 + * + * @return 当前列表信息类型 + */ + protected Class currentListClass() { + return (Class) this.typeArguments[2]; + } + + /** + * 获取当前详情信息类型 + * + * @return 当前详情信息类型 + */ + protected Class currentDetailClass() { + return (Class) this.typeArguments[3]; + } }