-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow factories to customize their generated Providers
This enables a few optimizations - A number of implementations can avoid `InternalContext` overheads addressing #1802 - `ConstantBindings`, one time bindings like `Initializable` and `@Singleton` bound values, and a number of multibinder bindings in particular. - We can leverage the cache within `BindingImpl` to share provider instances. The tricky part is about multibindings, which have complex multi-phase initialization semantics, so we need to be defensive about `makeProvider` getting called prior to being initialized. To help manage this I * reordered the `LookupProcessor` to run _after_ multibinders. - The comment justifying its current location in initialization is obsolete since multibinders were rewritten to be 'native'. - This ensures lookups get optimized providers! * made the `SyntheticProviderFactory` allocate its delegate lazily. * make `OptionalBinder` explicitly initialize their delegate bindings and also be defensive against recursive and duplicate initialization. This is necessary to force initialization to be correct when our `getExistingBinding` query ends up delegating to another multibinder. * be defensive about initialization failing to complete due to errors PiperOrigin-RevId: 714766545
- Loading branch information
1 parent
54f53e8
commit 35a1b49
Showing
19 changed files
with
678 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,23 +17,54 @@ | |
package com.google.inject.internal; | ||
|
||
import com.google.common.base.MoreObjects; | ||
import com.google.inject.Provider; | ||
import com.google.inject.spi.Dependency; | ||
|
||
/** @author [email protected] (Bob Lee) */ | ||
/** | ||
* @author [email protected] (Bob Lee) | ||
*/ | ||
final class ConstantFactory<T> implements InternalFactory<T> { | ||
private static <T> InternalFactory<T> nullFactory(Object source) { | ||
return new InternalFactory<T>() { | ||
@Override | ||
public T get(InternalContext context, Dependency<?> dependency, boolean linked) | ||
throws InternalProvisionException { | ||
if (!dependency.isNullable()) { | ||
InternalProvisionException.onNullInjectedIntoNonNullableDependency(source, dependency); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public Provider<T> makeProvider(InjectorImpl injector, Dependency<?> dependency) { | ||
return InternalFactory.makeProviderForNull(source, this, dependency); | ||
} | ||
}; | ||
} | ||
|
||
private final T instance; | ||
|
||
public ConstantFactory(T instance) { | ||
static <T> InternalFactory<T> create(T instance, Object source) { | ||
if (instance == null) { | ||
return nullFactory(source); | ||
} | ||
return new ConstantFactory<>(instance); | ||
} | ||
|
||
private ConstantFactory(T instance) { | ||
this.instance = instance; | ||
} | ||
|
||
@Override | ||
public T get(InternalContext context, Dependency<?> dependency, boolean linked) | ||
throws InternalProvisionException { | ||
public T get(InternalContext context, Dependency<?> dependency, boolean linked) { | ||
return instance; | ||
} | ||
|
||
@Override | ||
public Provider<T> makeProvider(InjectorImpl injector, Dependency<?> dependency) { | ||
return InternalFactory.makeProviderFor(instance, this); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return MoreObjects.toStringHelper(ConstantFactory.class).add("value", instance).toString(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.