15
15
*/
16
16
package org .apache .ibatis .executor .resultset ;
17
17
18
- import java .lang .reflect .Constructor ;
19
- import java .lang .reflect .ParameterizedType ;
20
- import java .lang .reflect .Type ;
21
18
import java .util .ArrayList ;
22
19
import java .util .Collection ;
23
- import java .util .Collections ;
24
20
import java .util .HashMap ;
25
21
import java .util .List ;
26
22
import java .util .Map ;
27
- import java .util .Optional ;
28
- import java .util .stream .Collectors ;
29
23
30
24
import org .apache .ibatis .executor .ExecutorException ;
31
25
import org .apache .ibatis .mapping .ResultMap ;
32
26
import org .apache .ibatis .mapping .ResultMapping ;
33
27
import org .apache .ibatis .reflection .ReflectionException ;
34
- import org .apache .ibatis .reflection .factory .DefaultObjectFactory ;
35
28
import org .apache .ibatis .reflection .factory .ObjectFactory ;
36
29
37
30
/**
@@ -108,87 +101,6 @@ void linkCollectionValue(ResultMapping constructorMapping, Object value) {
108
101
linkedCollectionsByKey .get (creationKey ).add (value );
109
102
}
110
103
111
- /**
112
- * Verifies preconditions before we can actually create the result object, this is more of a sanity check to ensure
113
- * all the mappings are as we expect them to be.
114
- * <p>
115
- * And if anything went wrong, provide the user with more information as to what went wrong
116
- *
117
- * @param objectFactory
118
- * the object factory
119
- */
120
- private void verifyCanCreate (ObjectFactory objectFactory ) {
121
- // if a custom object factory was supplied, we cannot reasionably verify that creation will work
122
- // thus, we disable verification and leave it up to the end user.
123
- if (!DefaultObjectFactory .class .equals (objectFactory .getClass ())) {
124
- return ;
125
- }
126
-
127
- // before we create, we need to get the constructor to be used and verify our types match
128
- // since we added to the collection completely unchecked
129
- final Constructor <?> resolvedConstructor = resolveConstructor (resultType , constructorArgTypes );
130
- final Type [] genericParameterTypes = resolvedConstructor .getGenericParameterTypes ();
131
- for (int i = 0 ; i < genericParameterTypes .length ; i ++) {
132
- if (!linkedCollectionMetaInfo .containsKey (i )) {
133
- continue ;
134
- }
135
-
136
- final PendingCreationMetaInfo creationMetaInfo = linkedCollectionMetaInfo .get (i );
137
- final Class <?> resolvedItemType = checkResolvedItemType (creationMetaInfo , genericParameterTypes [i ]);
138
-
139
- // ensure we have an empty collection if there are linked creations for this arg
140
- final PendingCreationKey pendingCreationKey = creationMetaInfo .getPendingCreationKey ();
141
- if (linkedCreationsByKey .containsKey (pendingCreationKey )) {
142
- final Object emptyCollection = constructorArgs .get (i );
143
- if (emptyCollection == null || !objectFactory .isCollection (emptyCollection .getClass ())) {
144
- throw new ExecutorException (
145
- "Expected empty collection for '" + resolvedItemType + "', MyBatis internal error!" );
146
- }
147
- } else {
148
- final Object linkedCollection = constructorArgs .get (i );
149
- if (!linkedCollectionsByKey .containsKey (pendingCreationKey )) {
150
- throw new ExecutorException (
151
- "Expected linked collection for key '" + pendingCreationKey + "', not found! MyBatis internal error!" );
152
- }
153
-
154
- // comparing memory locations here (we rely on that fact)
155
- if (linkedCollection != linkedCollectionsByKey .get (pendingCreationKey )) {
156
- throw new ExecutorException ("Expected linked collection in creation to be the same as arg for resultMap '"
157
- + pendingCreationKey + "', not equal! MyBatis internal error!" );
158
- }
159
- }
160
- }
161
- }
162
-
163
- private static <T > Constructor <T > resolveConstructor (Class <T > type , List <Class <?>> constructorArgTypes ) {
164
- try {
165
- if (constructorArgTypes == null ) {
166
- return type .getDeclaredConstructor ();
167
- }
168
-
169
- return type .getDeclaredConstructor (constructorArgTypes .toArray (new Class [0 ]));
170
- } catch (Exception e ) {
171
- String argTypes = Optional .ofNullable (constructorArgTypes ).orElseGet (Collections ::emptyList ).stream ()
172
- .map (Class ::getSimpleName ).collect (Collectors .joining ("," ));
173
- throw new ReflectionException (
174
- "Error resolving constructor for " + type + " with invalid types (" + argTypes + ") . Cause: " + e , e );
175
- }
176
- }
177
-
178
- private static Class <?> checkResolvedItemType (PendingCreationMetaInfo creationMetaInfo , Type genericParameterTypes ) {
179
- final ParameterizedType genericParameterType = (ParameterizedType ) genericParameterTypes ;
180
- final Class <?> expectedType = (Class <?>) genericParameterType .getActualTypeArguments ()[0 ];
181
- final Class <?> resolvedItemType = creationMetaInfo .getArgumentType ();
182
-
183
- if (!expectedType .isAssignableFrom (resolvedItemType )) {
184
- throw new ReflectionException (
185
- "Expected type '" + resolvedItemType + "', while the actual type of the collection was '" + expectedType
186
- + "', ensure your resultMap matches the type of the collection you are trying to inject" );
187
- }
188
-
189
- return resolvedItemType ;
190
- }
191
-
192
104
@ Override
193
105
public String toString () {
194
106
return "PendingConstructorCreation(" + this .hashCode () + "){" + "resultType=" + resultType + '}' ;
@@ -199,16 +111,10 @@ public String toString() {
199
111
*
200
112
* @param objectFactory
201
113
* the object factory
202
- * @param verifyCreate
203
- * should we verify this object can be created, should only be needed once
204
114
*
205
115
* @return the new immutable result
206
116
*/
207
- Object create (ObjectFactory objectFactory , boolean verifyCreate ) {
208
- if (verifyCreate ) {
209
- verifyCanCreate (objectFactory );
210
- }
211
-
117
+ Object create (ObjectFactory objectFactory ) {
212
118
final List <Object > newArguments = new ArrayList <>(constructorArgs .size ());
213
119
for (int i = 0 ; i < constructorArgs .size (); i ++) {
214
120
final PendingCreationMetaInfo creationMetaInfo = linkedCollectionMetaInfo .get (i );
@@ -228,7 +134,7 @@ Object create(ObjectFactory objectFactory, boolean verifyCreate) {
228
134
final List <PendingConstructorCreation > linkedCreations = linkedCreationsByKey .get (pendingCreationKey );
229
135
230
136
for (PendingConstructorCreation linkedCreation : linkedCreations ) {
231
- emptyCollection .add (linkedCreation .create (objectFactory , verifyCreate ));
137
+ emptyCollection .add (linkedCreation .create (objectFactory ));
232
138
}
233
139
234
140
newArguments .add (emptyCollection );
0 commit comments