perf(extension/crud): 优化 BaseServiceImpl 中获取各泛型参数类型的方式

This commit is contained in:
2024-01-03 21:56:45 +08:00
parent 81ed292840
commit 6fc0b51a57
2 changed files with 85 additions and 13 deletions

View File

@@ -0,0 +1,49 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* 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
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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;
}
}