Search in sources :

Example 61 with TypeLiteral

use of com.google.inject.TypeLiteral in project camel by apache.

the class GuiceyFruitModule method configure.

protected void configure() {
    // lets find all of the configures methods
    List<Method> configureMethods = getConfiguresMethods();
    if (!configureMethods.isEmpty()) {
        final GuiceyFruitModule moduleInstance = this;
        final Class<? extends GuiceyFruitModule> moduleType = getClass();
        TypeLiteral<? extends GuiceyFruitModule> type = TypeLiteral.get(moduleType);
        for (final Method method : configureMethods) {
            int size = method.getParameterTypes().length;
            if (size == 0) {
                throw new ProvisionException("No arguments on @Configures method " + method);
            } else if (size > 1) {
                throw new ProvisionException("Too many arguments " + size + " on @Configures method " + method);
            }
            final Class<?> paramType = getParameterType(type, method, 0);
            bindListener(new AbstractMatcher<TypeLiteral<?>>() {

                public boolean matches(TypeLiteral<?> typeLiteral) {
                    return typeLiteral.getRawType().equals(paramType);
                }
            }, new TypeListener() {

                public <I> void hear(TypeLiteral<I> injectableType, TypeEncounter<I> encounter) {
                    encounter.register(new MembersInjector<I>() {

                        public void injectMembers(I injectee) {
                            // lets invoke the configures method
                            try {
                                method.setAccessible(true);
                                method.invoke(moduleInstance, injectee);
                            } catch (IllegalAccessException e) {
                                throw new ProvisionException("Failed to invoke @Configures method " + method + ". Reason: " + e, e);
                            } catch (InvocationTargetException ie) {
                                Throwable e = ie.getTargetException();
                                throw new ProvisionException("Failed to invoke @Configures method " + method + ". Reason: " + e, e);
                            }
                        }
                    });
                }
            });
        }
    }
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ProvisionException(com.google.inject.ProvisionException) TypeLiteral(com.google.inject.TypeLiteral) TypeListener(com.google.inject.spi.TypeListener) MembersInjector(com.google.inject.MembersInjector)

Example 62 with TypeLiteral

use of com.google.inject.TypeLiteral in project camel by apache.

the class GuiceyFruitModule method bindAnnotationInjector.

private <A extends Annotation> void bindAnnotationInjector(final Class<A> annotationType, final EncounterProvider<AnnotationMemberProvider> memberProviderProvider) {
    bindListener(any(), new TypeListener() {

        Provider<? extends AnnotationMemberProvider> providerProvider;

        public <I> void hear(TypeLiteral<I> injectableType, TypeEncounter<I> encounter) {
            Set<Field> boundFields = Sets.newHashSet();
            Map<MethodKey, Method> boundMethods = Maps.newHashMap();
            TypeLiteral<?> startType = injectableType;
            while (true) {
                Class<?> type = startType.getRawType();
                if (type == Object.class) {
                    break;
                }
                Field[] fields = type.getDeclaredFields();
                for (Field field : fields) {
                    if (boundFields.add(field)) {
                        bindAnnotationInjectorToField(encounter, startType, field);
                    }
                }
                Method[] methods = type.getDeclaredMethods();
                for (final Method method : methods) {
                    MethodKey key = new MethodKey(method);
                    if (boundMethods.get(key) == null) {
                        boundMethods.put(key, method);
                        bindAnnotationInjectionToMember(encounter, startType, method);
                    }
                }
                Class<?> supertype = type.getSuperclass();
                if (supertype == Object.class) {
                    break;
                }
                startType = startType.getSupertype(supertype);
            }
        }

        protected <I> void bindAnnotationInjectionToMember(final TypeEncounter<I> encounter, final TypeLiteral<?> type, final Method method) {
            // TODO lets exclude methods with @Inject?
            final A annotation = method.getAnnotation(annotationType);
            if (annotation != null) {
                if (providerProvider == null) {
                    providerProvider = memberProviderProvider.get(encounter);
                }
                encounter.register(new MembersInjector<I>() {

                    public void injectMembers(I injectee) {
                        AnnotationMemberProvider provider = providerProvider.get();
                        int size = method.getParameterTypes().length;
                        Object[] values = new Object[size];
                        for (int i = 0; i < size; i++) {
                            Class<?> paramType = getParameterType(type, method, i);
                            Object value = provider.provide(annotation, type, method, paramType, i);
                            checkInjectedValueType(value, paramType, encounter);
                            // things
                            if (value == null && !provider.isNullParameterAllowed(annotation, method, paramType, i)) {
                                return;
                            }
                            values[i] = value;
                        }
                        try {
                            method.setAccessible(true);
                            method.invoke(injectee, values);
                        } catch (IllegalAccessException e) {
                            throw new ProvisionException("Failed to inject method " + method + ". Reason: " + e, e);
                        } catch (InvocationTargetException ie) {
                            Throwable e = ie.getTargetException();
                            throw new ProvisionException("Failed to inject method " + method + ". Reason: " + e, e);
                        }
                    }
                });
            }
        }

        protected <I> void bindAnnotationInjectorToField(final TypeEncounter<I> encounter, final TypeLiteral<?> type, final Field field) {
            // TODO lets exclude fields with @Inject?
            final A annotation = field.getAnnotation(annotationType);
            if (annotation != null) {
                if (providerProvider == null) {
                    providerProvider = memberProviderProvider.get(encounter);
                }
                encounter.register(new InjectionListener<I>() {

                    public void afterInjection(I injectee) {
                        AnnotationMemberProvider provider = providerProvider.get();
                        Object value = provider.provide(annotation, type, field);
                        checkInjectedValueType(value, field.getType(), encounter);
                        try {
                            field.setAccessible(true);
                            field.set(injectee, value);
                        } catch (IllegalAccessException e) {
                            throw new ProvisionException("Failed to inject field " + field + ". Reason: " + e, e);
                        }
                    }
                });
            }
        }
    });
}
Also used : Set(java.util.Set) InjectionListener(com.google.inject.spi.InjectionListener) Field(java.lang.reflect.Field) ProvisionException(com.google.inject.ProvisionException) TypeLiteral(com.google.inject.TypeLiteral) MethodKey(org.apache.camel.guice.support.internal.MethodKey) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) TypeListener(com.google.inject.spi.TypeListener) MembersInjector(com.google.inject.MembersInjector) Map(java.util.Map)

Example 63 with TypeLiteral

use of com.google.inject.TypeLiteral in project divide by HiddenStage.

the class MockAndroidModule method additionalConfig.

@Override
protected void additionalConfig(AndroidConfig config) {
    super.additionalConfig(config);
    // ORDER MATTER
    try {
        bind(KeyManager.class).toInstance(new MockKeyManager("someKey"));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    bind(new TypeLiteral<DAO<TransientObject, TransientObject>>() {
    }).to(new TypeLiteral<MockLocalStorage<TransientObject, TransientObject>>() {
    }).in(Singleton.class);
}
Also used : DAO(io.divide.shared.server.DAO) TypeLiteral(com.google.inject.TypeLiteral) MockKeyManager(io.divide.client.auth.MockKeyManager) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) TransientObject(io.divide.shared.transitory.TransientObject) MockKeyManager(io.divide.client.auth.MockKeyManager) KeyManager(io.divide.shared.server.KeyManager)

Example 64 with TypeLiteral

use of com.google.inject.TypeLiteral in project che by eclipse.

the class AccountJpaTckModule method configure.

@Override
protected void configure() {
    H2DBTestServer server = H2DBTestServer.startDefault();
    install(new PersistTestModuleBuilder().setDriver(Driver.class).runningOn(server).addEntityClass(AccountImpl.class).setExceptionHandler(H2ExceptionHandler.class).build());
    bind(DBInitializer.class).asEagerSingleton();
    bind(SchemaInitializer.class).toInstance(new FlywaySchemaInitializer(server.getDataSource(), "che-schema"));
    bind(TckResourcesCleaner.class).toInstance(new H2JpaCleaner(server));
    bind(new TypeLiteral<TckRepository<AccountImpl>>() {
    }).toInstance(new JpaTckRepository<>(AccountImpl.class));
    bind(AccountDao.class).to(JpaAccountDao.class);
}
Also used : TckResourcesCleaner(org.eclipse.che.commons.test.tck.TckResourcesCleaner) PersistTestModuleBuilder(org.eclipse.che.commons.test.db.PersistTestModuleBuilder) SchemaInitializer(org.eclipse.che.core.db.schema.SchemaInitializer) FlywaySchemaInitializer(org.eclipse.che.core.db.schema.impl.flyway.FlywaySchemaInitializer) FlywaySchemaInitializer(org.eclipse.che.core.db.schema.impl.flyway.FlywaySchemaInitializer) TypeLiteral(com.google.inject.TypeLiteral) H2DBTestServer(org.eclipse.che.commons.test.db.H2DBTestServer) DBInitializer(org.eclipse.che.core.db.DBInitializer) AccountImpl(org.eclipse.che.account.spi.AccountImpl) Driver(org.h2.Driver) AccountDao(org.eclipse.che.account.spi.AccountDao) JpaAccountDao(org.eclipse.che.account.spi.jpa.JpaAccountDao) H2JpaCleaner(org.eclipse.che.commons.test.db.H2JpaCleaner)

Example 65 with TypeLiteral

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

the class KillBillShiroWebModule method configureShiroForRBAC.

private void configureShiroForRBAC() {
    final RbacConfig config = new ConfigurationObjectFactory(configSource).build(RbacConfig.class);
    bind(RbacConfig.class).toInstance(config);
    // Note: order matters (the first successful match will win, see below)
    bindRealm().toProvider(IniRealmProvider.class).asEagerSingleton();
    bindRealm().to(KillBillJdbcRealm.class).asEagerSingleton();
    if (KillBillShiroModule.isLDAPEnabled()) {
        bindRealm().to(KillBillJndiLdapRealm.class).asEagerSingleton();
    }
    bindListener(new AbstractMatcher<TypeLiteral<?>>() {

        @Override
        public boolean matches(final TypeLiteral<?> o) {
            return Matchers.subclassesOf(WebSecurityManager.class).matches(o.getRawType());
        }
    }, new DefaultWebSecurityManagerTypeListener(getProvider(ShiroEhCacheInstrumentor.class)));
    if (KillBillShiroModule.isRBACEnabled()) {
        addFilterChain(JaxrsResource.PREFIX + "/**", Key.get(CorsBasicHttpAuthenticationFilter.class));
    }
}
Also used : TypeLiteral(com.google.inject.TypeLiteral) IniRealmProvider(org.killbill.billing.util.glue.IniRealmProvider) KillBillJdbcRealm(org.killbill.billing.util.security.shiro.realm.KillBillJdbcRealm) KillBillJndiLdapRealm(org.killbill.billing.util.security.shiro.realm.KillBillJndiLdapRealm) ConfigurationObjectFactory(org.skife.config.ConfigurationObjectFactory) RbacConfig(org.killbill.billing.util.config.definition.RbacConfig)

Aggregations

TypeLiteral (com.google.inject.TypeLiteral)118 AbstractModule (com.google.inject.AbstractModule)43 Injector (com.google.inject.Injector)38 Module (com.google.inject.Module)15 Map (java.util.Map)14 Key (com.google.inject.Key)12 Provider (com.google.inject.Provider)12 ParameterizedType (java.lang.reflect.ParameterizedType)12 ImmutableSet (com.google.common.collect.ImmutableSet)10 Binding (com.google.inject.Binding)10 Annotation (java.lang.annotation.Annotation)10 HashMap (java.util.HashMap)10 Set (java.util.Set)10 Binder (com.google.inject.Binder)9 InjectionPoint (com.google.inject.spi.InjectionPoint)9 Method (java.lang.reflect.Method)9 Type (java.lang.reflect.Type)9 HashSet (java.util.HashSet)9 ProvisionException (com.google.inject.ProvisionException)7 FactoryModuleBuilder (com.google.inject.assistedinject.FactoryModuleBuilder)7