JavassistProxyFactory.java

  1. /*
  2.  *    Copyright 2009-2024 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.  *       https://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.javassist;

  17. import java.lang.reflect.Method;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.Set;
  21. import java.util.concurrent.locks.ReentrantLock;

  22. import javassist.util.proxy.MethodHandler;
  23. import javassist.util.proxy.Proxy;
  24. import javassist.util.proxy.ProxyFactory;

  25. import org.apache.ibatis.executor.ExecutorException;
  26. import org.apache.ibatis.executor.loader.AbstractEnhancedDeserializationProxy;
  27. import org.apache.ibatis.executor.loader.AbstractSerialStateHolder;
  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 Eduardo Macarron
  40.  */
  41. public class JavassistProxyFactory implements org.apache.ibatis.executor.loader.ProxyFactory {

  42.   private static final String FINALIZE_METHOD = "finalize";
  43.   private static final String WRITE_REPLACE_METHOD = "writeReplace";

  44.   public JavassistProxyFactory() {
  45.     try {
  46.       Resources.classForName("javassist.util.proxy.ProxyFactory");
  47.     } catch (Throwable e) {
  48.       throw new IllegalStateException(
  49.           "Cannot enable lazy loading because Javassist is not available. Add Javassist to your classpath.", e);
  50.     }
  51.   }

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

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

  63.   static Object createStaticProxy(Class<?> type, MethodHandler callback, List<Class<?>> constructorArgTypes,
  64.       List<Object> constructorArgs) {

  65.     ProxyFactory enhancer = new ProxyFactory();
  66.     enhancer.setSuperclass(type);

  67.     try {
  68.       type.getDeclaredMethod(WRITE_REPLACE_METHOD);
  69.       // ObjectOutputStream will call writeReplace of objects returned by writeReplace
  70.       if (LogHolder.log.isDebugEnabled()) {
  71.         LogHolder.log.debug(WRITE_REPLACE_METHOD + " method was found on bean " + type + ", make sure it returns this");
  72.       }
  73.     } catch (NoSuchMethodException e) {
  74.       enhancer.setInterfaces(new Class[] { WriteReplaceInterface.class });
  75.     } catch (SecurityException e) {
  76.       // nothing to do here
  77.     }

  78.     Object enhanced;
  79.     Class<?>[] typesArray = constructorArgTypes.toArray(new Class[constructorArgTypes.size()]);
  80.     Object[] valuesArray = constructorArgs.toArray(new Object[constructorArgs.size()]);
  81.     try {
  82.       enhanced = enhancer.create(typesArray, valuesArray);
  83.     } catch (Exception e) {
  84.       throw new ExecutorException("Error creating lazy proxy.  Cause: " + e, e);
  85.     }
  86.     ((Proxy) enhanced).setHandler(callback);
  87.     return enhanced;
  88.   }

  89.   private static class EnhancedResultObjectProxyImpl implements MethodHandler {
  90.     private final Class<?> type;
  91.     private final ResultLoaderMap lazyLoader;
  92.     private final boolean aggressive;
  93.     private final Set<String> lazyLoadTriggerMethods;
  94.     private final ObjectFactory objectFactory;
  95.     private final List<Class<?>> constructorArgTypes;
  96.     private final List<Object> constructorArgs;
  97.     private final ReentrantLock lock = new ReentrantLock();

  98.     private EnhancedResultObjectProxyImpl(Class<?> type, ResultLoaderMap lazyLoader, Configuration configuration,
  99.         ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
  100.       this.type = type;
  101.       this.lazyLoader = lazyLoader;
  102.       this.aggressive = configuration.isAggressiveLazyLoading();
  103.       this.lazyLoadTriggerMethods = configuration.getLazyLoadTriggerMethods();
  104.       this.objectFactory = objectFactory;
  105.       this.constructorArgTypes = constructorArgTypes;
  106.       this.constructorArgs = constructorArgs;
  107.     }

  108.     public static Object createProxy(Object target, ResultLoaderMap lazyLoader, Configuration configuration,
  109.         ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
  110.       final Class<?> type = target.getClass();
  111.       EnhancedResultObjectProxyImpl callback = new EnhancedResultObjectProxyImpl(type, lazyLoader, configuration,
  112.           objectFactory, constructorArgTypes, constructorArgs);
  113.       Object enhanced = createStaticProxy(type, callback, constructorArgTypes, constructorArgs);
  114.       PropertyCopier.copyBeanProperties(type, target, enhanced);
  115.       return enhanced;
  116.     }

  117.     @Override
  118.     public Object invoke(Object enhanced, Method method, Method methodProxy, Object[] args) throws Throwable {
  119.       final String methodName = method.getName();
  120.       lock.lock();
  121.       try {
  122.         if (WRITE_REPLACE_METHOD.equals(methodName)) {
  123.           Object original;
  124.           if (constructorArgTypes.isEmpty()) {
  125.             original = objectFactory.create(type);
  126.           } else {
  127.             original = objectFactory.create(type, constructorArgTypes, constructorArgs);
  128.           }
  129.           PropertyCopier.copyBeanProperties(type, enhanced, original);
  130.           if (!lazyLoader.isEmpty()) {
  131.             return new JavassistSerialStateHolder(original, lazyLoader.getProperties(), objectFactory,
  132.                 constructorArgTypes, constructorArgs);
  133.           } else {
  134.             return original;
  135.           }
  136.         }
  137.         if (!lazyLoader.isEmpty() && !FINALIZE_METHOD.equals(methodName)) {
  138.           if (aggressive || lazyLoadTriggerMethods.contains(methodName)) {
  139.             lazyLoader.loadAll();
  140.           } else if (PropertyNamer.isSetter(methodName)) {
  141.             final String property = PropertyNamer.methodToProperty(methodName);
  142.             lazyLoader.remove(property);
  143.           } else if (PropertyNamer.isGetter(methodName)) {
  144.             final String property = PropertyNamer.methodToProperty(methodName);
  145.             if (lazyLoader.hasLoader(property)) {
  146.               lazyLoader.load(property);
  147.             }
  148.           }
  149.         }
  150.         return methodProxy.invoke(enhanced, args);
  151.       } catch (Throwable t) {
  152.         throw ExceptionUtil.unwrapThrowable(t);
  153.       } finally {
  154.         lock.unlock();
  155.       }
  156.     }
  157.   }

  158.   private static class EnhancedDeserializationProxyImpl extends AbstractEnhancedDeserializationProxy
  159.       implements MethodHandler {

  160.     private EnhancedDeserializationProxyImpl(Class<?> type, Map<String, ResultLoaderMap.LoadPair> unloadedProperties,
  161.         ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
  162.       super(type, unloadedProperties, objectFactory, constructorArgTypes, constructorArgs);
  163.     }

  164.     public static Object createProxy(Object target, Map<String, ResultLoaderMap.LoadPair> unloadedProperties,
  165.         ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
  166.       final Class<?> type = target.getClass();
  167.       EnhancedDeserializationProxyImpl callback = new EnhancedDeserializationProxyImpl(type, unloadedProperties,
  168.           objectFactory, constructorArgTypes, constructorArgs);
  169.       Object enhanced = createStaticProxy(type, callback, constructorArgTypes, constructorArgs);
  170.       PropertyCopier.copyBeanProperties(type, target, enhanced);
  171.       return enhanced;
  172.     }

  173.     @Override
  174.     public Object invoke(Object enhanced, Method method, Method methodProxy, Object[] args) throws Throwable {
  175.       final Object o = super.invoke(enhanced, method, args);
  176.       return o instanceof AbstractSerialStateHolder ? o : methodProxy.invoke(o, args);
  177.     }

  178.     @Override
  179.     protected AbstractSerialStateHolder newSerialStateHolder(Object userBean,
  180.         Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory,
  181.         List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
  182.       return new JavassistSerialStateHolder(userBean, unloadedProperties, objectFactory, constructorArgTypes,
  183.           constructorArgs);
  184.     }
  185.   }

  186.   private static class LogHolder {
  187.     private static final Log log = LogFactory.getLog(JavassistProxyFactory.class);
  188.   }

  189. }