View Javadoc
1   /*
2    *    Copyright 2009-2024 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.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   * Represents an object that is still to be created once all nested results with collection values have been gathered
32   *
33   * @author Willie Scholtz
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      // since all our keys are based on result map id, we know we will never go over args size
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        // this will allow us to verify the types of the collection before creating the final object
69        linkedCollectionMetaInfo.put(index, new PendingCreationMetaInfo(resultMap.getType(), k));
70  
71        // will be checked before we finally create the object) as we cannot reliably do that here
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      // not necessary to add null results to the collection
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    * Recursively creates the final result of this creation.
107    *
108    * @param objectFactory
109    *          the object factory
110    *
111    * @return the new immutable result
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         // we are not aware of this argument wrt pending creations
121         newArguments.add(existingArg);
122         continue;
123       }
124 
125       // time to finally build this collection
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       // handle the base collection (it was built inline already)
141       newArguments.add(existingArg);
142     }
143 
144     return objectFactory.create(resultType, constructorArgTypes, newArguments);
145   }
146 }