42
42
import dagger .internal .codegen .model .Key ;
43
43
import dagger .internal .codegen .model .Scope ;
44
44
import dagger .internal .codegen .xprocessing .XElements ;
45
+ import dagger .internal .codegen .xprocessing .XTypes ;
45
46
import java .util .Formatter ;
46
47
import java .util .HashMap ;
47
48
import java .util .Map ;
@@ -176,6 +177,9 @@ protected void checkAdditionalProperties() {}
176
177
protected void checkType () {
177
178
switch (ContributionType .fromBindingElement (element )) {
178
179
case UNIQUE :
180
+ // Basic checks on the types
181
+ bindingElementType ().ifPresent (this ::checkKeyType );
182
+
179
183
// Validate that a unique binding is not attempting to bind a framework type. This
180
184
// validation is only appropriate for unique bindings because multibindings may collect
181
185
// framework types. E.g. Set<Provider<Foo>> is perfectly reasonable.
@@ -185,17 +189,22 @@ protected void checkType() {
185
189
// This validation is only appropriate for unique bindings because multibindings may
186
190
// collect assisted types.
187
191
checkAssistedType ();
188
- // fall through
192
+
193
+ // Check for any specifically disallowed types
194
+ bindingElementType ().ifPresent (this ::checkDisallowedType );
195
+ break ;
189
196
190
197
case SET :
191
198
bindingElementType ().ifPresent (this ::checkSetValueFrameworkType );
192
199
break ;
200
+
193
201
case MAP :
194
202
bindingElementType ().ifPresent (this ::checkMapValueFrameworkType );
195
203
break ;
196
204
197
205
case SET_VALUES :
198
206
checkSetValuesType ();
207
+ break ;
199
208
}
200
209
}
201
210
@@ -360,24 +369,37 @@ private void checkScopes() {
360
369
*/
361
370
private void checkFrameworkType () {
362
371
if (bindingElementType ().filter (FrameworkTypes ::isFrameworkType ).isPresent ()) {
363
- report .addError (bindingElements ("must not %s framework types" , bindingElementTypeVerb ()));
372
+ report .addError (bindingElements ("must not %s framework types: %s" ,
373
+ bindingElementTypeVerb (), XTypes .toStableString (bindingElementType ().get ())));
364
374
}
365
375
}
366
376
367
377
private void checkSetValueFrameworkType (XType bindingType ) {
368
378
checkKeyType (bindingType );
369
379
if (FrameworkTypes .isSetValueFrameworkType (bindingType )) {
370
380
report .addError (bindingElements (
371
- "with @IntoSet/@ElementsIntoSet must not %s framework types" ,
372
- bindingElementTypeVerb ()));
381
+ "with @IntoSet/@ElementsIntoSet must not %s framework types: %s " ,
382
+ bindingElementTypeVerb (), XTypes . toStableString ( bindingType ) ));
373
383
}
384
+ checkDisallowedType (bindingType );
374
385
}
375
386
376
387
private void checkMapValueFrameworkType (XType bindingType ) {
377
388
checkKeyType (bindingType );
378
389
if (FrameworkTypes .isMapValueFrameworkType (bindingType )) {
379
390
report .addError (
380
- bindingElements ("with @IntoMap must not %s framework types" , bindingElementTypeVerb ()));
391
+ bindingElements ("with @IntoMap must not %s framework types: %s" ,
392
+ bindingElementTypeVerb (), XTypes .toStableString (bindingType )));
393
+ }
394
+ checkDisallowedType (bindingType );
395
+ }
396
+
397
+ private void checkDisallowedType (XType bindingType ) {
398
+ // TODO(erichang): Consider if we want to go inside complex types to ban
399
+ // dagger.internal.Provider as well? E.g. List<dagger.internal.Provider<Foo>>
400
+ if (FrameworkTypes .isDisallowedType (bindingType )) {
401
+ report .addError (bindingElements ("must not %s disallowed types: %s" ,
402
+ bindingElementTypeVerb (), XTypes .toStableString (bindingElementType ().get ())));
381
403
}
382
404
}
383
405
}
0 commit comments