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

mybatis / mybatis-3 / 3468

20 Jul 2026 10:37PM UTC coverage: 87.454% (+0.001%) from 87.453%
3468

push

github

web-flow
Merge pull request #3739 from mybatis/modernization

Modernization of source code collection usage

3875 of 4683 branches covered (82.75%)

33 of 36 new or added lines in 18 files covered. (91.67%)

9996 of 11430 relevant lines covered (87.45%)

0.87 hits per line

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

97.0
/src/main/java/org/apache/ibatis/type/TypeAliasRegistry.java
1
/*
2
 *    Copyright 2009-2026 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.type;
17

18
import java.math.BigDecimal;
19
import java.math.BigInteger;
20
import java.sql.ResultSet;
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.Locale;
28
import java.util.Map;
29
import java.util.Set;
30

31
import org.apache.ibatis.io.ResolverUtil;
32
import org.apache.ibatis.io.Resources;
33

34
/**
35
 * @author Clinton Begin
36
 */
37
public class TypeAliasRegistry {
38

39
  private final Map<String, Class<?>> typeAliases = new HashMap<>();
1✔
40

41
  public TypeAliasRegistry() {
1✔
42
    registerAlias("string", String.class);
1✔
43

44
    registerAlias("byte", Byte.class);
1✔
45
    registerAlias("char", Character.class);
1✔
46
    registerAlias("character", Character.class);
1✔
47
    registerAlias("long", Long.class);
1✔
48
    registerAlias("short", Short.class);
1✔
49
    registerAlias("int", Integer.class);
1✔
50
    registerAlias("integer", Integer.class);
1✔
51
    registerAlias("double", Double.class);
1✔
52
    registerAlias("float", Float.class);
1✔
53
    registerAlias("boolean", Boolean.class);
1✔
54

55
    registerAlias("byte[]", Byte[].class);
1✔
56
    registerAlias("char[]", Character[].class);
1✔
57
    registerAlias("character[]", Character[].class);
1✔
58
    registerAlias("long[]", Long[].class);
1✔
59
    registerAlias("short[]", Short[].class);
1✔
60
    registerAlias("int[]", Integer[].class);
1✔
61
    registerAlias("integer[]", Integer[].class);
1✔
62
    registerAlias("double[]", Double[].class);
1✔
63
    registerAlias("float[]", Float[].class);
1✔
64
    registerAlias("boolean[]", Boolean[].class);
1✔
65

66
    registerAlias("_byte", byte.class);
1✔
67
    registerAlias("_char", char.class);
1✔
68
    registerAlias("_character", char.class);
1✔
69
    registerAlias("_long", long.class);
1✔
70
    registerAlias("_short", short.class);
1✔
71
    registerAlias("_int", int.class);
1✔
72
    registerAlias("_integer", int.class);
1✔
73
    registerAlias("_double", double.class);
1✔
74
    registerAlias("_float", float.class);
1✔
75
    registerAlias("_boolean", boolean.class);
1✔
76

77
    registerAlias("_byte[]", byte[].class);
1✔
78
    registerAlias("_char[]", char[].class);
1✔
79
    registerAlias("_character[]", char[].class);
1✔
80
    registerAlias("_long[]", long[].class);
1✔
81
    registerAlias("_short[]", short[].class);
1✔
82
    registerAlias("_int[]", int[].class);
1✔
83
    registerAlias("_integer[]", int[].class);
1✔
84
    registerAlias("_double[]", double[].class);
1✔
85
    registerAlias("_float[]", float[].class);
1✔
86
    registerAlias("_boolean[]", boolean[].class);
1✔
87

88
    registerAlias("date", Date.class);
1✔
89
    registerAlias("decimal", BigDecimal.class);
1✔
90
    registerAlias("bigdecimal", BigDecimal.class);
1✔
91
    registerAlias("biginteger", BigInteger.class);
1✔
92
    registerAlias("object", Object.class);
1✔
93

94
    registerAlias("date[]", Date[].class);
1✔
95
    registerAlias("decimal[]", BigDecimal[].class);
1✔
96
    registerAlias("bigdecimal[]", BigDecimal[].class);
1✔
97
    registerAlias("biginteger[]", BigInteger[].class);
1✔
98
    registerAlias("object[]", Object[].class);
1✔
99

100
    registerAlias("map", Map.class);
1✔
101
    registerAlias("hashmap", HashMap.class);
1✔
102
    registerAlias("list", List.class);
1✔
103
    registerAlias("arraylist", ArrayList.class);
1✔
104
    registerAlias("collection", Collection.class);
1✔
105
    registerAlias("iterator", Iterator.class);
1✔
106

107
    registerAlias("ResultSet", ResultSet.class);
1✔
108
  }
1✔
109

110
  @SuppressWarnings("unchecked")
111
  // throws class cast exception as well if types cannot be assigned
112
  public <T> Class<T> resolveAlias(String string) {
113
    try {
114
      if (string == null) {
1!
115
        return null;
×
116
      }
117
      // issue #748
118
      String key = string.toLowerCase(Locale.ENGLISH);
1✔
119
      Class<T> value;
120
      if (typeAliases.containsKey(key)) {
1✔
121
        value = (Class<T>) typeAliases.get(key);
1✔
122
      } else {
123
        value = (Class<T>) Resources.classForName(string);
1✔
124
      }
125
      return value;
1✔
126
    } catch (ClassNotFoundException e) {
1✔
127
      throw new TypeException("Could not resolve type alias '" + string + "'.  Cause: " + e, e);
1✔
128
    }
129
  }
130

131
  public void registerAliases(String packageName) {
132
    registerAliases(packageName, Object.class);
1✔
133
  }
1✔
134

135
  public void registerAliases(String packageName, Class<?> superType) {
136
    ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<>();
1✔
137
    resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
1✔
138
    Set<Class<? extends Class<?>>> typeSet = resolverUtil.getClasses();
1✔
139
    for (Class<?> type : typeSet) {
1✔
140
      // Ignore inner classes and interfaces (including package-info.java)
141
      // Skip also inner classes. See issue #6
142
      if (!type.isAnonymousClass() && !type.isInterface() && !type.isMemberClass()) {
1!
143
        registerAlias(type);
1✔
144
      }
145
    }
1✔
146
  }
1✔
147

148
  public void registerAlias(Class<?> type) {
149
    String alias = type.getSimpleName();
1✔
150
    Alias aliasAnnotation = type.getAnnotation(Alias.class);
1✔
151
    if (aliasAnnotation != null) {
1✔
152
      alias = aliasAnnotation.value();
1✔
153
    }
154
    registerAlias(alias, type);
1✔
155
  }
1✔
156

157
  public void registerAlias(String alias, Class<?> value) {
158
    if (alias == null) {
1!
159
      throw new TypeException("The parameter alias cannot be null");
×
160
    }
161
    // issue #748
162
    String key = alias.toLowerCase(Locale.ENGLISH);
1✔
163
    if (typeAliases.containsKey(key) && typeAliases.get(key) != null && !typeAliases.get(key).equals(value)) {
1✔
164
      throw new TypeException(
1✔
165
          "The alias '" + alias + "' is already mapped to the value '" + typeAliases.get(key).getName() + "'.");
1✔
166
    }
167
    typeAliases.put(key, value);
1✔
168
  }
1✔
169

170
  public void registerAlias(String alias, String value) {
171
    try {
172
      registerAlias(alias, Resources.classForName(value));
1✔
173
    } catch (ClassNotFoundException e) {
1✔
174
      throw new TypeException("Error registering type alias " + alias + " for " + value + ". Cause: " + e, e);
1✔
175
    }
1✔
176
  }
1✔
177

178
  /**
179
   * Gets the type aliases.
180
   *
181
   * @return the type aliases
182
   *
183
   * @since 3.2.2
184
   */
185
  public Map<String, Class<?>> getTypeAliases() {
NEW
186
    return Map.copyOf(typeAliases);
×
187
  }
188

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

© 2026 Coveralls, Inc