/** * 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<Thing<Foo>>() {}); * </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))); }
@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; }
@SuppressWarnings({"unchecked"}) private Key<Collection<Realm>> realmCollectionKey() { return (Key<Collection<Realm>>) Key.get(Types.newParameterizedType(Collection.class, Realm.class)); }