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
commit fbbb2753c14139472612fedae4529043fc3dd990 Author: Claus Ibsen <[email protected]> AuthorDate: Mon Apr 3 21:09:28 2023 +0200 CAMEL-19244: camel-bean - Add support for declaring type in parameter values using 'type.class value' style --- .../apache/camel/component/bean/BeanHelper.java | 22 +++++++++++++++++----- .../org/apache/camel/component/bean/BeanInfo.java | 4 ++-- .../apache/camel/component/bean/MethodInfo.java | 13 +++++++++---- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanHelper.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanHelper.java index 25273912640..68bd15062c9 100644 --- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanHelper.java +++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanHelper.java @@ -35,7 +35,7 @@ public final class BeanHelper { * @param value the value * @return the parameter type the given value is being mapped as, or <tt>null</tt> if not valid. */ - public static Class<?> getValidParameterType(String value) { + public static Class<?> getValidParameterType(ClassResolver resolver, String value) { if (ObjectHelper.isEmpty(value)) { return null; } @@ -70,7 +70,12 @@ public final class BeanHelper { // numeric is valid boolean numeric = true; - for (char ch : value.toCharArray()) { + char[] chars = value.toCharArray(); + for (int i = 0; i < chars.length; i++) { + char ch = chars[i]; + if (i == 0 && ch == '-') { + continue; + } if (!Character.isDigit(ch)) { numeric = false; break; @@ -90,13 +95,13 @@ public final class BeanHelper { * @param value the value * @return <tt>true</tt> if valid, <tt>false</tt> otherwise */ - public static boolean isValidParameterValue(String value) { + public static boolean isValidParameterValue(ClassResolver classResolver, String value) { if (ObjectHelper.isEmpty(value)) { // empty value is valid return true; } - return getValidParameterType(value) != null; + return getValidParameterType(classResolver, value) != null; } /** @@ -117,7 +122,14 @@ public final class BeanHelper { * assignable, <tt>false</tt> if not assignable */ public static Boolean isAssignableToExpectedType(ClassResolver resolver, String parameterType, Class<?> expectedType) { - if (parameterType == null || !parameterType.endsWith(".class")) { + if (parameterType == null || !parameterType.contains(".class")) { + // not a class so return null + return null; + } + if (parameterType.contains(" ")) { + parameterType = StringHelper.before(parameterType, " "); + } + if (!parameterType.endsWith(".class")) { // not a class so return null return null; } 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 baf5febbb9a..b918711dc1a 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 @@ -682,7 +682,7 @@ public class BeanInfo { parameter = parameter.trim(); } - Class<?> parameterType = BeanHelper.getValidParameterType(parameter); + Class<?> parameterType = BeanHelper.getValidParameterType(exchange.getContext().getClassResolver(), parameter); Class<?> expectedType = info.getParameters().get(index).getType(); if (parameterType != null && expectedType != null) { @@ -1113,7 +1113,7 @@ public class BeanInfo { continue; } - if (BeanHelper.isValidParameterValue(qualifyType)) { + if (BeanHelper.isValidParameterValue(getCamelContext().getClassResolver(), qualifyType)) { // its a parameter value, so continue to next parameter // as we should only check for FQN/type parameters continue; 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 0a402f2e334..ee1b5cbb06f 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 @@ -259,7 +259,6 @@ public class MethodInfo { if (hasParameters) { if (parametersExpression != null) { parametersExpression.init(camelContext); - return parametersExpression.evaluate(exchange, Object[].class); } } @@ -648,10 +647,16 @@ public class MethodInfo { // convert the parameter value to a String String exp = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, parameterValue); + boolean valid; if (exp != null) { - // check if its a valid parameter value - boolean valid = BeanHelper.isValidParameterValue(exp); + int pos1 = exp.indexOf(' '); + int pos2 = exp.indexOf(".class"); + if (pos1 != -1 && pos2 != -1 && pos1 > pos2) { + exp = exp.substring(pos2 + 7); // clip <space>.class + } + // check if its a valid parameter value (no type declared via .class syntax) + valid = BeanHelper.isValidParameterValue(exchange.getContext().getClassResolver(), exp); if (!valid) { // it may be a parameter type instead, and if so, then we should return null, // as this method is only for evaluating parameter values @@ -697,7 +702,7 @@ public class MethodInfo { // 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); + valid = BeanHelper.isValidParameterValue(exchange.getContext().getClassResolver(), exp); } } }
