Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

like image 333
Chahine Avatar asked Jan 22 '15 00:01

Chahine


People also ask

What is inject annotation in dagger?

Dagger constructs instances of your application classes and satisfies their dependencies. It uses the javax. inject. Inject annotation to identify which constructors and fields it is interested in. Use @Inject to annotate the constructor that Dagger should use to create instances of a class.

How do you inject value at run time in dagger?

Inject values at runtime with UI in Dagger2:pureMathModule("Book Name") . build() . inject(this); The difference between DaggerComponent create() and in build() is - create() works when no runtime argument is passed into the constructor, else we use build() method.

What is Dagger dependency injection?

Dagger is a fully static, compile-time dependency injection framework for Java, Kotlin, and Android. It is an adaptation of an earlier version created by Square and now maintained by Google.

Why to use Dagger in Android?

Dagger automatically generates code that mimics the code you would otherwise have hand-written. Because the code is generated at compile time, it's traceable and more performant than other reflection-based solutions such as Guice. Note: Use Hilt for dependency injection on Android.


1 Answers

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.

like image 93
gk5885 Avatar answered Sep 23 '22 14:09

gk5885