Skip to content

Commit

Permalink
Have ProviderLookups and MemberInjectorLookups create their depen…
Browse files Browse the repository at this point in the history
…dent bindings early and satisfy delegates later on during initialization

This ensures that other phases can see the jit bindings they create, notably this is important for `OptionalBinder` which queries for bindings using `getExistingBinding`

This will allow us to optimize the way `Provider` instances are created in a later change.

PiperOrigin-RevId: 720621327
  • Loading branch information
lukesandberg authored and Guice Team committed Jan 28, 2025
1 parent a376396 commit 2a17feb
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 9 deletions.
2 changes: 2 additions & 0 deletions core/src/com/google/inject/internal/DeferredLookups.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ final class DeferredLookups implements Lookups {

/** Initialize the specified lookups, either immediately or when the injector is created. */
void initialize(Errors errors) {
// Since we are initializing the lookups, we can now use the injector as the lookups.
injector.lookups = injector;
new LookupBindingProcessor(errors).process(injector, lookups);
new LookupProcessor(errors).process(injector, lookups);
}

Expand Down
4 changes: 4 additions & 0 deletions core/src/com/google/inject/internal/InjectorShell.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ List<InjectorShell> build(
new BindingProcessor(errors, initializer, processedBindingData).process(injector, elements);
new UntargettedBindingProcessor(errors, processedBindingData).process(injector, elements);
stopwatch.resetAndLog("Binding creation");
// lookups can create jit bindings, so do that early, but after untargeted bindings so that
// we leverage those bindings if they exist.
new LookupBindingProcessor(errors).process(injector, elements);
stopwatch.resetAndLog("lookup binding creation");

new ModuleAnnotatedMethodScannerProcessor(errors).process(injector, elements);
stopwatch.resetAndLog("Module annotated method scanners creation");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,15 @@ private void initializeStatically() {
initializer.validateOustandingInjections(errors);
stopwatch.resetAndLog("Instance member validation");

processedBindingData.initializeDelayedBindings();
stopwatch.resetAndLog("Delayed Binding initialization");

new LookupProcessor(errors).process(shells);
for (InjectorShell shell : shells) {
((DeferredLookups) shell.getInjector().lookups).initialize(errors);
}
stopwatch.resetAndLog("Provider verification");

// This needs to come late since some user bindings rely on requireBinding calls to create
// jit bindings during the LookupProcessor.
processedBindingData.initializeDelayedBindings();
stopwatch.resetAndLog("Delayed Binding initialization");

for (InjectorShell shell : shells) {
if (!shell.getElements().isEmpty()) {
Expand Down
58 changes: 58 additions & 0 deletions core/src/com/google/inject/internal/LookupBindingProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.inject.internal;

import com.google.inject.internal.InjectorImpl.JitLimitation;
import com.google.inject.spi.MembersInjectorLookup;
import com.google.inject.spi.ProviderLookup;

/** Processes just MembersInjectorLookups and ProviderLookups to create their bindings. */
final class LookupBindingProcessor extends AbstractProcessor {

LookupBindingProcessor(Errors errors) {
super(errors);
}

@Override
public <T> Boolean visit(MembersInjectorLookup<T> lookup) {
injector.getBindingData().putMembersInjectorLookup(lookup);
// Members injector lookups are resolved by the lookup processor but may require jit bindings to
// be created, do that now, so that other phases can see them.
try {
var unused = injector.membersInjectorStore.get(lookup.getType(), errors);
} catch (ErrorsException e) {
errors.merge(e.getErrors());
}
return false; // leave the lookups for the LookupProcessor to handle
}

@Override
public <T> Boolean visit(ProviderLookup<T> lookup) {
injector.getBindingData().putProviderLookup(lookup);
// Provider lookups are resolved by the lookup processor but may require jit bindings to be
// created, do that now, so that other phases can see them.
try {
var unused = injector.getBindingOrThrow(lookup.getKey(), errors, JitLimitation.NO_JIT);
// ProviderLookups need a Provider but we cannot create it yet since it is too early in the
// processing.
} catch (ErrorsException e) {
errors.merge(e.getErrors());
}

return false; // leave the lookups for the LookupProcessor to handle
}
}
5 changes: 2 additions & 3 deletions core/src/com/google/inject/internal/LookupProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import com.google.inject.spi.ProviderLookup;

/**
* Handles {@code Binder.getProvider} and {@code Binder.getMembersInjector(TypeLiteral)} commands.
* Satisfies delegates for {@code Binder.getProvider} and {@code
* Binder.getMembersInjector(TypeLiteral)} commands.
*
* @author crazybob@google.com (Bob Lee)
* @author jessewilson@google.com (Jesse Wilson)
Expand All @@ -39,7 +40,6 @@ public <T> Boolean visit(MembersInjectorLookup<T> lookup) {
MembersInjector<T> membersInjector =
injector.membersInjectorStore.get(lookup.getType(), errors);
lookup.initializeDelegate(membersInjector);
injector.getBindingData().putMembersInjectorLookup(lookup);
} catch (ErrorsException e) {
errors.merge(e.getErrors()); // TODO: source
}
Expand All @@ -53,7 +53,6 @@ public <T> Boolean visit(ProviderLookup<T> lookup) {
try {
Provider<T> provider = injector.getProviderOrThrow(lookup.getDependency(), errors);
lookup.initializeDelegate(provider);
injector.getBindingData().putProviderLookup(lookup);
} catch (ErrorsException e) {
errors.merge(e.getErrors()); // TODO: source
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Guice configuration errors:
1) [Guice/ChildBindingAlreadySet]: Unable to create binding for ChildBindingAlreadySetErrorTest$DependsOnFoo because it was already configured on one or more child injectors or private modules.

Bound at:
1 : ChildBindingAlreadySetErrorTest$ChildModule4.configure(ChildBindingAlreadySetErrorTest.java:105)
2 : as a just-in-time binding
1 : as a just-in-time binding
2 : ChildBindingAlreadySetErrorTest$ChildModule4.configure(ChildBindingAlreadySetErrorTest.java:105)

1 error

Expand Down

0 comments on commit 2a17feb

Please sign in to comment.