-
Notifications
You must be signed in to change notification settings - Fork 12.9k
101: Add support for immutable collection constructor creation #3108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
epochcoder
merged 21 commits into
mybatis:master
from
epochcoder:feature/101-support-constructor-collection-injection
Jan 2, 2025
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
5c23022
101: Add support for immutable collection constructor creation
epochcoder d79f125
Merge branch 'master' into pr/3108
harawata 0497117
Added tests covering basics with simpler objects
harawata 7fdfb4d
Merge branch 'master' into feature/101-support-constructor-collection…
epochcoder 558c8a1
#101: Pass CollectionInConstructorTest
epochcoder a01b27e
#101: Remove experimental flag
epochcoder e163a91
#101: Keep track of pending creations that were set as property mappings
epochcoder bc53e84
#101: Add test which violates equals contract
epochcoder 685bafb
isle -> aisle 😑
harawata ccc2071
Cleanup by maven plugins
harawata 9ade243
Remove constructor verification process
harawata 462d890
Merge branch 'master' into pr/3108
harawata be53709
Merge branch 'master' into pr/3108
harawata d1dca2a
Merge branch 'master' into feature/101-support-constructor-collection…
epochcoder f3a2432
Remove redundant containsKey invocations, etc.
harawata 9886f1d
Merge branch 'master' into pr/3108
harawata 212cbad
Revert the unnecessary change (for diff)
harawata 1562473
Edited en doc a little bit and copied the doc changes to the other lo…
harawata 9a92894
License year
harawata 5916710
Remove unused variable
epochcoder fd69cf0
Merge branch 'master' into feature/101-support-constructor-collection…
epochcoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
336 changes: 315 additions & 21 deletions
336
src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java
Large diffs are not rendered by default.
Oops, something went wrong.
146 changes: 146 additions & 0 deletions
146
src/main/java/org/apache/ibatis/executor/resultset/PendingConstructorCreation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* Copyright 2009-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.ibatis.executor.resultset; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.apache.ibatis.executor.ExecutorException; | ||
import org.apache.ibatis.mapping.ResultMap; | ||
import org.apache.ibatis.mapping.ResultMapping; | ||
import org.apache.ibatis.reflection.ReflectionException; | ||
import org.apache.ibatis.reflection.factory.ObjectFactory; | ||
|
||
/** | ||
* Represents an object that is still to be created once all nested results with collection values have been gathered | ||
* | ||
* @author Willie Scholtz | ||
*/ | ||
final class PendingConstructorCreation { | ||
|
||
private final Class<?> resultType; | ||
private final List<Class<?>> constructorArgTypes; | ||
private final List<Object> constructorArgs; | ||
|
||
private final Map<Integer, PendingCreationMetaInfo> linkedCollectionMetaInfo; | ||
private final Map<PendingCreationKey, Collection<Object>> linkedCollectionsByKey; | ||
private final Map<PendingCreationKey, List<PendingConstructorCreation>> linkedCreationsByKey; | ||
|
||
PendingConstructorCreation(Class<?> resultType, List<Class<?>> types, List<Object> args) { | ||
// since all our keys are based on result map id, we know we will never go over args size | ||
final int maxSize = types.size(); | ||
|
||
this.linkedCollectionMetaInfo = new HashMap<>(maxSize); | ||
this.linkedCollectionsByKey = new HashMap<>(maxSize); | ||
this.linkedCreationsByKey = new HashMap<>(maxSize); | ||
|
||
this.resultType = resultType; | ||
this.constructorArgTypes = types; | ||
this.constructorArgs = args; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
Collection<Object> initializeCollectionForResultMapping(ObjectFactory objectFactory, ResultMap resultMap, | ||
ResultMapping constructorMapping, Integer index) { | ||
final Class<?> parameterType = constructorMapping.getJavaType(); | ||
if (!objectFactory.isCollection(parameterType)) { | ||
throw new ReflectionException( | ||
"Cannot add a collection result to non-collection based resultMapping: " + constructorMapping); | ||
} | ||
|
||
return linkedCollectionsByKey.computeIfAbsent(new PendingCreationKey(constructorMapping), k -> { | ||
// this will allow us to verify the types of the collection before creating the final object | ||
linkedCollectionMetaInfo.put(index, new PendingCreationMetaInfo(resultMap.getType(), k)); | ||
|
||
// will be checked before we finally create the object) as we cannot reliably do that here | ||
return (Collection<Object>) objectFactory.create(parameterType); | ||
}); | ||
} | ||
|
||
void linkCreation(ResultMapping constructorMapping, PendingConstructorCreation pcc) { | ||
final PendingCreationKey creationKey = new PendingCreationKey(constructorMapping); | ||
final List<PendingConstructorCreation> pendingConstructorCreations = linkedCreationsByKey | ||
.computeIfAbsent(creationKey, k -> new ArrayList<>()); | ||
|
||
if (pendingConstructorCreations.contains(pcc)) { | ||
throw new ExecutorException("Cannot link inner constructor creation with same value, MyBatis internal error!"); | ||
} | ||
|
||
pendingConstructorCreations.add(pcc); | ||
} | ||
|
||
void linkCollectionValue(ResultMapping constructorMapping, Object value) { | ||
// not necessary to add null results to the collection | ||
if (value == null) { | ||
return; | ||
} | ||
|
||
linkedCollectionsByKey.computeIfAbsent(new PendingCreationKey(constructorMapping), k -> { | ||
throw new ExecutorException("Cannot link collection value for key: " + constructorMapping | ||
+ ", resultMap has not been seen/initialized yet! Mybatis internal error!"); | ||
}).add(value); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "PendingConstructorCreation(" + this.hashCode() + "){" + "resultType=" + resultType + '}'; | ||
} | ||
|
||
/** | ||
* Recursively creates the final result of this creation. | ||
* | ||
* @param objectFactory | ||
* the object factory | ||
* | ||
* @return the new immutable result | ||
*/ | ||
Object create(ObjectFactory objectFactory) { | ||
final List<Object> newArguments = new ArrayList<>(constructorArgs.size()); | ||
for (int i = 0; i < constructorArgs.size(); i++) { | ||
final PendingCreationMetaInfo creationMetaInfo = linkedCollectionMetaInfo.get(i); | ||
final Object existingArg = constructorArgs.get(i); | ||
|
||
if (creationMetaInfo == null) { | ||
// we are not aware of this argument wrt pending creations | ||
newArguments.add(existingArg); | ||
continue; | ||
} | ||
|
||
// time to finally build this collection | ||
final PendingCreationKey pendingCreationKey = creationMetaInfo.getPendingCreationKey(); | ||
final List<PendingConstructorCreation> linkedCreations = linkedCreationsByKey.get(pendingCreationKey); | ||
if (linkedCreations != null) { | ||
@SuppressWarnings("unchecked") | ||
final Collection<Object> emptyCollection = (Collection<Object>) existingArg; | ||
|
||
for (PendingConstructorCreation linkedCreation : linkedCreations) { | ||
emptyCollection.add(linkedCreation.create(objectFactory)); | ||
} | ||
|
||
newArguments.add(emptyCollection); | ||
continue; | ||
} | ||
|
||
// handle the base collection (it was built inline already) | ||
newArguments.add(existingArg); | ||
} | ||
|
||
return objectFactory.create(resultType, constructorArgTypes, newArguments); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/org/apache/ibatis/executor/resultset/PendingCreationKey.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2009-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.ibatis.executor.resultset; | ||
|
||
import java.util.Objects; | ||
|
||
import org.apache.ibatis.mapping.ResultMapping; | ||
|
||
/** | ||
* A unique identifier for a pending constructor creation, prefix is used to distinguish between equal result maps for | ||
* different columns | ||
* | ||
* @author Willie Scholtz | ||
*/ | ||
final class PendingCreationKey { | ||
private final String resultMapId; | ||
private final String constructorColumnPrefix; | ||
|
||
PendingCreationKey(ResultMapping constructorMapping) { | ||
this.resultMapId = constructorMapping.getNestedResultMapId(); | ||
this.constructorColumnPrefix = constructorMapping.getColumnPrefix(); | ||
} | ||
|
||
String getConstructorColumnPrefix() { | ||
return constructorColumnPrefix; | ||
} | ||
|
||
String getResultMapId() { | ||
return resultMapId; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) | ||
return true; | ||
if (o == null || getClass() != o.getClass()) | ||
return false; | ||
|
||
PendingCreationKey that = (PendingCreationKey) o; | ||
return Objects.equals(resultMapId, that.resultMapId) | ||
&& Objects.equals(constructorColumnPrefix, that.constructorColumnPrefix); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(resultMapId, constructorColumnPrefix); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "PendingCreationKey{" + "resultMapId='" + resultMapId + '\'' + ", constructorColumnPrefix='" | ||
+ constructorColumnPrefix + '\'' + '}'; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/org/apache/ibatis/executor/resultset/PendingCreationMetaInfo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2009-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.ibatis.executor.resultset; | ||
|
||
/** | ||
* Used to keep track of specific argument types for pending creations | ||
* | ||
* @author Willie Scholtz | ||
*/ | ||
final class PendingCreationMetaInfo { | ||
|
||
private final Class<?> argumentType; | ||
private final PendingCreationKey pendingCreationKey; | ||
|
||
PendingCreationMetaInfo(Class<?> argumentType, PendingCreationKey pendingCreationKey) { | ||
this.argumentType = argumentType; | ||
this.pendingCreationKey = pendingCreationKey; | ||
} | ||
|
||
Class<?> getArgumentType() { | ||
return argumentType; | ||
} | ||
|
||
PendingCreationKey getPendingCreationKey() { | ||
return pendingCreationKey; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "PendingCreationMetaInfo{" + "argumentType=" + argumentType + ", pendingCreationKey='" + pendingCreationKey | ||
+ '\'' + '}'; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.