use of org.elasticsearch.common.inject.internal.BindingImpl in project elasticsearch by elastic.
the class BindingProcessor method visit.
@Override
public <T> Boolean visit(Binding<T> command) {
final Object source = command.getSource();
if (Void.class.equals(command.getKey().getRawType())) {
if (command instanceof ProviderInstanceBinding && ((ProviderInstanceBinding) command).getProviderInstance() instanceof ProviderMethod) {
errors.voidProviderMethod();
} else {
errors.missingConstantValues();
}
return true;
}
final Key<T> key = command.getKey();
Class<? super T> rawType = key.getTypeLiteral().getRawType();
if (rawType == Provider.class) {
errors.bindingToProvider();
return true;
}
validateKey(command.getSource(), command.getKey());
final Scoping scoping = Scopes.makeInjectable(((BindingImpl<?>) command).getScoping(), injector, errors);
command.acceptTargetVisitor(new BindingTargetVisitor<T, Void>() {
@Override
public Void visit(InstanceBinding<? extends T> binding) {
Set<InjectionPoint> injectionPoints = binding.getInjectionPoints();
T instance = binding.getInstance();
Initializable<T> ref = initializer.requestInjection(injector, instance, source, injectionPoints);
ConstantFactory<? extends T> factory = new ConstantFactory<>(ref);
InternalFactory<? extends T> scopedFactory = Scopes.scope(key, injector, factory, scoping);
putBinding(new InstanceBindingImpl<>(injector, key, source, scopedFactory, injectionPoints, instance));
return null;
}
@Override
public Void visit(ProviderInstanceBinding<? extends T> binding) {
Provider<? extends T> provider = binding.getProviderInstance();
Set<InjectionPoint> injectionPoints = binding.getInjectionPoints();
Initializable<Provider<? extends T>> initializable = initializer.<Provider<? extends T>>requestInjection(injector, provider, source, injectionPoints);
InternalFactory<T> factory = new InternalFactoryToProviderAdapter<>(initializable, source);
InternalFactory<? extends T> scopedFactory = Scopes.scope(key, injector, factory, scoping);
putBinding(new ProviderInstanceBindingImpl<>(injector, key, source, scopedFactory, scoping, provider, injectionPoints));
return null;
}
@Override
public Void visit(ProviderKeyBinding<? extends T> binding) {
Key<? extends Provider<? extends T>> providerKey = binding.getProviderKey();
BoundProviderFactory<T> boundProviderFactory = new BoundProviderFactory<>(injector, providerKey, source);
creationListeners.add(boundProviderFactory);
InternalFactory<? extends T> scopedFactory = Scopes.scope(key, injector, (InternalFactory<? extends T>) boundProviderFactory, scoping);
putBinding(new LinkedProviderBindingImpl<>(injector, key, source, scopedFactory, scoping, providerKey));
return null;
}
@Override
public Void visit(LinkedKeyBinding<? extends T> binding) {
Key<? extends T> linkedKey = binding.getLinkedKey();
if (key.equals(linkedKey)) {
errors.recursiveBinding();
}
FactoryProxy<T> factory = new FactoryProxy<>(injector, key, linkedKey, source);
creationListeners.add(factory);
InternalFactory<? extends T> scopedFactory = Scopes.scope(key, injector, factory, scoping);
putBinding(new LinkedBindingImpl<>(injector, key, source, scopedFactory, scoping, linkedKey));
return null;
}
@Override
public Void visit(UntargettedBinding<? extends T> untargetted) {
// @ImplementedBy annotation or something.
if (key.hasAnnotationType()) {
errors.missingImplementation(key);
putBinding(invalidBinding(injector, key, source));
return null;
}
// This cast is safe after the preceding check.
final BindingImpl<T> binding;
try {
binding = injector.createUnitializedBinding(key, scoping, source, errors);
putBinding(binding);
} catch (ErrorsException e) {
errors.merge(e.getErrors());
putBinding(invalidBinding(injector, key, source));
return null;
}
uninitializedBindings.add(new Runnable() {
@Override
public void run() {
try {
((InjectorImpl) binding.getInjector()).initializeBinding(binding, errors.withSource(source));
} catch (ErrorsException e) {
errors.merge(e.getErrors());
}
}
});
return null;
}
@Override
public Void visit(ExposedBinding<? extends T> binding) {
throw new IllegalArgumentException("Cannot apply a non-module element");
}
@Override
public Void visit(ConvertedConstantBinding<? extends T> binding) {
throw new IllegalArgumentException("Cannot apply a non-module element");
}
@Override
public Void visit(ConstructorBinding<? extends T> binding) {
throw new IllegalArgumentException("Cannot apply a non-module element");
}
@Override
public Void visit(ProviderBinding<? extends T> binding) {
throw new IllegalArgumentException("Cannot apply a non-module element");
}
});
return true;
}
use of org.elasticsearch.common.inject.internal.BindingImpl in project elasticsearch by elastic.
the class InjectorImpl method createJustInTimeBinding.
/**
* Returns a new just-in-time binding created by resolving {@code key}. The strategies used to
* create just-in-time bindings are:
* <ol>
* <li>Internalizing Providers. If the requested binding is for {@code Provider<T>}, we delegate
* to the binding for {@code T}.
* <li>Converting constants.
* <li>ImplementedBy and ProvidedBy annotations. Only for unannotated keys.
* <li>The constructor of the raw type. Only for unannotated keys.
* </ol>
*
* @throws org.elasticsearch.common.inject.internal.ErrorsException
* if the binding cannot be created.
*/
<T> BindingImpl<T> createJustInTimeBinding(Key<T> key, Errors errors) throws ErrorsException {
if (state.isBlacklisted(key)) {
throw errors.childBindingAlreadySet(key).toException();
}
// Handle cases where T is a Provider<?>.
if (isProvider(key)) {
// These casts are safe. We know T extends Provider<X> and that given Key<Provider<X>>,
// createProviderBinding() will return BindingImpl<Provider<X>>.
@SuppressWarnings("unchecked") BindingImpl binding = createProviderBinding((Key) key, errors);
return binding;
}
// Handle cases where T is a MembersInjector<?>
if (isMembersInjector(key)) {
// These casts are safe. T extends MembersInjector<X> and that given Key<MembersInjector<X>>,
// createMembersInjectorBinding() will return BindingImpl<MembersInjector<X>>.
@SuppressWarnings("unchecked") BindingImpl binding = createMembersInjectorBinding((Key) key, errors);
return binding;
}
// Try to convert a constant string binding to the requested type.
BindingImpl<T> convertedBinding = convertConstantStringBinding(key, errors);
if (convertedBinding != null) {
return convertedBinding;
}
// If the key has an annotation...
if (key.hasAnnotationType()) {
// Look for a binding without annotation attributes or return null.
if (key.hasAttributes()) {
try {
Errors ignored = new Errors();
return getBindingOrThrow(key.withoutAttributes(), ignored);
} catch (ErrorsException ignored) {
// throw with a more appropriate message below
}
}
throw errors.missingImplementation(key).toException();
}
Object source = key.getTypeLiteral().getRawType();
BindingImpl<T> binding = createUnitializedBinding(key, Scoping.UNSCOPED, source, errors);
initializeBinding(binding, errors);
return binding;
}
Aggregations