You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2026/02/03 14:17:46 UTC
(camel) branch main updated (6033580fe0d6 -> 1dbf795ab421)
This is an automated email from the ASF dual-hosted git repository.
orpiske pushed a change to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
from 6033580fe0d6 Latest version of couchdb container for ppc64le is 3.3.2
new 3fa0c1c3b1b2 (chores): reduce cognitive complexity in BeanInfo
new 952ed5c32612 (chores): reduce cognitive complexity in MethodInfo
new 67badb7a6341 (chores): reduce cognitive complexity in BeanExpression
new 1dbf795ab421 (chores): address PR comment - simplify null check in removeNonMatchingMethods
The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../org/apache/camel/component/bean/BeanInfo.java | 238 +++++++++++----------
.../apache/camel/component/bean/MethodInfo.java | 174 +++++++--------
.../apache/camel/language/bean/BeanExpression.java | 102 +++++----
3 files changed, 273 insertions(+), 241 deletions(-)
(camel) 04/04: (chores): address PR comment - simplify null check in removeNonMatchingMethods
Posted by or...@apache.org.
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 1dbf795ab421d73262982498258ce96dfddd78a6
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Tue Jan 27 14:44:29 2026 +0000
(chores): address PR comment - simplify null check in removeNonMatchingMethods
---
.../src/main/java/org/apache/camel/component/bean/BeanInfo.java | 6 ++----
1 file changed, 2 insertions(+), 4 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 1aab72b52ede..f51695b2796a 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
@@ -1114,11 +1114,9 @@ public class BeanInfo {
}
private void removeNonMatchingMethods(List<MethodInfo> methods, String name) {
- if (methods == null) {
- return;
+ if (methods != null) {
+ methods.removeIf(info -> !matchMethod(info.getMethod(), name));
}
- // method does not match so remove it
- methods.removeIf(info -> !matchMethod(info.getMethod(), name));
}
private void removeAllAbstractMethods(List<MethodInfo> methods) {
(camel) 02/04: (chores): reduce cognitive complexity in MethodInfo
Posted by or...@apache.org.
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 952ed5c32612c2245ea54ca8ba69c20f4c7ca452
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Tue Jan 27 13:18:10 2026 +0000
(chores): reduce cognitive complexity in MethodInfo
Extract helper methods from evaluateParameterValue: clipClassSuffix,
isParameterTypeNotValue, evaluateSimpleExpression, isValidParameterValue,
convertParameterValue. Use guard clauses for early return.
---
.../apache/camel/component/bean/MethodInfo.java | 174 +++++++++++----------
1 file changed, 89 insertions(+), 85 deletions(-)
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/MethodInfo.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/MethodInfo.java
index 6254cfecf208..29c7a46b8e3e 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/MethodInfo.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/MethodInfo.java
@@ -692,99 +692,103 @@ public class MethodInfo {
private Object evaluateParameterValue(
Exchange exchange, int index, Object parameterValue, Class<?> parameterType,
boolean varargs) {
- Object answer = null;
-
- // convert the parameter value to a String
String exp = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, parameterValue);
- boolean valid;
- if (exp != null) {
- int pos1 = exp.indexOf(' ');
- int pos2 = exp.indexOf(".class");
- if (pos1 != -1 && pos2 != -1 && pos1 > pos2) {
- exp = exp.substring(pos2 + 7); // clip <space>.class
- }
+ if (exp == null) {
+ return null;
+ }
- // check if its a valid parameter value (no type declared via .class syntax)
- valid = BeanHelper.isValidParameterValue(exp);
- if (!valid && !varargs) {
- // it may be a parameter type instead, and if so, then we should return null,
- // as this method is only for evaluating parameter values
- Boolean isClass = BeanHelper.isAssignableToExpectedType(exchange.getContext().getClassResolver(), exp,
- parameterType);
- // the method will return a non-null value if exp is a class
- if (isClass != null) {
- return null;
- }
- }
+ exp = clipClassSuffix(exp);
- // use simple language to evaluate the expression, as it may use the simple language to refer to message body, headers etc.
- Expression expression = null;
- try {
- expression = exchange.getContext().resolveLanguage("simple").createExpression(exp);
- parameterValue = expression.evaluate(exchange, Object.class);
- // use "null" to indicate the expression returned a null value which is a valid response we need to honor
- if (parameterValue == null) {
- parameterValue = "null";
- }
- } catch (Exception e) {
- throw new ExpressionEvaluationException(
- expression, "Cannot create/evaluate simple expression: " + exp
- + " to be bound to parameter at index: " + index + " on method: " + getMethod(),
- exchange, e);
- }
+ if (isParameterTypeNotValue(exchange, exp, parameterType, varargs)) {
+ return null;
+ }
- // special for explicit null parameter values (as end users can explicit indicate they want null as parameter)
- // see method javadoc for details
- if ("null".equals(parameterValue)) {
- return Void.TYPE;
- }
+ parameterValue = evaluateSimpleExpression(exchange, index, exp);
- // the parameter value may match the expected type, then we use it as-is
- if (varargs || parameterType.isAssignableFrom(parameterValue.getClass())) {
- valid = true;
- } else {
- // String values from the simple language is always valid
- if (!valid) {
- valid = parameterValue instanceof String;
- if (!valid) {
- // the parameter value was not already valid, but since the simple language have evaluated the expression
- // which may change the parameterValue, so we have to check it again to see if it is now valid
- exp = exchange.getContext().getTypeConverter().tryConvertTo(String.class, parameterValue);
- // re-validate if the parameter was not valid the first time
- valid = BeanHelper.isValidParameterValue(exp);
- }
- }
- }
+ if ("null".equals(parameterValue)) {
+ return Void.TYPE;
+ }
- if (valid) {
- // we need to unquote String parameters, as the enclosing quotes is there to denote a parameter value
- if (parameterValue instanceof String string) {
- parameterValue = StringHelper.removeLeadingAndEndingQuotes(string);
- }
- if (varargs) {
- // use the value as-is
- answer = parameterValue;
- } else {
- try {
- // it is a valid parameter value, so convert it to the expected type of the parameter
- answer = exchange.getContext().getTypeConverter().mandatoryConvertTo(parameterType, exchange,
- parameterValue);
- if (LOG.isTraceEnabled()) {
- LOG.trace("Parameter #{} evaluated as: {} type: {}", index, answer,
- org.apache.camel.util.ObjectHelper.type(answer));
- }
- } catch (Exception e) {
- if (LOG.isDebugEnabled()) {
- LOG.debug("Cannot convert from type: {} to type: {} for parameter #{}",
- org.apache.camel.util.ObjectHelper.type(parameterValue), parameterType, index);
- }
- throw new ParameterBindingException(e, method, index, parameterType, parameterValue);
- }
- }
- }
+ boolean valid = isValidParameterValue(exchange, exp, parameterValue, parameterType, varargs);
+ if (!valid) {
+ return null;
}
- return answer;
+ return convertParameterValue(exchange, index, parameterValue, parameterType, varargs);
+ }
+
+ private String clipClassSuffix(String exp) {
+ int pos1 = exp.indexOf(' ');
+ int pos2 = exp.indexOf(".class");
+ if (pos1 != -1 && pos2 != -1 && pos1 > pos2) {
+ return exp.substring(pos2 + 7);
+ }
+ return exp;
+ }
+
+ private boolean isParameterTypeNotValue(Exchange exchange, String exp, Class<?> parameterType, boolean varargs) {
+ boolean valid = BeanHelper.isValidParameterValue(exp);
+ if (!valid && !varargs) {
+ Boolean isClass = BeanHelper.isAssignableToExpectedType(
+ exchange.getContext().getClassResolver(), exp, parameterType);
+ return isClass != null;
+ }
+ return false;
+ }
+
+ private Object evaluateSimpleExpression(Exchange exchange, int index, String exp) {
+ Expression expression = null;
+ try {
+ expression = exchange.getContext().resolveLanguage("simple").createExpression(exp);
+ Object result = expression.evaluate(exchange, Object.class);
+ return result != null ? result : "null";
+ } catch (Exception e) {
+ throw new ExpressionEvaluationException(
+ expression, "Cannot create/evaluate simple expression: " + exp
+ + " to be bound to parameter at index: " + index + " on method: " + getMethod(),
+ exchange, e);
+ }
+ }
+
+ private boolean isValidParameterValue(
+ Exchange exchange, String exp, Object parameterValue, Class<?> parameterType, boolean varargs) {
+ if (varargs || parameterType.isAssignableFrom(parameterValue.getClass())) {
+ return true;
+ }
+ boolean valid = BeanHelper.isValidParameterValue(exp);
+ if (valid) {
+ return true;
+ }
+ if (parameterValue instanceof String) {
+ return true;
+ }
+ String reEvaluated = exchange.getContext().getTypeConverter().tryConvertTo(String.class, parameterValue);
+ return BeanHelper.isValidParameterValue(reEvaluated);
+ }
+
+ private Object convertParameterValue(
+ Exchange exchange, int index, Object parameterValue, Class<?> parameterType, boolean varargs) {
+ if (parameterValue instanceof String string) {
+ parameterValue = StringHelper.removeLeadingAndEndingQuotes(string);
+ }
+ if (varargs) {
+ return parameterValue;
+ }
+ try {
+ Object answer = exchange.getContext().getTypeConverter().mandatoryConvertTo(
+ parameterType, exchange, parameterValue);
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Parameter #{} evaluated as: {} type: {}", index, answer,
+ org.apache.camel.util.ObjectHelper.type(answer));
+ }
+ return answer;
+ } catch (Exception e) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Cannot convert from type: {} to type: {} for parameter #{}",
+ org.apache.camel.util.ObjectHelper.type(parameterValue), parameterType, index);
+ }
+ throw new ParameterBindingException(e, method, index, parameterType, parameterValue);
+ }
}
/**
(camel) 01/04: (chores): reduce cognitive complexity in BeanInfo
Posted by or...@apache.org.
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 3fa0c1c3b1b218f90b7141edf4715d48b9fb847e
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Tue Jan 27 13:16:35 2026 +0000
(chores): reduce cognitive complexity in BeanInfo
Extract helper methods from chooseMethod: copyIfNotEmpty, filterMethodLists,
selectHandlerAnnotationMethod, hasSingleElement, selectSimpleMethodMatch,
selectFromPossibleOperations, selectByParameters.
Add null guards to removeAllAbstractMethods, removeNonMatchingMethods,
removeAllSetterOrGetterMethods.
---
.../org/apache/camel/component/bean/BeanInfo.java | 236 +++++++++++----------
1 file changed, 129 insertions(+), 107 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 557934d46062..1aab72b52ede 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
@@ -554,131 +554,144 @@ public class BeanInfo {
// and last then try to select the best among the rest
// must use defensive copy, to avoid altering the shared lists
- // and we want to remove unwanted operations from these local lists
- List<MethodInfo> localOperationsWithBody = null;
- if (!operationsWithBody.isEmpty()) {
- localOperationsWithBody = new ArrayList<>(operationsWithBody);
- }
- List<MethodInfo> localOperationsWithNoBody = null;
- if (!operationsWithNoBody.isEmpty()) {
- localOperationsWithNoBody = new ArrayList<>(operationsWithNoBody);
- }
- List<MethodInfo> localOperationsWithCustomAnnotation = null;
- if (!operationsWithCustomAnnotation.isEmpty()) {
- localOperationsWithCustomAnnotation = new ArrayList<>(operationsWithCustomAnnotation);
- }
- List<MethodInfo> localOperationsWithHandlerAnnotation = null;
- if (!operationsWithHandlerAnnotation.isEmpty()) {
- localOperationsWithHandlerAnnotation = new ArrayList<>(operationsWithHandlerAnnotation);
- }
+ List<MethodInfo> localOperationsWithBody = copyIfNotEmpty(operationsWithBody);
+ List<MethodInfo> localOperationsWithNoBody = copyIfNotEmpty(operationsWithNoBody);
+ List<MethodInfo> localOperationsWithCustomAnnotation = copyIfNotEmpty(operationsWithCustomAnnotation);
+ List<MethodInfo> localOperationsWithHandlerAnnotation = copyIfNotEmpty(operationsWithHandlerAnnotation);
- // remove all abstract methods
- if (localOperationsWithBody != null) {
- removeAllAbstractMethods(localOperationsWithBody);
- }
- if (localOperationsWithNoBody != null) {
- removeAllAbstractMethods(localOperationsWithNoBody);
+ // remove all abstract methods from all lists
+ removeAllAbstractMethods(localOperationsWithBody);
+ removeAllAbstractMethods(localOperationsWithNoBody);
+ removeAllAbstractMethods(localOperationsWithCustomAnnotation);
+ removeAllAbstractMethods(localOperationsWithHandlerAnnotation);
+
+ // filter or clean methods based on whether a name is specified
+ filterMethodLists(name, localOperationsWithHandlerAnnotation, localOperationsWithCustomAnnotation,
+ localOperationsWithBody, localOperationsWithNoBody);
+
+ // check for handler annotation methods
+ MethodInfo handlerMethod = selectHandlerAnnotationMethod(exchange, localOperationsWithHandlerAnnotation);
+ if (handlerMethod != null) {
+ return handlerMethod;
}
- if (localOperationsWithCustomAnnotation != null) {
- removeAllAbstractMethods(localOperationsWithCustomAnnotation);
+
+ // check for single custom annotation method
+ if (hasSingleElement(localOperationsWithCustomAnnotation)) {
+ return localOperationsWithCustomAnnotation.get(0);
}
- if (localOperationsWithHandlerAnnotation != null) {
- removeAllAbstractMethods(localOperationsWithHandlerAnnotation);
+
+ // check for simple cases
+ MethodInfo simpleMatch = selectSimpleMethodMatch(name, localOperationsWithBody,
+ localOperationsWithNoBody, localOperationsWithCustomAnnotation);
+ if (simpleMatch != null) {
+ return simpleMatch;
}
+ // try to find best match from possible operations
+ return selectFromPossibleOperations(exchange, name, localOperationsWithBody, localOperationsWithCustomAnnotation);
+ }
+
+ private List<MethodInfo> copyIfNotEmpty(List<MethodInfo> source) {
+ return source.isEmpty() ? null : new ArrayList<>(source);
+ }
+
+ private void filterMethodLists(
+ String name,
+ List<MethodInfo> handlerOps,
+ List<MethodInfo> customOps,
+ List<MethodInfo> bodyOps,
+ List<MethodInfo> noBodyOps) {
if (name != null) {
- // filter all lists to only include methods with this name
- if (localOperationsWithHandlerAnnotation != null) {
- removeNonMatchingMethods(localOperationsWithHandlerAnnotation, name);
- }
- if (localOperationsWithCustomAnnotation != null) {
- removeNonMatchingMethods(localOperationsWithCustomAnnotation, name);
- }
- if (localOperationsWithBody != null) {
- removeNonMatchingMethods(localOperationsWithBody, name);
- }
- if (localOperationsWithNoBody != null) {
- removeNonMatchingMethods(localOperationsWithNoBody, name);
- }
+ removeNonMatchingMethods(handlerOps, name);
+ removeNonMatchingMethods(customOps, name);
+ removeNonMatchingMethods(bodyOps, name);
+ removeNonMatchingMethods(noBodyOps, name);
} else {
- // remove all getter/setter as we do not want to consider these methods
- if (localOperationsWithHandlerAnnotation != null) {
- removeAllSetterOrGetterMethods(localOperationsWithHandlerAnnotation);
- }
- if (localOperationsWithCustomAnnotation != null) {
- removeAllSetterOrGetterMethods(localOperationsWithCustomAnnotation);
- }
- if (localOperationsWithBody != null) {
- removeAllSetterOrGetterMethods(localOperationsWithBody);
- }
- if (localOperationsWithNoBody != null) {
- removeAllSetterOrGetterMethods(localOperationsWithNoBody);
- }
+ removeAllSetterOrGetterMethods(handlerOps);
+ removeAllSetterOrGetterMethods(customOps);
+ removeAllSetterOrGetterMethods(bodyOps);
+ removeAllSetterOrGetterMethods(noBodyOps);
}
+ }
- if (localOperationsWithHandlerAnnotation != null && localOperationsWithHandlerAnnotation.size() > 1) {
- // if we have more than 1 @Handler then its ambiguous
- throw new AmbiguousMethodCallException(exchange, localOperationsWithHandlerAnnotation);
+ private MethodInfo selectHandlerAnnotationMethod(Exchange exchange, List<MethodInfo> handlerOps)
+ throws AmbiguousMethodCallException {
+ if (handlerOps == null) {
+ return null;
}
-
- if (localOperationsWithHandlerAnnotation != null && localOperationsWithHandlerAnnotation.size() == 1) {
- // methods with handler should be preferred
- return localOperationsWithHandlerAnnotation.get(0);
- } else if (localOperationsWithCustomAnnotation != null && localOperationsWithCustomAnnotation.size() == 1) {
- // if there is one method with an annotation then use that one
- return localOperationsWithCustomAnnotation.get(0);
+ if (handlerOps.size() > 1) {
+ throw new AmbiguousMethodCallException(exchange, handlerOps);
}
+ if (handlerOps.size() == 1) {
+ return handlerOps.get(0);
+ }
+ return null;
+ }
+
+ private boolean hasSingleElement(List<?> list) {
+ return list != null && list.size() == 1;
+ }
- // named method and with no parameters
+ private MethodInfo selectSimpleMethodMatch(
+ String name,
+ List<MethodInfo> bodyOps,
+ List<MethodInfo> noBodyOps,
+ List<MethodInfo> customOps) {
boolean noParameters = name != null && name.endsWith("()");
- if (noParameters && localOperationsWithNoBody != null && localOperationsWithNoBody.size() == 1) {
- // if there was a method name configured and it has no parameters, then use the method with no body (eg no parameters)
- return localOperationsWithNoBody.get(0);
- } else if (!noParameters && localOperationsWithBody != null && localOperationsWithBody.size() == 1
- && localOperationsWithCustomAnnotation == null) {
- // if there is one method with body then use that one
- return localOperationsWithBody.get(0);
- }
-
- if (localOperationsWithBody != null || localOperationsWithCustomAnnotation != null) {
- Collection<MethodInfo> possibleOperations = new ArrayList<>();
- if (localOperationsWithBody != null) {
- possibleOperations.addAll(localOperationsWithBody);
- }
- if (localOperationsWithCustomAnnotation != null) {
- possibleOperations.addAll(localOperationsWithCustomAnnotation);
- }
+ if (noParameters && hasSingleElement(noBodyOps)) {
+ return noBodyOps.get(0);
+ }
+ if (!noParameters && hasSingleElement(bodyOps) && customOps == null) {
+ return bodyOps.get(0);
+ }
+ return null;
+ }
- if (!possibleOperations.isEmpty()) {
- MethodInfo answer = null;
+ private MethodInfo selectFromPossibleOperations(
+ Exchange exchange, String name,
+ List<MethodInfo> bodyOps,
+ List<MethodInfo> customOps)
+ throws AmbiguousMethodCallException {
+ if (bodyOps == null && customOps == null) {
+ return null;
+ }
- if (name != null) {
- // do we have hardcoded parameters values provided from the method name then use that for matching
- String parameters = StringHelper.between(name, "(", ")");
- if (parameters != null) {
- // special as we have hardcoded parameters, so we need to choose method that matches those parameters the best
- LOG.trace("Choosing best matching method matching parameters: {}", parameters);
- answer = chooseMethodWithMatchingParameters(exchange, parameters, possibleOperations);
- }
- }
- if (answer == null) {
- // multiple possible operations so find the best suited if possible
- answer = chooseMethodWithMatchingBody(exchange, possibleOperations, localOperationsWithCustomAnnotation);
- }
- if (answer == null && possibleOperations.size() > 1) {
- answer = getSingleCovariantMethod(possibleOperations);
- }
+ Collection<MethodInfo> possibleOperations = new ArrayList<>();
+ if (bodyOps != null) {
+ possibleOperations.addAll(bodyOps);
+ }
+ if (customOps != null) {
+ possibleOperations.addAll(customOps);
+ }
- if (answer == null) {
- throw new AmbiguousMethodCallException(exchange, possibleOperations);
- } else {
- return answer;
- }
- }
+ if (possibleOperations.isEmpty()) {
+ return null;
}
- // not possible to determine
- return null;
+ MethodInfo answer = selectByParameters(exchange, name, possibleOperations);
+ if (answer == null) {
+ answer = chooseMethodWithMatchingBody(exchange, possibleOperations, customOps);
+ }
+ if (answer == null && possibleOperations.size() > 1) {
+ answer = getSingleCovariantMethod(possibleOperations);
+ }
+ if (answer == null) {
+ throw new AmbiguousMethodCallException(exchange, possibleOperations);
+ }
+ return answer;
+ }
+
+ private MethodInfo selectByParameters(Exchange exchange, String name, Collection<MethodInfo> operations)
+ throws AmbiguousMethodCallException {
+ if (name == null) {
+ return null;
+ }
+ String parameters = StringHelper.between(name, "(", ")");
+ if (parameters == null) {
+ return null;
+ }
+ LOG.trace("Choosing best matching method matching parameters: {}", parameters);
+ return chooseMethodWithMatchingParameters(exchange, parameters, operations);
}
private MethodInfo chooseMethodWithMatchingParameters(
@@ -1084,6 +1097,9 @@ public class BeanInfo {
}
private static void removeAllSetterOrGetterMethods(List<MethodInfo> methods) {
+ if (methods == null) {
+ return;
+ }
Iterator<MethodInfo> it = methods.iterator();
while (it.hasNext()) {
MethodInfo info = it.next();
@@ -1098,11 +1114,17 @@ public class BeanInfo {
}
private void removeNonMatchingMethods(List<MethodInfo> methods, String name) {
+ if (methods == null) {
+ return;
+ }
// method does not match so remove it
methods.removeIf(info -> !matchMethod(info.getMethod(), name));
}
private void removeAllAbstractMethods(List<MethodInfo> methods) {
+ if (methods == null) {
+ return;
+ }
Iterator<MethodInfo> it = methods.iterator();
while (it.hasNext()) {
MethodInfo info = it.next();
(camel) 03/04: (chores): reduce cognitive complexity in BeanExpression
Posted by or...@apache.org.
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 <an...@gmail.com>
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);