public void testGenericProviderMethods() {
    Injector injector =
        Guice.createInjector(new ProvideTs<String>("A", "B") {}, new ProvideTs<Integer>(1, 2) {});

    assertEquals("A", injector.getInstance(Key.get(String.class, Names.named("First"))));
    assertEquals("B", injector.getInstance(Key.get(String.class, Names.named("Second"))));
    assertEquals(
        ImmutableSet.of("A", "B"), injector.getInstance(Key.get(Types.setOf(String.class))));

    assertEquals(1, injector.getInstance(Key.get(Integer.class, Names.named("First"))).intValue());
    assertEquals(2, injector.getInstance(Key.get(Integer.class, Names.named("Second"))).intValue());
    assertEquals(ImmutableSet.of(1, 2), injector.getInstance(Key.get(Types.setOf(Integer.class))));
  }
示例#2
0
 /**
  * Declare a singleton {@code DynamicItem<T>} with a binder.
  *
  * <p>Items must be defined in a Guice module before they can be bound:
  *
  * <pre>
  *   DynamicSet.itemOf(binder(), new TypeLiteral&lt;Thing&lt;Foo&gt;&gt;() {});
  * </pre>
  *
  * @param binder a new binder created in the module.
  * @param member type of entry to store.
  */
 public static <T> void itemOf(Binder binder, TypeLiteral<T> member) {
   @SuppressWarnings("unchecked")
   Key<DynamicItem<T>> key =
       (Key<DynamicItem<T>>)
           Key.get(Types.newParameterizedType(DynamicItem.class, member.getType()));
   binder.bind(key).toProvider(new DynamicItemProvider<>(member, key)).in(Scopes.SINGLETON);
 }
 public ComputeServiceContext buildComputeServiceContext() {
   // need the generic type information
   return (ComputeServiceContext)
       buildInjector()
           .getInstance(
               Key.get(
                   Types.newParameterizedType(
                       ComputeServiceContextImpl.class, syncClientType, asyncClientType)));
 }
示例#4
0
 @SuppressWarnings("unchecked")
 public <T> T create(Class<T> clazz) {
   return (T)
       create(
           clazz,
           (AsyncRestClientProxy<T>)
               injector.getInstance(
                   Key.get(
                       TypeLiteral.get(
                           Types.newParameterizedType(AsyncRestClientProxy.class, clazz)))));
 }
 private AutoBindProvider getAutoBindProvider(Annotation annotation) {
   AutoBindProvider autoBindProvider = null;
   if (annotation.annotationType().isAnnotationPresent(AutoBind.class)) {
     ParameterizedType parameterizedType =
         Types.newParameterizedType(AutoBindProvider.class, annotation.annotationType());
     autoBindProvider =
         (AutoBindProvider<?>) injector.getInstance(Key.get(TypeLiteral.get(parameterizedType)));
   } else if (annotation.annotationType().isAssignableFrom(AutoBind.class)) {
     autoBindProvider =
         injector.getInstance(Key.get(new TypeLiteral<AutoBindProvider<AutoBind>>() {}));
   }
   return autoBindProvider;
 }
示例#6
0
 @SuppressWarnings({"unchecked"})
 private Key<Collection<Realm>> realmCollectionKey() {
   return (Key<Collection<Realm>>)
       Key.get(Types.newParameterizedType(Collection.class, Realm.class));
 }
示例#7
0
 @SuppressWarnings({"unchecked"})
 private Key<Set<Realm>> realmSetKey() {
   return (Key<Set<Realm>>) Key.get(TypeLiteral.get(Types.setOf(Realm.class)));
 }