Search in sources :

Example 1 with TypeConverterAware

use of org.apache.camel.spi.TypeConverterAware in project camel by apache.

the class InstanceMethodTypeConverter method convertTo.

@SuppressWarnings("unchecked")
public <T> T convertTo(Class<T> type, Exchange exchange, Object value) {
    Object instance = injector.newInstance();
    if (instance == null) {
        throw new RuntimeCamelException("Could not instantiate an instance of: " + type.getCanonicalName());
    }
    // inject parent type converter
    if (instance instanceof TypeConverterAware) {
        if (registry instanceof TypeConverter) {
            TypeConverter parentTypeConverter = (TypeConverter) registry;
            ((TypeConverterAware) instance).setTypeConverter(parentTypeConverter);
        }
    }
    return useExchange ? (T) ObjectHelper.invokeMethod(method, instance, value, exchange) : (T) ObjectHelper.invokeMethod(method, instance, value);
}
Also used : TypeConverter(org.apache.camel.TypeConverter) TypeConverterAware(org.apache.camel.spi.TypeConverterAware) RuntimeCamelException(org.apache.camel.RuntimeCamelException)

Example 2 with TypeConverterAware

use of org.apache.camel.spi.TypeConverterAware in project camel by apache.

the class BaseTypeConverterRegistry method addFallbackTypeConverter.

@Override
public void addFallbackTypeConverter(TypeConverter typeConverter, boolean canPromote) {
    log.trace("Adding fallback type converter: {} which can promote: {}", typeConverter, canPromote);
    // add in top of fallback as the toString() fallback will nearly always be able to convert
    // the last one which is add to the FallbackTypeConverter will be called at the first place
    fallbackConverters.add(0, new FallbackTypeConverter(typeConverter, canPromote));
    if (typeConverter instanceof TypeConverterAware) {
        TypeConverterAware typeConverterAware = (TypeConverterAware) typeConverter;
        typeConverterAware.setTypeConverter(this);
    }
    if (typeConverter instanceof CamelContextAware) {
        CamelContextAware camelContextAware = (CamelContextAware) typeConverter;
        if (camelContext != null) {
            camelContextAware.setCamelContext(camelContext);
        }
    }
}
Also used : TypeConverterAware(org.apache.camel.spi.TypeConverterAware) CamelContextAware(org.apache.camel.CamelContextAware)

Aggregations

TypeConverterAware (org.apache.camel.spi.TypeConverterAware)2 CamelContextAware (org.apache.camel.CamelContextAware)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1 TypeConverter (org.apache.camel.TypeConverter)1