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

mybatis / mybatis-3 / #3633

24 May 2024 08:58AM CUT coverage: 87.257% (+0.09%) from 87.169%
#3633

Pull #3108

github

web-flow
Merge 7d3668285 into 043270b83
Pull Request #3108: #101: Add support for collection constructor creation

3644 of 4415 branches covered (82.54%)

209 of 227 new or added lines in 7 files covered. (92.07%)

3 existing lines in 1 file now uncovered.

9586 of 10986 relevant lines covered (87.26%)

0.87 hits per line

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

93.02
/src/main/java/org/apache/ibatis/reflection/factory/DefaultObjectFactory.java
1
/*
2
 *    Copyright 2009-2024 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.factory;
17

18
import java.io.Serializable;
19
import java.lang.reflect.Constructor;
20
import java.util.ArrayList;
21
import java.util.Collection;
22
import java.util.Collections;
23
import java.util.HashMap;
24
import java.util.HashSet;
25
import java.util.List;
26
import java.util.Map;
27
import java.util.Optional;
28
import java.util.Set;
29
import java.util.SortedSet;
30
import java.util.TreeSet;
31
import java.util.stream.Collectors;
32

33
import org.apache.ibatis.reflection.ReflectionException;
34
import org.apache.ibatis.reflection.Reflector;
35

36
/**
37
 * @author Clinton Begin
38
 */
39
public class DefaultObjectFactory implements ObjectFactory, Serializable {
1✔
40

41
  private static final long serialVersionUID = -8855120656740914948L;
42

43
  @Override
44
  public <T> T create(Class<T> type) {
45
    return create(type, null, null);
1✔
46
  }
47

48
  @SuppressWarnings("unchecked")
49
  @Override
50
  public <T> T create(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
51
    Class<?> classToCreate = resolveInterface(type);
1✔
52
    // we know types are assignable
53
    return (T) instantiateClass(classToCreate, constructorArgTypes, constructorArgs);
1✔
54
  }
55

56
  @Override
57
  public <T> Constructor<T> resolveConstructor(Class<T> type, List<Class<?>> constructorArgTypes) {
58
    try {
59
      if (constructorArgTypes == null) {
1!
NEW
UNCOV
60
        return type.getDeclaredConstructor();
×
61
      }
62

63
      return type.getDeclaredConstructor(constructorArgTypes.toArray(new Class[0]));
1✔
64
    } catch (Exception e) {
1✔
65
      String argTypes = Optional.ofNullable(constructorArgTypes).orElseGet(Collections::emptyList).stream()
1✔
66
          .map(Class::getSimpleName).collect(Collectors.joining(","));
1✔
67
      throw new ReflectionException(
1✔
68
          "Error resolving constructor for " + type + " with invalid types (" + argTypes + ") . Cause: " + e, e);
69
    }
70
  }
71

72
  private <T> T instantiateClass(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
73
    try {
74
      Constructor<T> constructor;
75
      if (constructorArgTypes == null || constructorArgs == null) {
1!
76
        constructor = type.getDeclaredConstructor();
1✔
77
        try {
78
          return constructor.newInstance();
1✔
79
        } catch (IllegalAccessException e) {
1✔
80
          if (Reflector.canControlMemberAccessible()) {
1!
81
            constructor.setAccessible(true);
1✔
82
            return constructor.newInstance();
1✔
83
          }
UNCOV
84
          throw e;
×
85
        }
86
      }
87
      constructor = type.getDeclaredConstructor(constructorArgTypes.toArray(new Class[0]));
1✔
88
      try {
89
        return constructor.newInstance(constructorArgs.toArray(new Object[0]));
1✔
90
      } catch (IllegalAccessException e) {
1✔
91
        if (Reflector.canControlMemberAccessible()) {
1!
92
          constructor.setAccessible(true);
1✔
93
          return constructor.newInstance(constructorArgs.toArray(new Object[0]));
1✔
94
        }
UNCOV
95
        throw e;
×
96
      }
97
    } catch (Exception e) {
1✔
98
      String argTypes = Optional.ofNullable(constructorArgTypes).orElseGet(Collections::emptyList).stream()
1✔
99
          .map(Class::getSimpleName).collect(Collectors.joining(","));
1✔
100
      String argValues = Optional.ofNullable(constructorArgs).orElseGet(Collections::emptyList).stream()
1✔
101
          .map(String::valueOf).collect(Collectors.joining(","));
1✔
102
      throw new ReflectionException("Error instantiating " + type + " with invalid types (" + argTypes + ") or values ("
1✔
103
          + argValues + "). Cause: " + e, e);
104
    }
105
  }
106

107
  protected Class<?> resolveInterface(Class<?> type) {
108
    Class<?> classToCreate;
109
    if (type == List.class || type == Collection.class || type == Iterable.class) {
1✔
110
      classToCreate = ArrayList.class;
1✔
111
    } else if (type == Map.class) {
1✔
112
      classToCreate = HashMap.class;
1✔
113
    } else if (type == SortedSet.class) { // issue #510 Collections Support
1✔
114
      classToCreate = TreeSet.class;
1✔
115
    } else if (type == Set.class) {
1✔
116
      classToCreate = HashSet.class;
1✔
117
    } else {
118
      classToCreate = type;
1✔
119
    }
120
    return classToCreate;
1✔
121
  }
122

123
  @Override
124
  public <T> boolean isCollection(Class<T> type) {
125
    return Collection.class.isAssignableFrom(type);
1✔
126
  }
127

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