Top Description Inners Fields Constructors Methods
com.google.inject.spi

public final Class BindingSourceRestriction

extends Object
Class Inheritance
Static Imports
com.google.common.base.Preconditions.checkNotNull, java.util.stream.Collectors.toList

Contains abstractions for enforcing RestrictedBindingSource.

Enforcement happens in two phases:

  1. Data structures for enforcement are built during Binder configuration. PermitMapConstruction encapsulates this process, and the PermitMap is the end result.
  2. Restrictions are enforced by checking each binding for violations with check, which uses the PermitMap(s) built during Binder configuration.

Note

None of this is thread-safe because it's only used while the Injector is being built, which happens on a single thread.

Author
[email protected] (Vladimir Makaric)
Since
5.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static interface
BindingSourceRestriction.PermitMap

Mapping between an element source and its permit annotations.

pack-priv static class
BindingSourceRestriction.PermitMapConstruction

Builds the map from each module to all the permit annotations on its module stack.

Field Summary

Modifier and TypeField and Description
private static final Logger

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static ImmutableList<Message>
check(GuiceInternal guiceInternal, List<Element> elements)

Returns all the restriction violations found on the given Module Elements, as error messages.

private static ImmutableList<Message>
check(List<Element> elements)

private static ImmutableList<Message>
check(Element element)

private static Optional<Message>
check(Binding<?> binding)

private static void
clear(Element element)

private static void
clear(ElementSource elementSource)

private static Iterable<String>
private static ImmutableSet<Class<? extends Annotation>>
getAllPermits(ElementSource elementSource)

Get all permits on the element source chain.

public static Optional<String>
getMissingImplementationSuggestion(GuiceInternal guiceInternal, Key<?> key)

Returns a suggestion for how a restricted binding should be created in case it's missing.

private static Stream<Class<? extends Annotation>>
getPermits(Class<?> clazz)

private static RestrictedBindingSource
private static String
getViolationMessage(Key<?> key, String explanation, ImmutableSet<Class<? extends Annotation>> acceptablePermits, boolean annotationRestricted)

private static boolean
isExempt(ElementSource elementSource, String exemptModulesRegex)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

loggerback to summary
private static final Logger logger

Constructor Detail

BindingSourceRestrictionback to summary
private BindingSourceRestriction()

Method Detail

checkback to summary
public static ImmutableList<Message> check(GuiceInternal guiceInternal, List<Element> elements)

Returns all the restriction violations found on the given Module Elements, as error messages.

Note: Intended to be used on Module Elements, not Injector Elements, ie. the result of Elements#getElements not Injector.getElements. The Module Elements this check cares about are:

  • Module Bindings, which are always explicit and always have an ElementSource (with a Module Stack), unlike Injector Bindings, which may be implicit and bereft of an ElementSource.
  • PrivateElements, which represent the recursive case of this check. They contain a list of elements that this check is recursively called on.
checkback to summary
private static ImmutableList<Message> check(List<Element> elements)
checkback to summary
private static ImmutableList<Message> check(Element element)
checkback to summary
private static Optional<Message> check(Binding<?> binding)
clearback to summary
private static void clear(Element element)
clearback to summary
private static void clear(ElementSource elementSource)
getAllModulesback to summary
private static Iterable<String> getAllModules(ElementSource elementSource)
getAllPermitsback to summary
private static ImmutableSet<Class<? extends Annotation>> getAllPermits(ElementSource elementSource)

Get all permits on the element source chain.

getMissingImplementationSuggestionback to summary
public static Optional<String> getMissingImplementationSuggestion(GuiceInternal guiceInternal, Key<?> key)

Returns a suggestion for how a restricted binding should be created in case it's missing.

getPermitsback to summary
private static Stream<Class<? extends Annotation>> getPermits(Class<?> clazz)
getRestrictionback to summary
private static RestrictedBindingSource getRestriction(Key<?> key)
getViolationMessageback to summary
private static String getViolationMessage(Key<?> key, String explanation, ImmutableSet<Class<? extends Annotation>> acceptablePermits, boolean annotationRestricted)
isExemptback to summary
private static boolean isExempt(ElementSource elementSource, String exemptModulesRegex)
com.google.inject.spi back to summary

pack-priv Interface BindingSourceRestriction.PermitMap

Known Direct Implementers
com.google.inject.spi.BindingSourceRestriction.PermitMapConstruction.PermitMapImpl

Mapping between an element source and its permit annotations.

Method Summary

Modifier and TypeMethod and Description
public void
public ImmutableSet<Class<? extends Annotation>>
getPermits(ElementSource elementSource)

Method Detail

clearback to summary
public void clear()
getPermitsback to summary
public ImmutableSet<Class<? extends Annotation>> getPermits(ElementSource elementSource)
com.google.inject.spi back to summary

pack-priv final Class BindingSourceRestriction.PermitMapConstruction

extends Object
Class Inheritance
  • java.lang.Object
  • com.google.inject.spi.BindingSourceRestriction.PermitMapConstruction

Builds the map from each module to all the permit annotations on its module stack.

Bindings refer to the module that created them via a ModuleSource. The map built here maps a module's ModuleSource to all the RestrictedBindingSource.Permit annotations found on the path from the root of the module hierarchy to it. This path contains all the modules that transitively install the module (including the module itself). This path is also known as the module stack.

The map is built by piggybacking on the depth-first traversal of the module hierarchy during Binder configuration.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
pack-priv ImmutableSet<Class<? extends Annotation>>
pack-priv final Map<ModuleSource, ImmutableSet<Class<? extends Annotation>>>
pack-priv final Deque<ImmutableSet<Class<? extends Annotation>>>
pack-priv final BindingSourceRestriction.PermitMapConstruction.PermitMapImpl

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv void
finish()

Finishes the PermitMap.

pack-priv BindingSourceRestriction.PermitMap
getPermitMap()

Returns a possibly unfinished map.

pack-priv static boolean
pack-priv void
popModule()

Called by the Binder when it exits a module's configure method.

pack-priv void
pushModule(Class<?> module, ModuleSource moduleSource)

Called by the Binder prior to entering a module's configure method.

pack-priv void
restoreCurrentModulePermits(ModuleSource moduleSource)

Sets the permits on the current module installation path to the permits on the given module source so that subsequently installed modules may inherit them.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

currentModulePermitsback to summary
pack-priv ImmutableSet<Class<? extends Annotation>> currentModulePermits
modulePermitsback to summary
pack-priv final Map<ModuleSource, ImmutableSet<Class<? extends Annotation>>> modulePermits
modulePermitsStackback to summary
pack-priv final Deque<ImmutableSet<Class<? extends Annotation>>> modulePermitsStack
permitMapback to summary
pack-priv final BindingSourceRestriction.PermitMapConstruction.PermitMapImpl permitMap

Constructor Detail

PermitMapConstructionback to summary
pack-priv PermitMapConstruction()

Method Detail

finishback to summary
pack-priv void finish()

Finishes the PermitMap. Called by the Binder when all modules are installed.

getPermitMapback to summary
pack-priv BindingSourceRestriction.PermitMap getPermitMap()

Returns a possibly unfinished map. The map should only be used after the construction is finished.

isElementSourceClearedback to summary
pack-priv static boolean isElementSourceCleared(ElementSource elementSource)
Annotations
@VisibleForTesting
popModuleback to summary
pack-priv void popModule()

Called by the Binder when it exits a module's configure method.

pushModuleback to summary
pack-priv void pushModule(Class<?> module, ModuleSource moduleSource)

Called by the Binder prior to entering a module's configure method.

restoreCurrentModulePermitsback to summary
pack-priv void restoreCurrentModulePermits(ModuleSource moduleSource)

Sets the permits on the current module installation path to the permits on the given module source so that subsequently installed modules may inherit them. Used only for method scanning, so that modules installed by scanners inherit permits from the method's module.

com.google.inject.spi back to summary

private final Class BindingSourceRestriction.PermitMapConstruction.PermitMapImpl

extends Object
implements PermitMap
Class Inheritance
  • java.lang.Object
  • com.google.inject.spi.BindingSourceRestriction.PermitMapConstruction.PermitMapImpl
All Implemented Interfaces
com.google.inject.spi.BindingSourceRestriction.PermitMap

Field Summary

Modifier and TypeField and Description
pack-priv Map<ModuleSource, ImmutableSet<Class<? extends Annotation>>>

Constructor Summary

AccessConstructor and Description
private

Field Detail

modulePermitsback to summary
pack-priv Map<ModuleSource, ImmutableSet<Class<? extends Annotation>>> modulePermits

Constructor Detail

PermitMapImplback to summary
private PermitMapImpl()

Method Detail

clearback to summary
public void clear()

Implements com.google.inject.spi.BindingSourceRestriction.PermitMap.clear.

Annotations
@Override
getPermitsback to summary
public ImmutableSet<Class<? extends Annotation>> getPermits(ElementSource elementSource)

Implements com.google.inject.spi.BindingSourceRestriction.PermitMap.getPermits.

Annotations
@Override