google / google/guice

Have access to target injection point from Providers

Open
#948 4 comments 1 reaction 0 assignees View on GitHub
Dominant language
Java
Stars
12.7k
Forks
1.7k
Avg merge
11m
Merged PRs (30d)
2

Description

I find myself often in the situation that what I would like to inject should depend on the values inside BindingAnnotations on the members to be injected. Guice can deal with that when all possible values are known upfront and bound in advance against implementations of the BindingAnnotations that implement equals and hashcode as specified by the JDK APIs.

However, the method of implementing BindingAnnotations and bind them in advance does not work when the possible values are not known a priori. Assisted injection is a (IMO, less elegant) alternative in many such cases, except when the type to be injected is an interface or class coming from a 3rd party library that cannot be modified (in case you wonder, I am having beef with org.apache.http.client.HttpClient).

In other words, I find myself missing (horribly) the feature of accessing Injection Points from @Provides methods that CDI provides ( https://docs.jboss.org/weld/reference/latest/en-US/html/injection.html ). One may also think if it makes sense to provide (eh) a new type of Provider that accepts the InjectionPoint as argument. Alternatively, in order not to introduce another interface with all the likely massive rework it would require in the codebase, if the provider has a field annotated with, say, @Inject@TargetInjectionPoint (where @TargetInjectionPoint would be a binding annotation provided by Guice), that field would be injected with the target Injection Point right before the Providder.get() method is invoked.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.