org.apache.commons.jexl3
Class Interpreter

java.lang.Object
  extended by org.apache.commons.jexl3.parser.ParserVisitor
      extended by org.apache.commons.jexl3.Interpreter

public class Interpreter
extends ParserVisitor

An interpreter of JEXL syntax.

Since:
2.0

Field Summary
protected  JexlArithmetic arithmetic
          The arithmetic handler.
protected  boolean cache
          Cache executors.
protected  boolean cancelled
          Cancellation support.
protected  JexlContext context
          The context to store/retrieve variables.
protected static Object[] EMPTY_PARAMS
          Empty parameters for method matching.
protected  Map<String,Object> functions
          The map of registered functions.
protected  Map<String,Object> functors
          The map of registered functions.
protected  org.apache.commons.logging.Log logger
          The logger.
protected  String[] parameters
          Parameter names if any.
protected  Object[] registers
          Registers or arguments.
protected  boolean silent
          Silent intepreter flag.
protected  boolean strict
          Strict interpreter flag.
protected  Uberspect uberspect
          The uberspect.
 
Constructor Summary
protected Interpreter(Interpreter base)
          Copy constructor.
  Interpreter(JexlEngine jexl, JexlContext aContext)
          Deprecated.  
  Interpreter(JexlEngine jexl, JexlContext aContext, boolean strictFlag, boolean silentFlag)
          Creates an interpreter.
 
Method Summary
protected  JexlNode findNullOperand(RuntimeException xrt, JexlNode node, Object left, Object right)
          Finds the node causing a NPE for diadic operators.
 Object getAttribute(Object object, Object attribute)
          Gets an attribute of an object.
protected  Object getAttribute(Object object, Object attribute, JexlNode node)
          Gets an attribute of an object.
protected  JexlContext getContext()
          Gets the context.
protected  Uberspect getUberspect()
          Gets the uberspect.
 Object interpret(JexlNode node)
          Interpret the given script/expression.
protected  Object invocationFailed(JexlException xjexl)
          Triggered when method, function or constructor invocation fails.
protected  boolean isCancelled()
          Checks whether this interpreter execution was cancelled due to thread interruption.
 boolean isSilent()
          Checks whether this interpreter throws JexlException when encountering errors.
 boolean isStrict()
          Checks whether this interpreter considers unknown variables, methods and constructors as errors.
protected  Object resolveNamespace(String prefix, JexlNode node)
          Resolves a namespace, eventually allocating an instance using context as constructor argument.
 void setAttribute(Object object, Object attribute, Object value)
          Sets an attribute of an object.
protected  void setAttribute(Object object, Object attribute, Object value, JexlNode node)
          Sets an attribute of an object.
protected  void setFrame(JexlEngine.Frame frame)
          Sets this interpreter parameters and arguments.
protected  void setRegisters(Object... theRegisters)
          Deprecated. 
protected  Object unknownVariable(JexlException xjexl)
          Triggered when variable can not be resolved.
protected  Object visit(ASTAdditiveNode node, Object data)
          
protected  Object visit(ASTAdditiveOperator node, Object data)
          
protected  Object visit(ASTAmbiguous node, Object data)
          Unused, should throw in Parser.
protected  Object visit(ASTAndNode node, Object data)
          
protected  Object visit(ASTArrayAccess node, Object data)
          
protected  Object visit(ASTArrayLiteral node, Object data)
          
protected  Object visit(ASTAssignment node, Object data)
          
protected  Object visit(ASTBitwiseAndNode node, Object data)
          
protected  Object visit(ASTBitwiseComplNode node, Object data)
          
protected  Object visit(ASTBitwiseOrNode node, Object data)
          
protected  Object visit(ASTBitwiseXorNode node, Object data)
          
protected  Object visit(ASTBlock node, Object data)
          
protected  Object visit(ASTConstructorNode node, Object data)
          
protected  Object visit(ASTDivNode node, Object data)
          
protected  Object visit(ASTEmptyFunction node, Object data)
          
protected  Object visit(ASTEQNode node, Object data)
          
protected  Object visit(ASTERNode node, Object data)
          
protected  Object visit(ASTFalseNode node, Object data)
          
protected  Object visit(ASTForeachStatement node, Object data)
          
protected  Object visit(ASTFunctionNode node, Object data)
          
protected  Object visit(ASTGENode node, Object data)
          
protected  Object visit(ASTGTNode node, Object data)
          
protected  Object visit(ASTIdentifier node, Object data)
          
protected  Object visit(ASTIfStatement node, Object data)
          
protected  Object visit(ASTJexlScript node, Object data)
          
protected  Object visit(ASTLENode node, Object data)
          
protected  Object visit(ASTLTNode node, Object data)
          
protected  Object visit(ASTMapEntry node, Object data)
          
protected  Object visit(ASTMapLiteral node, Object data)
          
protected  Object visit(ASTMethodNode node, Object data)
          
protected  Object visit(ASTModNode node, Object data)
          
protected  Object visit(ASTMulNode node, Object data)
          
protected  Object visit(ASTNENode node, Object data)
          
protected  Object visit(ASTNotNode node, Object data)
          
protected  Object visit(ASTNRNode node, Object data)
          
protected  Object visit(ASTNullLiteral node, Object data)
          
protected  Object visit(ASTNumberLiteral node, Object data)
          
protected  Object visit(ASTOrNode node, Object data)
          
protected  Object visit(ASTReferenceExpression node, Object data)
          
protected  Object visit(ASTReference node, Object data)
          
protected  Object visit(ASTReturnStatement node, Object data)
          
protected  Object visit(ASTSizeFunction node, Object data)
          
protected  Object visit(ASTSizeMethod node, Object data)
          
protected  Object visit(ASTStringLiteral node, Object data)
          
protected  Object visit(ASTTernaryNode node, Object data)
          
protected  Object visit(ASTTrueNode node, Object data)
          
protected  Object visit(ASTUnaryMinusNode node, Object data)
          
protected  Object visit(ASTVar node, Object data)
          
protected  Object visit(ASTWhileStatement node, Object data)
          
protected  Object visit(SimpleNode node, Object data)
          Unused, satisfy ParserVisitor interface.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final org.apache.commons.logging.Log logger
The logger.


uberspect

protected final Uberspect uberspect
The uberspect.


arithmetic

protected final JexlArithmetic arithmetic
The arithmetic handler.


functions

protected final Map<String,Object> functions
The map of registered functions.


functors

protected Map<String,Object> functors
The map of registered functions.


context

protected final JexlContext context
The context to store/retrieve variables.


strict

protected final boolean strict
Strict interpreter flag.


silent

protected final boolean silent
Silent intepreter flag.


cache

protected final boolean cache
Cache executors.


registers

protected Object[] registers
Registers or arguments.


parameters

protected String[] parameters
Parameter names if any.


cancelled

protected volatile boolean cancelled
Cancellation support.


EMPTY_PARAMS

protected static final Object[] EMPTY_PARAMS
Empty parameters for method matching.

Constructor Detail

Interpreter

@Deprecated
public Interpreter(JexlEngine jexl,
                              JexlContext aContext)
Deprecated. 

Creates an interpreter.

Parameters:
jexl - the engine creating this interpreter
aContext - the context to evaluate expression

Interpreter

public Interpreter(JexlEngine jexl,
                   JexlContext aContext,
                   boolean strictFlag,
                   boolean silentFlag)
Creates an interpreter.

Parameters:
jexl - the engine creating this interpreter
aContext - the context to evaluate expression
strictFlag - whether this interpreter runs in strict mode
silentFlag - whether this interpreter runs in silent mode

Interpreter

protected Interpreter(Interpreter base)
Copy constructor.

Parameters:
base - the base to copy
Method Detail

isStrict

public boolean isStrict()
Checks whether this interpreter considers unknown variables, methods and constructors as errors.

Returns:
true if strict, false otherwise

isSilent

public boolean isSilent()
Checks whether this interpreter throws JexlException when encountering errors.

Returns:
true if silent, false otherwise

interpret

public Object interpret(JexlNode node)
Interpret the given script/expression.

If the underlying JEXL engine is silent, errors will be logged through its logger as info.

Parameters:
node - the script or expression to interpret.
Returns:
the result of the interpretation.
Throws:
JexlException - if any error occurs during interpretation.

getContext

protected JexlContext getContext()
Gets the context.

Returns:
the JexlContext used for evaluation.

getUberspect

protected Uberspect getUberspect()
Gets the uberspect.

Returns:
an Uberspect

setRegisters

@Deprecated
protected void setRegisters(Object... theRegisters)
Deprecated. 

Sets this interpreter registers for bean access/assign expressions.

Use setFrame(...) instead.

Parameters:
theRegisters - the array of registers

setFrame

protected void setFrame(JexlEngine.Frame frame)
Sets this interpreter parameters and arguments.

Parameters:
frame - the calling frame

findNullOperand

protected JexlNode findNullOperand(RuntimeException xrt,
                                   JexlNode node,
                                   Object left,
                                   Object right)
Finds the node causing a NPE for diadic operators.

Parameters:
xrt - the RuntimeException
node - the parent node
left - the left argument
right - the right argument
Returns:
the left, right or parent node

unknownVariable

protected Object unknownVariable(JexlException xjexl)
Triggered when variable can not be resolved.

Parameters:
xjexl - the JexlException ("undefined variable " + variable)
Returns:
throws JexlException if strict, null otherwise

invocationFailed

protected Object invocationFailed(JexlException xjexl)
Triggered when method, function or constructor invocation fails.

Parameters:
xjexl - the JexlException wrapping the original error
Returns:
throws JexlException if strict, null otherwise

isCancelled

protected boolean isCancelled()
Checks whether this interpreter execution was cancelled due to thread interruption.

Returns:
true if cancelled, false otherwise

resolveNamespace

protected Object resolveNamespace(String prefix,
                                  JexlNode node)
Resolves a namespace, eventually allocating an instance using context as constructor argument. The lifetime of such instances span the current expression or script evaluation.

Parameters:
prefix - the prefix name (may be null for global namespace)
node - the AST node
Returns:
the namespace instance

visit

protected Object visit(ASTAdditiveNode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTAdditiveOperator node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTAndNode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTArrayAccess node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTArrayLiteral node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTAssignment node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTBitwiseAndNode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTBitwiseComplNode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTBitwiseOrNode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTBitwiseXorNode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTBlock node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTDivNode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTEmptyFunction node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTEQNode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTFalseNode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTForeachStatement node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTGENode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTGTNode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTERNode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTIdentifier node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTVar node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTIfStatement node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTNumberLiteral node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTJexlScript node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTLENode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTLTNode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTMapEntry node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTMapLiteral node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTMethodNode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTFunctionNode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTConstructorNode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTModNode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTMulNode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTNENode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTNRNode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTNotNode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTNullLiteral node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTOrNode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTReference node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTReferenceExpression node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTReturnStatement node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTSizeFunction node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTSizeMethod node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTStringLiteral node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTTernaryNode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTTrueNode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTUnaryMinusNode node,
                       Object data)

Specified by:
visit in class ParserVisitor

visit

protected Object visit(ASTWhileStatement node,
                       Object data)

Specified by:
visit in class ParserVisitor

getAttribute

public Object getAttribute(Object object,
                           Object attribute)
Gets an attribute of an object.

Parameters:
object - to retrieve value from
attribute - the attribute of the object, e.g. an index (1, 0, 2) or key for a map
Returns:
the attribute value

getAttribute

protected Object getAttribute(Object object,
                              Object attribute,
                              JexlNode node)
Gets an attribute of an object.

Parameters:
object - to retrieve value from
attribute - the attribute of the object, e.g. an index (1, 0, 2) or key for a map
node - the node that evaluated as the object
Returns:
the attribute value

setAttribute

public void setAttribute(Object object,
                         Object attribute,
                         Object value)
Sets an attribute of an object.

Parameters:
object - to set the value to
attribute - the attribute of the object, e.g. an index (1, 0, 2) or key for a map
value - the value to assign to the object's attribute

setAttribute

protected void setAttribute(Object object,
                            Object attribute,
                            Object value,
                            JexlNode node)
Sets an attribute of an object.

Parameters:
object - to set the value to
attribute - the attribute of the object, e.g. an index (1, 0, 2) or key for a map
value - the value to assign to the object's attribute
node - the node that evaluated as the object

visit

protected Object visit(SimpleNode node,
                       Object data)
Unused, satisfy ParserVisitor interface.

Specified by:
visit in class ParserVisitor
Parameters:
node - a node
data - the data
Returns:
does not return

visit

protected Object visit(ASTAmbiguous node,
                       Object data)
Unused, should throw in Parser.

Specified by:
visit in class ParserVisitor
Parameters:
node - a node
data - the data
Returns:
does not return


Copyright © 2001-2011 The Apache Software Foundation. All Rights Reserved.