Class VarResolver
Variables are of the form
The variable key can contain anything, even nested variables that get recursively resolved.
Variables are defined through the VarResolver.Builder.vars(Class[]) method.
The Var interface defines how variables are converted to values.
Example:
See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final VarResolverDefault string variable resolver with support for system properties, environment variables, and the built-inVarFunctioncatalog. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T> VarResolverAdds a bean to this session.Compiles a template string into a reusableVarTemplate.copy()Returns a new builder object using the settings in this resolver as a base.static VarResolver.Buildercreate()Instantiates a new clean-slateVarResolver.Builderobject.Creates a new resolver session with no session objects.createSession(WritableBeanStore beanStore) Same ascreateSession()except allows you to specify a bean store for resolving beans.protected Map<String,VarFunction> Returns an unmodifiable map ofVarFunctionsassociated with this context.Returns an unmodifiable map ofVarsassociated with this context.protected Var[]getVars()Returns an array of variables define in this variable resolver context.Resolve variables in the specified string.resolveSupplier(String input) Returns a threadsafeSupplierthat resolves the given template against this resolver each timeSupplier.get()is called.voidResolve variables in the specified string and sends the results to the specified writer.
-
Field Details
-
DEFAULT
Default string variable resolver with support for system properties, environment variables, and the built-inVarFunctioncatalog.Built-in
Varbindings:$S{key[,default]} -SystemPropertiesVar$E{key[,default]} -EnvVariablesVar$EF{key[,default]} -EnvFileVar$DE{key[,default]} -DotenvVar$A{key[,default]} -ArgsVar$MF{key[,default]} -ManifestFileVar
Built-in
VarFunctioncatalog:Ships ~68 built-in functions across 10 categories — string, type-conversion, arithmetic, boolean, conditional, regex, encoding, date/time, random/UUID, and JSON-navigation. Functions are invoked via the
#{name(args...)}syntax. SeeVarResolver.Builder.defaultFunctions()for the registration mechanism.The 11 transformation/conditional
Vars previously bundled here (SwitchVar,IfVar,CoalesceVar,PatternMatchVar,PatternReplaceVar,PatternExtractVar,UpperCaseVar,LowerCaseVar,NotEmptyVar,LenVar,SubstringVar) were deleted in 10.0.0; their behavior is now covered by the#{...}script syntax.
-
-
Constructor Details
-
VarResolver
Constructor.- Parameters:
builder- The builder for this object.
-
-
Method Details
-
create
Instantiates a new clean-slateVarResolver.Builderobject.- Returns:
- A new
VarResolver.Builderobject.
-
addBean
Adds a bean to this session.- Type Parameters:
T- The bean type.- Parameters:
c- The bean type.value- The bean.- Returns:
- This object .
-
copy
Returns a new builder object using the settings in this resolver as a base.- Returns:
- A new var resolver builder.
-
createSession
Creates a new resolver session with no session objects.- Returns:
- A new resolver session.
-
createSession
Same ascreateSession()except allows you to specify a bean store for resolving beans.- Parameters:
beanStore- The bean store to associate with this session. May benull.- Returns:
- A new resolver session.
-
resolve
Resolve variables in the specified string.This is a shortcut for calling
createSession(.null ).resolve(s);
This method can only be used if the string doesn't contain variables that rely on the existence of session variables.- Parameters:
s- The input string. May benull.- Returns:
- The string with variables resolved, or the same string if it doesn't contain any variables to resolve. Returns
nullif the input wasnull.
-
compile
Compiles a template string into a reusableVarTemplate.Compilation is the dual of
resolve(String)— instead of tokenizing and resolving in one pass, the input is parsed once and the result returned as an immutableVarTemplatethat callers can reuse across many resolutions:// Compile once... var tpl =vr .compile("hello, ${name:world}" );// ...resolve many times. for (var req :requests ) {var session =vr .createSession().bean(req );writer .write(tpl .resolve(session )); }The returned
VarTemplateis immutable and threadsafe; theVarResolverSessionpassed toVarTemplate.resolve(VarResolverSession)inherits its own threadsafety contract (sessions are not threadsafe). UseresolveSupplier(String)orVarTemplate.asSupplierWithFreshSessions(VarResolver)for a threadsafe re-evaluating Supplier.Compile-binding contract:
The compiled template is bound to this resolver. Cached
Var(and, in the future, function) references are resolved at compile time. Passing the compiled template to a session belonging to a differentVarResolveris undefined behavior. Rebuilding the resolver invalidates compiled templates from the previous instance.Templates are not cached on the resolver. Callers that compile the same input repeatedly (e.g. framework consumers compiling per
@Valuefield) should cache the returnedVarTemplateon their own per-site metadata.- Parameters:
input- The template string to compile. May benull.- Returns:
- A new
VarTemplatebound to this resolver.
-
resolveSupplier
Returns a threadsafeSupplierthat resolves the given template against this resolver each timeSupplier.get()is called.Each call to
Supplier.get()opens a freshVarResolverSession, so the returnedSupplieris safe to share across threads — making it the recommended default for user-facing code (e.g.@Value Supplier<String>field types, hot-reload config patterns).Equivalent to
compile(input).asSupplierWithFreshSessions(this).For a session-bound (and therefore not threadsafe) variant, see
VarResolverSession.resolveSupplier(String).- Parameters:
input- The template string. May benull.- Returns:
- A threadsafe
Supplier<String>.
-
resolveTo
Resolve variables in the specified string and sends the results to the specified writer.This is a shortcut for calling
createSession(.null ).resolveTo(s, w);
This method can only be used if the string doesn't contain variables that rely on the existence of session variables.- Parameters:
s- The input string. May benull, in which case the writer is left unchanged.w- The writer to send the result to. Must not benull.- Throws:
IOException- Thrown by underlying stream.
-
getVarMap
Returns an unmodifiable map ofVarsassociated with this context.- Returns:
- A map whose keys are var names (e.g.
"S" ) and values areVarinstances.
-
getVars
Returns an array of variables define in this variable resolver context.- Returns:
- A new array containing the variables in this context.
-
getFunctionMap
Returns an unmodifiable map ofVarFunctionsassociated with this context.- Returns:
- A map whose keys are function names (e.g.
"upper" ) and values areVarFunctioninstances.
-