Question

I have just unwittingly walked into a generics hole and hit a Dagger object graph validation error (I assume it was the dagger-compile validation but no specific error message was generated - just maven's pretty BUILD FAILURE message). I am attempting to inject an implementation of a wildcard type. I'm a bit at a loss as to what I should read-up on to better understand the problem I created so here is the code;

@Inject Provider<MarkerOption<?>> markerOptionProvider;

with module definition of;

  @Provides MarkerOptions<?> provideMarkerOptions() {
    MarkerOptions<?> options;
    if (ConnectionResult.SUCCESS == GooglePlayServicesUtil.isGooglePlayServicesAvailable(context)) {
      // This is the Google maps MarkerOption impl
      options = new GoogleMapMarkerOptions(new com.google.android.gms.maps.model.MarkerOptions());
    } else {
      // This is the osmdroid impl of MarkerOptions
      options = new OsmDroidMarkerOptions();
    }

    return options;
  }

I'm guessing the code smell here is the Provider injection but I'm interested to better understand the issue the dagger-compiler ran into when attempting to resolve the implementation. Notably, removing the wildcard generic <?> allowed Dagger to calculate the object graph needed for the application to function. I was expecting the injection of the MarkerOptions provider would have leveraged the module MarkerOptions binding regardless of the interface type.

If anyone could explain back to me what went wrong/why I should not have gotten this far, then that would be most appreciated.

Était-ce utile?

La solution

Dagger currently does not support wildcard types in the injection. You need to inject a concrete type, or a raw type (as you found).

In theory simple wildcards could be supported, so long as what was provided was Foo<?> and what was injected was Foo<?> - matching partially would be prohibitive.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top