org.directwebremoting.guice.util
Class Providers

java.lang.Object
  extended by org.directwebremoting.guice.util.Providers

public class Providers
extends java.lang.Object

Utilities for creating ad hoc providers. These come in two flavors, constructor and factory method. In both cases, you can specify additional methods to inject after creation by chaining calls to injecting(methodName, paramKeys). Each method has two variants, one with a Binder parameter and one without. The former reports construction errors via the binder (so more than one error can be reported), the latter throws a runtime exception immediately, preventing further error reporting.

Some examples:

   bind(SomeInterface.class)
       .toProvider(
           .fromConstructor(LegacyImpl.class,
               Key.get(String.class, MyAnnotation.class))
           .injecting("configure", Key.get(Configuration.class)));
 
This binds SomeInterface to a provider that uses the LegacyImpl constructor with a single String parameter. That parameter is injected as if it had been marked with @MyAnnotation. It also calls the LegacyImpl.configure method with an injected instance of Configuration.
   bind(SomeInterface.class)
       .toProvider(
           .fromFactoryMethod(SomeInterface.class,
               LegacyFactory.class, "newSomeInterface",
               Key.get(String.class, MyAnnotation.class)));
 
This binds SomeInterface to a provider that calls a factory method newSomeInterface of LegacyFactory with a single string parameter, which is injected as if it were marked with @MyAnnotation. If the method is not static, an instance of LegacyFactory is created by injection and used to call the method.

Author:
Tim Peierls [tim at peierls dot net]

Nested Class Summary
static interface Providers.BindingProvider<T>
          For fluent-style decoration with one or more method bindings when using fromConstructor(Class, Key...).
 
Method Summary
static
<T> Providers.BindingProvider<T>
fromConstructor(com.google.inject.Binder binder, java.lang.Class<T> type, com.google.inject.Key<?>... keys)
          Creates a chainable provider that constructs an instance of the given type given a list of constructor parameter types, specified as Guice keys.
static
<T> Providers.BindingProvider<T>
fromConstructor(java.lang.Class<T> type, com.google.inject.Key<?>... keys)
          Creates a chainable provider that constructs an instance of the given type given a list of constructor parameter types, specified as Guice keys.
static
<T> Providers.BindingProvider<T>
fromFactoryMethod(com.google.inject.Binder binder, java.lang.Class<T> providedType, java.lang.Class<?> factoryType, java.lang.String methodName, com.google.inject.Key<?>... keys)
          Creates a chainable provider that constructs an instance of providedType using a factory method defined by factoryType, methodName, and a list of method parameter types specified as Guice keys.
static
<T> Providers.BindingProvider<T>
fromFactoryMethod(com.google.inject.Binder binder, java.lang.Class<T> providedType, com.google.inject.Key<?> factoryKey, java.lang.String methodName, com.google.inject.Key<?>... keys)
          Creates a chainable provider that constructs an instance of providedType by calling method methodName of the type in factoryKey with method parameter types specified as Guice keys.
static
<T> Providers.BindingProvider<T>
fromFactoryMethod(java.lang.Class<T> providedType, java.lang.Class<?> factoryType, java.lang.String methodName, com.google.inject.Key<?>... keys)
          Creates a chainable provider that constructs an instance of providedType using a factory method defined by factoryType, methodName, and a list of method parameter types specified as Guice keys.
static
<T> Providers.BindingProvider<T>
fromFactoryMethod(java.lang.Class<T> providedType, com.google.inject.Key<?> factoryKey, java.lang.String methodName, com.google.inject.Key<?>... keys)
          Creates a chainable provider that constructs an instance of providedType by calling method methodName of the type in factoryKey with method parameter types specified as Guice keys.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

fromConstructor

public static <T> Providers.BindingProvider<T> fromConstructor(java.lang.Class<T> type,
                                                               com.google.inject.Key<?>... keys)
Creates a chainable provider that constructs an instance of the given type given a list of constructor parameter types, specified as Guice keys. Construction errors are thrown immediately.


fromConstructor

public static <T> Providers.BindingProvider<T> fromConstructor(com.google.inject.Binder binder,
                                                               java.lang.Class<T> type,
                                                               com.google.inject.Key<?>... keys)
Creates a chainable provider that constructs an instance of the given type given a list of constructor parameter types, specified as Guice keys. Construction errors are passed to binder to be thrown at the end of all binding.


fromFactoryMethod

public static <T> Providers.BindingProvider<T> fromFactoryMethod(java.lang.Class<T> providedType,
                                                                 java.lang.Class<?> factoryType,
                                                                 java.lang.String methodName,
                                                                 com.google.inject.Key<?>... keys)
Creates a chainable provider that constructs an instance of providedType using a factory method defined by factoryType, methodName, and a list of method parameter types specified as Guice keys. If the method is not static an instance of the factory type is created and injected. Construction errors are thrown immediately.


fromFactoryMethod

public static <T> Providers.BindingProvider<T> fromFactoryMethod(com.google.inject.Binder binder,
                                                                 java.lang.Class<T> providedType,
                                                                 java.lang.Class<?> factoryType,
                                                                 java.lang.String methodName,
                                                                 com.google.inject.Key<?>... keys)
Creates a chainable provider that constructs an instance of providedType using a factory method defined by factoryType, methodName, and a list of method parameter types specified as Guice keys. If the method is not static an instance of the factory type is created and injected. Construction errors are passed to binder to be thrown at the end of all binding.


fromFactoryMethod

public static <T> Providers.BindingProvider<T> fromFactoryMethod(java.lang.Class<T> providedType,
                                                                 com.google.inject.Key<?> factoryKey,
                                                                 java.lang.String methodName,
                                                                 com.google.inject.Key<?>... keys)
Creates a chainable provider that constructs an instance of providedType by calling method methodName of the type in factoryKey with method parameter types specified as Guice keys. If the method is not static an instance is created and injected using the factory key. Construction errors are thrown immediately.


fromFactoryMethod

public static <T> Providers.BindingProvider<T> fromFactoryMethod(com.google.inject.Binder binder,
                                                                 java.lang.Class<T> providedType,
                                                                 com.google.inject.Key<?> factoryKey,
                                                                 java.lang.String methodName,
                                                                 com.google.inject.Key<?>... keys)
Creates a chainable provider that constructs an instance of providedType by calling method methodName of the type in factoryKey with method parameter types specified as Guice keys. If the method is not static an instance is created and injected using the factory key. Construction errors are passed to binder to be thrown at the end of all binding.


Copyright 2008