001/*
002 *    Copyright 2009-2022 the original author or authors.
003 *
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 *
008 *       https://www.apache.org/licenses/LICENSE-2.0
009 *
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.mybatis.binding;
017
018import com.mybatisflex.core.mybatis.FlexConfiguration;
019import org.apache.ibatis.session.SqlSession;
020
021import java.lang.reflect.Method;
022import java.lang.reflect.Proxy;
023import java.util.Map;
024import java.util.concurrent.ConcurrentHashMap;
025
026/**
027 * @author Lasse Voss
028 * @author Michael Yang
029 */
030public class FlexMapperProxyFactory<T> {
031
032    private final Class<T> mapperInterface;
033    private final Map<Method, MybatisMapperProxy.MapperMethodInvoker> methodCache = new ConcurrentHashMap<>();
034
035    public FlexMapperProxyFactory(Class<T> mapperInterface) {
036        this.mapperInterface = mapperInterface;
037    }
038
039    public Class<T> getMapperInterface() {
040        return mapperInterface;
041    }
042
043    public Map<Method, MybatisMapperProxy.MapperMethodInvoker> getMethodCache() {
044        return methodCache;
045    }
046
047    @SuppressWarnings("unchecked")
048    protected T newInstance(FlexMapperProxy<T> mapperProxy) {
049        return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[]{mapperInterface}, mapperProxy);
050    }
051
052    public T newInstance(SqlSession sqlSession, FlexConfiguration configuration) {
053        final FlexMapperProxy<T> mapperProxy = new FlexMapperProxy<>(sqlSession, mapperInterface, methodCache, configuration);
054        return newInstance(mapperProxy);
055    }
056
057}