This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-4.14.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.14.x by this push:
new 719326b2b224 CAMEL-22787: camel-bean - Fix bean info to not cache
exchange instances. (#20438)
719326b2b224 is described below
commit 719326b2b224f34d8d94bf0022aa950f8bb7c437
Author: Claus Ibsen <[email protected]>
AuthorDate: Tue Dec 16 07:46:47 2025 +0100
CAMEL-22787: camel-bean - Fix bean info to not cache exchange instances.
(#20438)
* CAMEL-22787: camel-bean - Fix bean info to not cache exchange instances.
---
.../org/apache/camel/component/bean/BeanInfo.java | 9 ++++--
.../apache/camel/component/bean/BeanInfoTest.java | 35 ++++++++++++++++++++++
2 files changed, 42 insertions(+), 2 deletions(-)
diff --git
a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java
b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java
index b1d6ab85f162..bd0ab67c8765 100644
---
a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java
+++
b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java
@@ -44,6 +44,7 @@ import org.apache.camel.Message;
import org.apache.camel.PropertyInject;
import org.apache.camel.Variable;
import org.apache.camel.Variables;
+import org.apache.camel.support.DefaultExchange;
import org.apache.camel.support.ObjectHelper;
import org.apache.camel.support.builder.ExpressionBuilder;
import org.apache.camel.support.language.AnnotationExpressionFactory;
@@ -164,11 +165,13 @@ public class BeanInfo {
// key must be instance based for custom/handler annotations
boolean instanceBased = !operationsWithCustomAnnotation.isEmpty() ||
!operationsWithHandlerAnnotation.isEmpty();
+ // do not cache Exchange based beans
+ instanceBased &= DefaultExchange.class != type;
if (instanceBased) {
// add new bean info to cache (instance based)
component.addBeanInfoToCache(key, this);
} else {
- // add new bean info to cache (not instance based, favour key2 if
possible)
+ // add new bean info to cache (not instance based, favor key2 if
possible)
BeanInfoCacheKey k = key2 != null ? key2 : key;
component.addBeanInfoToCache(k, this);
}
@@ -460,10 +463,12 @@ public class BeanInfo {
Annotation[] parameterAnnotations
= parametersAnnotations[i].toArray(new Annotation[0]);
Expression expression = createParameterUnmarshalExpression(method,
parameterType, parameterAnnotations);
- hasCustomAnnotation |= expression != null;
if (expression == null) {
expression =
strategy.getDefaultParameterTypeExpression(parameterType);
}
+ // this is not entirely correct as the parameter may be a default
parameter type and not a custom annotation
+ // but we need to keep this logic for backwards compatability
+ hasCustomAnnotation |= expression != null;
// whether this parameter is vararg which must be last parameter
boolean varargs = method.isVarArgs() && i == size - 1;
diff --git
a/components/camel-bean/src/test/java/org/apache/camel/component/bean/BeanInfoTest.java
b/components/camel-bean/src/test/java/org/apache/camel/component/bean/BeanInfoTest.java
index 423ad137be50..719d21e97065 100644
---
a/components/camel-bean/src/test/java/org/apache/camel/component/bean/BeanInfoTest.java
+++
b/components/camel-bean/src/test/java/org/apache/camel/component/bean/BeanInfoTest.java
@@ -25,6 +25,7 @@ import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
import org.apache.camel.Handler;
import org.apache.camel.spi.Registry;
import org.apache.camel.support.DefaultExchange;
@@ -35,7 +36,9 @@ import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static net.bytebuddy.matcher.ElementMatchers.named;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.lenient;
@@ -118,6 +121,20 @@ public class BeanInfoTest {
assertFalse(info2.hasAnyMethodHandlerAnnotation());
}
+ @Test
+ public void testChooseExchangeMethod() {
+ DefaultExchange exchange = new DefaultExchange(context);
+ BeanInfo info = new BeanInfo(context, MyClassTwo.class);
+ MethodInfo mi = info.chooseMethod(null, exchange, null);
+ assertNotNull(mi);
+ assertTrue(mi.hasCustomAnnotation()); // not really correct; but
backwards compatible
+ assertFalse(mi.hasHandlerAnnotation());
+ assertTrue(mi.hasParameters());
+ assertFalse(mi.hasBodyParameter());
+ assertEquals("myExchangeMethod", mi.getMethod().getName());
+ assertEquals(Exchange.class, mi.getMethod().getParameterTypes()[0]);
+ }
+
private Object buildProxyObject() {
try {
return new ByteBuddy()
@@ -151,6 +168,24 @@ public class BeanInfoTest {
}
}
+ public static class MyClassTwo {
+
+ public void myMethod() {
+ }
+
+ public String myOtherMethod() {
+ return "";
+ }
+
+ public String myExchangeMethod(Exchange exchange) {
+ return exchange.getExchangeId();
+ }
+
+ public void myBooleanMethod(boolean fool) {
+ // noop
+ }
+ }
+
public static class MyDerivedClass extends MyClass {
@Override
public void myMethod() {