Class Var
- Direct Known Subclasses:
SimpleVar,StreamedVar,XVar
Vars are used to convert simple variables of the form VarResolver class.
Subclasses must implement one of the following two methods:
resolve(VarResolverSession,String)- For simple vars.resolveTo(VarResolverSession,Writer,String)- For streamed vars.
Subclasses MUST implement a no-arg constructor so that class names can be passed to the
VarResolver.Builder.vars(Class...) method.
They must also be thread safe!
Two direct abstract subclasses are provided to differentiated between simple and streamed vars:
See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanReturns whether nested variables are supported by this variable.protected booleanReturns whether variables in the resolved contents of this variable should also be resolved.protected booleancanResolve(VarResolverSession session) Returnstrue if this variable can be resolved in the specified session.protected StringdoResolve(VarResolverSession session, String arg) The method called fromVarResolver.protected StringgetName()Return the name of this variable.protected booleanisStable()Returnstrueif this variable's resolved value is stable for the lifetime of the resolver — same body produces the same output regardless of when (or in what session) it is resolved.abstract Stringresolve(VarResolverSession session, String arg) The interface that needs to be implemented for subclasses ofSimpleVar.abstract voidresolveTo(VarResolverSession session, Writer w, String arg) The interface that needs to be implemented for subclasses ofStreamedVar.
-
Constructor Details
-
Var
Constructor.- Parameters:
name- The name of this variable. Must not benull .streamed- Whether this variable is 'streamed', meaning theresolveTo(VarResolverSession, Writer, String)method is implemented. Iffalse , then theresolve(VarResolverSession, String)method is implemented.
-
-
Method Details
-
resolve
The interface that needs to be implemented for subclasses ofSimpleVar.- Parameters:
session- The session object used for a single instance of a var resolution.arg- The inside argument of the variable.- Returns:
- The resolved value.
- Throws:
Exception- Any exception can be thrown.
-
resolveTo
The interface that needs to be implemented for subclasses ofStreamedVar.- Parameters:
session- The session object used for a single instance of a var resolution.w- The writer to send the resolved value to.arg- The inside argument of the variable.- Throws:
Exception- Any exception can be thrown.
-
allowNested
Returns whether nested variables are supported by this variable.For example, in
"$X{$Y{xxx}}" , $Y is a nested variable that will be resolved if this method returnstrue .The default implementation of this method always returns
true . Subclasses can override this method to override the default behavior.- Returns:
true if nested variables are supported by this variable.
-
allowRecurse
Returns whether variables in the resolved contents of this variable should also be resolved.For example, if
"$X{xxx}" resolves to"$Y{xxx}" , then the $Y variable will be recursively resolved if this method returnstrue .The default implementation of this method always returns
true .
Subclasses can override this method to override the default behavior.As a general rule, variables that resolve user-entered data should not be recursively resolved as this may cause a security hole.- Returns:
true if resolved variables should be recursively resolved.
-
canResolve
Returnstrue if this variable can be resolved in the specified session.For example, some variable cannot resolve unless specific context or session objects are available.
- Parameters:
session- The current session.- Returns:
true if this variable can be resolved in the specified session.
-
isStable
Returnstrueif this variable's resolved value is stable for the lifetime of the resolver — same body produces the same output regardless of when (or in what session) it is resolved.Stable vars opt in to compile-time folding by
VarTemplateCompiler: rather than producing aVarRefSegmentthat dispatches at resolve time, the compiler resolves the var once at compile time and stores aLiteralSegmentcarrying the resolved value. This eliminates the per-resolve dispatch cost for hot paths like@Value("${HOSTNAME:localhost}")where the underlying var (env / system property / etc.) is treated as a build-time constant by the application.Default:
false. Vars that read mutable session-scoped state (request beans, args, config files) must leave thisfalse— folding would cache an arbitrary resolution as a literal and break per-session correctness.Common opt-ins:
SystemPropertiesVar— system properties are mutable in principle (viaSystem.setProperty(java.lang.String, java.lang.String)) but treated as stable in normal application code; if a deployment relies on post-resolver mutations, opt-out by sub-classing.EnvVariablesVar— process environment is immutable.DotenvVar/EnvFileVar— .env files are read once at startup.
Folding contract:
When a
Varreturnstruehere, the compiler invokesdoResolve(VarResolverSession, String)on a no-bean session at compile time. Implementations must not throw on absent session beans during stable folding; if the stable resolution would itself depend on session state (i.e. it isn't really stable), either returnfalsefrom this method or throw aVarResolverExceptionfromdoResolve(VarResolverSession, String)which the compiler treats as "cannot fold; emit a normalVarRefSegment".- Returns:
trueif this variable's resolution is invariant across sessions.
-
doResolve
The method called fromVarResolver.Can be overridden to intercept the request and do special handling.
Default implementation simply calls resolve(String).- Parameters:
session- The session object used for a single instance of a string resolution.arg- The inside argument of the variable.- Returns:
- The resolved value.
- Throws:
Exception- Any exception can be thrown.
-
getName
Return the name of this variable.For example, the system property variable returns
"S" since the format of the variable is"$S{system.property}" .- Returns:
- The name of this variable.
-