This is an automated email from the ASF dual-hosted git repository. orpiske pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit 67badb7a6341499ae0790de6f48fa5eadd70d0cd Author: Otavio Rodolfo Piske <[email protected]> AuthorDate: Tue Jan 27 13:27:35 2026 +0000 (chores): reduce cognitive complexity in BeanExpression --- .../apache/camel/language/bean/BeanExpression.java | 102 +++++++++++---------- 1 file changed, 55 insertions(+), 47 deletions(-) diff --git a/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java b/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java index 7ca9ad35c97c..d1227c971332 100644 --- a/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java +++ b/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java @@ -434,70 +434,28 @@ public class BeanExpression implements Expression, Predicate { List<String> methods = OgnlHelper.splitOgnl(ognl); for (String methodName : methods) { - BeanHolder holder; - if (beanToCall != null) { - holder = new ConstantBeanHolder(beanToCall, exchange.getContext(), parameterMappingStrategy, beanComponent); - } else if (beanType != null) { - holder = new ConstantTypeBeanHolder(beanType, exchange.getContext(), parameterMappingStrategy, beanComponent); - } else { - holder = null; - } - - // support the null safe operator + BeanHolder holder = createBeanHolder(beanToCall, beanType, exchange); boolean nullSafe = OgnlHelper.isNullSafeOperator(methodName); - if (holder == null) { - String name = getBeanName(exchange, null, beanHolder); - throw new RuntimeBeanExpressionException( - exchange, name, ognl, "last method returned null and therefore cannot continue to invoke method " - + methodName + " on a null instance"); - } + validateHolder(holder, exchange, beanHolder, ognl, methodName); - // keep up with how far are we doing ognlPath += methodName; - - // get rid of leading ?. or . as we only needed that to determine if null safe was enabled or not methodName = OgnlHelper.removeLeadingOperators(methodName); - // are we doing an index lookup (eg in Map/List/array etc)? - String key = null; KeyValueHolder<String, String> index = OgnlHelper.isOgnlIndex(methodName); + String key = null; if (index != null) { methodName = index.getKey(); key = index.getValue(); } - // only invoke if we have a method name to use to invoke - if (methodName != null) { - Object newResult = invokeBean(holder, beanName, methodName, resultExchange); + result = invokeMethodIfPresent(holder, beanName, methodName, exchange, resultExchange, result); + result = lookupByKeyIfPresent(key, result, resultExchange, exchange, nullSafe, ognlPath, holder); - // check for exception and rethrow if we failed - if (resultExchange.getException() != null) { - throw new RuntimeBeanExpressionException(exchange, beanName, methodName, resultExchange.getException()); - } - - result = newResult; - } - - // if there was a key then we need to lookup using the key - if (key != null) { - // if key is a nested simple expression then re-evaluate that again - if (LanguageSupport.hasSimpleFunction(key)) { - Expression exp = simple.createExpression(key); - exp.init(exchange.getContext()); - key = exp.evaluate(exchange, String.class); - } - if (key != null) { - result = lookupResult(resultExchange, key, result, nullSafe, ognlPath, holder.getBean(exchange)); - } - } - - // check null safe for null results if (result == null && nullSafe) { return null; } - // prepare for next bean to invoke beanToCall = result; beanType = null; } @@ -505,6 +463,56 @@ public class BeanExpression implements Expression, Predicate { return result; } + private BeanHolder createBeanHolder(Object beanToCall, Class<?> beanType, Exchange exchange) { + if (beanToCall != null) { + return new ConstantBeanHolder(beanToCall, exchange.getContext(), parameterMappingStrategy, beanComponent); + } + if (beanType != null) { + return new ConstantTypeBeanHolder(beanType, exchange.getContext(), parameterMappingStrategy, beanComponent); + } + return null; + } + + private void validateHolder( + BeanHolder holder, Exchange exchange, BeanHolder originalHolder, String ognl, String methodName) { + if (holder == null) { + String name = getBeanName(exchange, null, originalHolder); + throw new RuntimeBeanExpressionException( + exchange, name, ognl, "last method returned null and therefore cannot continue to invoke method " + + methodName + " on a null instance"); + } + } + + private Object invokeMethodIfPresent( + BeanHolder holder, String beanName, String methodName, + Exchange exchange, Exchange resultExchange, Object currentResult) { + if (methodName == null) { + return currentResult; + } + Object newResult = invokeBean(holder, beanName, methodName, resultExchange); + if (resultExchange.getException() != null) { + throw new RuntimeBeanExpressionException(exchange, beanName, methodName, resultExchange.getException()); + } + return newResult; + } + + private Object lookupByKeyIfPresent( + String key, Object result, Exchange resultExchange, Exchange exchange, + boolean nullSafe, String ognlPath, BeanHolder holder) { + if (key == null) { + return result; + } + if (LanguageSupport.hasSimpleFunction(key)) { + Expression exp = simple.createExpression(key); + exp.init(exchange.getContext()); + key = exp.evaluate(exchange, String.class); + } + if (key != null) { + return lookupResult(resultExchange, key, result, nullSafe, ognlPath, holder.getBean(exchange)); + } + return result; + } + private static Object lookupResult( Exchange exchange, String key, Object result, boolean nullSafe, String ognlPath, Object bean) { StringHelper.notEmpty(key, "key", "in Simple language ognl path: " + ognlPath);
