1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.executor.resultset;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.apache.ibatis.executor.ExecutorException;
25 import org.apache.ibatis.mapping.ResultMap;
26 import org.apache.ibatis.mapping.ResultMapping;
27 import org.apache.ibatis.reflection.ReflectionException;
28 import org.apache.ibatis.reflection.factory.ObjectFactory;
29
30
31
32
33
34
35 final class PendingConstructorCreation {
36
37 private final Class<?> resultType;
38 private final List<Class<?>> constructorArgTypes;
39 private final List<Object> constructorArgs;
40
41 private final Map<Integer, PendingCreationMetaInfo> linkedCollectionMetaInfo;
42 private final Map<PendingCreationKey, Collection<Object>> linkedCollectionsByKey;
43 private final Map<PendingCreationKey, List<PendingConstructorCreation>> linkedCreationsByKey;
44
45 PendingConstructorCreation(Class<?> resultType, List<Class<?>> types, List<Object> args) {
46
47 final int maxSize = types.size();
48
49 this.linkedCollectionMetaInfo = new HashMap<>(maxSize);
50 this.linkedCollectionsByKey = new HashMap<>(maxSize);
51 this.linkedCreationsByKey = new HashMap<>(maxSize);
52
53 this.resultType = resultType;
54 this.constructorArgTypes = types;
55 this.constructorArgs = args;
56 }
57
58 @SuppressWarnings("unchecked")
59 Collection<Object> initializeCollectionForResultMapping(ObjectFactory objectFactory, ResultMap resultMap,
60 ResultMapping constructorMapping, Integer index) {
61 final Class<?> parameterType = constructorMapping.getJavaType();
62 if (!objectFactory.isCollection(parameterType)) {
63 throw new ReflectionException(
64 "Cannot add a collection result to non-collection based resultMapping: " + constructorMapping);
65 }
66
67 return linkedCollectionsByKey.computeIfAbsent(new PendingCreationKey(constructorMapping), k -> {
68
69 linkedCollectionMetaInfo.put(index, new PendingCreationMetaInfo(resultMap.getType(), k));
70
71
72 return (Collection<Object>) objectFactory.create(parameterType);
73 });
74 }
75
76 void linkCreation(ResultMapping constructorMapping, PendingConstructorCreation pcc) {
77 final PendingCreationKey creationKey = new PendingCreationKey(constructorMapping);
78 final List<PendingConstructorCreation> pendingConstructorCreations = linkedCreationsByKey
79 .computeIfAbsent(creationKey, k -> new ArrayList<>());
80
81 if (pendingConstructorCreations.contains(pcc)) {
82 throw new ExecutorException("Cannot link inner constructor creation with same value, MyBatis internal error!");
83 }
84
85 pendingConstructorCreations.add(pcc);
86 }
87
88 void linkCollectionValue(ResultMapping constructorMapping, Object value) {
89
90 if (value == null) {
91 return;
92 }
93
94 linkedCollectionsByKey.computeIfAbsent(new PendingCreationKey(constructorMapping), k -> {
95 throw new ExecutorException("Cannot link collection value for key: " + constructorMapping
96 + ", resultMap has not been seen/initialized yet! Mybatis internal error!");
97 }).add(value);
98 }
99
100 @Override
101 public String toString() {
102 return "PendingConstructorCreation(" + this.hashCode() + "){" + "resultType=" + resultType + '}';
103 }
104
105
106
107
108
109
110
111
112
113 Object create(ObjectFactory objectFactory) {
114 final List<Object> newArguments = new ArrayList<>(constructorArgs.size());
115 for (int i = 0; i < constructorArgs.size(); i++) {
116 final PendingCreationMetaInfo creationMetaInfo = linkedCollectionMetaInfo.get(i);
117 final Object existingArg = constructorArgs.get(i);
118
119 if (creationMetaInfo == null) {
120
121 newArguments.add(existingArg);
122 continue;
123 }
124
125
126 final PendingCreationKey pendingCreationKey = creationMetaInfo.getPendingCreationKey();
127 final List<PendingConstructorCreation> linkedCreations = linkedCreationsByKey.get(pendingCreationKey);
128 if (linkedCreations != null) {
129 @SuppressWarnings("unchecked")
130 final Collection<Object> emptyCollection = (Collection<Object>) existingArg;
131
132 for (PendingConstructorCreation linkedCreation : linkedCreations) {
133 emptyCollection.add(linkedCreation.create(objectFactory));
134 }
135
136 newArguments.add(emptyCollection);
137 continue;
138 }
139
140
141 newArguments.add(existingArg);
142 }
143
144 return objectFactory.create(resultType, constructorArgTypes, newArguments);
145 }
146 }