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

mybatis / ibatis-2 / 730

28 Dec 2025 10:16PM UTC coverage: 65.615% (+0.5%) from 65.146%
730

push

github

web-flow
Update README.md

1602 of 2802 branches covered (57.17%)

5053 of 7701 relevant lines covered (65.61%)

0.66 hits per line

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

98.13
/src/main/java/com/ibatis/sqlmap/engine/type/TypeHandlerFactory.java
1
/*
1✔
2
 * Copyright 2004-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 com.ibatis.sqlmap.engine.type;
17

18
import com.ibatis.sqlmap.client.SqlMapException;
19

20
import java.math.BigDecimal;
21
import java.util.ArrayList;
22
import java.util.Collection;
23
import java.util.Date;
24
import java.util.HashMap;
25
import java.util.Iterator;
26
import java.util.List;
27
import java.util.Map;
28

29
/**
30
 * Not much of a suprise, this is a factory class for TypeHandler objects.
31
 */
32
public class TypeHandlerFactory {
33

34
  /** The type handler map. */
35
  private final Map typeHandlerMap = new HashMap<>();
1✔
36

37
  /** The unknown type handler. */
38
  private final TypeHandler unknownTypeHandler = new UnknownTypeHandler(this);
1✔
39

40
  /** The type aliases. */
41
  private final Map<String, String> typeAliases = new HashMap<>();
1✔
42

43
  /** The Constant reversePrimitiveMap. */
44
  private static final Map reversePrimitiveMap = new HashMap<>() {
1✔
45
    private static final long serialVersionUID = 1L;
46

47
    {
48
      put(Byte.class, byte.class);
1✔
49
      put(Short.class, short.class);
1✔
50
      put(Integer.class, int.class);
1✔
51
      put(Long.class, long.class);
1✔
52
      put(Float.class, float.class);
1✔
53
      put(Double.class, double.class);
1✔
54
      put(Boolean.class, boolean.class);
1✔
55
    }
56
  };
57

58
  /* Constructor */
59

60
  /**
61
   * Default constructor.
62
   */
63
  public TypeHandlerFactory() {
1✔
64
    TypeHandler handler = new BooleanTypeHandler();
1✔
65

66
    register(Boolean.class, handler);
1✔
67
    register(boolean.class, handler);
1✔
68

69
    handler = new ByteTypeHandler();
1✔
70
    register(Byte.class, handler);
1✔
71
    register(byte.class, handler);
1✔
72

73
    handler = new ShortTypeHandler();
1✔
74
    register(Short.class, handler);
1✔
75
    register(short.class, handler);
1✔
76

77
    handler = new IntegerTypeHandler();
1✔
78
    register(Integer.class, handler);
1✔
79
    register(int.class, handler);
1✔
80

81
    handler = new LongTypeHandler();
1✔
82
    register(Long.class, handler);
1✔
83
    register(long.class, handler);
1✔
84

85
    handler = new FloatTypeHandler();
1✔
86
    register(Float.class, handler);
1✔
87
    register(float.class, handler);
1✔
88

89
    handler = new DoubleTypeHandler();
1✔
90
    register(Double.class, handler);
1✔
91
    register(double.class, handler);
1✔
92

93
    register(String.class, new StringTypeHandler());
1✔
94
    register(String.class, "CLOB", new CustomTypeHandler(new ClobTypeHandlerCallback()));
1✔
95
    register(String.class, "LONGVARCHAR", new CustomTypeHandler(new ClobTypeHandlerCallback()));
1✔
96

97
    register(BigDecimal.class, new BigDecimalTypeHandler());
1✔
98

99
    register(byte[].class, new ByteArrayTypeHandler());
1✔
100
    register(byte[].class, "BLOB", new CustomTypeHandler(new BlobTypeHandlerCallback()));
1✔
101
    register(byte[].class, "LONGVARBINARY", new CustomTypeHandler(new BlobTypeHandlerCallback()));
1✔
102

103
    register(Object.class, new ObjectTypeHandler());
1✔
104
    register(Object.class, "OBJECT", new ObjectTypeHandler());
1✔
105

106
    register(Date.class, new DateTypeHandler());
1✔
107
    register(Date.class, "DATE", new DateOnlyTypeHandler());
1✔
108
    register(Date.class, "TIME", new TimeOnlyTypeHandler());
1✔
109

110
    register(java.sql.Date.class, new SqlDateTypeHandler());
1✔
111
    register(java.sql.Time.class, new SqlTimeTypeHandler());
1✔
112
    register(java.sql.Timestamp.class, new SqlTimestampTypeHandler());
1✔
113

114
    putTypeAlias("string", String.class.getName());
1✔
115
    putTypeAlias("byte", Byte.class.getName());
1✔
116
    putTypeAlias("long", Long.class.getName());
1✔
117
    putTypeAlias("short", Short.class.getName());
1✔
118
    putTypeAlias("int", Integer.class.getName());
1✔
119
    putTypeAlias("integer", Integer.class.getName());
1✔
120
    putTypeAlias("double", Double.class.getName());
1✔
121
    putTypeAlias("float", Float.class.getName());
1✔
122
    putTypeAlias("boolean", Boolean.class.getName());
1✔
123
    putTypeAlias("date", Date.class.getName());
1✔
124
    putTypeAlias("decimal", BigDecimal.class.getName());
1✔
125
    putTypeAlias("object", Object.class.getName());
1✔
126
    putTypeAlias("map", Map.class.getName());
1✔
127
    putTypeAlias("hashmap", HashMap.class.getName());
1✔
128
    putTypeAlias("list", List.class.getName());
1✔
129
    putTypeAlias("arraylist", ArrayList.class.getName());
1✔
130
    putTypeAlias("collection", Collection.class.getName());
1✔
131
    putTypeAlias("iterator", Iterator.class.getName());
1✔
132
    putTypeAlias("cursor", java.sql.ResultSet.class.getName());
1✔
133

134
  }
1✔
135

136
  /* Public Methods */
137

138
  /**
139
   * Get a TypeHandler for a class.
140
   *
141
   * @param type
142
   *          - the class you want a TypeHandler for
143
   *
144
   * @return - the handler
145
   */
146
  public TypeHandler getTypeHandler(Class type) {
147
    return getTypeHandler(type, null);
1✔
148
  }
149

150
  /**
151
   * Get a TypeHandler for a class and a JDBC type.
152
   *
153
   * @param type
154
   *          - the class
155
   * @param jdbcType
156
   *          - the jdbc type
157
   *
158
   * @return - the handler
159
   */
160
  public TypeHandler getTypeHandler(Class type, String jdbcType) {
161
    Map jdbcHandlerMap = (Map) typeHandlerMap.get(type);
1✔
162
    TypeHandler handler = null;
1✔
163
    if (jdbcHandlerMap != null) {
1✔
164
      handler = (TypeHandler) jdbcHandlerMap.get(jdbcType);
1✔
165
      if (handler == null) {
1✔
166
        handler = (TypeHandler) jdbcHandlerMap.get(null);
1✔
167
      }
168
    }
169
    if (handler == null && type != null && Enum.class.isAssignableFrom(type)) {
1!
170
      handler = new EnumTypeHandler(type);
1✔
171
    }
172
    return handler;
1✔
173
  }
174

175
  /**
176
   * When in doubt, get the "unknown" type handler.
177
   *
178
   * @return - if I told you, it would not be unknown, would it?
179
   */
180
  public TypeHandler getUnkownTypeHandler() {
181
    return unknownTypeHandler;
1✔
182
  }
183

184
  /**
185
   * Tells you if a particular class has a TypeHandler.
186
   *
187
   * @param type
188
   *          - the class
189
   *
190
   * @return - true if there is a TypeHandler
191
   */
192
  public boolean hasTypeHandler(Class type) {
193
    return type != null && (getTypeHandler(type) != null || Enum.class.isAssignableFrom(type));
1!
194
  }
195

196
  /**
197
   * Register (add) a type handler for a class.
198
   *
199
   * @param type
200
   *          - the class
201
   * @param handler
202
   *          - the handler instance
203
   */
204
  public void register(Class type, TypeHandler handler) {
205
    register(type, null, handler);
1✔
206
  }
1✔
207

208
  /**
209
   * Register (add) a type handler for a class and JDBC type.
210
   *
211
   * @param type
212
   *          - the class
213
   * @param jdbcType
214
   *          - the JDBC type
215
   * @param handler
216
   *          - the handler instance
217
   */
218
  public void register(Class type, String jdbcType, TypeHandler handler) {
219
    Map map = (Map) typeHandlerMap.get(type);
1✔
220
    if (map == null) {
1✔
221
      map = new HashMap<>();
1✔
222
      typeHandlerMap.put(type, map);
1✔
223
    }
224
    map.put(jdbcType, handler);
1✔
225

226
    if (reversePrimitiveMap.containsKey(type)) {
1✔
227
      register((Class) reversePrimitiveMap.get(type), jdbcType, handler);
1✔
228
    }
229
  }
1✔
230

231
  /**
232
   * Lookup an aliased class and return it's REAL name.
233
   *
234
   * @param string
235
   *          - the alias
236
   *
237
   * @return - the REAL name
238
   */
239
  public String resolveAlias(String string) {
240
    String key = null;
1✔
241
    if (string != null) {
1✔
242
      key = string.toLowerCase();
1✔
243
    }
244
    String value = null;
1✔
245
    if (typeAliases.containsKey(key)) {
1✔
246
      value = typeAliases.get(key);
1✔
247
    } else {
248
      value = string;
1✔
249
    }
250

251
    return value;
1✔
252
  }
253

254
  /**
255
   * Adds a type alias that is case insensitive. All of the following String, string, StRiNg will equate to the same
256
   * alias.
257
   *
258
   * @param alias
259
   *          - the alias
260
   * @param value
261
   *          - the real class name
262
   */
263
  public void putTypeAlias(String alias, String value) {
264
    String key = null;
1✔
265
    if (alias != null) {
1!
266
      key = alias.toLowerCase();
1✔
267
    }
268
    if (typeAliases.containsKey(key) && !typeAliases.get(key).equals(value)) {
1!
269
      throw new SqlMapException("Error in XmlSqlMapClientBuilder.  Alias name conflict occurred.  The alias '" + key
×
270
          + "' is already mapped to the value '" + typeAliases.get(alias) + "'.");
×
271
    }
272
    typeAliases.put(key, value);
1✔
273
  }
1✔
274

275
}
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