1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
87
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 }