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.update;
017
018import org.apache.ibatis.javassist.util.proxy.ProxyFactory;
019import org.apache.ibatis.javassist.util.proxy.ProxyObject;
020import org.apache.ibatis.logging.LogFactory;
021
022import java.util.Arrays;
023
024
025public class ModifyAttrsRecordProxyFactory {
026
027    private static final ModifyAttrsRecordProxyFactory instance = new ModifyAttrsRecordProxyFactory();
028
029    public static ModifyAttrsRecordProxyFactory getInstance() {
030        return instance;
031    }
032
033    private ModifyAttrsRecordProxyFactory() {
034    }
035
036    public <T> T get(Class<T> target) {
037        ProxyFactory factory = new ProxyFactory();
038        factory.setSuperclass(target);
039
040        Class<?>[] interfaces = Arrays.copyOf(target.getInterfaces(), target.getInterfaces().length + 1);
041        interfaces[interfaces.length - 1] = UpdateWrapper.class;
042        factory.setInterfaces(interfaces);
043
044
045        final Class<?> proxyClass = factory.createClass();
046
047        T proxyObject = null;
048        try {
049            proxyObject = (T) proxyClass.newInstance();
050            ((ProxyObject) proxyObject).setHandler(new ModifyAttrsRecordHandler());
051        } catch (Throwable e) {
052            LogFactory.getLog(ModifyAttrsRecordProxyFactory.class).error(e.toString(), e);
053        }
054
055        return proxyObject;
056    }
057
058
059}
060
061
062