This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch fix/CAMEL-24962 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 7713bf5e0ddab623aa0bbaa87ce08f379c7835ae Author: Claus Ibsen <[email protected]> AuthorDate: Wed Sep 23 19:15:32 2026 +0200 CAMEL-24971: simple - internal tidy-ups - remove the static SimpleLanguage singleton that was only written, never read - remove the always-true strict flag in SimpleFunctionExpression - import ObjectHelper instead of the fully qualified name in the math and collection builders - the logical operators && and || build their predicate once, not for every message - concat names its arguments first/second, and concat and pad show all arguments in toString - move the orphaned Javadoc to normalizeWhitespaceExpression Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../simple/CollectionExpressionBuilder.java | 14 +++--- .../language/simple/MathExpressionBuilder.java | 10 ++--- .../camel/language/simple/SimpleLanguage.java | 7 +-- .../language/simple/StringExpressionBuilder.java | 22 ++++++---- .../language/simple/ast/LogicalExpression.java | 30 +++---------- .../simple/ast/SimpleFunctionExpression.java | 51 +++++++++------------- 6 files changed, 54 insertions(+), 80 deletions(-) diff --git a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/CollectionExpressionBuilder.java b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/CollectionExpressionBuilder.java index 5ef3370a872a..d8c5b9ac15c6 100644 --- a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/CollectionExpressionBuilder.java +++ b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/CollectionExpressionBuilder.java @@ -35,8 +35,8 @@ import org.apache.camel.Predicate; import org.apache.camel.spi.ClassResolver; import org.apache.camel.support.ExchangeHelper; import org.apache.camel.support.ExpressionAdapter; +import org.apache.camel.support.ObjectHelper; import org.apache.camel.support.builder.ExpressionBuilder; -import org.apache.camel.util.ObjectHelper; /** * Expression builder for collection and list functions used by the simple language. @@ -69,7 +69,7 @@ public final class CollectionExpressionBuilder { for (Expression exp : exps) { Object o = exp.evaluate(exchange, Object.class); // this may be an object that we can iterate - Iterable<?> it = org.apache.camel.support.ObjectHelper.createIterable(o); + Iterable<?> it = ObjectHelper.createIterable(o); for (Object i : it) { answer.add(i); } @@ -107,7 +107,7 @@ public final class CollectionExpressionBuilder { for (Expression exp : exps) { Object o = exp.evaluate(exchange, Object.class); // this may be an object that we can iterate - Iterable<?> it = org.apache.camel.support.ObjectHelper.createIterable(o); + Iterable<?> it = ObjectHelper.createIterable(o); for (Object i : it) { answer.add(i); } @@ -146,7 +146,7 @@ public final class CollectionExpressionBuilder { for (Expression exp : exps) { Object o = exp.evaluate(exchange, Object.class); // this may be an object that we can iterate - Iterable<?> it = org.apache.camel.support.ObjectHelper.createIterable(o); + Iterable<?> it = ObjectHelper.createIterable(o); for (Object i : it) { answer.add(i); } @@ -319,7 +319,7 @@ public final class CollectionExpressionBuilder { List answer = new ArrayList<>(); Object o = exp.evaluate(exchange, Object.class); // this may be an object that we can iterate - Iterable<?> it = org.apache.camel.support.ObjectHelper.createIterable(o); + Iterable<?> it = ObjectHelper.createIterable(o); for (Object i : it) { answer.add(i); } @@ -359,7 +359,7 @@ public final class CollectionExpressionBuilder { public Object evaluate(Exchange exchange) { List<Object> answer = new ArrayList<>(); Object o = exp1.evaluate(exchange, Object.class); - Iterable<?> it = org.apache.camel.support.ObjectHelper.createIterable(o); + Iterable<?> it = ObjectHelper.createIterable(o); for (Object i : it) { // use a dummy exchange as the input is to be the message body Exchange dummy = ExchangeHelper.createCopy(exchange, true); @@ -558,7 +558,7 @@ public final class CollectionExpressionBuilder { public Object evaluate(Exchange exchange) { List<Object> answer = new ArrayList<>(); Object o = exp1.evaluate(exchange, Object.class); - Iterable<?> it = org.apache.camel.support.ObjectHelper.createIterable(o); + Iterable<?> it = ObjectHelper.createIterable(o); for (Object i : it) { // use a dummy exchange as the input is to be the message body Exchange dummy = ExchangeHelper.createCopy(exchange, true); diff --git a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/MathExpressionBuilder.java b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/MathExpressionBuilder.java index bcc830538791..dba909cd269c 100644 --- a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/MathExpressionBuilder.java +++ b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/MathExpressionBuilder.java @@ -22,7 +22,7 @@ import org.apache.camel.CamelContext; import org.apache.camel.Exchange; import org.apache.camel.Expression; import org.apache.camel.support.ExpressionAdapter; -import org.apache.camel.util.ObjectHelper; +import org.apache.camel.support.ObjectHelper; /** * Expression builder for numeric and math functions used by the simple language. @@ -180,7 +180,7 @@ public final class MathExpressionBuilder { for (Expression exp : exps) { Object o = exp.evaluate(exchange, Object.class); // this may be an object that we can iterate - Iterable<?> it = org.apache.camel.support.ObjectHelper.createIterable(o); + Iterable<?> it = ObjectHelper.createIterable(o); for (Object i : it) { Long val = exchange.getContext().getTypeConverter().tryConvertTo(Long.class, exchange, i); if (val != null) { @@ -224,7 +224,7 @@ public final class MathExpressionBuilder { for (Expression exp : exps) { Object o = exp.evaluate(exchange, Object.class); // this may be an object that we can iterate - Iterable<?> it = org.apache.camel.support.ObjectHelper.createIterable(o); + Iterable<?> it = ObjectHelper.createIterable(o); for (Object i : it) { Long val = exchange.getContext().getTypeConverter().tryConvertTo(Long.class, exchange, i); if (val != null) { @@ -268,7 +268,7 @@ public final class MathExpressionBuilder { for (Expression exp : exps) { Object o = exp.evaluate(exchange, Object.class); // this may be an object that we can iterate - Iterable<?> it = org.apache.camel.support.ObjectHelper.createIterable(o); + Iterable<?> it = ObjectHelper.createIterable(o); for (Object i : it) { Long val = exchange.getContext().getTypeConverter().tryConvertTo(Long.class, exchange, i); if (val != null) { @@ -313,7 +313,7 @@ public final class MathExpressionBuilder { for (Expression exp : exps) { Object o = exp.evaluate(exchange, Object.class); // this may be an object that we can iterate - Iterable<?> it = org.apache.camel.support.ObjectHelper.createIterable(o); + Iterable<?> it = ObjectHelper.createIterable(o); for (Object i : it) { Long val = exchange.getContext().getTypeConverter().tryConvertTo(Long.class, exchange, i); if (val != null) { diff --git a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java index c199f275b586..ddfd3f7c4b0d 100644 --- a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java +++ b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java @@ -44,9 +44,6 @@ public class SimpleLanguage extends LanguageSupport implements StaticService { private static final Logger LOG = LoggerFactory.getLogger(SimpleLanguage.class); - // singleton for expressions without a result type - private static final SimpleLanguage SIMPLE = new SimpleLanguage(); - // a special prefix to avoid cache clash private static final String CACHE_KEY_PREFIX = "@SIMPLE@"; @@ -87,9 +84,7 @@ public class SimpleLanguage extends LanguageSupport implements StaticService { @Override public void start() { - if (getCamelContext() != null) { - SIMPLE.setCamelContext(getCamelContext()); - } + // noop } @Override diff --git a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/StringExpressionBuilder.java b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/StringExpressionBuilder.java index e6bbf4d0bed6..12ea1031c213 100644 --- a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/StringExpressionBuilder.java +++ b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/StringExpressionBuilder.java @@ -302,7 +302,10 @@ public final class StringExpressionBuilder { @Override public String toString() { - return "pad(" + exp + "," + length + ")"; + if (separator != null) { + return "pad(" + expression + "," + length + "," + separator + ")"; + } + return "pad(" + expression + "," + length + ")"; } }; } @@ -310,16 +313,16 @@ public final class StringExpressionBuilder { /** * String concats the two expressions. */ - public static Expression concatExpression(final String right, final String left, String separator) { + public static Expression concatExpression(final String first, final String second, String separator) { return new ExpressionAdapter() { private Expression exp1; private Expression exp2; @Override public void init(CamelContext context) { - exp1 = context.resolveLanguage("simple").createExpression(right); + exp1 = context.resolveLanguage("simple").createExpression(first); exp1.init(context); - exp2 = context.resolveLanguage("simple").createExpression(left); + exp2 = context.resolveLanguage("simple").createExpression(second); exp2.init(context); } @@ -336,7 +339,10 @@ public final class StringExpressionBuilder { @Override public String toString() { - return "concat(" + right + "," + left + ")"; + if (separator != null) { + return "concat(" + first + "," + second + "," + separator + ")"; + } + return "concat(" + first + "," + second + ")"; } }; } @@ -539,9 +545,6 @@ public final class StringExpressionBuilder { }; } - /** - * Normalizes the whitespaces in the given expressions (uses message body if expression is null) - */ /** * Returns an expression that escapes special characters in the given expression (or message body if <tt>null</tt>) * according to the escape kind. @@ -584,6 +587,9 @@ public final class StringExpressionBuilder { }; } + /** + * Normalizes the whitespaces in the given expressions (uses message body if expression is null) + */ public static Expression normalizeWhitespaceExpression(final String expression) { return new ExpressionAdapter() { private Expression exp; diff --git a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/LogicalExpression.java b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/LogicalExpression.java index c59dcf233f60..f4e0c37ca1be 100644 --- a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/LogicalExpression.java +++ b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/LogicalExpression.java @@ -93,40 +93,22 @@ public class LogicalExpression extends BaseSimpleNode { final Expression leftExp = left.createExpression(camelContext, expression); final Expression rightExp = right.createExpression(camelContext, expression); + // build the predicate once, not for every message + final Predicate leftPredicate = ExpressionToPredicateAdapter.toPredicate(leftExp); + final Predicate rightPredicate = ExpressionToPredicateAdapter.toPredicate(rightExp); if (operator == LogicalOperatorType.AND) { - return createAndExpression(leftExp, rightExp); + return createExpression(PredicateBuilder.and(leftPredicate, rightPredicate)); } else if (operator == LogicalOperatorType.OR) { - return createOrExpression(leftExp, rightExp); + return createExpression(PredicateBuilder.or(leftPredicate, rightPredicate)); } throw new SimpleParserException("Unknown logical operator " + operator, token.getIndex()); } - private Expression createAndExpression(final Expression leftExp, final Expression rightExp) { + private Expression createExpression(final Predicate predicate) { return new Expression() { @Override public <T> T evaluate(Exchange exchange, Class<T> type) { - Predicate predicate = ExpressionToPredicateAdapter.toPredicate(leftExp); - predicate = PredicateBuilder.and(predicate, ExpressionToPredicateAdapter.toPredicate(rightExp)); - - boolean answer = predicate.matches(exchange); - return exchange.getContext().getTypeConverter().convertTo(type, answer); - } - - @Override - public String toString() { - return left + " " + token.getText() + " " + right; - } - }; - } - - private Expression createOrExpression(final Expression leftExp, final Expression rightExp) { - return new Expression() { - @Override - public <T> T evaluate(Exchange exchange, Class<T> type) { - Predicate predicate = ExpressionToPredicateAdapter.toPredicate(leftExp); - predicate = PredicateBuilder.or(predicate, ExpressionToPredicateAdapter.toPredicate(rightExp)); - boolean answer = predicate.matches(exchange); return exchange.getContext().getTypeConverter().convertTo(type, answer); } diff --git a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java index dc5204024177..00a839c12a32 100644 --- a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java +++ b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java @@ -61,7 +61,7 @@ public class SimpleFunctionExpression extends LiteralExpression { Expression answer = cacheExpression != null ? cacheExpression.get(function) : null; if (answer == null) { - answer = createSimpleExpression(camelContext, function, true); + answer = createSimpleExpression(camelContext, function); if (answer != null) { answer.init(camelContext); } @@ -74,7 +74,7 @@ public class SimpleFunctionExpression extends LiteralExpression { return answer; } - private Expression createSimpleExpression(CamelContext camelContext, String function, boolean strict) { + private Expression createSimpleExpression(CamelContext camelContext, String function) { Class<?> type = null; // is it a known result type (make it easy in simple to return the value as you need) @@ -94,7 +94,7 @@ public class SimpleFunctionExpression extends LiteralExpression { type = String.class; function = function.substring(7); } - Expression exp = doCreateSimpleExpression(camelContext, function, strict); + Expression exp = doCreateSimpleExpression(camelContext, function); if (type != null) { exp = ExpressionBuilder.convertToExpression(exp, type); } @@ -136,14 +136,12 @@ public class SimpleFunctionExpression extends LiteralExpression { }; } - private Expression doCreateSimpleExpression(CamelContext camelContext, String function, boolean strict) { - if (strict) { - // ${body != null && body.size() > 0}: the braces hold a predicate, which is what they hold in EL, - // Groovy and a JavaScript template, so read it as one (CAMEL-24921) - Expression predicate = createPredicateExpression(camelContext, function); - if (predicate != null) { - return predicate; - } + private Expression doCreateSimpleExpression(CamelContext camelContext, String function) { + // ${body != null && body.size() > 0}: the braces hold a predicate, which is what they hold in EL, + // Groovy and a JavaScript template, so read it as one (CAMEL-24921) + Expression predicate = createPredicateExpression(camelContext, function); + if (predicate != null) { + return predicate; } // return the function directly if we can create function without analyzing the prefix Expression answer = DIRECT_FACTORY.createFunction(camelContext, function, token.getIndex()); @@ -159,7 +157,7 @@ public class SimpleFunctionExpression extends LiteralExpression { // do not create file expressions but keep the function as-is as a constant value fileExpression = ExpressionBuilder.constantExpression("${" + function + "}"); } else { - fileExpression = createSimpleFileExpression(remainder, strict); + fileExpression = createSimpleFileExpression(remainder); } if (fileExpression != null) { return fileExpression; @@ -193,17 +191,13 @@ public class SimpleFunctionExpression extends LiteralExpression { } } - if (strict) { - String hint = SimpleSyntaxHints.unknownFunction(function); - throw new SimpleParserException( - "Unknown function: " + function + (hint != null ? " (" + hint + ")" : ""), - token.getIndex()); - } else { - return null; - } + String hint = SimpleSyntaxHints.unknownFunction(function); + throw new SimpleParserException( + "Unknown function: " + function + (hint != null ? " (" + hint + ")" : ""), + token.getIndex()); } - private Expression createSimpleFileExpression(String remainder, boolean strict) { + private Expression createSimpleFileExpression(String remainder) { if (ObjectHelper.equal(remainder, "name")) { return FileExpressionBuilder.fileNameExpression(); } else if (ObjectHelper.equal(remainder, "name.noext")) { @@ -233,15 +227,12 @@ public class SimpleFunctionExpression extends LiteralExpression { } else if (ObjectHelper.equal(remainder, "modified")) { return FileExpressionBuilder.fileLastModifiedExpression(); } - if (strict) { - throw new SimpleParserException( - "Unknown file language syntax: " + remainder + " (the file: functions describe the file being consumed:" - + " ${file:name}, ${file:size}, ${file:parent}, ${file:absolute.path};" - + " they do not read a file. To read a file into the body use the poll" - + " EIP with a file: endpoint)", - token.getIndex()); - } - return null; + throw new SimpleParserException( + "Unknown file language syntax: " + remainder + " (the file: functions describe the file being consumed:" + + " ${file:name}, ${file:size}, ${file:parent}, ${file:absolute.path};" + + " they do not read a file. To read a file into the body use the poll" + + " EIP with a file: endpoint)", + token.getIndex()); } @Deprecated(since = "4.21")
