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

mybatis / mybatis-3 / #3097

pending completion
#3097

push

github

web-flow
Update dependency com.mysql:mysql-connector-j to v8.0.33

9398 of 10735 relevant lines covered (87.55%)

0.88 hits per line

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

96.0
/src/main/java/org/apache/ibatis/type/TypeAliasRegistry.java
1
/*
2
 *    Copyright 2009-2023 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.Collections;
24
import java.util.Date;
25
import java.util.HashMap;
26
import java.util.Iterator;
27
import java.util.List;
28
import java.util.Locale;
29
import java.util.Map;
30
import java.util.Set;
31

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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