001/*
002 *  Copyright (c) 2022-2025, Mybatis-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.mybatisflex.core.util;
017
018import com.mybatisflex.core.exception.FlexExceptions;
019import com.mybatisflex.core.query.QueryColumn;
020import com.mybatisflex.core.table.TableInfo;
021import com.mybatisflex.core.table.TableInfoFactory;
022
023import java.io.Serializable;
024import java.lang.invoke.SerializedLambda;
025import java.lang.reflect.Method;
026import java.util.Map;
027import java.util.concurrent.ConcurrentHashMap;
028
029public class LambdaUtil {
030
031    private LambdaUtil() {
032    }
033
034    private static final Map<Class<?>, String> fieldNameMap = new ConcurrentHashMap<>();
035    private static final Map<Class<?>, Class<?>> implClassMap = new ConcurrentHashMap<>();
036    private static final Map<Class<?>, QueryColumn> queryColumnMap = new ConcurrentHashMap<>();
037
038    public static Map<Class<?>, String> getFieldNameMap() {
039        return fieldNameMap;
040    }
041
042    public static <T> String getFieldName(LambdaGetter<T> getter) {
043        return MapUtil.computeIfAbsent(fieldNameMap, getter.getClass(), aClass -> {
044            SerializedLambda lambda = getSerializedLambda(getter);
045            // 兼容 Kotlin KProperty 的 Lambda 解析
046            if (lambda.getCapturedArgCount() == 1) {
047                Object capturedArg = lambda.getCapturedArg(0);
048                try {
049                    return (String) capturedArg.getClass()
050                        .getMethod("getName")
051                        .invoke(capturedArg);
052                } catch (Exception e) {
053                    // 忽略这个异常,使用其他方式获取方法名
054                }
055            }
056            String methodName = lambda.getImplMethodName();
057            return StringUtil.methodToProperty(methodName);
058        });
059    }
060
061
062    public static <T> Class<?> getImplClass(LambdaGetter<T> getter) {
063        return MapUtil.computeIfAbsent(implClassMap, getter.getClass(), aClass -> {
064            SerializedLambda lambda = getSerializedLambda(getter);
065            return getImplClass0(lambda);
066        });
067    }
068
069
070    public static <T> String getAliasName(LambdaGetter<T> getter, boolean withPrefix) {
071        QueryColumn queryColumn = getQueryColumn(getter);
072        if (queryColumn != null) {
073            String alias = StringUtil.hasText(queryColumn.getAlias()) ? queryColumn.getAlias() : queryColumn.getName();
074            return withPrefix ? queryColumn.getTable().getName() + "$" + alias : alias;
075        }
076        return getFieldName(getter);
077    }
078
079
080    public static <T> QueryColumn getQueryColumn(LambdaGetter<T> getter) {
081        return MapUtil.computeIfAbsent(queryColumnMap, getter.getClass(), aClass -> {
082            SerializedLambda lambda = getSerializedLambda(getter);
083            Class<?> entityClass = getImplClass0(lambda);
084            TableInfo tableInfo = TableInfoFactory.ofEntityClass(entityClass);
085            String propertyName = getFieldName(getter);
086            return tableInfo.getQueryColumnByProperty(propertyName);
087        });
088    }
089
090
091    private static SerializedLambda getSerializedLambda(Serializable getter) {
092        try {
093            Method method = getter.getClass().getDeclaredMethod("writeReplace");
094            method.setAccessible(Boolean.TRUE);
095            return (SerializedLambda) method.invoke(getter);
096        } catch (Exception e) {
097            throw new RuntimeException(e);
098        }
099    }
100
101
102    private static Class<?> getImplClass0(SerializedLambda lambda) {
103        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
104        String implClass = getImplClassName(lambda);
105        try {
106            return Class.forName(implClass.replace("/", "."), true, classLoader);
107        } catch (ClassNotFoundException e) {
108            throw FlexExceptions.wrap(e);
109        }
110    }
111
112    private static String getImplClassName(SerializedLambda lambda) {
113        String type = lambda.getInstantiatedMethodType();
114        return type.substring(2, type.indexOf(";"));
115    }
116
117}