Path: redb-route/redb.Route/Expressions/BeanMethodExpression.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Expressions/BeanMethodExpression.cs
----
using redb.Route.Abstractions;
using redb.Route.Components.Bean;
using redb.Route.Predicates;
using redb.Route.Core;
namespace redb.Route.Expressions;
///
/// What builds: a registry bean's method as an expression and a condition,
/// Apache Camel's method(ref, "name"). The same call as ${bean:name.Method(args)}, typed and written in
/// the bean: URI spelling (IsGold(${header.name})), through the same binding and the same per-context
/// resolution. One difference: in a condition the engine awaits , so a method
/// returning Task<bool> works there; a value position () is synchronous and refuses
/// it as the function does.
///
/// Exposed as , so Filter(Method(...)) picks the expression overload
/// without ambiguity; the condition path finds the it also is.
///
///
internal sealed class BeanMethodExpression : Expression, IPredicateExpression, IPredicate
{
private readonly string _reference;
private readonly string _spec;
private readonly StringExpression[] _arguments;
private readonly BeanCallSite _site;
internal BeanMethodExpression(string reference, string methodSpec)
{
ArgumentException.ThrowIfNullOrWhiteSpace(reference);
ArgumentException.ThrowIfNullOrWhiteSpace(methodSpec);
var name = reference.Trim();
if (name.Contains(',', StringComparison.Ordinal) || (!name.StartsWith('#') && name.Contains('.', StringComparison.Ordinal)))
throw new ArgumentException(
$"Method(\"{reference}\", ...): in an expression the bean is a registry name (\"#name\"). " +
"Declare an instance of a type with or AddToRegistry first.", nameof(reference));
_reference = name.TrimStart('#');
_spec = methodSpec.Trim();
var (method, arguments) = BeanInvoker.ParseMethodSpec(_spec, "#" + _reference);
_arguments = [.. arguments.Select(a => new StringExpression(a))];
_site = new BeanCallSite(_reference, method, _arguments.Length);
}
private object?[] Values(IExchange exchange) => [.. _arguments.Select(a => a.Evaluate