This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 58f557dcbc72 CAMEL-26982: camel-core - addTypeConverters invokes the
converter methods on the given instance (#30010)
58f557dcbc72 is described below
commit 58f557dcbc7271fba1577a367f20e236c7a063ef
Author: Federico Mariani <[email protected]>
AuthorDate: Sun Nov 15 16:35:06 2026 +0100
CAMEL-26982: camel-core - addTypeConverters invokes the converter methods
on the given instance (#30010)
TypeConverterRegistry.addTypeConverters(Object) kept only the class, so the
non-static @Converter methods ran on a new instance created by the injector.
The given object is now used, and the Class overload still uses the injector.
CAMEL-26982: camel-core - the upgrade note says the given instance is not
bean post-processed, and the createInjector Javadoc says when the instance is
created
Co-Authored-By: Claude <[email protected]>
---
.../converter/AnnotationTypeConverterLoader.java | 13 +++-
.../camel/impl/converter/CachingInjector.java | 8 ++
.../camel/impl/converter/TypeConvertersLoader.java | 25 ++++++-
.../camel/impl/converter/TypeConvertersTest.java | 87 ++++++++++++++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 9 +++
5 files changed, 138 insertions(+), 4 deletions(-)
diff --git
a/core/camel-base/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java
b/core/camel-base/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java
index 6307e50c7932..bb385ffed66d 100644
---
a/core/camel-base/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java
+++
b/core/camel-base/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java
@@ -322,6 +322,15 @@ public class AnnotationTypeConverterLoader implements
TypeConverterLoader {
return true;
}
+ /**
+ * Creates the injector that provides the instance the non-static
converter methods of the given type are invoked
+ * on. By default, the injector creates the instance lazily, the first
time one of these methods is invoked.
+ * Override to supply an instance that already exists (see {@link
TypeConvertersLoader}).
+ */
+ protected CachingInjector<?> createInjector(TypeConverterRegistry
registry, Class<?> type) {
+ return new CachingInjector<>(registry, CastUtils.cast(type,
Object.class));
+ }
+
private CachingInjector<?> handleHasConverterAnnotation(
TypeConverterRegistry registry, Class<?> type,
CachingInjector<?> injector, Method method, boolean allowNull) {
@@ -343,7 +352,7 @@ public class AnnotationTypeConverterLoader implements
TypeConverterLoader {
new StaticMethodTypeConverter(method,
allowNull));
} else {
if (injector == null) {
- injector = new CachingInjector<>(registry,
CastUtils.cast(type, Object.class));
+ injector = createInjector(registry, type);
}
registerTypeConverter(registry, method, toType,
fromType,
new InstanceMethodTypeConverter(injector,
method, registry, allowNull));
@@ -378,7 +387,7 @@ public class AnnotationTypeConverterLoader implements
TypeConverterLoader {
new StaticMethodFallbackTypeConverter(method,
registry, allowNull), method);
} else {
if (injector == null) {
- injector = new CachingInjector<>(registry,
CastUtils.cast(type, Object.class));
+ injector = createInjector(registry, type);
}
registerFallbackTypeConverter(registry,
new
InstanceMethodFallbackTypeConverter(injector, method, registry, allowNull),
method);
diff --git
a/core/camel-base/src/main/java/org/apache/camel/impl/converter/CachingInjector.java
b/core/camel-base/src/main/java/org/apache/camel/impl/converter/CachingInjector.java
index 944d710403a3..5041aa690182 100644
---
a/core/camel-base/src/main/java/org/apache/camel/impl/converter/CachingInjector.java
+++
b/core/camel-base/src/main/java/org/apache/camel/impl/converter/CachingInjector.java
@@ -31,8 +31,16 @@ public class CachingInjector<T> {
private T instance;
public CachingInjector(TypeConverterRegistry repository, Class<T> type) {
+ this(repository, type, null);
+ }
+
+ /**
+ * Creates the injector that returns the given instance, and only creates
a new instance when it is null.
+ */
+ public CachingInjector(TypeConverterRegistry repository, Class<T> type, T
instance) {
this.repository = repository;
this.type = type;
+ this.instance = instance;
}
public T newInstance() {
diff --git
a/core/camel-base/src/main/java/org/apache/camel/impl/converter/TypeConvertersLoader.java
b/core/camel-base/src/main/java/org/apache/camel/impl/converter/TypeConvertersLoader.java
index d14f2e5c1960..00595efb9959 100644
---
a/core/camel-base/src/main/java/org/apache/camel/impl/converter/TypeConvertersLoader.java
+++
b/core/camel-base/src/main/java/org/apache/camel/impl/converter/TypeConvertersLoader.java
@@ -18,6 +18,10 @@ package org.apache.camel.impl.converter;
import java.io.IOException;
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.spi.TypeConverterRegistry;
+import org.apache.camel.util.CastUtils;
+
/**
* A type converter loader, that <b>only</b> supports scanning a {@link
org.apache.camel.TypeConverters} class for
* methods that has been annotated with {@link org.apache.camel.Converter}.
@@ -25,14 +29,16 @@ import java.io.IOException;
public class TypeConvertersLoader extends AnnotationTypeConverterLoader {
private final String name;
+ private final Object instance;
/**
* Creates the loader
*
- * @param typeConverters The implementation that has the type converters
+ * @param typeConverters The implementation that has the type converters,
its non-static converter methods are
+ * invoked on this instance
*/
public TypeConvertersLoader(Object typeConverters) {
- this(typeConverters.getClass());
+ this(typeConverters.getClass(), typeConverters);
}
/**
@@ -41,8 +47,13 @@ public class TypeConvertersLoader extends
AnnotationTypeConverterLoader {
* @param clazz the class with the @Converter annotation and converter
methods
*/
public TypeConvertersLoader(Class<?> clazz) {
+ this(clazz, null);
+ }
+
+ private TypeConvertersLoader(Class<?> clazz, Object instance) {
super(new TypeConvertersPackageScanClassResolver(clazz));
this.name = clazz.getPackage().getName();
+ this.instance = instance;
}
@Override
@@ -51,4 +62,14 @@ public class TypeConvertersLoader extends
AnnotationTypeConverterLoader {
return new String[] { name };
}
+ @Override
+ protected CachingInjector<?> createInjector(TypeConverterRegistry
registry, Class<?> type) {
+ if (instance != null && type.isInstance(instance)) {
+ // use the given instance (also for the converter methods of its
super classes)
+ CamelContextAware.trySetCamelContext(instance,
registry.getCamelContext());
+ return new CachingInjector<>(registry, CastUtils.cast(type,
Object.class), instance);
+ }
+ return super.createInjector(registry, type);
+ }
+
}
diff --git
a/core/camel-core/src/test/java/org/apache/camel/impl/converter/TypeConvertersTest.java
b/core/camel-core/src/test/java/org/apache/camel/impl/converter/TypeConvertersTest.java
index 9ca920bebc9b..acf2415e663a 100644
---
a/core/camel-core/src/test/java/org/apache/camel/impl/converter/TypeConvertersTest.java
+++
b/core/camel-core/src/test/java/org/apache/camel/impl/converter/TypeConvertersTest.java
@@ -16,16 +16,49 @@
*/
package org.apache.camel.impl.converter;
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Converter;
+import org.apache.camel.TypeConverters;
+import org.apache.camel.spi.TypeConverterRegistry;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
public class TypeConvertersTest extends ContextTestSupport {
private final MyConverters converters = new MyConverters();
+ @Test
+ public void testAddInstanceWithoutDefaultConstructor() {
+ context.getTypeConverterRegistry().addTypeConverters(new
PrefixConverters("x-"));
+
+ Target target = context.getTypeConverter().convertTo(Target.class, new
Source("a"));
+ assertNotNull(target);
+ assertEquals("x-a", target.value());
+ }
+
+ @Test
+ public void testAddConfiguredInstance() {
+ ConfigurableConverters configurable = new ConfigurableConverters();
+ configurable.setPrefix("x-");
+
+ context.getTypeConverterRegistry().addTypeConverters(configurable);
+
+ Target target = context.getTypeConverter().convertTo(Target.class, new
Source("a"));
+ assertNotNull(target);
+ assertEquals("x-a", target.value());
+
+ FallbackTarget fallback =
context.getTypeConverter().convertTo(FallbackTarget.class, new Source("b"));
+ assertNotNull(fallback);
+ assertEquals("x-b", fallback.value());
+
+ assertSame(context, configurable.getCamelContext());
+ }
+
@Test
public void testAdd() {
int before = context.getTypeConverterRegistry().size();
@@ -57,4 +90,58 @@ public class TypeConvertersTest extends ContextTestSupport {
assertEquals('a',
context.getTypeConverter().mandatoryConvertTo(char.class, "a"));
assertEquals(Boolean.parseBoolean("true"),
context.getTypeConverter().mandatoryConvertTo(boolean.class, "true"));
}
+
+ public record Source(String value) {
+ }
+
+ public record Target(String value) {
+ }
+
+ public record FallbackTarget(String value) {
+ }
+
+ public static class PrefixConverters implements TypeConverters {
+ private final String prefix;
+
+ public PrefixConverters(String prefix) {
+ this.prefix = prefix;
+ }
+
+ @Converter
+ public Target toTarget(Source source) {
+ return new Target(prefix + source.value());
+ }
+ }
+
+ public static class ConfigurableConverters implements TypeConverters,
CamelContextAware {
+ private CamelContext camelContext;
+ private String prefix = "default-";
+
+ @Override
+ public CamelContext getCamelContext() {
+ return camelContext;
+ }
+
+ @Override
+ public void setCamelContext(CamelContext camelContext) {
+ this.camelContext = camelContext;
+ }
+
+ public void setPrefix(String prefix) {
+ this.prefix = prefix;
+ }
+
+ @Converter
+ public Target toTarget(Source source) {
+ return new Target(prefix + source.value());
+ }
+
+ @Converter(fallback = true)
+ public Object toFallbackTarget(Class<?> type, Object value,
TypeConverterRegistry registry) {
+ if (type == FallbackTarget.class && value instanceof Source
source) {
+ return new FallbackTarget(prefix + source.value());
+ }
+ return null;
+ }
+ }
}
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index dde82b1575b0..813b37ca6542 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -407,6 +407,15 @@ sibling package whose name merely started with the scanned
package name, such as
and its sub packages, so a `RoutesBuilder` or `@Configuration` class in such a
sibling package is no longer
discovered when running from a JAR; list that package explicitly if it should
be scanned.
+=== camel-core - addTypeConverters uses the given instance
+
+`TypeConverterRegistry.addTypeConverters(Object)`, also used for the
`TypeConverters` beans found in the registry, now
+invokes the non-static `@Converter` methods on the given object, and sets the
`CamelContext` on it when it is
+`CamelContextAware`. Before, they ran on a new instance created by the
injector: the state of the given object was
+ignored, and a class without a no-arg constructor failed at conversion time.
The given object is used as is: unlike
+the instance created by the injector, it is not bean post-processed
(`@PropertyInject`, `@BeanInject`, ...), so set its
+fields before adding it. Passing a `Class` still creates the instance with the
injector.
+
=== camel-core - Model has a new getModelLock method
The `org.apache.camel.model.Model` interface (and so `ModelCamelContext`) has
a new method `getModelLock()`, which