Skip to content

Commit 6b12edc

Browse files
committed
feat: Centralise constructor resolver
1 parent b5a8c62 commit 6b12edc

File tree

12 files changed

+808
-175
lines changed

12 files changed

+808
-175
lines changed

src/main/java/org/apache/ibatis/builder/MapperBuilderAssistant.java

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
*/
1616
package org.apache.ibatis.builder;
1717

18-
import java.lang.reflect.Constructor;
1918
import java.sql.ResultSet;
2019
import java.util.ArrayList;
21-
import java.util.Arrays;
2220
import java.util.Collections;
2321
import java.util.HashMap;
2422
import java.util.HashSet;
@@ -27,7 +25,6 @@
2725
import java.util.Properties;
2826
import java.util.Set;
2927
import java.util.StringTokenizer;
30-
import java.util.stream.Collectors;
3128

3229
import org.apache.ibatis.cache.Cache;
3330
import org.apache.ibatis.cache.decorators.LruCache;
@@ -469,75 +466,4 @@ private Class<?> resolveParameterJavaType(Class<?> resultType, String property,
469466
}
470467
return javaType;
471468
}
472-
473-
/**
474-
* Attempts to assign a {@code javaType} to result mappings when it has been omitted, this is done based on matching
475-
* constructors of the specified {@code resultType}
476-
*
477-
* @param resultType
478-
* the result type of the object to be built
479-
* @param resultMappings
480-
* the current mappings
481-
*
482-
* @return null if there are no missing javaType mappings, or if no suitable mapping could be determined
483-
*
484-
* @see <a href="https://github.com/mybatis/mybatis-3/issues/2618">#2618</a>
485-
*/
486-
public List<ResultMapping> autoTypeResultMappingsForUnknownJavaTypes(Class<?> resultType,
487-
List<ResultMapping> resultMappings) {
488-
final List<Class<?>> typesToMatch = resultMappings.stream().map(ResultMapping::getJavaType)
489-
.collect(Collectors.toList());
490-
491-
// check if we have any undefined java types present, and try to set them automatically
492-
if (typesToMatch.stream().noneMatch(type -> type == null || Object.class.equals(type))) {
493-
return null;
494-
}
495-
496-
final List<List<Class<?>>> matchingConstructors = Arrays.stream(resultType.getDeclaredConstructors())
497-
.map(Constructor::getParameterTypes).filter(parameters -> parameters.length == resultMappings.size())
498-
.map(Arrays::asList).collect(Collectors.toList());
499-
500-
List<Class<?>> matchingTypes = null;
501-
for (List<Class<?>> actualTypes : matchingConstructors) {
502-
boolean matchesType = true;
503-
for (int j = 0; j < typesToMatch.size(); j++) {
504-
final Class<?> type = typesToMatch.get(j);
505-
506-
// pre-filled a type, check if it matches the constructor
507-
if (type != null && !Object.class.equals(type) && !type.equals(actualTypes.get(j))) {
508-
matchesType = false;
509-
break;
510-
}
511-
}
512-
513-
if (!matchesType) {
514-
continue;
515-
}
516-
517-
if (matchingTypes != null) {
518-
// multiple matches found, abort as we cannot reliably guess the correct one.
519-
matchingTypes = null;
520-
break;
521-
}
522-
523-
matchingTypes = actualTypes;
524-
}
525-
526-
if (matchingTypes == null) {
527-
return null;
528-
}
529-
530-
final List<ResultMapping> adjustedAutoTypeResultMappings = new ArrayList<>();
531-
for (int i = 0; i < resultMappings.size(); i++) {
532-
ResultMapping otherMapping = resultMappings.get(i);
533-
Class<?> identifiedMatchingJavaType = matchingTypes.get(i);
534-
535-
// given that we selected a new java type, overwrite the currently
536-
// selected type handler so it can get retrieved again from the registry
537-
adjustedAutoTypeResultMappings
538-
.add(new ResultMapping.Builder(otherMapping).javaType(identifiedMatchingJavaType).typeHandler(null).build());
539-
}
540-
541-
return adjustedAutoTypeResultMappings;
542-
}
543469
}

0 commit comments

Comments
 (0)