View Javadoc
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  
18  import java.lang.reflect.Method;
19  import java.util.List;
20  import java.util.Map;
21  import java.util.Set;
22  
23  import net.sf.cglib.proxy.Callback;
24  import net.sf.cglib.proxy.Enhancer;
25  import net.sf.cglib.proxy.MethodInterceptor;
26  import net.sf.cglib.proxy.MethodProxy;
27  
28  import org.apache.ibatis.executor.loader.AbstractEnhancedDeserializationProxy;
29  import org.apache.ibatis.executor.loader.AbstractSerialStateHolder;
30  import org.apache.ibatis.executor.loader.ProxyFactory;
31  import org.apache.ibatis.executor.loader.ResultLoaderMap;
32  import org.apache.ibatis.executor.loader.WriteReplaceInterface;
33  import org.apache.ibatis.io.Resources;
34  import org.apache.ibatis.logging.Log;
35  import org.apache.ibatis.logging.LogFactory;
36  import org.apache.ibatis.reflection.ExceptionUtil;
37  import org.apache.ibatis.reflection.factory.ObjectFactory;
38  import org.apache.ibatis.reflection.property.PropertyCopier;
39  import org.apache.ibatis.reflection.property.PropertyNamer;
40  import org.apache.ibatis.session.Configuration;
41  
42  /**
43   * @author Clinton Begin
44   * @deprecated Since 3.5.10, use Javassist instead.
45   */
46  @Deprecated
47  public class CglibProxyFactory implements ProxyFactory {
48  
49    private static final String FINALIZE_METHOD = "finalize";
50    private static final String WRITE_REPLACE_METHOD = "writeReplace";
51  
52    public CglibProxyFactory() {
53      try {
54        Resources.classForName("net.sf.cglib.proxy.Enhancer");
55      } catch (Throwable e) {
56        throw new IllegalStateException("Cannot enable lazy loading because CGLIB is not available. Add CGLIB to your classpath.", e);
57      }
58    }
59  
60    @Override
61    public Object createProxy(Object target, ResultLoaderMap lazyLoader, Configuration configuration, ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
62      return EnhancedResultObjectProxyImpl.createProxy(target, lazyLoader, configuration, objectFactory, constructorArgTypes, constructorArgs);
63    }
64  
65    public Object createDeserializationProxy(Object target, Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
66      return EnhancedDeserializationProxyImpl.createProxy(target, unloadedProperties, objectFactory, constructorArgTypes, constructorArgs);
67    }
68  
69    static Object crateProxy(Class<?> type, Callback callback, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
70      LogHolder.log.warn("CglibProxyFactory is deprecated. Use another proxy factory implementation.");
71      Enhancer enhancer = new Enhancer();
72      enhancer.setCallback(callback);
73      enhancer.setSuperclass(type);
74      try {
75        type.getDeclaredMethod(WRITE_REPLACE_METHOD);
76        // ObjectOutputStream will call writeReplace of objects returned by writeReplace
77        if (LogHolder.log.isDebugEnabled()) {
78          LogHolder.log.debug(WRITE_REPLACE_METHOD + " method was found on bean " + type + ", make sure it returns this");
79        }
80      } catch (NoSuchMethodException e) {
81        enhancer.setInterfaces(new Class[] { WriteReplaceInterface.class });
82      } catch (SecurityException e) {
83        // nothing to do here
84      }
85      Object enhanced;
86      if (constructorArgTypes.isEmpty()) {
87        enhanced = enhancer.create();
88      } else {
89        Class<?>[] typesArray = constructorArgTypes.toArray(new Class[constructorArgTypes.size()]);
90        Object[] valuesArray = constructorArgs.toArray(new Object[constructorArgs.size()]);
91        enhanced = enhancer.create(typesArray, valuesArray);
92      }
93      return enhanced;
94    }
95  
96    private static class EnhancedResultObjectProxyImpl implements MethodInterceptor {
97  
98      private final Class<?> type;
99      private final ResultLoaderMap lazyLoader;
100     private final boolean aggressive;
101     private final Set<String> lazyLoadTriggerMethods;
102     private final ObjectFactory objectFactory;
103     private final List<Class<?>> constructorArgTypes;
104     private final List<Object> constructorArgs;
105 
106     private EnhancedResultObjectProxyImpl(Class<?> type, ResultLoaderMap lazyLoader, Configuration configuration, ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
107       this.type = type;
108       this.lazyLoader = lazyLoader;
109       this.aggressive = configuration.isAggressiveLazyLoading();
110       this.lazyLoadTriggerMethods = configuration.getLazyLoadTriggerMethods();
111       this.objectFactory = objectFactory;
112       this.constructorArgTypes = constructorArgTypes;
113       this.constructorArgs = constructorArgs;
114     }
115 
116     public static Object createProxy(Object target, ResultLoaderMap lazyLoader, Configuration configuration, ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
117       final Class<?> type = target.getClass();
118       EnhancedResultObjectProxyImpl callback = new EnhancedResultObjectProxyImpl(type, lazyLoader, configuration, objectFactory, constructorArgTypes, constructorArgs);
119       Object enhanced = crateProxy(type, callback, constructorArgTypes, constructorArgs);
120       PropertyCopier.copyBeanProperties(type, target, enhanced);
121       return enhanced;
122     }
123 
124     @Override
125     public Object intercept(Object enhanced, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
126       final String methodName = method.getName();
127       try {
128         synchronized (lazyLoader) {
129           if (WRITE_REPLACE_METHOD.equals(methodName)) {
130             Object original;
131             if (constructorArgTypes.isEmpty()) {
132               original = objectFactory.create(type);
133             } else {
134               original = objectFactory.create(type, constructorArgTypes, constructorArgs);
135             }
136             PropertyCopier.copyBeanProperties(type, enhanced, original);
137             if (lazyLoader.size() > 0) {
138               return new CglibSerialStateHolder(original, lazyLoader.getProperties(), objectFactory, constructorArgTypes, constructorArgs);
139             } else {
140               return original;
141             }
142           } else {
143             if (lazyLoader.size() > 0 && !FINALIZE_METHOD.equals(methodName)) {
144               if (aggressive || lazyLoadTriggerMethods.contains(methodName)) {
145                 lazyLoader.loadAll();
146               } else if (PropertyNamer.isSetter(methodName)) {
147                 final String property = PropertyNamer.methodToProperty(methodName);
148                 lazyLoader.remove(property);
149               } else if (PropertyNamer.isGetter(methodName)) {
150                 final String property = PropertyNamer.methodToProperty(methodName);
151                 if (lazyLoader.hasLoader(property)) {
152                   lazyLoader.load(property);
153                 }
154               }
155             }
156           }
157         }
158         return methodProxy.invokeSuper(enhanced, args);
159       } catch (Throwable t) {
160         throw ExceptionUtil.unwrapThrowable(t);
161       }
162     }
163   }
164 
165   private static class EnhancedDeserializationProxyImpl extends AbstractEnhancedDeserializationProxy implements MethodInterceptor {
166 
167     private EnhancedDeserializationProxyImpl(Class<?> type, Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory,
168             List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
169       super(type, unloadedProperties, objectFactory, constructorArgTypes, constructorArgs);
170     }
171 
172     public static Object createProxy(Object target, Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory,
173             List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
174       final Class<?> type = target.getClass();
175       EnhancedDeserializationProxyImpl callback = new EnhancedDeserializationProxyImpl(type, unloadedProperties, objectFactory, constructorArgTypes, constructorArgs);
176       Object enhanced = crateProxy(type, callback, constructorArgTypes, constructorArgs);
177       PropertyCopier.copyBeanProperties(type, target, enhanced);
178       return enhanced;
179     }
180 
181     @Override
182     public Object intercept(Object enhanced, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
183       final Object o = super.invoke(enhanced, method, args);
184       return o instanceof AbstractSerialStateHolder ? o : methodProxy.invokeSuper(o, args);
185     }
186 
187     @Override
188     protected AbstractSerialStateHolder newSerialStateHolder(Object userBean, Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory,
189             List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
190       return new CglibSerialStateHolder(userBean, unloadedProperties, objectFactory, constructorArgTypes, constructorArgs);
191     }
192   }
193 
194   private static class LogHolder {
195     private static final Log log = LogFactory.getLog(CglibProxyFactory.class);
196   }
197 
198 }