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

mybatis / mybatis-3 / 3060

13 Dec 2025 08:30AM UTC coverage: 87.389%. Remained the same
3060

Pull #3551

github

web-flow
Merge c9c637dc2 into 58e2d5e9b
Pull Request #3551: fix: Reflector should reflect record

3844 of 4659 branches covered (82.51%)

9 of 10 new or added lines in 1 file covered. (90.0%)

6 existing lines in 1 file now uncovered.

9951 of 11387 relevant lines covered (87.39%)

0.87 hits per line

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

87.0
/src/main/java/org/apache/ibatis/builder/ParameterMappingTokenHandler.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.builder;
17

18
import java.lang.reflect.Type;
19
import java.sql.ResultSet;
20
import java.util.List;
21
import java.util.Map;
22
import java.util.Map.Entry;
23

24
import org.apache.ibatis.binding.MapperMethod.ParamMap;
25
import org.apache.ibatis.mapping.ParameterMapping;
26
import org.apache.ibatis.mapping.ParameterMode;
27
import org.apache.ibatis.parsing.TokenHandler;
28
import org.apache.ibatis.reflection.MetaClass;
29
import org.apache.ibatis.reflection.MetaObject;
30
import org.apache.ibatis.reflection.ParamNameResolver;
31
import org.apache.ibatis.reflection.property.PropertyTokenizer;
32
import org.apache.ibatis.session.Configuration;
33
import org.apache.ibatis.type.JdbcType;
34
import org.apache.ibatis.type.TypeHandler;
35

36
public class ParameterMappingTokenHandler extends BaseBuilder implements TokenHandler {
37

38
  private static final String PARAMETER_PROPERTIES = "javaType,jdbcType,mode,numericScale,resultMap,typeHandler,jdbcTypeName";
39
  private final List<ParameterMapping> parameterMappings;
40
  private final Class<?> parameterType;
41
  private final MetaObject metaParameters;
42
  private final Object parameterObject;
43
  private final boolean paramExists;
44
  private final ParamNameResolver paramNameResolver;
45

46
  private Type genericType = null;
1✔
47
  private TypeHandler<?> typeHandler = null;
1✔
48

49
  public ParameterMappingTokenHandler(List<ParameterMapping> parameterMappings, Configuration configuration,
50
      Object parameterObject, Class<?> parameterType, Map<String, Object> additionalParameters,
51
      ParamNameResolver paramNameResolver, boolean paramExists) {
52
    super(configuration);
1✔
53
    this.parameterType = parameterObject == null ? (parameterType == null ? Object.class : parameterType)
1✔
54
        : parameterObject.getClass();
1✔
55
    this.metaParameters = configuration.newMetaObject(additionalParameters);
1✔
56
    this.parameterObject = parameterObject;
1✔
57
    this.paramExists = paramExists;
1✔
58
    this.parameterMappings = parameterMappings;
1✔
59
    this.paramNameResolver = paramNameResolver;
1✔
60
  }
1✔
61

62
  public ParameterMappingTokenHandler(List<ParameterMapping> parameterMappings, Configuration configuration,
63
      Class<?> parameterType, Map<String, Object> additionalParameters, ParamNameResolver paramNameResolver) {
64
    super(configuration);
1✔
65
    this.parameterType = parameterType;
1✔
66
    this.metaParameters = configuration.newMetaObject(additionalParameters);
1✔
67
    this.parameterObject = null;
1✔
68
    this.paramExists = false;
1✔
69
    this.parameterMappings = parameterMappings;
1✔
70
    this.paramNameResolver = paramNameResolver;
1✔
71
  }
1✔
72

73
  public List<ParameterMapping> getParameterMappings() {
74
    return parameterMappings;
1✔
75
  }
76

77
  @Override
78
  public String handleToken(String content) {
79
    genericType = null;
1✔
80
    typeHandler = null;
1✔
81
    parameterMappings.add(buildParameterMapping(content));
1✔
82
    return "?";
1✔
83
  }
84

85
  private ParameterMapping buildParameterMapping(String content) {
86
    Map<String, String> propertiesMap = parseParameterMapping(content);
1✔
87

88
    final String property = propertiesMap.remove("property");
1✔
89
    final JdbcType jdbcType = resolveJdbcType(propertiesMap.remove("jdbcType"));
1✔
90
    final String typeHandlerAlias = propertiesMap.remove("typeHandler");
1✔
91

92
    ParameterMapping.Builder builder = new ParameterMapping.Builder(configuration, property, (Class<?>) null);
1✔
93
    PropertyTokenizer propertyTokenizer = new PropertyTokenizer(property);
1✔
94
    builder.jdbcType(jdbcType);
1✔
95
    final Class<?> javaType = figureOutJavaType(propertiesMap, property, propertyTokenizer, jdbcType);
1✔
96
    builder.javaType(javaType);
1✔
97
    if (genericType == null) {
1✔
98
      genericType = javaType;
1✔
99
    }
100
    if (typeHandler == null || typeHandlerAlias != null) {
1!
101
      typeHandler = resolveTypeHandler(genericType, jdbcType, typeHandlerAlias);
1✔
102
    }
103
    builder.typeHandler(typeHandler);
1✔
104

105
    ParameterMode mode = null;
1✔
106
    for (Map.Entry<String, String> entry : propertiesMap.entrySet()) {
1✔
107
      String name = entry.getKey();
1✔
108
      String value = entry.getValue();
1✔
109
      if ("mode".equals(name)) {
1!
110
        mode = resolveParameterMode(value);
1✔
111
        builder.mode(mode);
1✔
112
      } else if ("numericScale".equals(name)) {
×
113
        builder.numericScale(Integer.valueOf(value));
×
114
      } else if ("resultMap".equals(name)) {
×
115
        builder.resultMapId(value);
×
116
      } else if ("jdbcTypeName".equals(name)) {
×
117
        builder.jdbcTypeName(value);
×
UNCOV
118
      } else if ("expression".equals(name)) {
×
119
        throw new BuilderException("Expression based parameters are not supported yet");
×
120
      } else {
UNCOV
121
        throw new BuilderException("An invalid property '" + name + "' was found in mapping #{" + content
×
122
            + "}.  Valid properties are " + PARAMETER_PROPERTIES);
123
      }
124
    }
1✔
125
    if (!ParameterMode.OUT.equals(mode) && paramExists) {
1✔
126
      if (metaParameters.hasGetter(propertyTokenizer.getName())) {
1✔
127
        builder.value(metaParameters.getValue(property));
1✔
128
      } else if (parameterObject == null) {
1!
UNCOV
129
        builder.value(null);
×
130
      } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
1✔
131
        builder.value(parameterObject);
1✔
132
      } else {
133
        MetaObject metaObject = configuration.newMetaObject(parameterObject);
1✔
134
        builder.value(metaObject.getValue(property));
1✔
135
      }
136
    }
137
    return builder.build();
1✔
138
  }
139

140
  private Class<?> figureOutJavaType(Map<String, String> propertiesMap, String property,
141
      PropertyTokenizer propertyTokenizer, JdbcType jdbcType) {
142
    Class<?> javaType = resolveClass(propertiesMap.remove("javaType"));
1✔
143
    if (javaType != null) {
1✔
144
      return javaType;
1✔
145
    }
146
    if (metaParameters.hasGetter(propertyTokenizer.getName())) { // issue #448 get type from additional params
1✔
147
      return metaParameters.getGetterType(property);
1✔
148
    }
149
    typeHandler = resolveTypeHandler(parameterType, jdbcType, (Class<? extends TypeHandler<?>>) null);
1✔
150
    if (typeHandler != null) {
1✔
151
      return parameterType;
1✔
152
    }
153
    if (JdbcType.CURSOR.equals(jdbcType)) {
1!
UNCOV
154
      return ResultSet.class;
×
155
    }
156
    if (paramNameResolver != null && ParamMap.class.equals(parameterType)) {
1✔
157
      Type actualParamType = paramNameResolver.getType(property);
1✔
158
      if (actualParamType instanceof Type) {
1✔
159
        MetaClass metaClass = MetaClass.forClass(actualParamType, configuration.getReflectorFactory());
1✔
160
        String multiParamsPropertyName;
161
        if (propertyTokenizer.hasNext()) {
1✔
162
          multiParamsPropertyName = propertyTokenizer.getChildren();
1✔
163
          if (metaClass.hasGetter(multiParamsPropertyName)) {
1✔
164
            Entry<Type, Class<?>> getterType = metaClass.getGenericGetterType(multiParamsPropertyName);
1✔
165
            genericType = getterType.getKey();
1✔
166
            return getterType.getValue();
1✔
167
          }
168
        } else {
169
          genericType = actualParamType;
1✔
170
        }
171
      }
172
      return Object.class;
1✔
173
    }
174
    if (Map.class.isAssignableFrom(parameterType)) {
1✔
175
      return Object.class;
1✔
176
    }
177
    MetaClass metaClass = MetaClass.forClass(parameterType, configuration.getReflectorFactory());
1✔
178
    if (metaClass.hasGetter(property)) {
1✔
179
      Entry<Type, Class<?>> getterType = metaClass.getGenericGetterType(property);
1✔
180
      genericType = getterType.getKey();
1✔
181
      return getterType.getValue();
1✔
182
    }
183
    return Object.class;
1✔
184
  }
185

186
  private Map<String, String> parseParameterMapping(String content) {
187
    try {
188
      return new ParameterExpression(content);
1✔
UNCOV
189
    } catch (BuilderException ex) {
×
UNCOV
190
      throw ex;
×
191
    } catch (Exception ex) {
1✔
192
      throw new BuilderException("Parsing error was found in mapping #{" + content
1✔
193
          + "}.  Check syntax #{property|(expression), var1=value1, var2=value2, ...} ", ex);
194
    }
195
  }
196
}
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