Top Description Inners Fields Constructors Methods
com.google.escapevelocity

pack-priv Class Macro

extends Object
Class Inheritance
Imports
com.google.common.base.Verify, com.google.common.collect.ImmutableList, .ImmutableSet, java.lang.reflect.Method, java.util.LinkedHashMap, .List, .Map

A macro definition. Macros appear in templates using the syntax #macro (m $x $y) ... #end and each one produces an instance of this class. Evaluating a macro involves setting the parameters (here {$x $y)} and evaluating the macro body. Macro arguments are call-by-name, which means that we need to set each parameter variable to the node in the parse tree that corresponds to it, and arrange for that node to be evaluated when the variable is actually referenced.

There are two ways to invoke a macro. #m('foo', 'bar') sets $x and $y. #@m('foo', 'bar') ... #end sets $x and $y, and also sets $bodyContent to the template text ....

Author
[email protected] (Éamonn McManus)

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
Macro.MacroEvaluationContext

The context for evaluation within macros.

Field Summary

Modifier and TypeField and Description
private final int
private final Node
private final String
private final ImmutableList<String>

Constructor Summary

AccessConstructor and Description
pack-priv
Macro(int definitionLineNumber, String name, List<String> parameterNames, Node macroBody)

Method Summary

Modifier and TypeMethod and Description
pack-priv int
pack-priv void
render(EvaluationContext context, List<ExpressionNode> thunks, Node bodyContent, StringBuilder output)

Renders a call to this macro with the arguments in thunks and with a possibly-null bodyContent.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

definitionLineNumberback to summary
private final int definitionLineNumber
macroBodyback to summary
private final Node macroBody
nameback to summary
private final String name
parameterNamesback to summary
private final ImmutableList<String> parameterNames

Constructor Detail

Macroback to summary
pack-priv Macro(int definitionLineNumber, String name, List<String> parameterNames, Node macroBody)

Method Detail

parameterCountback to summary
pack-priv int parameterCount()
renderback to summary
pack-priv void render(EvaluationContext context, List<ExpressionNode> thunks, Node bodyContent, StringBuilder output)

Renders a call to this macro with the arguments in thunks and with a possibly-null bodyContent. The bodyContent is non-null if the macro call looks like #@foo(...) ... #end; the #@ indicates that the text up to the matching #end should be made available as the variable $bodyContent inside the macro.

com.google.escapevelocity back to summary

pack-priv Class Macro.MacroEvaluationContext

extends Object
implements EvaluationContext
Class Inheritance
All Implemented Interfaces
com.google.escapevelocity.EvaluationContext

The context for evaluation within macros. This wraps an existing EvaluationContext but intercepts reads of the macro's parameters so that they result in a call-by-name evaluation of whatever was passed as the parameter. For example, if you write...
#macro (mymacro $x)
$x $x
#end
#mymacro($foo.bar(23))
...then the #mymacro call will result in $foo.bar(23) being evaluated twice, once for each time $x appears. The way this works is that $x is a thunk. Historically a thunk is a piece of code to evaluate an expression in the context where it occurs, for call-by-name procedures as in Algol 60. Here, it is not exactly a piece of code, but it has the same responsibility.

Field Summary

Modifier and TypeField and Description
private final Node
private final EvaluationContext
private final Map<String, ExpressionNode>

Constructor Summary

AccessConstructor and Description
pack-priv
MacroEvaluationContext(Map<String, ExpressionNode> parameterThunks, EvaluationContext originalEvaluationContext, Node bodyContent)

Method Summary

Modifier and TypeMethod and Description
public Map<String, Macro>
getMacros()

Implements com.google.escapevelocity.EvaluationContext.getMacros.

Returns a modifiable mapping from macro names to macros.
public Object
public ImmutableSet<Method>
public Runnable
setVar(final String var, Object value)

Implements com.google.escapevelocity.EvaluationContext.setVar.

Sets the given variable to the given value.
public boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bodyContentback to summary
private final Node bodyContent
originalEvaluationContextback to summary
private final EvaluationContext originalEvaluationContext
parameterThunksback to summary
private final Map<String, ExpressionNode> parameterThunks

Constructor Detail

MacroEvaluationContextback to summary
pack-priv MacroEvaluationContext(Map<String, ExpressionNode> parameterThunks, EvaluationContext originalEvaluationContext, Node bodyContent)

Method Detail

getMacrosback to summary
public Map<String, Macro> getMacros()

Implements com.google.escapevelocity.EvaluationContext.getMacros.

Doc from com.google.escapevelocity.EvaluationContext.getMacros.

Returns a modifiable mapping from macro names to macros. Initially this map starts off with the macros that were defined in the root template. As #parse directives are encountered in the evaluation, they may add further macros to the map.

Annotations
@Override
getVarback to summary
public Object getVar(String var)

Implements com.google.escapevelocity.EvaluationContext.getVar.

Annotations
@Override
publicMethodsWithNameback to summary
public ImmutableSet<Method> publicMethodsWithName(Class<?> startClass, String name)

Implements com.google.escapevelocity.EvaluationContext.publicMethodsWithName.

Doc from com.google.escapevelocity.EvaluationContext.publicMethodsWithName.

See MethodFinder#publicMethodsWithName.

Annotations
@Override
setVarback to summary
public Runnable setVar(final String var, Object value)

Implements com.google.escapevelocity.EvaluationContext.setVar.

Doc from com.google.escapevelocity.EvaluationContext.setVar.

Sets the given variable to the given value.

Returns:Runnable

a Runnable that will restore the variable to the value it had before. If the variable was undefined before this method was executed, the Runnable will make it undefined again. This allows us to restore the state of $x after #foreach ($x in ...).

Annotations
@Override
varIsDefinedback to summary
public boolean varIsDefined(String var)

Implements com.google.escapevelocity.EvaluationContext.varIsDefined.

Annotations
@Override