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> QueryColumn getQueryColumn(LambdaGetter<T> getter) {
047        SerializedLambda lambda = getSerializedLambda(getter);
048        String methodName = lambda.getImplMethodName();
049        String implClass = lambda.getImplClass();
050        Class<?> entityClass = MapUtil.computeIfAbsent(classMap, implClass, s -> {
051            try {
052                return Class.forName(s.replace("/","."));
053            } catch (ClassNotFoundException e) {
054                throw FlexExceptions.wrap(e);
055            }
056        });
057        TableInfo tableInfo = TableInfoFactory.ofEntityClass(entityClass);
058        return tableInfo.getQueryColumnByProperty(PropertyNamer.methodToProperty(methodName));
059    }
060
061
062    private static SerializedLambda getSerializedLambda(Serializable getter) {
063        return MapUtil.computeIfAbsent(lambdaMap, getter.getClass(), aClass -> {
064            try {
065                Method method = getter.getClass().getDeclaredMethod("writeReplace");
066                method.setAccessible(Boolean.TRUE);
067                return (SerializedLambda) method.invoke(getter);
068            } catch (Exception e) {
069                throw new RuntimeException(e);
070            }
071        });
072    }
073
074}