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

mybatis / mybatis-3 / 2686

01 Feb 2025 09:55PM UTC coverage: 87.093% (-0.1%) from 87.217%
2686

Pull #3379

github

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

3825 of 4663 branches covered (82.03%)

515 of 579 new or added lines in 36 files covered. (88.95%)

28 existing lines in 6 files now uncovered.

9912 of 11381 relevant lines covered (87.09%)

0.87 hits per line

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

94.74
/src/main/java/org/apache/ibatis/builder/BaseBuilder.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.util.Arrays;
20
import java.util.HashSet;
21
import java.util.Set;
22
import java.util.regex.Pattern;
23

24
import org.apache.ibatis.mapping.ParameterMode;
25
import org.apache.ibatis.mapping.ResultSetType;
26
import org.apache.ibatis.session.Configuration;
27
import org.apache.ibatis.type.JdbcType;
28
import org.apache.ibatis.type.TypeAliasRegistry;
29
import org.apache.ibatis.type.TypeHandler;
30
import org.apache.ibatis.type.TypeHandlerRegistry;
31

32
/**
33
 * @author Clinton Begin
34
 */
35
public abstract class BaseBuilder {
36
  protected final Configuration configuration;
37
  protected final TypeAliasRegistry typeAliasRegistry;
38
  protected final TypeHandlerRegistry typeHandlerRegistry;
39

40
  public BaseBuilder(Configuration configuration) {
1✔
41
    this.configuration = configuration;
1✔
42
    this.typeAliasRegistry = this.configuration.getTypeAliasRegistry();
1✔
43
    this.typeHandlerRegistry = this.configuration.getTypeHandlerRegistry();
1✔
44
  }
1✔
45

46
  public Configuration getConfiguration() {
47
    return configuration;
1✔
48
  }
49

50
  protected Pattern parseExpression(String regex, String defaultValue) {
51
    return Pattern.compile(regex == null ? defaultValue : regex);
1✔
52
  }
53

54
  protected Boolean booleanValueOf(String value, Boolean defaultValue) {
55
    return value == null ? defaultValue : Boolean.valueOf(value);
1✔
56
  }
57

58
  protected Integer integerValueOf(String value, Integer defaultValue) {
59
    return value == null ? defaultValue : Integer.valueOf(value);
1✔
60
  }
61

62
  protected Set<String> stringSetValueOf(String value, String defaultValue) {
63
    value = value == null ? defaultValue : value;
1!
64
    return new HashSet<>(Arrays.asList(value.split(",")));
1✔
65
  }
66

67
  protected JdbcType resolveJdbcType(String alias) {
68
    try {
69
      return alias == null ? null : JdbcType.valueOf(alias);
1✔
70
    } catch (IllegalArgumentException e) {
1✔
71
      throw new BuilderException("Error resolving JdbcType. Cause: " + e, e);
1✔
72
    }
73
  }
74

75
  protected ResultSetType resolveResultSetType(String alias) {
76
    try {
77
      return alias == null ? null : ResultSetType.valueOf(alias);
1✔
78
    } catch (IllegalArgumentException e) {
1✔
79
      throw new BuilderException("Error resolving ResultSetType. Cause: " + e, e);
1✔
80
    }
81
  }
82

83
  protected ParameterMode resolveParameterMode(String alias) {
84
    try {
85
      return alias == null ? null : ParameterMode.valueOf(alias);
1✔
86
    } catch (IllegalArgumentException e) {
1✔
87
      throw new BuilderException("Error resolving ParameterMode. Cause: " + e, e);
1✔
88
    }
89
  }
90

91
  protected Object createInstance(String alias) {
92
    Class<?> clazz = resolveClass(alias);
1✔
93
    try {
94
      return clazz == null ? null : clazz.getDeclaredConstructor().newInstance();
1✔
95
    } catch (Exception e) {
1✔
96
      throw new BuilderException("Error creating instance. Cause: " + e, e);
1✔
97
    }
98
  }
99

100
  protected <T> Class<? extends T> resolveClass(String alias) {
101
    try {
102
      return alias == null ? null : resolveAlias(alias);
1✔
103
    } catch (Exception e) {
1✔
104
      throw new BuilderException("Error resolving class. Cause: " + e, e);
1✔
105
    }
106
  }
107

108
  @Deprecated
109
  protected TypeHandler<?> resolveTypeHandler(Class<?> javaType, String typeHandlerAlias) {
NEW
110
    return resolveTypeHandler(null, null, javaType, null, typeHandlerAlias);
×
111
  }
112

113
  @Deprecated
114
  protected TypeHandler<?> resolveTypeHandler(Class<?> javaType, Class<? extends TypeHandler<?>> typeHandlerType) {
NEW
115
    return resolveTypeHandler(null, null, javaType, null, typeHandlerType);
×
116
  }
117

118
  protected TypeHandler<?> resolveTypeHandler(Class<?> parameterType, String propertyName, Type propertyType,
119
      JdbcType jdbcType, String typeHandlerAlias) {
120
    Class<? extends TypeHandler<?>> typeHandlerType = null;
1✔
121
    typeHandlerType = resolveClass(typeHandlerAlias);
1✔
122
    if (typeHandlerType != null && !TypeHandler.class.isAssignableFrom(typeHandlerType)) {
1✔
123
      throw new BuilderException("Type " + typeHandlerType.getName()
1✔
124
          + " is not a valid TypeHandler because it does not implement TypeHandler interface");
125
    }
126
    return resolveTypeHandler(parameterType, propertyName, propertyType, jdbcType, typeHandlerType);
1✔
127
  }
128

129
  protected TypeHandler<?> resolveTypeHandler(Class<?> parameterType, String propertyName, Type propertyType,
130
      JdbcType jdbcType, Class<? extends TypeHandler<?>> typeHandlerType) {
131
    if (typeHandlerType == null && jdbcType == null) {
1✔
132
      return null;
1✔
133
    }
134
    return configuration.getTypeHandlerRegistry().resolve(parameterType, propertyType, propertyName, jdbcType,
1✔
135
        typeHandlerType);
136
  }
137

138
  protected <T> Class<? extends T> resolveAlias(String alias) {
139
    return typeAliasRegistry.resolveAlias(alias);
1✔
140
  }
141
}
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

© 2025 Coveralls, Inc