use of org.elasticsearch.common.inject.internal.MatcherAndConverter in project elasticsearch by elastic.
the class InjectorImpl method convertConstantStringBinding.
/**
* Converts a constant string binding to the required type.
*
* @return the binding if it could be resolved, or null if the binding doesn't exist
* @throws org.elasticsearch.common.inject.internal.ErrorsException
* if there was an error resolving the binding
*/
private <T> BindingImpl<T> convertConstantStringBinding(Key<T> key, Errors errors) throws ErrorsException {
// Find a constant string binding.
Key<String> stringKey = key.ofType(String.class);
BindingImpl<String> stringBinding = state.getExplicitBinding(stringKey);
if (stringBinding == null || !stringBinding.isConstant()) {
return null;
}
String stringValue = stringBinding.getProvider().get();
Object source = stringBinding.getSource();
// Find a matching type converter.
TypeLiteral<T> type = key.getTypeLiteral();
MatcherAndConverter matchingConverter = state.getConverter(stringValue, type, errors, source);
if (matchingConverter == null) {
// No converter can handle the given type.
return null;
}
// Try to convert the string. A failed conversion results in an error.
try {
// This cast is safe because we double check below.
@SuppressWarnings("unchecked") T converted = (T) matchingConverter.getTypeConverter().convert(stringValue, type);
if (converted == null) {
throw errors.converterReturnedNull(stringValue, source, type, matchingConverter).toException();
}
if (!type.getRawType().isInstance(converted)) {
throw errors.conversionTypeError(stringValue, source, type, matchingConverter, converted).toException();
}
return new ConvertedConstantBindingImpl<>(this, key, converted, stringBinding);
} catch (ErrorsException e) {
throw e;
} catch (RuntimeException e) {
throw errors.conversionError(stringValue, source, type, matchingConverter, e).toException();
}
}
Aggregations