Bindings restricted by this annotation may only be created by sources annotated with a permit
from permits -- otherwise, an error message including the explanation is
issued.
There are two kinds of binding source:
ModuleAnnotatedMethodScanner): If a binding was created by a
scanner, then that scanner is also a source of the binding (in addition to the module
sources) and a permit may be given to the scanner by annotating its class.
Bindings with qualifier annotations are restricted solely by the annotation on their qualifier (restrictions on the type are ignored for qualified bindings). Unqualified bindings are restricted by the annotation on their type.
This allows libraries to prevent their clients from binding their keys, similar to how
declaring a class final prevents subtyping. For example, a library may want to prevent users from
creating mock bindings for tests, using the explanation - included in the error message
- to point them to a supported testing module.
Example usage:
@RestrictedBindingSource.Permit
@Retention(RetentionPolicy.RUNTIME)
@interface NetworkPermit {}
@RestrictedBindingSource(
explanation = "Only NetworkModule can create network bindings.",
permits = {NetworkPermit.class})
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface GatewayIpAdress {}
@NetworkPermit
public final class NetworkModule extends AbstractModule {
// Allowed because the module is annotated with @NetworkPermit.
@Provides
@GatewayIpAdress
int provideGatewayIp() { ... }
}
| Modifier and Type | Class and Description |
|---|---|
| public static @interface | RestrictedBindingSource.
Meta-annotation indicating that the target annotation is a permit for binding restricted bindings. |
| public static enum | RestrictedBindingSource.
Level of restriction. |
| Modifier and Type | Method and Description |
|---|---|
| public String | |
| public String | |
| public Class | permits()
List of |
| public RestrictedBindingSource. |
| exemptModules | back to summary |
|---|---|
| public String exemptModules() Exempt modules whose fully qualified class names match this regex. If any module on the binding's module stack matches this regex, the binding is allowed (no permit necessary). No module is exempt by default (empty string). Inteded to be used when retrofitting a binding with this restriction. When restricting an existing binding, it's often practical to first restrict with exemptions for existing violations (to prevent new violations), before updating the code in violation to use the permitted module(s). | |
| explanation | back to summary |
|---|---|
| public String explanation() Explanation of why binding this target type is restricted. Will appear as the error message if the target type is bound by non-allowed modules. | |
| permits | back to summary |
|---|---|
| public Class List of | |
| restrictionLevel | back to summary |
|---|---|
| public RestrictedBindingSource. | |
permits).
| Modifier and Type | Field and Description |
|---|---|
| public static final RestrictedBindingSource. | |
| public static final RestrictedBindingSource. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static RestrictedBindingSource. | |
| public static RestrictedBindingSource. |
| ERROR | back to summary |
|---|---|
| public static final RestrictedBindingSource. | |
| WARNING | back to summary |
|---|---|
| public static final RestrictedBindingSource. | |
| RestrictionLevel | back to summary |
|---|---|
| private RestrictionLevel() | |
| valueOf | back to summary |
|---|---|
| public static RestrictedBindingSource. | |
| values | back to summary |
|---|---|
| public static RestrictedBindingSource. | |