public interface ConfigurationBuilder
Implementations of this class type check the bindings against an underlying ClassHierarchy implementation. Typically, the backing ClassHierarchy will be delegate to the default classloader (the one that loaded the code that is invoking Tang), though other scenarios are possible. For instance, the ClassHierarchy could incorporate additional Jars, or it might not delegate to the default classloader at all. In fact, Tang supports ClassHierarchy objects that are derived from reflection data from other languages, such as C#. This enables cross-language injection sessions, where Java code configures C# code, or vice versa.
When possible, the methods in this interface eagerly check for these errors. Methods that check for configuration and other runtime or application-level errors are declared to throw BindException.
Furthermore, all methods in Tang, may throw RuntimeException if they encounter inconsistencies in the underlying ClassHierarchy. Such errors reflect problems that existed when the application was compiled or packaged, and cannot be corrected at runtime. Examples include inconsistent type hierarchies (such as version mismatches between jars), and unparsable default values (such as an int that defaults to "false" or "five"). These exceptions are analogous to the runtime exceptions thrown by the Java classloader; other than logging them or reporting them to an end user, applications have little recourse when such problems are encountered.
for convenience methods that assume the
underlying ClassHierarchy object delegates to the default
classloader, and enable many compile time static checks.
,
which pushes additional type checks to class load
time. This allows Tint, Tang's static analysis tool, to detect a wide
range of runtime configuration errors at build time.
Modifier and Type | Method and Description |
---|---|
void |
addConfiguration(Configuration c)
Add all configuration parameters from the given Configuration object.
|
void |
bind(Node iface,
Node impl)
Bind classes to each other, based on their full class names; alternatively,
bound a NamedParameter configuration option to a configuration value.
|
<T> void |
bind(String iface,
String impl)
Bind classes to each other, based on their full class names; alternatively,
bound a NamedParameter configuration option to a configuration value.
|
<T> void |
bindConstructor(ClassNode<T> iface,
ClassNode<? extends ExternalConstructor<? extends T>> impl)
Register an ExternalConstructor implementation with Tang.
|
<T> void |
bindList(NamedParameterNode<List<T>> iface,
List implList)
Bind an list of implementations(Class or String) to an given NamedParameter.
|
void |
bindList(String iface,
List implList) |
<T> void |
bindSetEntry(NamedParameterNode<Set<T>> iface,
Node impl) |
<T> void |
bindSetEntry(NamedParameterNode<Set<T>> iface,
String impl) |
void |
bindSetEntry(String iface,
Node impl) |
void |
bindSetEntry(String iface,
String impl) |
Configuration |
build()
Produce an immutable Configuration object that contains the current
bindings and ClassHierarchy of this ConfigurationBuilder.
|
String |
classPrettyDefaultString(String longName)
Pretty print the default implementation / value of the provided class / NamedParameter.
|
String |
classPrettyDescriptionString(String longName)
Pretty print the human readable documentation of the provided class / NamedParameter.
|
ClassHierarchy |
getClassHierarchy()
Each ConfigurationBuilder instance is associated with a ClassHierarchy.
|
void |
registerLegacyConstructor(ClassNode<?> cn,
ClassNode<?>... args)
Force Tang to treat the specified constructor as though it had an @Inject
annotation.
|
void |
registerLegacyConstructor(ClassNode<?> c,
ConstructorArg... args)
Force Tang to treat the specified constructor as though it had an @Inject
annotation.
|
void |
registerLegacyConstructor(String cn,
String... args)
Force Tang to treat the specified constructor as though it had an @Inject
annotation.
|
void addConfiguration(Configuration c) throws BindException
c
- the configuration to be addedBindException
ClassHierarchy getClassHierarchy()
void registerLegacyConstructor(ClassNode<?> cn, ClassNode<?>... args) throws BindException
This method takes ClassNode objects. Like all of the methods in this API, the ClassNode objects must come from the ClassHierarchy instance returned by getClassHierarchy().
cn
- The class the constructor instantiates.args
- The types of the arguments taken by the constructor, in declaration order.BindException
- if the constructor does not exist, or if it has already been bound as a legacy constructor.void registerLegacyConstructor(String cn, String... args) throws BindException
cn
- The full name of the class the constructor instantiates.args
- The full names of the types of the arguments taken by the constructor, in declaration order.BindException
- if the constructor does not exist, or if it has already been bound as a legacy constructor.void registerLegacyConstructor(ClassNode<?> c, ConstructorArg... args) throws BindException
This method takes ClassNode and ConstructorArg objects. Like all of the methods in this API, these objects must come from the ClassHierarchy instance returned by getClassHierarchy().
c
- The class the constructor instantiates.args
- The parsed ConstructorArg objects corresponding to the types of the arguments taken by the constructor,
in declaration order.BindException
- if the constructor does not exist, or if it has already been bound as a legacy constructor.<T> void bind(String iface, String impl) throws BindException
T
- a typeiface
- The full name of the interface that should resolve to impl,
or the NamedParameter to be set.impl
- The full name of the implementation that will be used in
place of impl, or the value the NamedParameter should be set to.BindException
- If (In the case of interfaces and implementations)
the underlying ClassHierarchy does not recognise iface and
impl as known, valid classes, or if impl is not a in
implementation of iface, or (in the case of NamedParameters
and values) if iface is not a NamedParameter, or if impl
fails to parse as the type the iface expects.void bind(Node iface, Node impl) throws BindException
This method takes Node objects. Like all of the methods in this API, these objects must come from the ClassHierarchy instance returned by getClassHierarchy().
iface
- The interface / NamedParameter to be bound.impl
- The implementation / value iface should be set to.BindException
- if there is a type checking error
See bind(String,String)
for a more complete description.<T> void bindConstructor(ClassNode<T> iface, ClassNode<? extends ExternalConstructor<? extends T>> impl) throws BindException
To see how the second use case could be useful, consider a implementing a URI interface with a distinct subclass for each valid URI prefix (e.g., http://, ssh://, etc...). An ExternalConstructor could examine the prefix and delegate to a constructor of the correct implementation (e.g, HttpURL, SshURL, etc...) which would validate the remainder of the provided string. URI's external constructor would return the validated subclass of URI that corresponds to the provided string, allowing instanceof and downcasts to behave as expected in the code that invoked Tang.
Both use cases should be avoided when possible, since they can unnecessarily complicate object injections and undermine Tang's ability to statically check a given configuration.
This method takes ClassNode objects. Like all of the methods in this API, these objects must come from the ClassHierarchy instance returned by getClassHierarchy().
T
- a typeiface
- The class or interface to be instantiated.impl
- The ExternalConstructor class that will be used to instantiate iface.BindException
- If impl does not instantiate a subclass of iface.String classPrettyDefaultString(String longName) throws BindException
longName
- a name of class or parameterBindException
- if name resolution failsString classPrettyDescriptionString(String longName) throws BindException
longName
- a name of class or parameterBindException
- if name resolution failsConfiguration build()
Since Tang eagerly checks for configuration errors, this method does not perform any additional validation, and does not throw any checkable exceptions.
<T> void bindSetEntry(NamedParameterNode<Set<T>> iface, Node impl) throws BindException
BindException
<T> void bindSetEntry(NamedParameterNode<Set<T>> iface, String impl) throws BindException
BindException
void bindSetEntry(String iface, String impl) throws BindException
BindException
void bindSetEntry(String iface, Node impl) throws BindException
BindException
<T> void bindList(NamedParameterNode<List<T>> iface, List implList) throws BindException
Since ordering of the list is important, list binding cannot be repeated or merged unlike set binding. If the elements of the list are Classes, the objects created by the Classes will be injected. If the elements are Strings, the values will be injected directly to a given list parameter.
T
- a typeiface
- The list named parameter to be instantiatedimplList
- The list of class or value will be used to instantiated the named parameterBindException
- if implementation class does not fit to the named parametervoid bindList(String iface, List implList) throws BindException
BindException
Copyright © 2016 The Apache Software Foundation. All rights reserved.