forked from google/guice
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for ProvisionListeners to notify on toInstance & constant…
… bindings. --------------------- Manually synced. COMMIT=41634417
- Loading branch information
Showing
13 changed files
with
154 additions
and
37 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
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 |
---|---|---|
|
@@ -18,22 +18,33 @@ | |
|
||
import com.google.common.base.Function; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableSet; | ||
import com.google.common.collect.Lists; | ||
import com.google.common.collect.MapMaker; | ||
import com.google.inject.Binding; | ||
import com.google.inject.Injector; | ||
import com.google.inject.Key; | ||
import com.google.inject.Stage; | ||
import com.google.inject.spi.ProvisionListener; | ||
import com.google.inject.spi.ProvisionListenerBinding; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* {@link ProvisionListenerStackCallback} for each key. | ||
* | ||
* @author [email protected] (Sam Berlin) | ||
*/ | ||
final class ProvisionListenerCallbackStore { | ||
|
||
// TODO(sameb): Consider exposing this in the API somehow? Maybe? | ||
// Lots of code often want to skip over the internal stuffs. | ||
private static final Set<Key<?>> INTERNAL_BINDINGS = | ||
ImmutableSet.of(Key.get(Injector.class), Key.get(Stage.class), Key.get(Logger.class)); | ||
|
||
private final ImmutableList<ProvisionListenerBinding> listenerBindings; | ||
|
||
private final Map<KeyBinding, ProvisionListenerStackCallback<?>> cache | ||
|
@@ -52,7 +63,12 @@ public ProvisionListenerStackCallback<?> apply(KeyBinding key) { | |
*/ | ||
@SuppressWarnings("unchecked") // the ProvisionListenerStackCallback type always agrees with the passed type | ||
public <T> ProvisionListenerStackCallback<T> get(Binding<T> binding) { | ||
return (ProvisionListenerStackCallback<T>) cache.get(new KeyBinding(binding.getKey(), binding)); | ||
// Never notify any listeners for internal bindings. | ||
if (!INTERNAL_BINDINGS.contains(binding.getKey())) { | ||
return (ProvisionListenerStackCallback<T>) cache.get( | ||
new KeyBinding(binding.getKey(), binding)); | ||
} | ||
return ProvisionListenerStackCallback.emptyListener(); | ||
} | ||
|
||
/** | ||
|
@@ -82,8 +98,10 @@ private <T> ProvisionListenerStackCallback<T> create(Binding<T> binding) { | |
listeners.addAll(provisionBinding.getListeners()); | ||
} | ||
} | ||
if (listeners == null) { | ||
listeners = ImmutableList.of(); | ||
if (listeners == null || listeners.isEmpty()) { | ||
// Optimization: don't bother constructing the callback if there are | ||
// no listeners. | ||
return ProvisionListenerStackCallback.emptyListener(); | ||
} | ||
return new ProvisionListenerStackCallback<T>(binding, listeners); | ||
} | ||
|
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
Oops, something went wrong.