• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / mybatis-3 / 2695

12 Feb 2025 06:57PM UTC coverage: 87.212% (-0.005%) from 87.217%
2695

Pull #3379

github

web-flow
Merge 1261a3400 into 12d286ea2
Pull Request #3379: Resolve type handler based on `java.lang.reflect.Type` instead of `Class` and respect runtime JDBC type

3820 of 4639 branches covered (82.35%)

525 of 591 new or added lines in 42 files covered. (88.83%)

13 existing lines in 4 files now uncovered.

9882 of 11331 relevant lines covered (87.21%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.81
/src/main/java/org/apache/ibatis/executor/resultset/ResultSetWrapper.java
1
/*
2
 *    Copyright 2009-2025 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.Type;
19
import java.sql.ResultSet;
20
import java.sql.ResultSetMetaData;
21
import java.sql.SQLException;
22
import java.util.ArrayList;
23
import java.util.Collections;
24
import java.util.HashMap;
25
import java.util.HashSet;
26
import java.util.List;
27
import java.util.Locale;
28
import java.util.Map;
29
import java.util.Set;
30

31
import org.apache.ibatis.io.Resources;
32
import org.apache.ibatis.mapping.ResultMap;
33
import org.apache.ibatis.session.Configuration;
34
import org.apache.ibatis.type.JdbcType;
35
import org.apache.ibatis.type.ObjectTypeHandler;
36
import org.apache.ibatis.type.TypeHandler;
37
import org.apache.ibatis.type.TypeHandlerRegistry;
38

39
/**
40
 * @author Iwao AVE!
41
 */
42
public class ResultSetWrapper {
43

44
  private final ResultSet resultSet;
45
  private final TypeHandlerRegistry typeHandlerRegistry;
46
  private final List<String> columnNames = new ArrayList<>();
1✔
47
  private final List<String> classNames = new ArrayList<>();
1✔
48
  private final List<JdbcType> jdbcTypes = new ArrayList<>();
1✔
49
  private final Map<String, Map<Type, TypeHandler<?>>> typeHandlerMap = new HashMap<>();
1✔
50
  private final Map<String, Set<String>> mappedColumnNamesMap = new HashMap<>();
1✔
51
  private final Map<String, List<String>> unMappedColumnNamesMap = new HashMap<>();
1✔
52

53
  public ResultSetWrapper(ResultSet rs, Configuration configuration) throws SQLException {
1✔
54
    this.typeHandlerRegistry = configuration.getTypeHandlerRegistry();
1✔
55
    this.resultSet = rs;
1✔
56
    final ResultSetMetaData metaData = rs.getMetaData();
1✔
57
    final int columnCount = metaData.getColumnCount();
1✔
58
    for (int i = 1; i <= columnCount; i++) {
1✔
59
      columnNames.add(configuration.isUseColumnLabel() ? metaData.getColumnLabel(i) : metaData.getColumnName(i));
1✔
60
      jdbcTypes.add(JdbcType.forCode(metaData.getColumnType(i)));
1✔
61
      classNames.add(metaData.getColumnClassName(i));
1✔
62
    }
63
  }
1✔
64

65
  public ResultSet getResultSet() {
66
    return resultSet;
1✔
67
  }
68

69
  public List<String> getColumnNames() {
70
    return this.columnNames;
1✔
71
  }
72

73
  public List<String> getClassNames() {
74
    return Collections.unmodifiableList(classNames);
1✔
75
  }
76

77
  public List<JdbcType> getJdbcTypes() {
78
    return jdbcTypes;
1✔
79
  }
80

81
  public JdbcType getJdbcType(String columnName) {
82
    int columnIndex = getColumnIndex(columnName);
1✔
83
    return columnIndex == -1 ? null : jdbcTypes.get(columnIndex);
1✔
84
  }
85

86
  /**
87
   * Gets the type handler to use when reading the result set. Tries to get from the TypeHandlerRegistry by searching
88
   * for the property type. If not found it gets the column JDBC type and tries to get a handler for it.
89
   *
90
   * @param propertyType
91
   *          the property type
92
   * @param columnName
93
   *          the column name
94
   *
95
   * @return the type handler
96
   */
97
  public TypeHandler<?> getTypeHandler(Type propertyType, String columnName) {
98
    return typeHandlerMap.computeIfAbsent(columnName, k -> new HashMap<>()).computeIfAbsent(propertyType, k -> {
1✔
99
      int index = getColumnIndex(columnName);
1✔
100
      if (index == -1) {
1!
NEW
101
        return ObjectTypeHandler.INSTANCE;
×
102
      }
103

104
      JdbcType jdbcType = jdbcTypes.get(index);
1✔
105
      TypeHandler<?> handler = typeHandlerRegistry.getTypeHandler(k, jdbcType, null);
1✔
106
      if (handler != null) {
1✔
107
        return handler;
1✔
108
      }
109

110
      Class<?> javaType = resolveClass(classNames.get(index));
1✔
111
      if (!(k instanceof Class && ((Class<?>) k).isAssignableFrom(javaType))) {
1✔
112
        // Clearly incompatible
113
        return null;
1✔
114
      }
115

116
      handler = typeHandlerRegistry.getTypeHandler(javaType, jdbcType, null);
1✔
117
      if (handler == null) {
1✔
118
        handler = typeHandlerRegistry.getTypeHandler(jdbcType);
1✔
119
      }
120
      return handler == null ? ObjectTypeHandler.INSTANCE : handler;
1!
121
    });
122
  }
123

124
  static Class<?> resolveClass(String className) {
125
    try {
126
      // #699 className could be null
127
      if (className != null) {
1!
128
        return Resources.classForName(className);
1✔
129
      }
130
    } catch (ClassNotFoundException e) {
×
131
      // ignore
132
    }
×
133
    return null;
×
134
  }
135

136
  private int getColumnIndex(String columnName) {
137
    for (int i = 0; i < columnNames.size(); i++) {
1✔
138
      if (columnNames.get(i).equalsIgnoreCase(columnName)) {
1✔
139
        return i;
1✔
140
      }
141
    }
142
    return -1;
1✔
143
  }
144

145
  private void loadMappedAndUnmappedColumnNames(ResultMap resultMap, String columnPrefix) throws SQLException {
146
    Set<String> mappedColumnNames = new HashSet<>();
1✔
147
    List<String> unmappedColumnNames = new ArrayList<>();
1✔
148
    final String upperColumnPrefix = columnPrefix == null ? null : columnPrefix.toUpperCase(Locale.ENGLISH);
1✔
149
    final Set<String> mappedColumns = prependPrefixes(resultMap.getMappedColumns(), upperColumnPrefix);
1✔
150
    for (String columnName : columnNames) {
1✔
151
      final String upperColumnName = columnName.toUpperCase(Locale.ENGLISH);
1✔
152
      if (mappedColumns.contains(upperColumnName)) {
1✔
153
        mappedColumnNames.add(upperColumnName);
1✔
154
      } else {
155
        unmappedColumnNames.add(columnName);
1✔
156
      }
157
    }
1✔
158
    mappedColumnNamesMap.put(getMapKey(resultMap, columnPrefix), mappedColumnNames);
1✔
159
    unMappedColumnNamesMap.put(getMapKey(resultMap, columnPrefix), unmappedColumnNames);
1✔
160
  }
1✔
161

162
  public Set<String> getMappedColumnNames(ResultMap resultMap, String columnPrefix) throws SQLException {
163
    Set<String> mappedColumnNames = mappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));
1✔
164
    if (mappedColumnNames == null) {
1✔
165
      loadMappedAndUnmappedColumnNames(resultMap, columnPrefix);
1✔
166
      mappedColumnNames = mappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));
1✔
167
    }
168
    return mappedColumnNames;
1✔
169
  }
170

171
  public List<String> getUnmappedColumnNames(ResultMap resultMap, String columnPrefix) throws SQLException {
172
    List<String> unMappedColumnNames = unMappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));
1✔
173
    if (unMappedColumnNames == null) {
1✔
174
      loadMappedAndUnmappedColumnNames(resultMap, columnPrefix);
1✔
175
      unMappedColumnNames = unMappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));
1✔
176
    }
177
    return unMappedColumnNames;
1✔
178
  }
179

180
  private String getMapKey(ResultMap resultMap, String columnPrefix) {
181
    return resultMap.getId() + ":" + columnPrefix;
1✔
182
  }
183

184
  private Set<String> prependPrefixes(Set<String> columnNames, String prefix) {
185
    if (columnNames == null || columnNames.isEmpty() || prefix == null || prefix.length() == 0) {
1!
186
      return columnNames;
1✔
187
    }
188
    final Set<String> prefixed = new HashSet<>();
1✔
189
    for (String columnName : columnNames) {
1✔
190
      prefixed.add(prefix + columnName);
1✔
191
    }
1✔
192
    return prefixed;
1✔
193
  }
194

195
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc