18
18
*/
19
19
class DBeanMap {
20
20
21
- private final Map <String , DContextEntry > beans ;
21
+ private final Map <String , DContextEntry > beans = new LinkedHashMap <>() ;
22
22
23
23
/**
24
- * Create for root builder with supplied beans (usually test doubles).
25
- * <p>
26
- * Supplied beans are typically test doubles (used in testing) that replace the normally
27
- * injected bean. For example, a stub for a database API.
28
- * </p>
24
+ * Create for context builder.
29
25
*/
30
- DBeanMap (List <Object > suppliedBeans ) {
31
- if (suppliedBeans .isEmpty ()) {
32
- beans = null ;
33
- } else {
34
- beans = new LinkedHashMap <>();
35
- for (Object suppliedBean : suppliedBeans ) {
36
- addSuppliedBean (suppliedBean );
37
- }
38
- }
26
+ DBeanMap () {
39
27
}
40
28
41
29
/**
42
- * Create for context builder .
30
+ * Add test double supplied beans .
43
31
*/
44
- DBeanMap () {
45
- beans = new LinkedHashMap <>();
32
+ void add (List <SuppliedBean > suppliedBeans ) {
33
+ for (SuppliedBean suppliedBean : suppliedBeans ) {
34
+ addSuppliedBean (suppliedBean );
35
+ }
46
36
}
47
37
48
- private void addSuppliedBean (Object bean ) {
38
+ private void addSuppliedBean (SuppliedBean supplied ) {
49
39
50
- Class <?> suppliedClass = bean .getClass ();
51
- Class <?> suppliedType = suppliedType (suppliedClass );
52
- Named annotation = suppliedClass .getAnnotation (Named .class );
40
+ Class <?> suppliedType = supplied .getType ();
41
+ Named annotation = suppliedType .getAnnotation (Named .class );
53
42
String name = (annotation == null ) ? null : annotation .value ();
54
43
55
- DContextEntryBean entryBean = DContextEntryBean .of (bean , name , SUPPLIED );
44
+ DContextEntryBean entryBean = DContextEntryBean .of (supplied . getBean () , name , SUPPLIED );
56
45
beans .computeIfAbsent (suppliedType .getCanonicalName (), s -> new DContextEntry ()).add (entryBean );
57
- for (Class <?> anInterface : suppliedClass .getInterfaces ()) {
46
+ for (Class <?> anInterface : suppliedType .getInterfaces ()) {
58
47
beans .computeIfAbsent (anInterface .getCanonicalName (), s -> new DContextEntry ()).add (entryBean );
59
48
}
60
49
}
61
50
62
- /**
63
- * Return the type that we map the supplied bean to.
64
- */
65
- private Class <?> suppliedType (Class <?> suppliedClass ) {
66
- Class <?> suppliedSuper = suppliedClass .getSuperclass ();
67
- if (Object .class .equals (suppliedSuper )) {
68
- return suppliedClass ;
69
- } else {
70
- // prefer to use the super type of the supplied bean (test double)
71
- return suppliedSuper ;
72
- }
73
- }
74
-
75
51
void registerPrimary (Object bean , String name , Class <?>... types ) {
76
52
registerWith (PRIMARY , bean , name , types );
77
53
}
@@ -102,10 +78,6 @@ void registerWith(int flag, Object bean, String name, Class<?>... types) {
102
78
@ SuppressWarnings ("unchecked" )
103
79
<T > T getBean (Class <T > type , String name ) {
104
80
105
- if (beans == null ) {
106
- // no beans in the root suppliedBeanMap
107
- return null ;
108
- }
109
81
DContextEntry entry = beans .get (type .getCanonicalName ());
110
82
if (entry != null ) {
111
83
T bean = (T ) entry .get (name );
@@ -118,10 +90,6 @@ <T> T getBean(Class<T> type, String name) {
118
90
119
91
<T > BeanEntry <T > candidate (Class <T > type , String name ) {
120
92
121
- if (beans == null ) {
122
- // no beans in the root suppliedBeanMap
123
- return null ;
124
- }
125
93
DContextEntry entry = beans .get (type .getCanonicalName ());
126
94
if (entry != null ) {
127
95
return entry .candidate (name );
@@ -134,25 +102,17 @@ <T> BeanEntry<T> candidate(Class<T> type, String name) {
134
102
*/
135
103
@ SuppressWarnings ("unchecked" )
136
104
void addAll (Class type , List list ) {
137
- if (beans != null ) {
138
- DContextEntry entry = beans .get (type .getCanonicalName ());
139
- if (entry != null ) {
140
- entry .addAll (list );
141
- }
105
+ DContextEntry entry = beans .get (type .getCanonicalName ());
106
+ if (entry != null ) {
107
+ entry .addAll (list );
142
108
}
143
109
}
144
110
145
111
/**
146
- * Return true if the bean for the given type should be created.
147
- * <p>
148
- * Return false indicates the type has a supplied (test double) instance that should be used instead and that
149
- * means context building will skip creating this bean.
150
- * </p>
112
+ * Return true if there is a supplied bean for this type.
151
113
*/
152
- boolean isAddBeanFor (String type ) {
153
- if (beans == null ) {
154
- return true ;
155
- }
156
- return !beans .containsKey (type );
114
+ boolean isSupplied (String type ) {
115
+ DContextEntry entry = beans .get (type );
116
+ return entry != null && entry .isSupplied ();
157
117
}
158
118
}
0 commit comments