001/*
002 *  Copyright (c) 2022-2023, 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;
022import org.apache.ibatis.reflection.property.PropertyNamer;
023import org.apache.ibatis.util.MapUtil;
024
025import java.io.Serializable;
026import java.lang.invoke.SerializedLambda;
027import java.lang.reflect.Method;
028import java.util.Map;
029import java.util.concurrent.ConcurrentHashMap;
030
031public class LambdaUtil {
032
033    private LambdaUtil() {
034    }
035
036    private static final Map<Class<?>, SerializedLambda> lambdaMap = new ConcurrentHashMap<>();
037    private static final Map<String, Class<?>> classMap = new ConcurrentHashMap<>();
038
039    public static <T> String getFieldName(LambdaGetter<T> getter) {
040        SerializedLambda lambda = getSerializedLambda(getter);
041        String methodName = lambda.getImplMethodName();
042        return PropertyNamer.methodToProperty(methodName);
043    }
044
045    public static <T> Class<?> getImplClass(LambdaGetter<T> getter) {
046        SerializedLambda lambda = getSerializedLambda(getter);
047        return getImplClass(lambda);
048    }
049
050
051    public static <T> String getAliasName(LambdaGetter<T> getter, boolean withPrefix) {
052        QueryColumn queryColumn = getQueryColumn(getter);
053        String alias = StringUtil.isNotBlank(queryColumn.getAlias()) ? queryColumn.getAlias() : queryColumn.getName();
054        return withPrefix ? queryColumn.getTable().getName() + "$" + alias : alias;
055    }
056
057
058    public static <T> QueryColumn getQueryColumn(LambdaGetter<T> getter) {
059        SerializedLambda lambda = getSerializedLambda(getter);
060        String methodName = lambda.getImplMethodName();
061        Class<?> entityClass = getImplClass(lambda);
062        TableInfo tableInfo = TableInfoFactory.ofEntityClass(entityClass);
063        return tableInfo.getQueryColumnByProperty(PropertyNamer.methodToProperty(methodName));
064    }
065
066
067    private static SerializedLambda getSerializedLambda(Serializable getter) {
068        return MapUtil.computeIfAbsent(lambdaMap, getter.getClass(), aClass -> {
069            try {
070                Method method = getter.getClass().getDeclaredMethod("writeReplace");
071                method.setAccessible(Boolean.TRUE);
072                return (SerializedLambda) method.invoke(getter);
073            } catch (Exception e) {
074                throw new RuntimeException(e);
075            }
076        });
077    }
078
079
080    private static Class<?> getImplClass(SerializedLambda lambda) {
081        String implClass = getImplClassName(lambda);
082        return MapUtil.computeIfAbsent(classMap, implClass, s -> {
083            try {
084                return Class.forName(s.replace("/", "."));
085            } catch (ClassNotFoundException e) {
086                throw FlexExceptions.wrap(e);
087            }
088        });
089    }
090
091    private static String getImplClassName(SerializedLambda lambda) {
092        String type = lambda.getInstantiatedMethodType();
093        return type.substring(2, type.indexOf(";"));
094    }
095
096}