View Javadoc
1   /*
2    *    Copyright 2009-2026 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;
17  
18  import java.lang.reflect.Method;
19  import java.util.List;
20  import java.util.Locale;
21  import java.util.Map;
22  import java.util.concurrent.locks.ReentrantLock;
23  
24  import org.apache.ibatis.executor.ErrorContext;
25  import org.apache.ibatis.executor.ExecutorException;
26  import org.apache.ibatis.reflection.ExceptionUtil;
27  import org.apache.ibatis.reflection.factory.ObjectFactory;
28  import org.apache.ibatis.reflection.property.PropertyCopier;
29  import org.apache.ibatis.reflection.property.PropertyNamer;
30  
31  /**
32   * @author Clinton Begin
33   */
34  public abstract class AbstractEnhancedDeserializationProxy {
35  
36    protected static final String FINALIZE_METHOD = "finalize";
37    protected static final String WRITE_REPLACE_METHOD = "writeReplace";
38    private final Class<?> type;
39    private final Map<String, ResultLoaderMap.LoadPair> unloadedProperties;
40    private final ObjectFactory objectFactory;
41    private final List<Class<?>> constructorArgTypes;
42    private final List<Object> constructorArgs;
43    private final ReentrantLock lock = new ReentrantLock();
44    private boolean reloadingProperty;
45  
46    protected AbstractEnhancedDeserializationProxy(Class<?> type,
47        Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory,
48        List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
49      this.type = type;
50      this.unloadedProperties = unloadedProperties;
51      this.objectFactory = objectFactory;
52      this.constructorArgTypes = constructorArgTypes;
53      this.constructorArgs = constructorArgs;
54      this.reloadingProperty = false;
55    }
56  
57    public final Object invoke(Object enhanced, Method method, Object[] args) throws Throwable {
58      final String methodName = method.getName();
59      try {
60        if (WRITE_REPLACE_METHOD.equals(methodName)) {
61          final Object original;
62          if (constructorArgTypes.isEmpty()) {
63            original = objectFactory.create(type);
64          } else {
65            original = objectFactory.create(type, constructorArgTypes, constructorArgs);
66          }
67  
68          PropertyCopier.copyBeanProperties(type, enhanced, original);
69          return this.newSerialStateHolder(original, unloadedProperties, objectFactory, constructorArgTypes,
70              constructorArgs);
71        }
72        lock.lock();
73        try {
74          if (!FINALIZE_METHOD.equals(methodName) && PropertyNamer.isProperty(methodName) && !reloadingProperty) {
75            final String property = PropertyNamer.methodToProperty(methodName);
76            final String propertyKey = property.toUpperCase(Locale.ENGLISH);
77            if (unloadedProperties.containsKey(propertyKey)) {
78              final ResultLoaderMap.LoadPair loadPair = unloadedProperties.remove(propertyKey);
79              if (loadPair != null) {
80                try {
81                  reloadingProperty = true;
82                  loadPair.load(enhanced);
83                } finally {
84                  reloadingProperty = false;
85                  ErrorContext.instance().reset();
86                }
87              } else {
88                /*
89                 * I'm not sure if this case can really happen or is just in tests - we have an unread property but no
90                 * loadPair to load it.
91                 */
92                throw new ExecutorException("An attempt has been made to read a not loaded lazy property '" + property
93                    + "' of a disconnected object");
94              }
95            }
96          }
97  
98          return enhanced;
99        } finally {
100         lock.unlock();
101       }
102     } catch (Throwable t) {
103       throw ExceptionUtil.unwrapThrowable(t);
104     }
105   }
106 
107   protected abstract AbstractSerialStateHolder newSerialStateHolder(Object userBean,
108       Map<String, ResultLoaderMap.LoadPair> unloadedProperties, ObjectFactory objectFactory,
109       List<Class<?>> constructorArgTypes, List<Object> constructorArgs);
110 
111 }