Interface VarFunction

All Known Implementing Classes:
ArithmeticFunctions.Abs, ArithmeticFunctions.Add, ArithmeticFunctions.Divide, ArithmeticFunctions.Max, ArithmeticFunctions.Min, ArithmeticFunctions.Modulo, ArithmeticFunctions.Multiply, ArithmeticFunctions.Subtract, BooleanFunctions.And, BooleanFunctions.Eq, BooleanFunctions.Gt, BooleanFunctions.Gte, BooleanFunctions.Lt, BooleanFunctions.Lte, BooleanFunctions.Neq, BooleanFunctions.Not, BooleanFunctions.Or, BooleanFunctions.Xor, ConditionalFunctions.Coalesce, ConditionalFunctions.If, ConditionalFunctions.NotEmpty, ConditionalFunctions.Switch, DateFunctions.FormatDate, DateFunctions.Now, DateFunctions.ParseDate, EncodingFunctions.Base64Decode, EncodingFunctions.Base64Encode, EncodingFunctions.HtmlEscape, EncodingFunctions.HtmlUnescape, EncodingFunctions.UrlDecode, EncodingFunctions.UrlEncode, JsonFunctions.Get, JsonFunctions.JsonPath, JsonFunctions.Keys, JsonFunctions.Size, JsonFunctions.Values, RandomFunctions.Rand, RandomFunctions.RandChoice, RandomFunctions.RandInt, RandomFunctions.RandLong, RandomFunctions.RandString, RandomFunctions.Uuid, RegexFunctions.Extract, RegexFunctions.Match, RegexFunctions.ReplaceRegex, StringFunctions.Concat, StringFunctions.Contains, StringFunctions.EndsWith, StringFunctions.Format, StringFunctions.Join, StringFunctions.Len, StringFunctions.Lower, StringFunctions.PathToken, StringFunctions.Repeat, StringFunctions.Replace, StringFunctions.Reverse, StringFunctions.Split, StringFunctions.StartsWith, StringFunctions.StripLeading, StringFunctions.StripSlashes, StringFunctions.StripTrailing, StringFunctions.Substring, StringFunctions.Trim, StringFunctions.Upper, TypeConversionFunctions.ToBool, TypeConversionFunctions.ToDouble, TypeConversionFunctions.ToInt, TypeConversionFunctions.ToLong, TypedFunction

public interface VarFunction
SPI for SVL #{...} script-evaluation functions.

A VarFunction is invoked when an SVL template contains a #{name(arg1, arg2, ...)} reference — distinct from $X{...} variables. Functions compose freely with ${...} lookups and other #{...} calls; arguments are pre-resolved by the runtime before invoke is called.

Naming note:

Named VarFunction rather than the shorter Function because Juneau's serializer/parser/test code makes heavy use of Function and routinely co-imports org.apache.juneau.commons.svl.*; a bare Function type in this package would trigger ambiguous-reference errors across many callers. The chosen name also matches the existing Var/VarResolver/VarTemplate/VarResolverSession naming family.

Implementations can register through any of four discovery channels (later wins on name collision):

  1. The built-in catalog auto-registered by VarResolver.DEFAULT.
  2. META-INF/services/org.apache.juneau.commons.svl.VarFunction (Java ServiceLoader).
  3. BeanStore.getBeans(VarFunction.class) on the resolver's bean store.
  4. Explicit registrations via VarResolver.Builder.functions(...).

For typed reflection-based dispatch (the recommended path), extend TypedFunction — it derives minArity(), maxArity(), and per-arg coercion automatically from the subclass's invoke(...) method signature, eliminating the per-function plumbing.

Example:

// Direct VarFunction impl (less common — TypedFunction is preferred). public class Reverse implements VarFunction { @Override public String name() { return "reverse"; } @Override public int minArity() { return 1; } @Override public int maxArity() { return 1; } @Override public String invoke(VarResolverSession session, List<Object> args) { return new StringBuilder((String) args.get(0)).reverse().toString(); } }

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    Invokes this function with already-resolved, type-coerced arguments.
    int
    Maximum argument count this function accepts.
    int
    Minimum argument count this function accepts.
    The function's invocation name as it appears between #{} and the opening parenthesis in the template.
  • Method Details

    • name

      The function's invocation name as it appears between #{} and the opening parenthesis in the template. Must consist of ASCII letters, digits, and underscores; must begin with a letter.
      Returns:
      The invocation name. Never null or empty.
    • minArity

      int minArity()
      Minimum argument count this function accepts.
      Returns:
      The minimum number of args. 0 means the function may be called with no args. Returns -1 for "any number of args" — equivalent to declaring minArity == 0 && maxArity == Integer.MAX_VALUE.
    • maxArity

      int maxArity()
      Maximum argument count this function accepts.
      Returns:
      The maximum number of args, or Integer.MAX_VALUE for unbounded variadic functions.
    • invoke

      Invokes this function with already-resolved, type-coerced arguments.

      The runtime resolves each #{...} arg template against the session, then coerces the resolved string to the target Java type via ArgCoercer. The resulting List<Object> is passed in here.

      Parameters:
      session - The current resolver session. Must not be null.
      args - The coerced arguments. Size is in [minArity(), maxArity()]. Each element is the type the function expects per its signature; for variadic / Object-typed slots, elements remain String.
      Returns:
      The function's result as a String. May be empty but should not be null (the runtime treats null as the empty string for SVL output).