private FactoryProvider(TypeLiteral<F> factoryType, Map<Method, AssistedConstructor<?>> factoryMethodToConstructor) { this.factoryType = factoryType; this.factoryMethodToConstructor = factoryMethodToConstructor; checkDeclaredExceptionsMatch(); }
private void checkDeclaredExceptionsMatch() { for (Map.Entry<Method, AssistedConstructor<?>> entry : factoryMethodToConstructor.entrySet()) { for (Class<?> constructorException : entry.getValue().getDeclaredExceptions()) { if (!isConstructorExceptionCompatibleWithFactoryExeception( constructorException, entry.getKey().getExceptionTypes())) { throw newConfigurationException("Constructor %s declares an exception, but no compatible " + "exception is thrown by the factory method %s", entry.getValue(), entry.getKey()); } } } }
public static <F> Provider<F> newFactory( Class<F> factoryType, Class<?> implementationType) { return newFactory(TypeLiteral.get(factoryType), TypeLiteral.get(implementationType)); }
@Inject void setInjectorAndCheckUnboundParametersAreInjectable(Injector injector) { this.injector = injector; for (AssistedConstructor<?> c : factoryMethodToConstructor.values()) { for (Parameter p : c.getAllParameters()) { if (!p.isProvidedByFactory() && !paramCanBeInjected(p, injector)) { // this is lame - we're not using the proper mechanism to add an // error to the injector. Throughout this class we throw exceptions // to add errors, which isn't really the best way in Guice throw newConfigurationException("Parameter of type '%s' is not injectable or annotated " + "with @Assisted for Constructor '%s'", p, c); } } } }
throw newConfigurationException("Constructor mismatch: %s has %s @AssistedInject " + "constructors, factory %s has %s creation methods", implementationType, constructors.size(), factoryType, factoryMethods.length); for (Method method : factoryMethods) { if (!method.getReturnType().isAssignableFrom(implementationType.getRawType())) { throw newConfigurationException("Return type of method %s is not assignable from %s", method, implementationType); throw newConfigurationException("%s has no @AssistInject constructor that takes the " + "@Assisted parameters %s in that order. @AssistInject constructors are %s", implementationType, methodParams, paramsToConstructor.values()); for (Annotation parameterAnnotation : parameterAnnotations) { if (parameterAnnotation.annotationType() == Assisted.class) { throw newConfigurationException("Factory method %s has an @Assisted parameter, which " + "is incompatible with the deprecated @AssistedInject annotation. Please replace " + "@AssistedInject with @Inject on the %s constructor.",
public static <F> Provider<F> newFactory( TypeLiteral<F> factoryType, TypeLiteral<?> implementationType) { Map<Method, AssistedConstructor<?>> factoryMethodToConstructor = createMethodMapping(factoryType, implementationType); if (!factoryMethodToConstructor.isEmpty()) { return new FactoryProvider<>(factoryType, factoryMethodToConstructor); } else { return new FactoryProvider2<>(factoryType, Key.get(implementationType)); } }
private void configureFilters() { MapBinder<String, FilterHttpServerAdapterFactory> filterBinder = MapBinder.newMapBinder(binder(), String.class, FilterHttpServerAdapterFactory.class); Map<String, Settings> filtersSettings = componentSettings.getGroups("http_filter"); for (Map.Entry<String, Settings> entry : filtersSettings.entrySet()) { String filterName = entry.getKey(); Settings filterSettings = entry.getValue(); Class<? extends FilterHttpServerAdapter> type = null; try { type = filterSettings.getAsClass("type", null, "com.sonian.elasticsearch.http.filter.", "FilterHttpServerAdapter"); } catch (NoClassSettingsException e) { // Ignore } if (type == null) { throw new ElasticsearchIllegalArgumentException("Http Filter [" + filterName + "] must have a type associated with it"); } filterBinder.addBinding(filterName) .toProvider(FactoryProvider.newFactory(FilterHttpServerAdapterFactory.class, type)) .in(Scopes.SINGLETON); } } }
private void checkDeclaredExceptionsMatch() { for (Map.Entry<Method, AssistedConstructor<?>> entry : factoryMethodToConstructor.entrySet()) { for (Class<?> constructorException : entry.getValue().getDeclaredExceptions()) { if (!isConstructorExceptionCompatibleWithFactoryExeception( constructorException, entry.getKey().getExceptionTypes())) { throw newConfigurationException("Constructor %s declares an exception, but no compatible " + "exception is thrown by the factory method %s", entry.getValue(), entry.getKey()); } } } }
private FactoryProvider(TypeLiteral<F> factoryType, Map<Method, AssistedConstructor<?>> factoryMethodToConstructor) { this.factoryType = factoryType; this.factoryMethodToConstructor = factoryMethodToConstructor; checkDeclaredExceptionsMatch(); }
@Override protected void configure() { MapBinder<String, SimilarityProvider.Factory> similarityBinder = MapBinder.newMapBinder(binder(), String.class, SimilarityProvider.Factory.class); Map<String, Settings> similaritySettings = settings.getGroups(SIMILARITY_SETTINGS_PREFIX); for (Map.Entry<String, Settings> entry : similaritySettings.entrySet()) { String name = entry.getKey(); Settings settings = entry.getValue(); String typeName = settings.get("type"); if (typeName == null) { throw new IllegalArgumentException("Similarity [" + name + "] must have an associated type"); } else if (similarities.containsKey(typeName) == false) { throw new IllegalArgumentException("Unknown Similarity type [" + typeName + "] for [" + name + "]"); } similarityBinder.addBinding(entry.getKey()).toProvider(FactoryProvider.newFactory(SimilarityProvider.Factory.class, similarities.get(typeName))).in(Scopes.SINGLETON); } for (PreBuiltSimilarityProvider.Factory factory : Similarities.listFactories()) { if (!similarities.containsKey(factory.name())) { similarityBinder.addBinding(factory.name()).toInstance(factory); } } bind(SimilarityLookupService.class).asEagerSingleton(); bind(SimilarityService.class).asEagerSingleton(); } }
private void checkDeclaredExceptionsMatch() { for (Map.Entry<Method, AssistedConstructor<?>> entry : factoryMethodToConstructor.entrySet()) { for (Class<?> constructorException : entry.getValue().getDeclaredExceptions()) { if (!isConstructorExceptionCompatibleWithFactoryExeception( constructorException, entry.getKey().getExceptionTypes())) { throw newConfigurationException("Constructor %s declares an exception, but no compatible " + "exception is thrown by the factory method %s", entry.getValue(), entry.getKey()); } } } }
private FactoryProvider(TypeLiteral<F> factoryType, Map<Method, AssistedConstructor<?>> factoryMethodToConstructor) { this.factoryType = factoryType; this.factoryMethodToConstructor = factoryMethodToConstructor; checkDeclaredExceptionsMatch(); }
throw new IllegalArgumentException("Unknown CharFilter type [" + typeName + "] for [" + charFilterName + "]"); charFilterBinder.addBinding(charFilterName).toProvider(FactoryProvider.newFactory(CharFilterFactoryFactory.class, type)).in(Scopes.SINGLETON); charFilterBinder.addBinding(charFilterName).toProvider(FactoryProvider.newFactory(CharFilterFactoryFactory.class, clazz)).in(Scopes.SINGLETON); throw new IllegalArgumentException("Unknown TokenFilter type [" + typeName + "] for [" + tokenFilterName + "]"); tokenFilterBinder.addBinding(tokenFilterName).toProvider(FactoryProvider.newFactory(TokenFilterFactoryFactory.class, type)).in(Scopes.SINGLETON); tokenFilterBinder.addBinding(tokenFilterName).toProvider(FactoryProvider.newFactory(TokenFilterFactoryFactory.class, clazz)).in(Scopes.SINGLETON); throw new IllegalArgumentException("Unknown Tokenizer type [" + typeName + "] for [" + tokenizerName + "]"); tokenizerBinder.addBinding(tokenizerName).toProvider(FactoryProvider.newFactory(TokenizerFactoryFactory.class, type)).in(Scopes.SINGLETON); tokenizerBinder.addBinding(tokenizerName).toProvider(FactoryProvider.newFactory(TokenizerFactoryFactory.class, clazz)).in(Scopes.SINGLETON); analyzerBinder.addBinding(analyzerName).toProvider(FactoryProvider.newFactory(AnalyzerProviderFactory.class, type)).in(Scopes.SINGLETON); analyzerBinder.addBinding(analyzerName).toProvider(FactoryProvider.newFactory(AnalyzerProviderFactory.class, clazz)).in(Scopes.SINGLETON);
private void checkDeclaredExceptionsMatch() { for (Map.Entry<Method, AssistedConstructor<?>> entry : factoryMethodToConstructor.entrySet()) { for (Class<?> constructorException : entry.getValue().getDeclaredExceptions()) { if (!isConstructorExceptionCompatibleWithFactoryExeception( constructorException, entry.getKey().getExceptionTypes())) { throw newConfigurationException("Constructor %s declares an exception, but no compatible " + "exception is thrown by the factory method %s", entry.getValue(), entry.getKey()); } } } }
private FactoryProvider(TypeLiteral<F> factoryType, Map<Method, AssistedConstructor<?>> factoryMethodToConstructor) { this.factoryType = factoryType; this.factoryMethodToConstructor = factoryMethodToConstructor; checkDeclaredExceptionsMatch(); }
.newFactory(WriterCollectorFactory.class, BulkWriterCollector.class));
private void checkDeclaredExceptionsMatch() { for (Map.Entry<Method, AssistedConstructor<?>> entry : factoryMethodToConstructor.entrySet()) { for (Class<?> constructorException : entry.getValue().getDeclaredExceptions()) { if (!isConstructorExceptionCompatibleWithFactoryExeception( constructorException, entry.getKey().getExceptionTypes())) { throw newConfigurationException("Constructor %s declares an exception, but no compatible " + "exception is thrown by the factory method %s", entry.getValue(), entry.getKey()); } } } }
private FactoryProvider(TypeLiteral<F> factoryType, Map<Method, AssistedConstructor<?>> factoryMethodToConstructor) { this.factoryType = factoryType; this.factoryMethodToConstructor = factoryMethodToConstructor; checkDeclaredExceptionsMatch(); }