Search in sources :

Example 41 with TypeLiteral

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

the class MultibinderTest method testMultibinderSetShowsBothElementsIfToStringDifferent.

public void testMultibinderSetShowsBothElementsIfToStringDifferent() {
    // considers.
    class ValueType {

        int a;

        int b;

        ValueType(int a, int b) {
            this.a = a;
            this.b = b;
        }

        @Override
        public boolean equals(Object obj) {
            return (obj instanceof ValueType) && (((ValueType) obj).a == a);
        }

        @Override
        public int hashCode() {
            return a;
        }

        @Override
        public String toString() {
            return String.format("ValueType(%d,%d)", a, b);
        }
    }
    Module module1 = new AbstractModule() {

        @Override
        protected void configure() {
            final Multibinder<ValueType> multibinder = Multibinder.newSetBinder(binder(), ValueType.class);
            multibinder.addBinding().toProvider(Providers.of(new ValueType(1, 2)));
        }
    };
    Module module2 = new AbstractModule() {

        @Override
        protected void configure() {
            final Multibinder<ValueType> multibinder = Multibinder.newSetBinder(binder(), ValueType.class);
            multibinder.addBinding().toInstance(new ValueType(1, 3));
        }
    };
    Injector injector = Guice.createInjector(module1, module2);
    TypeLiteral<ValueType> valueType = TypeLiteral.get(ValueType.class);
    TypeLiteral<Set<ValueType>> setOfValueType = new TypeLiteral<Set<ValueType>>() {
    };
    try {
        injector.getInstance(Key.get(setOfValueType));
        fail();
    } catch (ProvisionException expected) {
        assertContains(expected.getMessage(), "Duplicate elements found in Multibinder Set<MultibinderTest$1ValueType>.", "Element: ValueType(1,2)", "Bound at: MultibinderTest$21.configure", "Element: ValueType(1,3)", "Bound at: MultibinderTest$22.configure");
    }
    // But we can still visit the module!
    assertSetVisitor(Key.get(setOfValueType), valueType, setOf(module1, module2), MODULE, false, 0, instance(new ValueType(1, 2)), instance(new ValueType(1, 3)));
}
Also used : ProvisionException(com.google.inject.ProvisionException) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) TypeLiteral(com.google.inject.TypeLiteral) Injector(com.google.inject.Injector) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule)

Example 42 with TypeLiteral

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

the class HiveConnectorFactory method create.

@Override
public Connector create(String catalogName, Map<String, String> config, ConnectorContext context) {
    requireNonNull(config, "config is null");
    try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(classLoader)) {
        Bootstrap app = new Bootstrap(new EventModule(), new MBeanModule(), new JsonModule(), new SmileModule(), new HiveClientModule(catalogName), new HiveS3Module(catalogName), new HiveGcsModule(), new HiveMetastoreModule(catalogName, metastore), new HiveSecurityModule(), new HiveAuthenticationModule(), new HiveProcedureModule(), new CachingModule(), binder -> {
            MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
            binder.bind(MBeanServer.class).toInstance(new RebindSafeMBeanServer(platformMBeanServer));
            binder.bind(NodeVersion.class).toInstance(new NodeVersion(context.getNodeManager().getCurrentNode().getVersion()));
            binder.bind(NodeManager.class).toInstance(context.getNodeManager());
            binder.bind(TypeManager.class).toInstance(context.getTypeManager());
            binder.bind(PageIndexerFactory.class).toInstance(context.getPageIndexerFactory());
            binder.bind(PageSorter.class).toInstance(context.getPageSorter());
            binder.bind(StandardFunctionResolution.class).toInstance(context.getStandardFunctionResolution());
            binder.bind(FunctionMetadataManager.class).toInstance(context.getFunctionMetadataManager());
            binder.bind(RowExpressionService.class).toInstance(context.getRowExpressionService());
            binder.bind(FilterStatsCalculatorService.class).toInstance(context.getFilterStatsCalculatorService());
            binder.bind(BlockEncodingSerde.class).toInstance(context.getBlockEncodingSerde());
        });
        Injector injector = app.doNotInitializeLogging().setRequiredConfigurationProperties(config).quiet().initialize();
        LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
        HiveMetadataFactory metadataFactory = injector.getInstance(HiveMetadataFactory.class);
        HiveTransactionManager transactionManager = injector.getInstance(HiveTransactionManager.class);
        ConnectorSplitManager splitManager = injector.getInstance(ConnectorSplitManager.class);
        ConnectorPageSourceProvider connectorPageSource = injector.getInstance(ConnectorPageSourceProvider.class);
        ConnectorPageSinkProvider pageSinkProvider = injector.getInstance(ConnectorPageSinkProvider.class);
        ConnectorNodePartitioningProvider connectorDistributionProvider = injector.getInstance(ConnectorNodePartitioningProvider.class);
        HiveSessionProperties hiveSessionProperties = injector.getInstance(HiveSessionProperties.class);
        HiveTableProperties hiveTableProperties = injector.getInstance(HiveTableProperties.class);
        HiveAnalyzeProperties hiveAnalyzeProperties = injector.getInstance(HiveAnalyzeProperties.class);
        ConnectorAccessControl accessControl = new SystemTableAwareAccessControl(injector.getInstance(ConnectorAccessControl.class));
        Set<Procedure> procedures = injector.getInstance(Key.get(new TypeLiteral<Set<Procedure>>() {
        }));
        ConnectorPlanOptimizerProvider planOptimizerProvider = injector.getInstance(ConnectorPlanOptimizerProvider.class);
        ConnectorMetadataUpdaterProvider metadataUpdaterProvider = injector.getInstance(ConnectorMetadataUpdaterProvider.class);
        return new HiveConnector(lifeCycleManager, metadataFactory, transactionManager, new ClassLoaderSafeConnectorSplitManager(splitManager, classLoader), new ClassLoaderSafeConnectorPageSourceProvider(connectorPageSource, classLoader), new ClassLoaderSafeConnectorPageSinkProvider(pageSinkProvider, classLoader), new ClassLoaderSafeNodePartitioningProvider(connectorDistributionProvider, classLoader), ImmutableSet.of(), procedures, hiveSessionProperties.getSessionProperties(), HiveSchemaProperties.SCHEMA_PROPERTIES, hiveTableProperties.getTableProperties(), hiveAnalyzeProperties.getAnalyzeProperties(), accessControl, planOptimizerProvider, metadataUpdaterProvider, classLoader);
    } catch (Exception e) {
        throwIfUnchecked(e);
        throw new RuntimeException(e);
    }
}
Also used : FilterStatsCalculatorService(com.facebook.presto.spi.plan.FilterStatsCalculatorService) ClassLoaderSafeConnectorPageSourceProvider(com.facebook.presto.spi.connector.classloader.ClassLoaderSafeConnectorPageSourceProvider) Injector(com.google.inject.Injector) PageSorter(com.facebook.presto.spi.PageSorter) Procedure(com.facebook.presto.spi.procedure.Procedure) HiveMetastoreModule(com.facebook.presto.hive.metastore.HiveMetastoreModule) ClassLoaderSafeNodePartitioningProvider(com.facebook.presto.spi.connector.classloader.ClassLoaderSafeNodePartitioningProvider) MBeanServer(javax.management.MBeanServer) ConnectorAccessControl(com.facebook.presto.spi.connector.ConnectorAccessControl) ClassLoaderSafeConnectorPageSinkProvider(com.facebook.presto.spi.connector.classloader.ClassLoaderSafeConnectorPageSinkProvider) JsonModule(com.facebook.airlift.json.JsonModule) PageIndexerFactory(com.facebook.presto.spi.PageIndexerFactory) NodeManager(com.facebook.presto.spi.NodeManager) LifeCycleManager(com.facebook.airlift.bootstrap.LifeCycleManager) ConnectorSplitManager(com.facebook.presto.spi.connector.ConnectorSplitManager) ClassLoaderSafeConnectorSplitManager(com.facebook.presto.spi.connector.classloader.ClassLoaderSafeConnectorSplitManager) SystemTableAwareAccessControl(com.facebook.presto.hive.security.SystemTableAwareAccessControl) MBeanModule(org.weakref.jmx.guice.MBeanModule) ConnectorPlanOptimizerProvider(com.facebook.presto.spi.connector.ConnectorPlanOptimizerProvider) EventModule(com.facebook.airlift.event.client.EventModule) TypeLiteral(com.google.inject.TypeLiteral) RowExpressionService(com.facebook.presto.spi.relation.RowExpressionService) HiveSecurityModule(com.facebook.presto.hive.security.HiveSecurityModule) Bootstrap(com.facebook.airlift.bootstrap.Bootstrap) ThreadContextClassLoader(com.facebook.presto.spi.classloader.ThreadContextClassLoader) ConnectorMetadataUpdaterProvider(com.facebook.presto.spi.connector.ConnectorMetadataUpdaterProvider) HiveAuthenticationModule(com.facebook.presto.hive.authentication.HiveAuthenticationModule) BlockEncodingSerde(com.facebook.presto.common.block.BlockEncodingSerde) CachingModule(com.facebook.presto.cache.CachingModule) FunctionMetadataManager(com.facebook.presto.spi.function.FunctionMetadataManager) HiveS3Module(com.facebook.presto.hive.s3.HiveS3Module) ClassLoaderSafeConnectorPageSinkProvider(com.facebook.presto.spi.connector.classloader.ClassLoaderSafeConnectorPageSinkProvider) ConnectorPageSinkProvider(com.facebook.presto.spi.connector.ConnectorPageSinkProvider) HiveGcsModule(com.facebook.presto.hive.gcs.HiveGcsModule) ConnectorPageSourceProvider(com.facebook.presto.spi.connector.ConnectorPageSourceProvider) ClassLoaderSafeConnectorPageSourceProvider(com.facebook.presto.spi.connector.classloader.ClassLoaderSafeConnectorPageSourceProvider) SmileModule(com.facebook.airlift.json.smile.SmileModule) ConnectorNodePartitioningProvider(com.facebook.presto.spi.connector.ConnectorNodePartitioningProvider) ClassLoaderSafeConnectorSplitManager(com.facebook.presto.spi.connector.classloader.ClassLoaderSafeConnectorSplitManager) TypeManager(com.facebook.presto.common.type.TypeManager) StandardFunctionResolution(com.facebook.presto.spi.function.StandardFunctionResolution)

Example 43 with TypeLiteral

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

the class InjectorSpiTest method testGetAllMembersInjectorInjectionPoints_getInstanceAndInjectMembers.

public void testGetAllMembersInjectorInjectionPoints_getInstanceAndInjectMembers() {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(Integer.class).toInstance(42);
            bind(String.class).toInstance("bar");
        }
    });
    injector.injectMembers(new ClassWithInjectableField("foo"));
    injector.getInstance(ClassWithInjectableField.class);
    Map<TypeLiteral<?>, List<InjectionPoint>> injectionPoints = injector.getAllMembersInjectorInjectionPoints();
    TypeLiteral<ClassWithInjectableField> expectedTypeLiteral = TypeLiteral.get(ClassWithInjectableField.class);
    assertThat(injectionPoints.keySet()).containsExactly(expectedTypeLiteral);
    Key<?> actualDependencyKey = Iterables.getOnlyElement(Iterables.getOnlyElement(injectionPoints.get(expectedTypeLiteral)).getDependencies()).getKey();
    assertEquals(Key.get(Integer.class), actualDependencyKey);
}
Also used : TypeLiteral(com.google.inject.TypeLiteral) Injector(com.google.inject.Injector) ArrayList(java.util.ArrayList) List(java.util.List) AbstractModule(com.google.inject.AbstractModule)

Example 44 with TypeLiteral

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

the class InjectorSpiTest method testGetAllMembersInjectorInjectionPoints_getInstance.

public void testGetAllMembersInjectorInjectionPoints_getInstance() {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(Integer.class).toInstance(42);
            bind(String.class).toInstance("bar");
        }
    });
    injector.getInstance(ClassWithInjectableField.class);
    Map<TypeLiteral<?>, List<InjectionPoint>> injectionPoints = injector.getAllMembersInjectorInjectionPoints();
    assertThat(injectionPoints).isEmpty();
}
Also used : TypeLiteral(com.google.inject.TypeLiteral) Injector(com.google.inject.Injector) ArrayList(java.util.ArrayList) List(java.util.List) AbstractModule(com.google.inject.AbstractModule)

Example 45 with TypeLiteral

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

the class ElementsTest method testBindKeysWithAnnotationType.

public void testBindKeysWithAnnotationType() {
    FailingElementVisitor annotationChecker = new FailingElementVisitor() {

        @Override
        public <T> Void visit(Binding<T> command) {
            assertEquals(Key.get(String.class, SampleAnnotation.class), command.getKey());
            return null;
        }
    };
    checkModule(new AbstractModule() {

        @Override
        protected void configure() {
            bind(String.class).annotatedWith(SampleAnnotation.class).toInstance("A");
            bind(new TypeLiteral<String>() {
            }).annotatedWith(SampleAnnotation.class).toInstance("B");
        }
    }, annotationChecker, annotationChecker);
}
Also used : Binding(com.google.inject.Binding) TypeLiteral(com.google.inject.TypeLiteral) AbstractModule(com.google.inject.AbstractModule)

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