1515 */
1616package org .apache .ibatis .executor .resultset ;
1717
18- import java .lang .reflect .Constructor ;
19- import java .lang .reflect .ParameterizedType ;
20- import java .lang .reflect .Type ;
2118import java .util .ArrayList ;
2219import java .util .Collection ;
23- import java .util .Collections ;
2420import java .util .HashMap ;
2521import java .util .List ;
2622import java .util .Map ;
27- import java .util .Optional ;
28- import java .util .stream .Collectors ;
2923
3024import org .apache .ibatis .executor .ExecutorException ;
3125import org .apache .ibatis .mapping .ResultMap ;
3226import org .apache .ibatis .mapping .ResultMapping ;
3327import org .apache .ibatis .reflection .ReflectionException ;
34- import org .apache .ibatis .reflection .factory .DefaultObjectFactory ;
3528import org .apache .ibatis .reflection .factory .ObjectFactory ;
3629
3730/**
@@ -108,87 +101,6 @@ void linkCollectionValue(ResultMapping constructorMapping, Object value) {
108101 linkedCollectionsByKey .get (creationKey ).add (value );
109102 }
110103
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-
192104 @ Override
193105 public String toString () {
194106 return "PendingConstructorCreation(" + this .hashCode () + "){" + "resultType=" + resultType + '}' ;
@@ -199,16 +111,10 @@ public String toString() {
199111 *
200112 * @param objectFactory
201113 * the object factory
202- * @param verifyCreate
203- * should we verify this object can be created, should only be needed once
204114 *
205115 * @return the new immutable result
206116 */
207- Object create (ObjectFactory objectFactory , boolean verifyCreate ) {
208- if (verifyCreate ) {
209- verifyCanCreate (objectFactory );
210- }
211-
117+ Object create (ObjectFactory objectFactory ) {
212118 final List <Object > newArguments = new ArrayList <>(constructorArgs .size ());
213119 for (int i = 0 ; i < constructorArgs .size (); i ++) {
214120 final PendingCreationMetaInfo creationMetaInfo = linkedCollectionMetaInfo .get (i );
@@ -228,7 +134,7 @@ Object create(ObjectFactory objectFactory, boolean verifyCreate) {
228134 final List <PendingConstructorCreation > linkedCreations = linkedCreationsByKey .get (pendingCreationKey );
229135
230136 for (PendingConstructorCreation linkedCreation : linkedCreations ) {
231- emptyCollection .add (linkedCreation .create (objectFactory , verifyCreate ));
137+ emptyCollection .add (linkedCreation .create (objectFactory ));
232138 }
233139
234140 newArguments .add (emptyCollection );
0 commit comments