Search in sources :

Example 6 with InstanceBindingImpl

use of org.elasticsearch.common.inject.internal.InstanceBindingImpl in project crate by crate.

the class InjectorImpl method createTypeLiteralBinding.

/**
 * Converts a binding for a {@code Key<TypeLiteral<T>>} to the value {@code TypeLiteral<T>}. It's
 * a bit awkward because we have to pull out the inner type in the type literal.
 */
private <T> BindingImpl<TypeLiteral<T>> createTypeLiteralBinding(Key<TypeLiteral<T>> key, Errors errors) throws ErrorsException {
    Type typeLiteralType = key.getTypeLiteral().getType();
    if (!(typeLiteralType instanceof ParameterizedType)) {
        throw errors.cannotInjectRawTypeLiteral().toException();
    }
    ParameterizedType parameterizedType = (ParameterizedType) typeLiteralType;
    Type innerType = parameterizedType.getActualTypeArguments()[0];
    // this proves problematic, we can probably fix TypeLiteral to support type variables
    if (!(innerType instanceof Class) && !(innerType instanceof GenericArrayType) && !(innerType instanceof ParameterizedType)) {
        throw errors.cannotInjectTypeLiteralOf(innerType).toException();
    }
    // by definition, innerType == T, so this is safe
    @SuppressWarnings("unchecked") TypeLiteral<T> value = (TypeLiteral<T>) TypeLiteral.get(innerType);
    InternalFactory<TypeLiteral<T>> factory = new ConstantFactory<>(Initializables.of(value));
    return new InstanceBindingImpl<>(this, key, SourceProvider.UNKNOWN_SOURCE, factory, emptySet(), value);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) InstanceBindingImpl(org.elasticsearch.common.inject.internal.InstanceBindingImpl) GenericArrayType(java.lang.reflect.GenericArrayType)

Aggregations

InstanceBindingImpl (org.elasticsearch.common.inject.internal.InstanceBindingImpl)6 GenericArrayType (java.lang.reflect.GenericArrayType)4 ParameterizedType (java.lang.reflect.ParameterizedType)4 Type (java.lang.reflect.Type)4 Set (java.util.Set)2 BindingImpl (org.elasticsearch.common.inject.internal.BindingImpl)2 ErrorsException (org.elasticsearch.common.inject.internal.ErrorsException)2 ExposedBindingImpl (org.elasticsearch.common.inject.internal.ExposedBindingImpl)2 InternalFactory (org.elasticsearch.common.inject.internal.InternalFactory)2 LinkedBindingImpl (org.elasticsearch.common.inject.internal.LinkedBindingImpl)2 LinkedProviderBindingImpl (org.elasticsearch.common.inject.internal.LinkedProviderBindingImpl)2 ProviderInstanceBindingImpl (org.elasticsearch.common.inject.internal.ProviderInstanceBindingImpl)2 ProviderMethod (org.elasticsearch.common.inject.internal.ProviderMethod)2 Scoping (org.elasticsearch.common.inject.internal.Scoping)2 UntargettedBindingImpl (org.elasticsearch.common.inject.internal.UntargettedBindingImpl)2 ProviderInstanceBinding (org.elasticsearch.common.inject.spi.ProviderInstanceBinding)2 Collections.unmodifiableSet (java.util.Collections.unmodifiableSet)1 Sets.newHashSet (org.elasticsearch.common.util.set.Sets.newHashSet)1