All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cerner.beadledom.guice.dynamicbindings.DynamicAnnotations Maven / Gradle / Ivy

There is a newer version: 4.1.2
Show newest version

package com.cerner.beadledom.guice.dynamicbindings;

import com.google.inject.Binder;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import com.google.inject.util.Types;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;

/**
 * Static convenience methods for creating {@link DynamicBindingProvider} instances.
 *
 * @author John Leacox
 * @see DynamicBindingProvider
 * @since 1.2
 */
class DynamicAnnotations {
  private DynamicAnnotations() {
  }

  /**
   * Binds a {@link DynamicBindingProvider} for the specified class.
   *
   * 

The instance bound to the class can later be retrieved through * {@link DynamicBindingProvider#get(Class)} given the same annotation provided during binding * time. This method also 'requires' a binding of {@code clazz} to {@code annotation} to exist. * *

This method requires a binder, and must be used from a Guice module that is in the * configure phase. * * @param binder the Guice binder to bind with * @param clazz the class to create a {@link DynamicBindingProvider} for * @param annotation the binding annotation that must be bound with the provided class */ static void bindDynamicProvider( Binder binder, Class clazz, Class annotation) { binder.getProvider(Key.get(clazz, annotation)); ParameterizedType type = Types.newParameterizedType(DynamicBindingProvider.class, clazz); DynamicBindingProvider provider = new DynamicBindingProviderImpl(TypeLiteral.get(clazz)); @SuppressWarnings("unchecked") Key> key = (Key>) Key.get(type); binder.bind(key).toInstance(provider); } /** * Binds a {@link DynamicBindingProvider} for the specified key. * *

The instance bound to the key can later be retrieved through * {@link DynamicBindingProvider#get(Class)} given the same annotation provided during binding * time. This method also 'requires' a binding for {@code key}. * *

This method requires a binder, and must be used from a Guice module that is in the * configure phase. * *

Note: The annotation on the key will only be used for required binding checks by Guice. * * @param binder the Guice binder to bind with * @param key the key to create a {@link DynamicBindingProvider} for */ static void bindDynamicProvider(Binder binder, Key key) { binder.getProvider(key); ParameterizedType type = Types.newParameterizedType(DynamicBindingProvider.class, key.getTypeLiteral().getType()); @SuppressWarnings("unchecked") DynamicBindingProvider provider = new DynamicBindingProviderImpl((TypeLiteral) key.getTypeLiteral()); @SuppressWarnings("unchecked") Key> dynamicKey = (Key>) Key.get(type); binder.bind(dynamicKey).toInstance(provider); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy