|
1 | | -/** |
2 | | - * Copyright 2009-2016 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 | | - * http://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 java.lang.reflect.Constructor; |
19 | | - |
20 | | -import org.apache.ibatis.type.TypeException; |
21 | | -import org.apache.ibatis.type.TypeHandler; |
22 | | - |
23 | | -import com.google.common.base.Objects; |
24 | | -import com.google.inject.Inject; |
25 | | -import com.google.inject.Injector; |
26 | | -import com.google.inject.TypeLiteral; |
27 | | - |
28 | | -import javax.inject.Provider; |
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) { |
40 | | - this.typeHandlerTypeLiteral = TypeLiteral.get(typeHandlerType); |
41 | | - this.handledType = handledType; |
42 | | - } |
43 | | - |
44 | | - public TypeHandlerProvider(TypeLiteral<TH> typeHandlerType, Class<T> handledType) { |
45 | | - this.typeHandlerTypeLiteral = typeHandlerType; |
46 | | - this.handledType = handledType; |
47 | | - } |
48 | | - |
49 | | - @Override |
50 | | - @SuppressWarnings("unchecked") |
51 | | - public TH get() { |
52 | | - TH instance = null; |
53 | | - if (handledType != null) { |
54 | | - try { |
55 | | - Constructor<?> c = typeHandlerTypeLiteral.getRawType().getConstructor(Class.class); |
56 | | - instance = (TH) c.newInstance(handledType); |
57 | | - injector.injectMembers(instance); |
58 | | - } catch (NoSuchMethodException ignored) { |
59 | | - // ignored |
60 | | - } catch (Exception e) { |
61 | | - throw new TypeException("Failed invoking constructor for handler " + typeHandlerTypeLiteral.getType(), e); |
62 | | - } |
63 | | - } |
64 | | - if (instance == null) { |
65 | | - try { |
66 | | - instance = (TH) typeHandlerTypeLiteral.getRawType().newInstance(); |
67 | | - injector.injectMembers(instance); |
68 | | - } catch (Exception e) { |
69 | | - throw new TypeException("Failed invoking constructor for handler " + typeHandlerTypeLiteral.getType(), e); |
70 | | - } |
71 | | - } |
72 | | - return instance; |
73 | | - } |
74 | | - |
75 | | - @Override |
76 | | - public int hashCode() { |
77 | | - return Objects.hashCode(this.typeHandlerTypeLiteral, this.handledType); |
78 | | - } |
79 | | - |
80 | | - @Override |
81 | | - public boolean equals(Object obj) { |
82 | | - if(obj == null){ |
83 | | - return false; |
84 | | - } |
85 | | - TypeHandlerProvider other = (TypeHandlerProvider) obj; |
86 | | - return Objects.equal(this.typeHandlerTypeLiteral, other.typeHandlerTypeLiteral) && |
87 | | - Objects.equal(this.handledType, other.handledType); |
88 | | - } |
89 | | -} |
| 1 | +/** |
| 2 | + * Copyright 2009-2016 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 | + * http://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 java.lang.reflect.Constructor; |
| 19 | + |
| 20 | +import org.apache.ibatis.type.TypeException; |
| 21 | +import org.apache.ibatis.type.TypeHandler; |
| 22 | + |
| 23 | +import com.google.common.base.Objects; |
| 24 | +import com.google.inject.Inject; |
| 25 | +import com.google.inject.Injector; |
| 26 | +import com.google.inject.TypeLiteral; |
| 27 | + |
| 28 | +import javax.inject.Provider; |
| 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) { |
| 40 | + this.typeHandlerTypeLiteral = TypeLiteral.get(typeHandlerType); |
| 41 | + this.handledType = handledType; |
| 42 | + } |
| 43 | + |
| 44 | + public TypeHandlerProvider(TypeLiteral<TH> typeHandlerType, Class<T> handledType) { |
| 45 | + this.typeHandlerTypeLiteral = typeHandlerType; |
| 46 | + this.handledType = handledType; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + @SuppressWarnings("unchecked") |
| 51 | + public TH get() { |
| 52 | + TH instance = null; |
| 53 | + if (handledType != null) { |
| 54 | + try { |
| 55 | + Constructor<?> c = typeHandlerTypeLiteral.getRawType().getConstructor(Class.class); |
| 56 | + instance = (TH) c.newInstance(handledType); |
| 57 | + injector.injectMembers(instance); |
| 58 | + } catch (NoSuchMethodException ignored) { |
| 59 | + // ignored |
| 60 | + } catch (Exception e) { |
| 61 | + throw new TypeException("Failed invoking constructor for handler " + typeHandlerTypeLiteral.getType(), e); |
| 62 | + } |
| 63 | + } |
| 64 | + if (instance == null) { |
| 65 | + try { |
| 66 | + instance = (TH) typeHandlerTypeLiteral.getRawType().newInstance(); |
| 67 | + injector.injectMembers(instance); |
| 68 | + } catch (Exception e) { |
| 69 | + throw new TypeException("Failed invoking constructor for handler " + typeHandlerTypeLiteral.getType(), e); |
| 70 | + } |
| 71 | + } |
| 72 | + return instance; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public int hashCode() { |
| 77 | + return Objects.hashCode(this.typeHandlerTypeLiteral, this.handledType); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public boolean equals(Object obj) { |
| 82 | + if(obj == null){ |
| 83 | + return false; |
| 84 | + } |
| 85 | + TypeHandlerProvider other = (TypeHandlerProvider) obj; |
| 86 | + return Objects.equal(this.typeHandlerTypeLiteral, other.typeHandlerTypeLiteral) && |
| 87 | + Objects.equal(this.handledType, other.handledType); |
| 88 | + } |
| 89 | +} |
0 commit comments