CglibProxyFactory.java

  1. /*
  2.  *    Copyright 2009-2022 the original author or authors.
  3.  *
  4.  *    Licensed under the Apache License, Version 2.0 (the "License");
  5.  *    you may not use this file except in compliance with the License.
  6.  *    You may obtain a copy of the License at
  7.  *
  8.  *       http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  *    Unless required by applicable law or agreed to in writing, software
  11.  *    distributed under the License is distributed on an "AS IS" BASIS,
  12.  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  *    See the License for the specific language governing permissions and
  14.  *    limitations under the License.
  15.  */
  16. package org.apache.ibatis.executor.loader.cglib;

  17. import java.lang.reflect.Method;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.Set;

  21. import net.sf.cglib.proxy.Callback;
  22. import net.sf.cglib.proxy.Enhancer;
  23. import net.sf.cglib.proxy.MethodInterceptor;
  24. import net.sf.cglib.proxy.MethodProxy;

  25. import org.apache.ibatis.executor.loader.AbstractEnhancedDeserializationProxy;
  26. import org.apache.ibatis.executor.loader.AbstractSerialStateHolder;
  27. import org.apache.ibatis.executor.loader.ProxyFactory;
  28. import org.apache.ibatis.executor.loader.ResultLoaderMap;
  29. import org.apache.ibatis.executor.loader.WriteReplaceInterface;
  30. import org.apache.ibatis.io.Resources;
  31. import org.apache.ibatis.logging.Log;
  32. import org.apache.ibatis.logging.LogFactory;
  33. import org.apache.ibatis.reflection.ExceptionUtil;
  34. import org.apache.ibatis.reflection.factory.ObjectFactory;
  35. import org.apache.ibatis.reflection.property.PropertyCopier;
  36. import org.apache.ibatis.reflection.property.PropertyNamer;
  37. import org.apache.ibatis.session.Configuration;

  38. /**
  39.  * @author Clinton Begin
  40.  * @deprecated Since 3.5.10, use Javassist instead.
  41.  */
  42. @Deprecated
  43. public class CglibProxyFactory implements ProxyFactory {

  44.   private static final String FINALIZE_METHOD = "finalize";
  45.   private static final String WRITE_REPLACE_METHOD = "writeReplace";

  46.   public CglibProxyFactory() {
  47.     try {
  48.       Resources.classForName("net.sf.cglib.proxy.Enhancer");
  49.     } catch (Throwable e) {
  50.       throw new IllegalStateException("Cannot enable lazy loading because CGLIB is not available. Add CGLIB to your classpath.", e);
  51.     }
  52.   }

  53.   @Override
  54.   public Object createProxy(Object target, ResultLoaderMap lazyLoader, Configuration configuration, ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
  55.     return EnhancedResultObjectProxyImpl.createProxy(target, lazyLoader, configuration, objectFactory, constructorArgTypes, constructorArgs);
  56.   }

  57.   public Object createDeserializationProxy(Object target, Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
  58.     return EnhancedDeserializationProxyImpl.createProxy(target, unloadedProperties, objectFactory, constructorArgTypes, constructorArgs);
  59.   }

  60.   static Object crateProxy(Class<?> type, Callback callback, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
  61.     LogHolder.log.warn("CglibProxyFactory is deprecated. Use another proxy factory implementation.");
  62.     Enhancer enhancer = new Enhancer();
  63.     enhancer.setCallback(callback);
  64.     enhancer.setSuperclass(type);
  65.     try {
  66.       type.getDeclaredMethod(WRITE_REPLACE_METHOD);
  67.       // ObjectOutputStream will call writeReplace of objects returned by writeReplace
  68.       if (LogHolder.log.isDebugEnabled()) {
  69.         LogHolder.log.debug(WRITE_REPLACE_METHOD + " method was found on bean " + type + ", make sure it returns this");
  70.       }
  71.     } catch (NoSuchMethodException e) {
  72.       enhancer.setInterfaces(new Class[] { WriteReplaceInterface.class });
  73.     } catch (SecurityException e) {
  74.       // nothing to do here
  75.     }
  76.     Object enhanced;
  77.     if (constructorArgTypes.isEmpty()) {
  78.       enhanced = enhancer.create();
  79.     } else {
  80.       Class<?>[] typesArray = constructorArgTypes.toArray(new Class[constructorArgTypes.size()]);
  81.       Object[] valuesArray = constructorArgs.toArray(new Object[constructorArgs.size()]);
  82.       enhanced = enhancer.create(typesArray, valuesArray);
  83.     }
  84.     return enhanced;
  85.   }

  86.   private static class EnhancedResultObjectProxyImpl implements MethodInterceptor {

  87.     private final Class<?> type;
  88.     private final ResultLoaderMap lazyLoader;
  89.     private final boolean aggressive;
  90.     private final Set<String> lazyLoadTriggerMethods;
  91.     private final ObjectFactory objectFactory;
  92.     private final List<Class<?>> constructorArgTypes;
  93.     private final List<Object> constructorArgs;

  94.     private EnhancedResultObjectProxyImpl(Class<?> type, ResultLoaderMap lazyLoader, Configuration configuration, ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
  95.       this.type = type;
  96.       this.lazyLoader = lazyLoader;
  97.       this.aggressive = configuration.isAggressiveLazyLoading();
  98.       this.lazyLoadTriggerMethods = configuration.getLazyLoadTriggerMethods();
  99.       this.objectFactory = objectFactory;
  100.       this.constructorArgTypes = constructorArgTypes;
  101.       this.constructorArgs = constructorArgs;
  102.     }

  103.     public static Object createProxy(Object target, ResultLoaderMap lazyLoader, Configuration configuration, ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
  104.       final Class<?> type = target.getClass();
  105.       EnhancedResultObjectProxyImpl callback = new EnhancedResultObjectProxyImpl(type, lazyLoader, configuration, objectFactory, constructorArgTypes, constructorArgs);
  106.       Object enhanced = crateProxy(type, callback, constructorArgTypes, constructorArgs);
  107.       PropertyCopier.copyBeanProperties(type, target, enhanced);
  108.       return enhanced;
  109.     }

  110.     @Override
  111.     public Object intercept(Object enhanced, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
  112.       final String methodName = method.getName();
  113.       try {
  114.         synchronized (lazyLoader) {
  115.           if (WRITE_REPLACE_METHOD.equals(methodName)) {
  116.             Object original;
  117.             if (constructorArgTypes.isEmpty()) {
  118.               original = objectFactory.create(type);
  119.             } else {
  120.               original = objectFactory.create(type, constructorArgTypes, constructorArgs);
  121.             }
  122.             PropertyCopier.copyBeanProperties(type, enhanced, original);
  123.             if (lazyLoader.size() > 0) {
  124.               return new CglibSerialStateHolder(original, lazyLoader.getProperties(), objectFactory, constructorArgTypes, constructorArgs);
  125.             } else {
  126.               return original;
  127.             }
  128.           } else {
  129.             if (lazyLoader.size() > 0 && !FINALIZE_METHOD.equals(methodName)) {
  130.               if (aggressive || lazyLoadTriggerMethods.contains(methodName)) {
  131.                 lazyLoader.loadAll();
  132.               } else if (PropertyNamer.isSetter(methodName)) {
  133.                 final String property = PropertyNamer.methodToProperty(methodName);
  134.                 lazyLoader.remove(property);
  135.               } else if (PropertyNamer.isGetter(methodName)) {
  136.                 final String property = PropertyNamer.methodToProperty(methodName);
  137.                 if (lazyLoader.hasLoader(property)) {
  138.                   lazyLoader.load(property);
  139.                 }
  140.               }
  141.             }
  142.           }
  143.         }
  144.         return methodProxy.invokeSuper(enhanced, args);
  145.       } catch (Throwable t) {
  146.         throw ExceptionUtil.unwrapThrowable(t);
  147.       }
  148.     }
  149.   }

  150.   private static class EnhancedDeserializationProxyImpl extends AbstractEnhancedDeserializationProxy implements MethodInterceptor {

  151.     private EnhancedDeserializationProxyImpl(Class<?> type, Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory,
  152.             List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
  153.       super(type, unloadedProperties, objectFactory, constructorArgTypes, constructorArgs);
  154.     }

  155.     public static Object createProxy(Object target, Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory,
  156.             List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
  157.       final Class<?> type = target.getClass();
  158.       EnhancedDeserializationProxyImpl callback = new EnhancedDeserializationProxyImpl(type, unloadedProperties, objectFactory, constructorArgTypes, constructorArgs);
  159.       Object enhanced = crateProxy(type, callback, constructorArgTypes, constructorArgs);
  160.       PropertyCopier.copyBeanProperties(type, target, enhanced);
  161.       return enhanced;
  162.     }

  163.     @Override
  164.     public Object intercept(Object enhanced, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
  165.       final Object o = super.invoke(enhanced, method, args);
  166.       return o instanceof AbstractSerialStateHolder ? o : methodProxy.invokeSuper(o, args);
  167.     }

  168.     @Override
  169.     protected AbstractSerialStateHolder newSerialStateHolder(Object userBean, Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory,
  170.             List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
  171.       return new CglibSerialStateHolder(userBean, unloadedProperties, objectFactory, constructorArgTypes, constructorArgs);
  172.     }
  173.   }

  174.   private static class LogHolder {
  175.     private static final Log log = LogFactory.getLog(CglibProxyFactory.class);
  176.   }

  177. }