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

mybatis / guice / #1210

04 Nov 2023 08:23PM CUT coverage: 80.045%. Remained the same
#1210

Pull #633

github

web-flow
Merge 9d1860fb9 into 408340d1e
Pull Request #633: [pom] Sortpom

1408 of 1759 relevant lines covered (80.05%)

0.8 hits per line

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

65.79
/src/main/java/org/mybatis/guice/type/TypeHandlerProvider.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.mybatis.guice.type;
17

18
import com.google.inject.Injector;
19
import com.google.inject.TypeLiteral;
20

21
import jakarta.inject.Inject;
22
import jakarta.inject.Provider;
23

24
import java.lang.reflect.Constructor;
25
import java.util.Objects;
26

27
import org.apache.ibatis.type.TypeException;
28
import org.apache.ibatis.type.TypeHandler;
29

30
/**
31
 * A generic MyBatis type provider.
32
 */
33
public final class TypeHandlerProvider<TH extends TypeHandler<? extends T>, T> implements Provider<TH> {
34
  private final TypeLiteral<TH> typeHandlerTypeLiteral;
35
  private final Class<T> handledType;
36
  @Inject
37
  private Injector injector;
38

39
  public TypeHandlerProvider(Class<TH> typeHandlerType, Class<T> handledType) {
1✔
40
    this.typeHandlerTypeLiteral = TypeLiteral.get(typeHandlerType);
1✔
41
    this.handledType = handledType;
1✔
42
  }
1✔
43

44
  public TypeHandlerProvider(TypeLiteral<TH> typeHandlerType, Class<T> handledType) {
1✔
45
    this.typeHandlerTypeLiteral = typeHandlerType;
1✔
46
    this.handledType = handledType;
1✔
47
  }
1✔
48

49
  TypeHandlerProvider(Injector injector, Class<TH> typeHandlerType, Class<T> handledType) {
50
    this(typeHandlerType, handledType);
1✔
51
    this.injector = injector;
1✔
52
  }
1✔
53

54
  TypeHandlerProvider(Injector injector, TypeLiteral<TH> typeHandlerType, Class<T> handledType) {
55
    this(typeHandlerType, handledType);
1✔
56
    this.injector = injector;
1✔
57
  }
1✔
58

59
  @Override
60
  @SuppressWarnings("unchecked")
61
  public TH get() {
62
    TH instance = null;
1✔
63
    if (handledType != null) {
1✔
64
      try {
65
        Constructor<?> c = typeHandlerTypeLiteral.getRawType().getConstructor(Class.class);
1✔
66
        instance = (TH) c.newInstance(handledType);
1✔
67
        injector.injectMembers(instance);
1✔
68
      } catch (NoSuchMethodException ignored) {
×
69
        // ignored
70
      } catch (Exception e) {
×
71
        throw new TypeException("Failed invoking constructor for handler " + typeHandlerTypeLiteral.getType(), e);
×
72
      }
1✔
73
    }
74
    if (instance == null) {
1✔
75
      try {
76
        instance = (TH) typeHandlerTypeLiteral.getRawType().newInstance();
1✔
77
        injector.injectMembers(instance);
1✔
78
      } catch (Exception e) {
×
79
        throw new TypeException("Failed invoking constructor for handler " + typeHandlerTypeLiteral.getType(), e);
×
80
      }
1✔
81
    }
82
    return instance;
1✔
83
  }
84

85
  @Override
86
  public int hashCode() {
87
    return Objects.hash(this.typeHandlerTypeLiteral, this.handledType);
×
88
  }
89

90
  @Override
91
  public boolean equals(Object obj) {
92
    if (obj == null) {
×
93
      return false;
×
94
    }
95
    if (this.getClass() != obj.getClass()) {
×
96
      return false;
×
97
    }
98
    TypeHandlerProvider other = (TypeHandlerProvider) obj;
×
99
    return Objects.equals(this.typeHandlerTypeLiteral, other.typeHandlerTypeLiteral)
×
100
        && Objects.equals(this.handledType, other.handledType);
×
101
  }
102
}
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