[
https://issues.apache.org/jira/browse/CAMEL-24489?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Claus Ibsen resolved CAMEL-24489.
---------------------------------
Resolution: Fixed
> TypeConversionException prints 'from type: null' for anonymous/local classes
> (getCanonicalName() returns null)
> --------------------------------------------------------------------------------------------------------------
>
> Key: CAMEL-24489
> URL: https://issues.apache.org/jira/browse/CAMEL-24489
> Project: Camel
> Issue Type: Improvement
> Reporter: mayur mohan
> Priority: Minor
> Labels: camel-api, diagnostic
> Fix For: 4.23.0
>
>
> h2. Problem
> When a type conversion fails and the source value is an anonymous or local
> class (e.g. {{com.jcraft.jsch.ChannelSftp$2}}),
> {{TypeConversionException.createMessage()}} prints {{from type: null}} in the
> error message instead of the actual class name.
> This happens because {{Class.getCanonicalName()}} returns {{null}} for
> anonymous/local/synthetic classes, and the existing code used it directly
> without a fallback to {{getName()}}.
> The body value is *not* null -- only the canonical name of its class is null.
> h2. Root Cause
> File:
> {{core/camel-api/src/main/java/org/apache/camel/TypeConversionException.java}}
> {code:java}
> return "Error during type conversion from type: "
> + (value != null ? value.getClass().getCanonicalName() : null) ...
> {code}
> {{Class.getCanonicalName()}} returns {{null}} for anonymous/local/synthetic
> classes such as anonymous inner classes like {{ChannelSftp$2}}. The message
> then literally prints {{from type: null}} even though the value is not null.
> h2. Stack Trace
> {noformat}
> org.apache.camel.StreamCacheException: Error during type conversion from
> type: org.apache.camel.component.file.remote.RemoteFile to the required type:
> org.apache.camel.StreamCache with value
> RemoteFile[iad@KN3164_OMD.I.170_20260215-205802-554.txt] due to
> org.apache.camel.TypeConversionException: Error during type conversion from
> type: null to the required type: org.apache.camel.StreamCache with value
> com.jcraft.jsch.ChannelSftp$2@56b4885a due to java.io.IOException: error
> at
> org.apache.camel.impl.engine.CamelInternalProcessor$StreamCachingAdvice.before(CamelInternalProcessor.java:929)
> at
> org.apache.camel.impl.engine.CamelInternalProcessor$StreamCachingAdvice.before(CamelInternalProcessor.java:893)
> at
> org.apache.camel.impl.engine.CamelInternalProcessor.process(CamelInternalProcessor.java:341)
> at org.apache.camel.processor.Pipeline$PipelineTask.run(Pipeline.java:109)
> at
> org.apache.camel.impl.engine.DefaultReactiveExecutor$Worker.schedule(DefaultReactiveExecutor.java:193)
> at
> org.apache.camel.impl.engine.DefaultReactiveExecutor.scheduleMain(DefaultReactiveExecutor.java:64)
> at org.apache.camel.processor.Pipeline.process(Pipeline.java:185)
> at
> org.apache.camel.impl.engine.CamelInternalProcessor.process(CamelInternalProcessor.java:398)
> at
> org.apache.camel.impl.engine.DefaultAsyncProcessorAwaitManager.process(DefaultAsyncProcessorAwaitManager.java:83)
> at
> org.apache.camel.support.AsyncProcessorSupport.process(AsyncProcessorSupport.java:41)
> at
> org.apache.camel.component.file.GenericFileConsumer.processExchange(GenericFileConsumer.java:485)
> at
> org.apache.camel.component.file.remote.RemoteFileConsumer.processExchange(RemoteFileConsumer.java:156)
> at
> org.apache.camel.support.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:202)
> at
> org.apache.camel.support.ScheduledPollConsumer.run(ScheduledPollConsumer.java:116)
> Caused by: org.apache.camel.TypeConversionException: Error during type
> conversion from type: null to the required type: org.apache.camel.StreamCache
> with value com.jcraft.jsch.ChannelSftp$2@56b4885a due to java.io.IOException:
> error
> at
> org.apache.camel.converter.stream.StreamCacheBulkConverterLoader.convertTo(StreamCacheBulkConverterLoader.java:60)
> at
> org.apache.camel.spi.BulkTypeConverters.convertTo(BulkTypeConverters.java:122)
> at
> org.apache.camel.component.file.GenericFileConverter.convertTo(GenericFileConverter.java:103)
> at
> org.apache.camel.support.SimpleTypeConverter.convertTo(SimpleTypeConverter.java:101)
> at
> org.apache.camel.impl.converter.CoreTypeConverterRegistry.doConvertTo(CoreTypeConverterRegistry.java:516)
> at
> org.apache.camel.impl.converter.CoreTypeConverterRegistry.convertTo(CoreTypeConverterRegistry.java:203)
> at org.apache.camel.support.MessageSupport.getBody(MessageSupport.java:96)
> at
> org.apache.camel.impl.engine.DefaultStreamCachingStrategy.cache(DefaultStreamCachingStrategy.java:208)
> at
> org.apache.camel.impl.engine.CamelInternalProcessor$StreamCachingAdvice.before(CamelInternalProcessor.java:922)
> ... 19 more
> Caused by: java.io.IOException: error
> at com.jcraft.jsch.ChannelSftp$2.read(ChannelSftp.java:1427)
> at com.jcraft.jsch.ChannelSftp$2.read(ChannelSftp.java:1336)
> at org.apache.camel.util.IOHelper.copy(IOHelper.java:193)
> at org.apache.camel.util.IOHelper.copyAndCloseInput(IOHelper.java:229)
> at
> org.apache.camel.converter.stream.StreamCacheConverter.convertToStreamCache(StreamCacheConverter.java:54)
> at
> org.apache.camel.converter.stream.StreamCacheBulkConverterLoader.doConvertTo(StreamCacheBulkConverterLoader.java:78)
> at
> org.apache.camel.converter.stream.StreamCacheBulkConverterLoader.convertTo(StreamCacheBulkConverterLoader.java:51)
> ... 31 more
> {noformat}
> h2. Fix
> Add a private {{typeName(Class<?>)}} helper that falls back to {{getName()}}
> when {{getCanonicalName()}} is null:
> {code:java}
> private static String typeName(Class<?> clazz) {
> if (clazz == null) { return "null"; }
> String name = clazz.getCanonicalName();
> return name != null ? name : clazz.getName();
> }
> {code}
> h2. Before / After
> *Before:*
> {noformat}
> Error during type conversion from type: null to the required type:
> org.apache.camel.StreamCache
> with value com.jcraft.jsch.ChannelSftp$2@56b4885a due to java.io.IOException:
> error
> {noformat}
> *After:*
> {noformat}
> Error during type conversion from type: com.jcraft.jsch.ChannelSftp$2 to the
> required type: org.apache.camel.StreamCache
> with value com.jcraft.jsch.ChannelSftp$2@56b4885a due to java.io.IOException:
> error
> {noformat}
> h2. PR
> https://github.com/apache/camel/pull/25738
--
This message was sent by Atlassian Jira
(v8.20.10#820010)