@Override public <T extends Annotation> T synthesize(Class<T> annotationClass) { T annotation = childContext.synthesize(annotationClass); if (annotation == null) { return thisContext.synthesize(annotationClass); } return annotation; }
@Override public Iterator<ConversionError> iterator() { return thisContext.iterator(); } };
/** * Create a simple {@link ConversionContext} for the given generic type variables. * * @param <T> type Generic * @param argument The argument * @return The conversion context */ static <T> ArgumentConversionContext<T> of(Argument<T> argument) { return of(argument, null, null); }
private Object doConvertInput(ConversionService<?> conversionService, Argument<?> arg, Object object) { ArgumentConversionContext conversionContext = ConversionContext.of(arg); Optional<?> convert = conversionService.convert(object, conversionContext); if (convert.isPresent()) { return convert.get(); } else { Optional<ConversionError> lastError = conversionContext.getLastError(); if (lastError.isPresent()) { throw new ConversionErrorException(arg, lastError.get()); } } return null; } }
/** * Attempts to convert the given object to the given target type. If conversion fails or is not possible an empty {@link Optional} is returned. * * @param object The object to convert * @param targetType The target type * @param <T> The generic type * @return The optional */ default <T> Optional<T> convert(Object object, Argument<T> targetType) { return convert(object, targetType.getType(), ConversionContext.of(targetType)); }
/** * Attempts to convert the given object to the given target type. If conversion fails or is not possible an empty {@link Optional} is returned. * * @param object The object to convert * @param context The {@link ArgumentConversionContext} * @param <T> The generic type * @return The optional */ default <T> Optional<T> convert(Object object, ArgumentConversionContext<T> context) { return convert(object, context.getArgument().getType(), context); } }
/** * Attempts to convert the given object to the given target type. If conversion fails or is not possible an empty {@link Optional} is returned. * * @param object The object to convert * @param targetType The target type * @param <T> The generic type * @return The optional */ default <T> Optional<T> convert(Object object, Class<T> targetType) { return convert(object, targetType, ConversionContext.DEFAULT); }
/** * Augment this context with annotation metadata. * * @param annotationMetadata The annotation metadata * @return The conversion context */ @SuppressWarnings("unchecked") default ArgumentConversionContext<T> with(AnnotationMetadata annotationMetadata) { ConversionContext thisContext = this; return new DefaultArgumentConversionContext(getArgument(), thisContext.getLocale(), thisContext.getCharset()) { @Override public AnnotationMetadata getAnnotationMetadata() { return annotationMetadata; } }; } }
@Override public void reject(Exception exception) { thisContext.reject(exception); }
/** * @param argument The argument * @param conversionError The conversion error */ public ConversionErrorException(Argument argument, ConversionError conversionError) { super(buildMessage(argument, conversionError), conversionError.getCause()); this.argument = argument; this.conversionError = conversionError; }
@Override public <S, T> DefaultConversionService addConverter(Class<S> sourceType, Class<T> targetType, Function<S, T> function) { ConvertiblePair pair = new ConvertiblePair(sourceType, targetType); TypeConverter<S, T> typeConverter = TypeConverter.of(sourceType, targetType, function); typeConverters.put(pair, typeConverter); converterCache.put(pair, typeConverter); return this; }
private <S, T> ConvertiblePair newPair(Class<S> sourceType, Class<T> targetType, TypeConverter<S, T> typeConverter) { ConvertiblePair pair; if (typeConverter instanceof FormattingTypeConverter) { pair = new ConvertiblePair(sourceType, targetType, ((FormattingTypeConverter) typeConverter).annotationType()); } else { pair = new ConvertiblePair(sourceType, targetType); } return pair; }
@Override public <S, T> boolean canConvert(Class<S> sourceType, Class<T> targetType) { ConvertiblePair pair = new ConvertiblePair(sourceType, targetType, null); TypeConverter typeConverter = converterCache.get(pair); if (typeConverter == null) { typeConverter = findTypeConverter(sourceType, targetType, null); if (typeConverter != null) { converterCache.put(pair, typeConverter); return true; } return false; } return true; }
/** * Constructor. */ public DefaultConversionService() { registerDefaultConverters(); }
/** * Converts from the given source object type to the target type. * * @param object The object type * @param targetType The target type being converted to * @return The converted type or empty if the conversion is not possible */ default Optional<T> convert(S object, Class<T> targetType) { return convert(object, targetType, ConversionContext.DEFAULT); }
@Override public Optional<CharSequence> convert(HttpData upload, Class<CharSequence> targetType, ConversionContext context) { try { if (!upload.isCompleted()) { return Optional.empty(); } ByteBuf byteBuf = upload.getByteBuf(); return conversionService.convert(byteBuf, targetType, context); } catch (Exception e) { context.reject(e); return Optional.empty(); } } }
/** * Get the value of the {@code value} member of the annotation. * * @param argument The argument * @param <T> The type * @return The result */ public final <T> Optional<T> getValue(Argument<T> argument) { return getValue(ConversionContext.of(argument)); }