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
046    public static <T> Class<?> getImplClass(LambdaGetter<T> getter) {
047        SerializedLambda lambda = getSerializedLambda(getter);
048        return getImplClass(lambda, getter.getClass().getClassLoader());
049    }
050
051
052    public static <T> String getAliasName(LambdaGetter<T> getter, boolean withPrefix) {
053        QueryColumn queryColumn = getQueryColumn(getter);
054        if (queryColumn != null) {
055            String alias = StringUtil.isNotBlank(queryColumn.getAlias()) ? queryColumn.getAlias() : queryColumn.getName();
056            return withPrefix ? queryColumn.getTable().getName() + "$" + alias : alias;
057        }
058        return getFieldName(getter);
059    }
060
061
062    public static <T> QueryColumn getQueryColumn(LambdaGetter<T> getter) {
063        ClassLoader classLoader = getter.getClass().getClassLoader();
064        SerializedLambda lambda = getSerializedLambda(getter);
065        String methodName = lambda.getImplMethodName();
066        Class<?> entityClass = getImplClass(lambda, classLoader);
067        TableInfo tableInfo = TableInfoFactory.ofEntityClass(entityClass);
068        return tableInfo.getQueryColumnByProperty(PropertyNamer.methodToProperty(methodName));
069    }
070
071
072    private static SerializedLambda getSerializedLambda(Serializable getter) {
073        return MapUtil.computeIfAbsent(lambdaMap, getter.getClass(), aClass -> {
074            try {
075                Method method = getter.getClass().getDeclaredMethod("writeReplace");
076                method.setAccessible(Boolean.TRUE);
077                return (SerializedLambda) method.invoke(getter);
078            } catch (Exception e) {
079                throw new RuntimeException(e);
080            }
081        });
082    }
083
084
085    private static Class<?> getImplClass(SerializedLambda lambda, ClassLoader classLoader) {
086        String implClass = getImplClassName(lambda);
087        return MapUtil.computeIfAbsent(classMap, implClass, s -> {
088            try {
089                return Class.forName(s.replace("/", "."), true, classLoader);
090            } catch (ClassNotFoundException e) {
091                throw FlexExceptions.wrap(e);
092            }
093        });
094    }
095
096    private static String getImplClassName(SerializedLambda lambda) {
097        String type = lambda.getInstantiatedMethodType();
098        return type.substring(2, type.indexOf(";"));
099    }
100
101}