-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuiceWildcardTest.java
70 lines (52 loc) · 1.88 KB
/
GuiceWildcardTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
public class GuiceWildcardTest {
@Test
public void a() {
final Injector injector = Guice.createInjector(new AbstractModule() {
@Override protected void configure() {
bind(new TypeLiteral<Class<? extends SuperClass>>() {}).toInstance(SubClass.class);
}
});
final ClassHolder holder = new ClassHolder();
injector.injectMembers(holder);
assertEquals(SubClass.class, holder.type);
}
static class SuperClass {}
static class SubClass extends SuperClass {}
static class ClassHolder {
@Inject Class<? extends SuperClass> type;
}
@Test
public void b() {
final Injector injector = Guice.createInjector(new AbstractModule(){
@Override
protected void configure() {
bind(new TypeLiteral<Service<Class<?>>>(){});
}
});
TypeLiteral<Service<Bean1>> s1literal = new TypeLiteral<Service<Bean1>>(){};
Service<Bean1> s1 = injector.getInstance(Key.get(s1literal));
assertTrue(s1literal.toString().contains(s1.type.toString()));
TypeLiteral<Service<Bean3>> s3literal = new TypeLiteral<Service<Bean3>>(){};
Service<Bean3> s3 = injector.getInstance(Key.get(s3literal));
assertTrue(s3literal.toString().contains(s3.type.toString()));
}
public static class MyBean { }
public static class Bean1 extends MyBean { }
public static class Bean3 {}
public static class Service<T> {
TypeLiteral<T> type;
@Inject
public Service(TypeLiteral<T> type){
this.type = type;
}
}
}