Chahine
Chahine

Reputation: 265

dagger cannot inject type parameter field

I'm working on an android application and I'm trying to inject a field which is type parameterized in an abstract class : BaseListFragment

public abstract class BaseListFragment<E, A extends ArrayAdapter<E>, S> extends BaseFragment
{
    @Inject protected S service;
}

But I get this following error at compile : error: cannot find symbol class S

Here is my code for BaseFragment :

public class BaseFragment extends Fragment
{
    @Override public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        ((App) getActivity().getApplication()).inject(this);
    }
}

here is my service module :

@Module(
        complete = false,
        library = true
)
public class ServiceModule
{
    @Provides @Singleton BucketService provideBucketService(RestAdapter restAdapter)
    {
        return restAdapter.create(BucketService.class);
    }

    @Provides @Singleton ProjectService provideProjectService(RestAdapter restAdapter)
    {
        return restAdapter.create(ProjectService.class);
    }

    @Provides @Singleton ShotService provideShotService(RestAdapter restAdapter)
    {
        return restAdapter.create(ShotService.class);
    }

    @Provides @Singleton TeamService provideTeamService(RestAdapter restAdapter)
    {
        return restAdapter.create(TeamService.class);
    }

    @Provides @Singleton UserService provideUserService(RestAdapter restAdapter)
    {
        return restAdapter.create(UserService.class);
    }
}

And here is an example of a class extending BaseListFragment :

public class ProjectFragment extends BaseEntityFragment<Project, ProjectViewAdapter, ProjectService>
{
}

Is there anyway to inject a parameterized type ?

Regards,

Upvotes: 8

Views: 5099

Answers (3)

gk5885
gk5885

Reputation: 3762

As of the 2.0.1 release, Dagger 2 does support the type of injection that you're talking about. Take a look at GenericTest for all of the various permutations.

Upvotes: 7

etherton
etherton

Reputation: 942

I had the same problem and got around it by adding a non-parameterized inner class and injecting into that. Then using a getter to get the injected class out.

It looks like this:

public class MainClass<T>{

   UtilityClass utility;

   public MainClass(){
       utility = new InjectorHelper().getHelper();
   }

   ...

   public static class InjectorHelper{

       @Inject
       UtilityClass utilityClass;

       public InjectorHelper(){
           Injector.getInstance().getAppComponent().inject(this);
       }

       public UtilityClass getUtilityClass(){
           return utilityClass
       }
   }
}

Upvotes: 4

Gregor Zeitlinger
Gregor Zeitlinger

Reputation: 439

I also could not get it work either and I think it's against the design philosophy of dagger, which is to generate code that is exactly what a developer would also write.

In this case, it generates an injection adapter for the abstract class and another one for concrete class. With generic arguments to the abstract class, you essentially have to inject the fields from the abstract class in each concrete class injector.

If it wasn't an android object (which the android runtime creates), I'd suggest to pass the service in the call to the super constructor.

In this case, I'd suggest to inject the service in the concrete classes and to provide it using an abstract method:

protected abstract S getService();

Upvotes: 0

Related Questions