Search in sources :

Example 36 with TypeLiteral

use of com.google.inject.TypeLiteral in project presto by prestodb.

the class VerifierModule method setup.

protected final void setup(Binder binder) {
    configBinder(binder).bindConfig(VerifierConfig.class);
    configBinder(binder).bindConfig(DeterminismAnalyzerConfig.class);
    configBinder(binder).bindConfig(QueryConfigurationOverridesConfig.class, ForControl.class, "control");
    configBinder(binder).bindConfig(QueryConfigurationOverridesConfig.class, ForTest.class, "test");
    binder.bind(QueryConfigurationOverrides.class).annotatedWith(ForControl.class).to(Key.get(QueryConfigurationOverridesConfig.class, ForControl.class)).in(SINGLETON);
    binder.bind(QueryConfigurationOverrides.class).annotatedWith(ForTest.class).to(Key.get(QueryConfigurationOverridesConfig.class, ForTest.class)).in(SINGLETON);
    for (Class<? extends Predicate<SourceQuery>> customQueryFilterClass : customQueryFilterClasses) {
        binder.bind(customQueryFilterClass).in(SINGLETON);
    }
    // block encoding
    binder.bind(BlockEncodingSerde.class).to(BlockEncodingManager.class).in(Scopes.SINGLETON);
    newSetBinder(binder, BlockEncoding.class);
    // catalog
    binder.bind(CatalogManager.class).in(Scopes.SINGLETON);
    // function
    binder.bind(FunctionAndTypeManager.class).in(SINGLETON);
    // handle resolver
    binder.install(new HandleJsonModule());
    // parser
    binder.bind(SqlParserOptions.class).toInstance(sqlParserOptions);
    binder.bind(SqlParser.class).in(SINGLETON);
    // transaction
    configBinder(binder).bindConfig(TransactionManagerConfig.class);
    // type
    configBinder(binder).bindConfig(FeaturesConfig.class);
    binder.bind(TypeManager.class).to(FunctionAndTypeManager.class).in(SINGLETON);
    newSetBinder(binder, Type.class);
    // verifier
    install(new VerificationQueryRewriterModule());
    install(FailureResolverModule.BUILT_IN);
    binder.bind(VerificationManager.class).in(SINGLETON);
    binder.bind(VerificationFactory.class).in(SINGLETON);
    binder.bind(ChecksumValidator.class).in(SINGLETON);
    MapBinder<Category, ColumnValidator> columnValidatorBinder = MapBinder.newMapBinder(binder, Category.class, ColumnValidator.class);
    columnValidatorBinder.addBinding(SIMPLE).to(SimpleColumnValidator.class).in(SINGLETON);
    columnValidatorBinder.addBinding(FLOATING_POINT).to(FloatingPointColumnValidator.class).in(SINGLETON);
    columnValidatorBinder.addBinding(ARRAY).to(ArrayColumnValidator.class).in(SINGLETON);
    columnValidatorBinder.addBinding(ROW).to(RowColumnValidator.class).in(SINGLETON);
    columnValidatorBinder.addBinding(MAP).to(MapColumnValidator.class).in(SINGLETON);
    binder.bind(new TypeLiteral<List<Predicate<SourceQuery>>>() {
    }).toProvider(new CustomQueryFilterProvider(customQueryFilterClasses));
}
Also used : SqlParserOptions(com.facebook.presto.sql.parser.SqlParserOptions) Category(com.facebook.presto.verifier.framework.Column.Category) ArrayColumnValidator(com.facebook.presto.verifier.checksum.ArrayColumnValidator) SqlParser(com.facebook.presto.sql.parser.SqlParser) FloatingPointColumnValidator(com.facebook.presto.verifier.checksum.FloatingPointColumnValidator) CatalogManager(com.facebook.presto.metadata.CatalogManager) Predicate(java.util.function.Predicate) BlockEncodingManager(com.facebook.presto.common.block.BlockEncodingManager) FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) TypeLiteral(com.google.inject.TypeLiteral) MapColumnValidator(com.facebook.presto.verifier.checksum.MapColumnValidator) ColumnValidator(com.facebook.presto.verifier.checksum.ColumnValidator) SimpleColumnValidator(com.facebook.presto.verifier.checksum.SimpleColumnValidator) RowColumnValidator(com.facebook.presto.verifier.checksum.RowColumnValidator) FloatingPointColumnValidator(com.facebook.presto.verifier.checksum.FloatingPointColumnValidator) ArrayColumnValidator(com.facebook.presto.verifier.checksum.ArrayColumnValidator) HandleJsonModule(com.facebook.presto.metadata.HandleJsonModule) MapColumnValidator(com.facebook.presto.verifier.checksum.MapColumnValidator) ChecksumValidator(com.facebook.presto.verifier.checksum.ChecksumValidator) RowColumnValidator(com.facebook.presto.verifier.checksum.RowColumnValidator) SimpleColumnValidator(com.facebook.presto.verifier.checksum.SimpleColumnValidator) VerificationQueryRewriterModule(com.facebook.presto.verifier.rewrite.VerificationQueryRewriterModule)

Example 37 with TypeLiteral

use of com.google.inject.TypeLiteral in project presto by prestodb.

the class RedisConnectorFactory method create.

@Override
public Connector create(String catalogName, Map<String, String> config, ConnectorContext context) {
    requireNonNull(catalogName, "catalogName is null");
    requireNonNull(config, "config is null");
    try {
        Bootstrap app = new Bootstrap(new JsonModule(), new RedisConnectorModule(), binder -> {
            binder.bind(RedisConnectorId.class).toInstance(new RedisConnectorId(catalogName));
            binder.bind(TypeManager.class).toInstance(context.getTypeManager());
            binder.bind(NodeManager.class).toInstance(context.getNodeManager());
            if (tableDescriptionSupplier.isPresent()) {
                binder.bind(new TypeLiteral<Supplier<Map<SchemaTableName, RedisTableDescription>>>() {
                }).toInstance(tableDescriptionSupplier.get());
            } else {
                binder.bind(new TypeLiteral<Supplier<Map<SchemaTableName, RedisTableDescription>>>() {
                }).to(RedisTableDescriptionSupplier.class).in(Scopes.SINGLETON);
            }
        });
        Injector injector = app.doNotInitializeLogging().setRequiredConfigurationProperties(config).initialize();
        return injector.getInstance(RedisConnector.class);
    } catch (Exception e) {
        throwIfUnchecked(e);
        throw new RuntimeException(e);
    }
}
Also used : JsonModule(com.facebook.airlift.json.JsonModule) SchemaTableName(com.facebook.presto.spi.SchemaTableName) NodeManager(com.facebook.presto.spi.NodeManager) TypeLiteral(com.google.inject.TypeLiteral) Injector(com.google.inject.Injector) Bootstrap(com.facebook.airlift.bootstrap.Bootstrap) TypeManager(com.facebook.presto.common.type.TypeManager) Supplier(java.util.function.Supplier) Map(java.util.Map)

Example 38 with TypeLiteral

use of com.google.inject.TypeLiteral in project guice by google.

the class CheckedProviderMethodsModule method createProviderMethod.

<T> CheckedProviderMethod<T> createProviderMethod(Binder binder, final Method method, CheckedProvides checkedProvides) {
    // Class literal uses rawtypes.
    @SuppressWarnings("rawtypes") Class<? extends CheckedProvider> throwingProvider = checkedProvides.value();
    binder = binder.withSource(method);
    Errors errors = new Errors(method);
    // prepare the parameter providers
    List<Dependency<?>> dependencies = Lists.newArrayList();
    List<Provider<?>> parameterProviders = Lists.newArrayList();
    List<TypeLiteral<?>> parameterTypes = typeLiteral.getParameterTypes(method);
    Annotation[][] parameterAnnotations = method.getParameterAnnotations();
    for (int i = 0; i < parameterTypes.size(); i++) {
        Key<?> key = getKey(errors, parameterTypes.get(i), method, parameterAnnotations[i]);
        if (key.equals(LOGGER_KEY)) {
            // If it was a Logger, change the key to be unique & bind it to a
            // provider that provides a logger with a proper name.
            // This solves issue 482 (returning a new anonymous logger on every call exhausts memory)
            Key<Logger> loggerKey = Key.get(Logger.class, UniqueAnnotations.create());
            binder.bind(loggerKey).toProvider(new LogProvider(method));
            key = loggerKey;
        }
        dependencies.add(Dependency.get(key));
        parameterProviders.add(binder.getProvider(key));
    }
    // Define T as the method's return type.
    @SuppressWarnings("unchecked") TypeLiteral<T> returnType = (TypeLiteral<T>) typeLiteral.getReturnType(method);
    List<TypeLiteral<?>> exceptionTypes = typeLiteral.getExceptionTypes(method);
    Key<T> key = getKey(errors, returnType, method, method.getAnnotations());
    Class<? extends Annotation> scopeAnnotation = Annotations.findScopeAnnotation(errors, method.getAnnotations());
    for (Message message : errors.getMessages()) {
        binder.addError(message);
    }
    return new CheckedProviderMethod<T>(key, method, delegate, ImmutableSet.copyOf(dependencies), parameterProviders, scopeAnnotation, throwingProvider, exceptionTypes, checkedProvides.scopeExceptions());
}
Also used : Message(com.google.inject.spi.Message) Dependency(com.google.inject.spi.Dependency) Logger(java.util.logging.Logger) Provider(com.google.inject.Provider) Errors(com.google.inject.internal.Errors) TypeLiteral(com.google.inject.TypeLiteral)

Example 39 with TypeLiteral

use of com.google.inject.TypeLiteral in project guice by google.

the class MapBinderTest method testMapBinderMatching.

@Marker
public void testMapBinderMatching() throws Exception {
    Method m = MapBinderTest.class.getDeclaredMethod("testMapBinderMatching");
    assertNotNull(m);
    final Annotation marker = m.getAnnotation(Marker.class);
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        public void configure() {
            MapBinder<Integer, Integer> mb1 = MapBinder.newMapBinder(binder(), Integer.class, Integer.class, Marker.class);
            MapBinder<Integer, Integer> mb2 = MapBinder.newMapBinder(binder(), Integer.class, Integer.class, marker);
            mb1.addBinding(1).toInstance(1);
            mb2.addBinding(2).toInstance(2);
            // This assures us that the two binders are equivalent, so we expect the instance added to
            // each to have been added to one set.
            assertEquals(mb1, mb2);
        }
    });
    TypeLiteral<Map<Integer, Integer>> t = new TypeLiteral<Map<Integer, Integer>>() {
    };
    Map<Integer, Integer> s1 = injector.getInstance(Key.get(t, Marker.class));
    Map<Integer, Integer> s2 = injector.getInstance(Key.get(t, marker));
    // This assures us that the two sets are in fact equal.  They may not be same set (as in Java
    // object identical), but we shouldn't expect that, since probably Guice creates the set each
    // time in case the elements are dependent on scope.
    assertEquals(s1, s2);
    // This ensures that MultiBinder is internally using the correct set name --
    // making sure that instances of marker annotations have the same set name as
    // MarkerAnnotation.class.
    Map<Integer, Integer> expected = new HashMap<>();
    expected.put(1, 1);
    expected.put(2, 2);
    assertEquals(expected, s1);
}
Also used : HashMap(java.util.HashMap) Method(java.lang.reflect.Method) BindingAnnotation(com.google.inject.BindingAnnotation) Annotation(java.lang.annotation.Annotation) AbstractModule(com.google.inject.AbstractModule) TypeLiteral(com.google.inject.TypeLiteral) Injector(com.google.inject.Injector) MapBinder(com.google.inject.multibindings.MapBinder) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 40 with TypeLiteral

use of com.google.inject.TypeLiteral in project guice by google.

the class MoreTypesTest method testGetRawType_wildcard.

public <T> void testGetRawType_wildcard() throws Exception {
    WildcardType wildcard = (WildcardType) ((ParameterizedType) new TypeLiteral<List<?>>() {
    }.getType()).getActualTypeArguments()[0];
    assertEquals(Object.class, MoreTypes.getRawType(wildcard));
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) TypeLiteral(com.google.inject.TypeLiteral)

Aggregations

TypeLiteral (com.google.inject.TypeLiteral)195 Injector (com.google.inject.Injector)69 AbstractModule (com.google.inject.AbstractModule)56 Module (com.google.inject.Module)29 Test (org.junit.Test)28 Binder (com.google.inject.Binder)20 Key (com.google.inject.Key)18 Map (java.util.Map)18 Set (java.util.Set)18 Method (java.lang.reflect.Method)15 ParameterizedType (java.lang.reflect.ParameterizedType)15 ImmutableSet (com.google.common.collect.ImmutableSet)14 InjectionPoint (com.google.inject.spi.InjectionPoint)14 Provider (com.google.inject.Provider)12 Annotation (java.lang.annotation.Annotation)12 List (java.util.List)12 Type (java.lang.reflect.Type)11 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)11 HashSet (java.util.HashSet)11