Java Code Examples for com.google.inject.TypeLiteral#get()

The following examples show how to use com.google.inject.TypeLiteral#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GenericBindingStrategy.java    From seed with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void resolve(Binder binder) {
    // Bind all the possible types for one class or interface.
    // For instance: Repository<Customer,String>, Repository<Order, Long>, etc.
    FactoryModuleBuilder guiceFactoryBuilder = new FactoryModuleBuilder();

    if (constructorParamsMap != null) {
        for (Map.Entry<Type[], Key<?>> entry : constructorParamsMap.entrySet()) {
            bindKey(binder, guiceFactoryBuilder, entry.getKey(), entry.getValue());
        }
    } else {
        for (Type[] params : constructorParams) {
            bindKey(binder, guiceFactoryBuilder, params, null);
        }
    }

    TypeLiteral<?> guiceAssistedFactory = TypeLiteral.get(
            Types.newParameterizedType(DEFAULT_IMPL_FACTORY_CLASS, genericImplClass));
    binder.install(guiceFactoryBuilder.build(guiceAssistedFactory));
}
 
Example 2
Source File: ParserManifest.java    From ProjectAres with GNU Affero General Public License v3.0 6 votes vote down vote up
private <T extends Number & Comparable<T>> void bindNumber(Class<T> rawType) {
    final TypeLiteral<T> type = TypeLiteral.get(rawType);
    final TypeArgument<T> typeArg = new TypeArgument<T>(type){};

    final TypeLiteral<NumberParser<T>> parserType = new ResolvableType<NumberParser<T>>(){}.with(typeArg);
    bind(parserType);
    bind(new ResolvableType<TransfiniteParser<T>>(){}.with(typeArg)).to(parserType);
    bind(new ResolvableType<PrimitiveParser<T>>(){}.with(typeArg)).to(parserType);
    bind(new ResolvableType<Parser<T>>(){}.with(typeArg)).to(parserType);

    final TypeLiteral<VectorParser<T>> vectorParserType = new ResolvableType<VectorParser<T>>(){}.with(typeArg);
    bind(vectorParserType);

    install(new PropertyManifest<>(type, new ResolvableType<NumberProperty<T>>(){}.with(typeArg)));

    if(Types.isAssignable(Comparable.class, type)) {
        install(new RangeParserManifest(type));
    }
}
 
Example 3
Source File: RestoreModules.java    From cassandra-backup with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure() {
    installOperationBindings(binder(),
                             "restore",
                             RestoreOperationRequest.class,
                             RestoreOperation.class);

    @SuppressWarnings("unchecked") final TypeLiteral<OperationCoordinator<RestoreOperationRequest>> operationCoordinator =
        (TypeLiteral<OperationCoordinator<RestoreOperationRequest>>) TypeLiteral.get(newParameterizedType(OperationCoordinator.class, RestoreOperationRequest.class));

    newOptionalBinder(binder(), operationCoordinator).setDefault().to(DefaultRestoreOperationCoordinator.class);
}
 
Example 4
Source File: BackupModules.java    From cassandra-backup with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure() {
    installOperationBindings(binder(),
                             "backup",
                             BackupOperationRequest.class,
                             BackupOperation.class);

    @SuppressWarnings("unchecked") final TypeLiteral<OperationCoordinator<BackupOperationRequest>> operationCoordinator =
        (TypeLiteral<OperationCoordinator<BackupOperationRequest>>) TypeLiteral.get(newParameterizedType(OperationCoordinator.class, BackupOperationRequest.class));

    newOptionalBinder(binder(), operationCoordinator).setDefault().to(DefaultBackupOperationCoordinator.class);
}
 
Example 5
Source File: BindingUtils.java    From seed with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Resolve the key for injectee class, including qualifier from implClass and resolved type variables.
 * <p>
 * Useful when we can not resolve type variable from one implementation.
 *
 * @param injecteeClass       the injectee class
 * @param genericImplClass    the generic implementation
 * @param typeVariableClasses the type variable classes
 * @return {@link com.google.inject.Key}
 */
@SuppressWarnings("unchecked")
public static <T> Key<T> resolveKey(Class<T> injecteeClass, Class<? extends T> genericImplClass,
        Type... typeVariableClasses) {
    Optional<Annotation> qualifier = Annotations.on(genericImplClass)
            .findAll()
            .filter(AnnotationPredicates.annotationAnnotatedWith(Qualifier.class, false))
            .findFirst();
    TypeLiteral<T> genericInterface = (TypeLiteral<T>) TypeLiteral.get(
            Types.newParameterizedType(injecteeClass, typeVariableClasses));
    return qualifier.map(annotation -> Key.get(genericInterface, annotation)).orElseGet(
            () -> Key.get(genericInterface));
}
 
Example 6
Source File: InnerFactoryManifest.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
public static <I> InnerFactoryManifest<?, I> forInnerClass(Key<I> key) {
    final Class<?> outer = key.getTypeLiteral().getRawType().getEnclosingClass();
    if(outer == null) {
        throw new IllegalArgumentException(key + " is not an inner class");
    }
    return new InnerFactoryManifest(key, TypeLiteral.get(outer));
}
 
Example 7
Source File: MethodScanner.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public MethodScanner(Class<D> decl, Predicate<? super Method> filter) {
    this(TypeLiteral.get(decl), filter);
}
 
Example 8
Source File: PrimitiveParserManifest.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public PrimitiveParserManifest(Class<T> type, Class<? extends Parser<T>> ParserTImpl) {
    this(TypeLiteral.get(type), TypeLiteral.get(ParserTImpl));
}
 
Example 9
Source File: ConfigurationInjectorProvider.java    From soabase with Apache License 2.0 4 votes vote down vote up
/**
 * @param configurationType type to bind to
 * @param modules additional modules
 */
public ConfigurationInjectorProvider(Class<T> configurationType, Module... modules)
{
    super(modules);
    this.configurationType = TypeLiteral.get(configurationType);
}
 
Example 10
Source File: TypeResolver.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public <T> TypeLiteral<T> resolve(TypeLiteral<T> type) {
    return (TypeLiteral<T>) TypeLiteral.get(resolver.resolveType(type.getType()));
}
 
Example 11
Source File: PropertyManifest.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public PropertyManifest(Class<T> type, Class<B> builderType, boolean defaultForType) {
    this(TypeLiteral.get(type), TypeLiteral.get(builderType), defaultForType);
}
 
Example 12
Source File: PropertyManifest.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public PropertyManifest(Class<T> type) {
    this(TypeLiteral.get(type));
}
 
Example 13
Source File: EnumParserManifest.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public EnumParserManifest(Class<T> type) {
    this(TypeLiteral.get(type));
}
 
Example 14
Source File: NumberParser.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
private NumberParser(Class<T> type) {
    this(TypeLiteral.get(type), NumberFactory.get(type));
}
 
Example 15
Source File: FeatureManifest.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public FeatureManifest(@Nullable Class<T> T) {
    this(TypeLiteral.get(T));
}
 
Example 16
Source File: FeatureBinder.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public FeatureBinder(Binder binder, Class<T> type) {
    this(binder, TypeLiteral.get(type));
}
 
Example 17
Source File: RootFeatureManifest.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public RootFeatureManifest(@Nullable Class<T> type, @Nullable NodeFinder nodeFinder) {
    this(type == null ? null : TypeLiteral.get(type), nodeFinder);
}
 
Example 18
Source File: TypeUtils.java    From karyon with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> Key<T> keyFor(Class<?> type, Class<?> typeArg1, Class<?> typeArg2, Annotation annotation) {
    TypeLiteral<T> typeLiteral = (TypeLiteral<T>) TypeLiteral.get(Types.newParameterizedType(type, typeArg1, typeArg2));
    return Key.get(typeLiteral, annotation);
}
 
Example 19
Source File: ApiMain.java    From data-transfer-project with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public static final <T> TypeLiteral<Set<T>> setOf(Class<T> type) {
  return (TypeLiteral<Set<T>>) TypeLiteral.get(Types.setOf(type));
}
 
Example 20
Source File: BackupRestoreBindings.java    From cassandra-backup with Apache License 2.0 4 votes vote down vote up
public static <RESTORER extends Restorer, BACKUPER extends Backuper, BUCKET_SERVICE extends BucketService>
void installBindings(final Binder binder,
                     final String typeId,
                     final Class<RESTORER> restorerClass,
                     final Class<BACKUPER> backuperClass,
                     final Class<BUCKET_SERVICE> bucketServiceClass) {

    @SuppressWarnings("unchecked")
    final TypeLiteral<RestorerFactory<RESTORER>> restorerFactoryType =
            (TypeLiteral<RestorerFactory<RESTORER>>) TypeLiteral.get(Types.newParameterizedType(RestorerFactory.class, restorerClass));

    @SuppressWarnings("unchecked")
    final TypeLiteral<BackuperFactory<BACKUPER>> backuperFactoryType =
            (TypeLiteral<BackuperFactory<BACKUPER>>) TypeLiteral.get(Types.newParameterizedType(BackuperFactory.class, backuperClass));

    @SuppressWarnings("unchecked")
    final TypeLiteral<BucketServiceFactory<BUCKET_SERVICE>> bucketServiceFactoryType =
        (TypeLiteral<BucketServiceFactory<BUCKET_SERVICE>>) TypeLiteral.get(Types.newParameterizedType(BucketServiceFactory.class, bucketServiceClass));


    binder.install(new FactoryModuleBuilder()
                           .implement(Restorer.class, restorerClass)
                           .build(restorerFactoryType));

    binder.install(new FactoryModuleBuilder()
                           .implement(Backuper.class, backuperClass)
                           .build(backuperFactoryType));

    binder.install(new FactoryModuleBuilder()
                       .implement(BucketService.class, bucketServiceClass)
                       .build(bucketServiceFactoryType));

    // add an entry to the Map<String, RestorerFactory> for the factory created above
    MapBinder.newMapBinder(binder, TypeLiteral.get(String.class), TypeLiteral.get(RestorerFactory.class))
            .addBinding(typeId).to(restorerFactoryType);

    // add an entry to the Map<String, BackuperFactory> for the factory created above
    MapBinder.newMapBinder(binder, TypeLiteral.get(String.class), TypeLiteral.get(BackuperFactory.class))
            .addBinding(typeId).to(backuperFactoryType);

    // add an entry to the Map<String, BucketServiceFactory> for the factory created above
    MapBinder.newMapBinder(binder, TypeLiteral.get(String.class), TypeLiteral.get(BucketServiceFactory.class))
        .addBinding(typeId).to(bucketServiceFactoryType);

    // Map<String, RestorerFactory>
    newMapBinder(binder,
                 new TypeLiteral<String>() {},
                 restorerFactoryType)
            .addBinding(typeId).to(restorerFactoryType);

    // Map<String, BackuperFactory>
    newMapBinder(binder,
                 new TypeLiteral<String>() {},
                 backuperFactoryType)
            .addBinding(typeId).to(backuperFactoryType);

    // Map<String, BucketServiceFactory>
    newMapBinder(binder,
                 new TypeLiteral<String>() {},
                 bucketServiceFactoryType)
        .addBinding(typeId).to(bucketServiceFactoryType);

    Multibinder.newSetBinder(binder, String.class, StorageProviders.class).addBinding().toInstance(typeId);
}