|
| 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.lang.reflect.Constructor; |
| 19 | +import java.lang.reflect.ParameterizedType; |
| 20 | +import java.lang.reflect.Type; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Collection; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +import org.apache.ibatis.executor.ExecutorException; |
| 28 | +import org.apache.ibatis.mapping.ResultMap; |
| 29 | +import org.apache.ibatis.mapping.ResultMapping; |
| 30 | +import org.apache.ibatis.reflection.ReflectionException; |
| 31 | +import org.apache.ibatis.reflection.factory.ObjectFactory; |
| 32 | + |
| 33 | +/** |
| 34 | + * Represents an object that is still to be created once all nested results with collection values have been gathered |
| 35 | + * |
| 36 | + * @author Willie Scholtz |
| 37 | + */ |
| 38 | +final class PendingConstructorCreation { |
| 39 | + private final Class<?> resultType; |
| 40 | + private final List<Class<?>> constructorArgTypes; |
| 41 | + private final List<Object> constructorArgs; |
| 42 | + private final Map<Integer, PendingCreationMetaInfo> linkedCollectionMetaInfo; |
| 43 | + private final Map<String, Collection<Object>> linkedCollectionsByResultMapId; |
| 44 | + private final Map<String, List<PendingConstructorCreation>> linkedCreationsByResultMapId; |
| 45 | + |
| 46 | + PendingConstructorCreation(Class<?> resultType, List<Class<?>> types, List<Object> args) { |
| 47 | + this.linkedCollectionMetaInfo = new HashMap<>(); |
| 48 | + this.linkedCollectionsByResultMapId = new HashMap<>(); |
| 49 | + this.linkedCreationsByResultMapId = new HashMap<>(); |
| 50 | + this.resultType = resultType; |
| 51 | + this.constructorArgTypes = types; |
| 52 | + this.constructorArgs = args; |
| 53 | + } |
| 54 | + |
| 55 | + @SuppressWarnings("unchecked") |
| 56 | + Collection<Object> initializeCollectionForResultMapping(ObjectFactory objectFactory, ResultMap resultMap, |
| 57 | + ResultMapping constructorMapping, Integer index) { |
| 58 | + final Class<?> parameterType = constructorMapping.getJavaType(); |
| 59 | + if (!objectFactory.isCollection(parameterType)) { |
| 60 | + throw new ExecutorException( |
| 61 | + "Cannot add a collection result to non-collection based resultMapping: " + constructorMapping); |
| 62 | + } |
| 63 | + |
| 64 | + final String resultMapId = constructorMapping.getNestedResultMapId(); |
| 65 | + return linkedCollectionsByResultMapId.computeIfAbsent(resultMapId, (k) -> { |
| 66 | + // this will allow us to verify the types of the collection before creating the final object |
| 67 | + linkedCollectionMetaInfo.put(index, new PendingCreationMetaInfo(resultMap.getType(), resultMapId)); |
| 68 | + |
| 69 | + // will be checked before we finally create the object) as we cannot reliably do that here |
| 70 | + return (Collection<Object>) objectFactory.create(constructorMapping.getJavaType()); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + void linkCreation(ResultMap nestedResultMap, PendingConstructorCreation pcc) { |
| 75 | + final String resultMapId = nestedResultMap.getId(); |
| 76 | + final List<PendingConstructorCreation> pendingConstructorCreations = linkedCreationsByResultMapId |
| 77 | + .computeIfAbsent(resultMapId, (k) -> new ArrayList<>()); |
| 78 | + |
| 79 | + if (pendingConstructorCreations.contains(pcc)) { |
| 80 | + throw new ExecutorException("Cannot link inner pcc with same value, MyBatis programming error!"); |
| 81 | + } |
| 82 | + |
| 83 | + pendingConstructorCreations.add(pcc); |
| 84 | + } |
| 85 | + |
| 86 | + void linkCollectionValue(ResultMapping constructorMapping, Object value) { |
| 87 | + // not necessary to add null results to the collection (is this a config flag?) |
| 88 | + if (value == null) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + final String resultMapId = constructorMapping.getNestedResultMapId(); |
| 93 | + if (!linkedCollectionsByResultMapId.containsKey(resultMapId)) { |
| 94 | + throw new ExecutorException("Cannot link collection value for resultMapping: " + constructorMapping |
| 95 | + + ", resultMap has not been seen/initialized yet! Internal error"); |
| 96 | + } |
| 97 | + |
| 98 | + linkedCollectionsByResultMapId.get(resultMapId).add(value); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Verifies preconditions before we can actually create the result object, this is more of a sanity check to ensure |
| 103 | + * all the mappings are as we expect them to be. |
| 104 | + * |
| 105 | + * @param objectFactory |
| 106 | + * the object factory |
| 107 | + */ |
| 108 | + void verifyCanCreate(ObjectFactory objectFactory) { |
| 109 | + // before we create, we need to get the constructor to be used and verify our types match |
| 110 | + // since we added to the collection completely unchecked |
| 111 | + final Constructor<?> resolvedConstructor = objectFactory.resolveConstructor(resultType, constructorArgTypes); |
| 112 | + final Type[] genericParameterTypes = resolvedConstructor.getGenericParameterTypes(); |
| 113 | + for (int i = 0; i < genericParameterTypes.length; i++) { |
| 114 | + if (!linkedCollectionMetaInfo.containsKey(i)) { |
| 115 | + continue; |
| 116 | + } |
| 117 | + |
| 118 | + final PendingCreationMetaInfo creationMetaInfo = linkedCollectionMetaInfo.get(i); |
| 119 | + final Class<?> resolvedItemType = checkResolvedItemType(creationMetaInfo, genericParameterTypes[i]); |
| 120 | + |
| 121 | + // ensure we have an empty collection if there are linked creations for this arg |
| 122 | + final String resultMapId = creationMetaInfo.getResultMapId(); |
| 123 | + if (linkedCreationsByResultMapId.containsKey(resultMapId)) { |
| 124 | + final Object emptyCollection = constructorArgs.get(i); |
| 125 | + if (emptyCollection == null || !objectFactory.isCollection(emptyCollection.getClass())) { |
| 126 | + throw new ExecutorException( |
| 127 | + "Expected empty collection for '" + resolvedItemType + "', this is a MyBatis internal error!"); |
| 128 | + } |
| 129 | + } else { |
| 130 | + final Object linkedCollection = constructorArgs.get(i); |
| 131 | + if (!linkedCollectionsByResultMapId.containsKey(resultMapId)) { |
| 132 | + throw new ExecutorException("Expected linked collection for resultMap '" + resultMapId |
| 133 | + + "', not found! this is a MyBatis internal error!"); |
| 134 | + } |
| 135 | + |
| 136 | + // comparing memory locations here (we rely on that fact) |
| 137 | + if (linkedCollection != linkedCollectionsByResultMapId.get(resultMapId)) { |
| 138 | + throw new ExecutorException("Expected linked collection in creation to be the same as arg for resultMap '" |
| 139 | + + resultMapId + "', not equal! this is a MyBatis internal error!"); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private static Class<?> checkResolvedItemType(PendingCreationMetaInfo creationMetaInfo, Type genericParameterTypes) { |
| 146 | + final ParameterizedType genericParameterType = (ParameterizedType) genericParameterTypes; |
| 147 | + final Class<?> expectedType = (Class<?>) genericParameterType.getActualTypeArguments()[0]; |
| 148 | + final Class<?> resolvedItemType = creationMetaInfo.getArgumentType(); |
| 149 | + |
| 150 | + if (!expectedType.isAssignableFrom(resolvedItemType)) { |
| 151 | + throw new ReflectionException( |
| 152 | + "Expected type '" + resolvedItemType + "', while the actual type of the collection was '" + expectedType |
| 153 | + + "', ensure your resultMap matches the type of the collection you are trying to inject"); |
| 154 | + } |
| 155 | + |
| 156 | + return resolvedItemType; |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public String toString() { |
| 161 | + return "PendingConstructorCreation(" + this.hashCode() + "){" + "resultType=" + resultType + '}'; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Recursively creates the final result of this creation. |
| 166 | + * |
| 167 | + * @param objectFactory |
| 168 | + * the object factory |
| 169 | + * |
| 170 | + * @return the new immutable result |
| 171 | + */ |
| 172 | + Object create(ObjectFactory objectFactory) { |
| 173 | + final List<Object> newArguments = new ArrayList<>(constructorArgs.size()); |
| 174 | + for (int i = 0; i < constructorArgs.size(); i++) { |
| 175 | + final PendingCreationMetaInfo creationMetaInfo = linkedCollectionMetaInfo.get(i); |
| 176 | + final Object existingArg = constructorArgs.get(i); |
| 177 | + |
| 178 | + if (creationMetaInfo == null) { |
| 179 | + // we are not aware of this argument wrt pending creations |
| 180 | + newArguments.add(existingArg); |
| 181 | + continue; |
| 182 | + } |
| 183 | + |
| 184 | + // time to finally build this collection |
| 185 | + final String resultMapId = creationMetaInfo.getResultMapId(); |
| 186 | + if (linkedCreationsByResultMapId.containsKey(resultMapId)) { |
| 187 | + @SuppressWarnings("unchecked") |
| 188 | + final Collection<Object> emptyCollection = (Collection<Object>) existingArg; |
| 189 | + final List<PendingConstructorCreation> linkedCreations = linkedCreationsByResultMapId.get(resultMapId); |
| 190 | + |
| 191 | + for (PendingConstructorCreation linkedCreation : linkedCreations) { |
| 192 | + linkedCreation.verifyCanCreate(objectFactory); |
| 193 | + emptyCollection.add(linkedCreation.create(objectFactory)); |
| 194 | + } |
| 195 | + |
| 196 | + newArguments.add(emptyCollection); |
| 197 | + continue; |
| 198 | + } |
| 199 | + |
| 200 | + // handle the base collection (it was built inline already) |
| 201 | + newArguments.add(existingArg); |
| 202 | + } |
| 203 | + |
| 204 | + return objectFactory.create(resultType, constructorArgTypes, newArguments); |
| 205 | + } |
| 206 | +} |
0 commit comments