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