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

mybatis / mybatis-3 / 4017

02 Dec 2026 01:21PM UTC coverage: 87.564% (+0.1%) from 87.454%
4017

Pull #3809

github

web-flow
Merge de6182685 into 19b82e6ae
Pull Request #3809: Fix JDBC array cleanup when binding or extraction fails

3907 of 4711 branches covered (82.93%)

22 of 22 new or added lines in 1 file covered. (100.0%)

152 existing lines in 10 files now uncovered.

10034 of 11459 relevant lines covered (87.56%)

0.88 hits per line

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

93.63
/src/main/java/org/apache/ibatis/reflection/TypeParameterResolver.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.reflection;
17

18
import java.lang.reflect.Array;
19
import java.lang.reflect.Field;
20
import java.lang.reflect.GenericArrayType;
21
import java.lang.reflect.Method;
22
import java.lang.reflect.ParameterizedType;
23
import java.lang.reflect.Type;
24
import java.lang.reflect.TypeVariable;
25
import java.lang.reflect.WildcardType;
26
import java.util.Arrays;
27
import java.util.Objects;
28

29
/**
30
 * @author Iwao AVE!
31
 * @author Vladimir Sitnikov
32
 */
33
public class TypeParameterResolver {
34

35
  public static Type[] resolveClassTypeParams(Class<?> classWithTypeParams, Class<?> childClass) {
36
    TypeVariable<?>[] typeArgs = classWithTypeParams.getTypeParameters();
1 ✔
37
    Type[] result = new Type[typeArgs.length];
1 ✔
38
    for (int i = 0; i < typeArgs.length; i++) {
1 ✔
39
      result[i] = resolveTypeVar(typeArgs[i], childClass, classWithTypeParams);
1 ✔
40
    }
41
    return result;
1 ✔
42
  }
43

44
  /**
45
   * Resolve field type.
46
   *
47
   * @param field
48
   *          the field
49
   * @param srcType
50
   *          the src type
51
   *
52
   * @return The field type as {@link Type}. If it has type parameters in the declaration,<br>
53
   *         they will be resolved to the actual runtime {@link Type}s.
54
   */
55
  public static Type resolveFieldType(Field field, Type srcType) {
56
    Type fieldType = field.getGenericType();
1 ✔
57
    Class<?> declaringClass = field.getDeclaringClass();
1 ✔
58
    return resolveType(fieldType, srcType, declaringClass);
1 ✔
59
  }
60

61
  /**
62
   * Resolve return type.
63
   *
64
   * @param method
65
   *          the method
66
   * @param srcType
67
   *          the src type
68
   *
69
   * @return The return type of the method as {@link Type}. If it has type parameters in the declaration,<br>
70
   *         they will be resolved to the actual runtime {@link Type}s.
71
   */
72
  public static Type resolveReturnType(Method method, Type srcType) {
73
    Type returnType = method.getGenericReturnType();
1 ✔
74
    Class<?> declaringClass = method.getDeclaringClass();
1 ✔
75
    return resolveType(returnType, srcType, declaringClass);
1 ✔
76
  }
77

78
  /**
79
   * Resolve param types.
80
   *
81
   * @param method
82
   *          the method
83
   * @param srcType
84
   *          the src type
85
   *
86
   * @return The parameter types of the method as an array of {@link Type}s. If they have type parameters in the
87
   *         declaration,<br>
88
   *         they will be resolved to the actual runtime {@link Type}s.
89
   */
90
  public static Type[] resolveParamTypes(Method method, Type srcType) {
91
    Type[] paramTypes = method.getGenericParameterTypes();
1 ✔
92
    Class<?> declaringClass = method.getDeclaringClass();
1 ✔
93
    return resolveTypes(paramTypes, srcType, declaringClass);
1 ✔
94
  }
95

96
  private static Type[] resolveTypes(Type[] types, Type srcType, Class<?> declaringClass) {
97
    Type[] args = new Type[types.length];
1 ✔
98
    for (int i = 0; i < types.length; i++) {
1 ✔
99
      args[i] = resolveType(types[i], srcType, declaringClass);
1 ✔
100
    }
101
    return args;
1 ✔
102
  }
103

104
  private static Type resolveType(Type type, Type srcType, Class<?> declaringClass) {
105
    if (type instanceof TypeVariable) {
1 ✔
106
      return resolveTypeVar((TypeVariable<?>) type, srcType, declaringClass);
1 ✔
107
    } else if (type instanceof ParameterizedType) {
1 ✔
108
      return resolveParameterizedType((ParameterizedType) type, srcType, declaringClass);
1 ✔
109
    } else if (type instanceof GenericArrayType) {
1 ✔
110
      return resolveGenericArrayType((GenericArrayType) type, srcType, declaringClass);
1 ✔
111
    } else if (type instanceof WildcardType) {
1 ✔
112
      return resolveWildcardType((WildcardType) type, srcType, declaringClass);
1 ✔
113
    } else {
114
      return type;
1 ✔
115
    }
116
  }
117

118
  private static Type resolveGenericArrayType(GenericArrayType genericArrayType, Type srcType,
119
      Class<?> declaringClass) {
120
    Type componentType = genericArrayType.getGenericComponentType();
1 ✔
121
    Type resolvedComponentType = resolveType(componentType, srcType, declaringClass);
1 ✔
122
    if (resolvedComponentType instanceof Class) {
1 ✔
123
      return Array.newInstance((Class<?>) resolvedComponentType, 0).getClass();
1 ✔
124
    } else {
125
      return new GenericArrayTypeImpl(resolvedComponentType);
1 ✔
126
    }
127
  }
128

129
  private static ParameterizedType resolveParameterizedType(ParameterizedType parameterizedType, Type srcType,
130
      Class<?> declaringClass) {
131
    Class<?> rawType = (Class<?>) parameterizedType.getRawType();
1 ✔
132
    Type ownerType = parameterizedType.getOwnerType();
1 ✔
133
    Type[] typeArgs = parameterizedType.getActualTypeArguments();
1 ✔
134
    Type[] args = resolveTypes(typeArgs, srcType, declaringClass);
1 ✔
135
    return new ParameterizedTypeImpl(rawType, ownerType, args);
1 ✔
136
  }
137

138
  private static Type resolveWildcardType(WildcardType wildcardType, Type srcType, Class<?> declaringClass) {
139
    Type[] lowerBounds = resolveTypes(wildcardType.getLowerBounds(), srcType, declaringClass);
1 ✔
140
    Type[] upperBounds = resolveTypes(wildcardType.getUpperBounds(), srcType, declaringClass);
1 ✔
141
    return new WildcardTypeImpl(lowerBounds, upperBounds);
1 ✔
142
  }
143

144
  private static Type resolveTypeVar(TypeVariable<?> typeVar, Type srcType, Class<?> declaringClass) {
145
    Type result;
146
    Class<?> clazz;
147
    if (srcType instanceof Class) {
1 ✔
148
      clazz = (Class<?>) srcType;
1 ✔
149
    } else if (srcType instanceof ParameterizedType) {
1 !
150
      ParameterizedType parameterizedType = (ParameterizedType) srcType;
1 ✔
151
      clazz = (Class<?>) parameterizedType.getRawType();
1 ✔
152
      if (clazz == declaringClass) {
1 ✔
153
        TypeVariable<?>[] typeVars = declaringClass.getTypeParameters();
1 ✔
154
        for (int i = 0; i < typeVars.length; i++) {
1 ✔
155
          if (typeVar.equals(typeVars[i])) {
1 ✔
156
            return parameterizedType.getActualTypeArguments()[i];
1 ✔
157
          }
158
        }
159
      }
160
    } else {
1 ✔
161
      throw new IllegalArgumentException(
×
162
          "The 2nd arg must be Class or ParameterizedType, but was: " + srcType.getClass());
×
163
    }
164

165
    if (clazz == declaringClass) {
1 ✔
166
      Type[] bounds = typeVar.getBounds();
1 ✔
167
      if (bounds.length > 0) {
1 !
168
        return bounds[0];
1 ✔
169
      }
170
      return Object.class;
×
171
    }
172

173
    Type superclass = clazz.getGenericSuperclass();
1 ✔
174
    result = scanSuperTypes(typeVar, srcType, declaringClass, clazz, superclass);
1 ✔
175
    if (result != null) {
1 ✔
176
      return result;
1 ✔
177
    }
178

179
    Type[] superInterfaces = clazz.getGenericInterfaces();
1 ✔
180
    for (Type superInterface : superInterfaces) {
1 !
181
      result = scanSuperTypes(typeVar, srcType, declaringClass, clazz, superInterface);
1 ✔
182
      if (result != null) {
1 ✔
183
        return result;
1 ✔
184
      }
185
    }
186
    return Object.class;
×
187
  }
188

189
  private static Type scanSuperTypes(TypeVariable<?> typeVar, Type srcType, Class<?> declaringClass, Class<?> clazz,
190
      Type superclass) {
191
    if (superclass instanceof ParameterizedType) {
1 ✔
192
      ParameterizedType parentAsType = (ParameterizedType) superclass;
1 ✔
193
      Class<?> parentAsClass = (Class<?>) parentAsType.getRawType();
1 ✔
194
      TypeVariable<?>[] parentTypeVars = parentAsClass.getTypeParameters();
1 ✔
195
      if (srcType instanceof ParameterizedType) {
1 ✔
196
        parentAsType = translateParentTypeVars((ParameterizedType) srcType, clazz, parentAsType);
1 ✔
197
      }
198
      if (declaringClass == parentAsClass) {
1 ✔
199
        for (int i = 0; i < parentTypeVars.length; i++) {
1 ✔
200
          if (typeVar.equals(parentTypeVars[i])) {
1 ✔
201
            Type actualType = parentAsType.getActualTypeArguments()[i];
1 ✔
202
            return actualType instanceof TypeVariable<?> ? Object.class : actualType;
1 ✔
203
          }
204
        }
205
      }
206
      if (declaringClass.isAssignableFrom(parentAsClass)) {
1 !
207
        return resolveTypeVar(typeVar, parentAsType, declaringClass);
1 ✔
208
      }
209
    } else if (superclass instanceof Class && declaringClass.isAssignableFrom((Class<?>) superclass)) {
1 ✔
210
      return resolveTypeVar(typeVar, superclass, declaringClass);
1 ✔
211
    }
212
    return null;
1 ✔
213
  }
214

215
  private static ParameterizedType translateParentTypeVars(ParameterizedType srcType, Class<?> srcClass,
216
      ParameterizedType parentType) {
217
    Type[] parentTypeArgs = parentType.getActualTypeArguments();
1 ✔
218
    Type[] srcTypeArgs = srcType.getActualTypeArguments();
1 ✔
219
    TypeVariable<?>[] srcTypeVars = srcClass.getTypeParameters();
1 ✔
220
    Type[] newParentArgs = new Type[parentTypeArgs.length];
1 ✔
221
    boolean noChange = true;
1 ✔
222
    for (int i = 0; i < parentTypeArgs.length; i++) {
1 ✔
223
      if (parentTypeArgs[i] instanceof TypeVariable) {
1 ✔
224
        for (int j = 0; j < srcTypeVars.length; j++) {
1 ✔
225
          if (srcTypeVars[j].equals(parentTypeArgs[i])) {
1 ✔
226
            noChange = false;
1 ✔
227
            newParentArgs[i] = srcTypeArgs[j];
1 ✔
228
          }
229
        }
230
      } else {
231
        newParentArgs[i] = parentTypeArgs[i];
1 ✔
232
      }
233
    }
234
    return noChange ? parentType
1 !
235
        : new ParameterizedTypeImpl((Class<?>) parentType.getRawType(), parentType.getOwnerType(), newParentArgs);
1 ✔
236
  }
237

238
  private TypeParameterResolver() {
239
    super();
240
  }
241

242
  static class ParameterizedTypeImpl implements ParameterizedType {
243
    private final Class<?> rawType;
244

245
    private final Type ownerType;
246

247
    private final Type[] actualTypeArguments;
248

249
    ParameterizedTypeImpl(Class<?> rawType, Type ownerType, Type[] actualTypeArguments) {
250
      super();
1 ✔
251
      this.rawType = rawType;
1 ✔
252
      this.ownerType = ownerType;
1 ✔
253
      this.actualTypeArguments = actualTypeArguments;
1 ✔
254
    }
1 ✔
255

256
    @Override
257
    public Type[] getActualTypeArguments() {
258
      return actualTypeArguments;
1 ✔
259
    }
260

261
    @Override
262
    public Type getOwnerType() {
263
      return ownerType;
1 ✔
264
    }
265

266
    @Override
267
    public Type getRawType() {
268
      return rawType;
1 ✔
269
    }
270

271
    @Override
272
    public int hashCode() {
273
      return (ownerType == null ? 0 : ownerType.hashCode()) ^ Arrays.hashCode(actualTypeArguments) ^ rawType.hashCode();
1 ✔
274
    }
275

276
    @Override
277
    public boolean equals(Object obj) {
278
      if (!(obj instanceof ParameterizedType)) {
1 !
279
        return false;
×
280
      }
281
      ParameterizedType other = (ParameterizedType) obj;
1 ✔
282
      return rawType.equals(other.getRawType()) && Objects.equals(ownerType, other.getOwnerType())
1 !
283
          && Arrays.equals(actualTypeArguments, other.getActualTypeArguments());
1 !
284
    }
285

286
    @Override
287
    public String toString() {
288
      StringBuilder s = new StringBuilder();
1 ✔
289
      if (ownerType != null) {
1 ✔
290
        s.append(ownerType.getTypeName()).append("$");
1 ✔
291
        s.append(rawType.getSimpleName());
1 ✔
292
      } else {
293
        s.append(rawType.getName());
1 ✔
294
      }
295
      int argLength = actualTypeArguments.length;
1 ✔
296
      if (argLength > 0) {
1 ✔
297
        s.append("<");
1 ✔
298
        for (int i = 0; i < argLength; i++) {
1 ✔
299
          if (i > 0) {
1 ✔
300
            s.append(", ");
1 ✔
301
          }
302
          s.append(actualTypeArguments[i].getTypeName());
1 ✔
303
        }
304
        s.append(">");
1 ✔
305
      }
306
      return s.toString();
1 ✔
307
    }
308
  }
309

310
  static class WildcardTypeImpl implements WildcardType {
311
    private final Type[] lowerBounds;
312

313
    private final Type[] upperBounds;
314

315
    WildcardTypeImpl(Type[] lowerBounds, Type[] upperBounds) {
316
      super();
1 ✔
317
      this.lowerBounds = lowerBounds;
1 ✔
318
      this.upperBounds = upperBounds;
1 ✔
319
    }
1 ✔
320

321
    @Override
322
    public Type[] getLowerBounds() {
323
      return lowerBounds;
1 ✔
324
    }
325

326
    @Override
327
    public Type[] getUpperBounds() {
328
      return upperBounds;
1 ✔
329
    }
330

331
    @Override
332
    public int hashCode() {
333
      return Arrays.hashCode(lowerBounds) ^ Arrays.hashCode(upperBounds);
1 ✔
334
    }
335

336
    @Override
337
    public boolean equals(Object obj) {
338
      if (this == obj) {
1 !
UNCOV
339
        return true;
×
340
      }
341
      if (!(obj instanceof WildcardType)) {
1 !
UNCOV
342
        return false;
×
343
      }
344
      WildcardType other = (WildcardType) obj;
1 ✔
345
      return Arrays.equals(lowerBounds, other.getLowerBounds()) && Arrays.equals(upperBounds, other.getUpperBounds());
1 !
346
    }
347

348
    @Override
349
    public String toString() {
350
      StringBuilder s = new StringBuilder().append("?");
1 ✔
351
      if (lowerBounds.length > 0) {
1 !
UNCOV
352
        s.append(" super ").append(lowerBounds[0].getTypeName());
×
353
      } else if (upperBounds.length > 0 && upperBounds[0] != Object.class) {
1 !
354
        s.append(" extends ").append(upperBounds[0].getTypeName());
1 ✔
355
      }
356
      return s.toString();
1 ✔
357
    }
358
  }
359

360
  static class GenericArrayTypeImpl implements GenericArrayType {
361
    private final Type genericComponentType;
362

363
    GenericArrayTypeImpl(Type genericComponentType) {
364
      super();
1 ✔
365
      this.genericComponentType = genericComponentType;
1 ✔
366
    }
1 ✔
367

368
    @Override
369
    public Type getGenericComponentType() {
370
      return genericComponentType;
1 ✔
371
    }
372

373
    @Override
374
    public int hashCode() {
375
      return Objects.hashCode(genericComponentType);
1 ✔
376
    }
377

378
    @Override
379
    public boolean equals(Object obj) {
380
      if (this == obj) {
1 !
UNCOV
381
        return true;
×
382
      }
383
      if (!(obj instanceof GenericArrayType)) {
1 !
UNCOV
384
        return false;
×
385
      }
386
      GenericArrayType other = (GenericArrayType) obj;
1 ✔
387
      return Objects.equals(genericComponentType, other.getGenericComponentType());
1 ✔
388
    }
389

390
    @Override
391
    public String toString() {
392
      return new StringBuilder().append(genericComponentType.getTypeName()).append("[]").toString();
1 ✔
393
    }
394
  }
395
}
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