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