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