public class RuntimeJastInterpreter
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
(package private) class |
RuntimeJastInterpreter.CharacterExprAdapter
Implements an interpreted
CharacterExpr lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) class |
RuntimeJastInterpreter.CharacterExpressionAdapter
Adapter for an adapter class built using an anonymous constructor of the class
CharacterExpression . |
(package private) class |
RuntimeJastInterpreter.ClientWhere
Implements an interpreted
Supplier<logical> lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) class |
RuntimeJastInterpreter.DateExprAdapter
Implements an interpreted
DateExpr lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) class |
RuntimeJastInterpreter.DateExpressionAdapter
Adapter for an adapter class built using an anonymous constructor of the class
DateExpression . |
(package private) class |
RuntimeJastInterpreter.DatetimeExprAdapter
Implements an interpreted
DatetimeExpr lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) class |
RuntimeJastInterpreter.DatetimeExpressionAdapter
Adapter for an adapter class built using an anonymous constructor of the class
DatetimeExpression . |
(package private) class |
RuntimeJastInterpreter.DatetimeTzExprAdapter
Implements an interpreted
DatetimeTzExpr lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) class |
RuntimeJastInterpreter.DatetimeTzExpressionAdapter
Adapter for an adapter class built using an anonymous constructor of the class
DatetimeTzExpression . |
(package private) class |
RuntimeJastInterpreter.DecimalExprAdapter
Implements an interpreted
DecimalExpr lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) class |
RuntimeJastInterpreter.DecimalExpressionAdapter
Adapter for an adapter class built using an anonymous constructor of the class
DecimalExpression . |
(package private) class |
RuntimeJastInterpreter.Int64ExprAdapter
Implements an interpreted
Int64Expr lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) class |
RuntimeJastInterpreter.Int64ExpressionAdapter
Adapter for an adapter class built using an anonymous constructor of the class
Int64Expression . |
(package private) class |
RuntimeJastInterpreter.IntegerExprAdapter
Implements an interpreted
IntegerExpr lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) class |
RuntimeJastInterpreter.IntegerExpressionAdapter
Adapter for an adapter class built using an anonymous constructor of the class
IntegerExpression . |
(package private) static class |
RuntimeJastInterpreter.InterpreterException
Exception thrown within the RuntimeJastInterpreter.
|
(package private) class |
RuntimeJastInterpreter.Lambda
Implements an interpreted generic lambda expression stored as a JAST node, using the
RuntimeJastInterpreter . |
(package private) class |
RuntimeJastInterpreter.LogicalExprAdapter
Implements an interpreted
LogicalExpr lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) class |
RuntimeJastInterpreter.LogicalExpressionAdapter
Adapter for an adapter class built using an anonymous constructor of the class
LogicalExpression . |
(package private) class |
RuntimeJastInterpreter.LogicalLambda
Implements an interpreted
LogicalOp lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) class |
RuntimeJastInterpreter.P2JQueryParameter
Implements an interpreted
P2JQuery.Parameter lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) class |
RuntimeJastInterpreter.RawExprAdapter
Implements an interpreted
RawExpr lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) class |
RuntimeJastInterpreter.RawExpressionAdapter
Adapter for an adapter class built using an anonymous constructor of the class
RawExpression . |
(package private) class |
RuntimeJastInterpreter.RecidExprAdapter
Implements an interpreted
RecidExpr lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) class |
RuntimeJastInterpreter.ResolvableAdapter
Implements an interpreted
Resolvable lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) class |
RuntimeJastInterpreter.RowidExprAdapter
Implements an interpreted
RowidExpr lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) class |
RuntimeJastInterpreter.RowidExpressionAdapter
Adapter for an adapter class built using an anonymous constructor of the class
RowidExpression . |
Modifier and Type | Field and Description |
---|---|
private java.util.Map<java.lang.String,Buffer> |
buffers
The list of known buffers, by their name.
|
private java.util.Map<java.lang.String,Aast> |
classMethods
The list of the method defined in this class.
|
private java.lang.String[] |
imports
The list of import paths.
|
private ScopedDictionary<java.lang.String,java.lang.Object> |
methodArgs
The stack of parameters from calling methods with actual values.
|
private java.lang.Class<?>[] |
staticImports
The list of statically imported classes.
|
private java.util.Map<java.lang.String,java.lang.Object> |
variables
The list of the variables, by their name.
|
Constructor and Description |
---|
RuntimeJastInterpreter()
The default constructor.
|
RuntimeJastInterpreter(java.util.List<Buffer> buffers,
java.util.Map<java.lang.String,java.lang.Object> variables)
A constructor that receives a set of buffers and pre-initialized variables.
|
Modifier and Type | Method and Description |
---|---|
private Resolvable |
buildTypedExpression(Aast typedExpr,
java.lang.String retType)
Build an anonymous object that extends some kind of typed expression:
IntegerExpression , LogicalExpression , DateExpression etc. |
private java.lang.Object |
callCtor(Aast ctorNode)
Call a constructor and returns the built object.
|
private java.lang.Object |
callMethod(Aast aMethod)
Call a normal member method.
|
private java.lang.Object |
callStaticMethod(Aast staticMethod)
Call a static method.
|
private void |
collectImportPaths(Aast cu)
Collects the import paths used for symbol resolution.
|
private java.lang.Object[] |
collectParameters(Aast methNode,
java.lang.Class[] signature,
int startAt)
Collects the list of parameters to a method/staticMethod/constructor and, optionally, the
signature in a pre-allocated table.
|
private void |
collectStaticImports(Aast cu)
Collects the static import paths used for symbol resolution.
|
private void |
compileUnit(Aast cu)
Prepares the JAST for use.
|
private void |
declareVars(Aast classVars)
Collects the variables defined in this class.
|
private java.lang.Object |
doAssign(java.lang.String varName,
Aast assignNode)
Performs a variable assignment.
|
private java.lang.Object |
evalExpression(Aast exprNode)
Evaluates an expression to a value that is returned.
|
private java.lang.Object |
execMethod(Aast aMethod,
java.lang.Object... params)
Executes a method from this class by interpreting the sequence of statements from its body.
|
private static java.lang.reflect.Constructor |
findMatchingConstructor(java.lang.Class aClass,
java.lang.Class[] signature)
Look into the list of constructors for a class for the one that matches the list of
argument types.
|
private java.lang.reflect.Method |
findMatchingMethod(java.lang.Class aClass,
java.lang.String methodName,
java.lang.Class[] signature,
int minargs)
Look into the list of methods of a class for the one that matches name and the list of
argument types.
|
private java.lang.reflect.Method |
findMatchingMethodFromClass(java.lang.Class aClass,
java.lang.String methodName,
java.lang.Class[] signature,
int minargs)
Look into the list of methods of a class for the one that matches name and the list of
argument types.
|
private java.lang.Object[] |
fixupParameters(java.lang.reflect.Method method,
boolean isStatic,
java.lang.Object[] parValues)
For varargs methods, rework the last parameters into an array of the proper type.
|
private java.lang.Object |
getCache(Aast node,
java.lang.String key)
Inspects the node annotation for a runtime cached value.
|
private java.lang.Class<?> |
getClass(java.lang.String nodeText)
Obtain the class by its name.
|
java.lang.Object |
getVariableValue(java.lang.String varName)
Obtain the value of a variable.
|
java.lang.Object |
interpret(java.lang.String methodName,
java.lang.Object... args)
Executes a method.
|
void |
prepare(JavaAst jast)
The preparation step.
|
private int |
readMinArgs(Aast node)
Check the given node for a "minargs" annotation and return it if present.
|
void |
setBuffers(java.util.List<Buffer> buffers)
Updates the set of known buffers.
|
void |
setVariables(java.util.Map<java.lang.String,java.lang.Object> variables)
Updates the set of known variables.
|
private java.util.Map<java.lang.String,Buffer> buffers
private java.util.Map<java.lang.String,java.lang.Object> variables
private ScopedDictionary<java.lang.String,java.lang.Object> methodArgs
NOTE:This is not a perfect emulation of a java scopes.
private java.lang.String[] imports
private java.lang.Class<?>[] staticImports
private java.util.Map<java.lang.String,Aast> classMethods
public RuntimeJastInterpreter()
public RuntimeJastInterpreter(java.util.List<Buffer> buffers, java.util.Map<java.lang.String,java.lang.Object> variables)
buffers
- The list of buffers to be used. Must be open and ready to use.variables
- The list of variables (values mapped by their variable names).public void setBuffers(java.util.List<Buffer> buffers)
buffers
- The new list of buffers to be used. Must be open and ready to use.public void setVariables(java.util.Map<java.lang.String,java.lang.Object> variables)
variables
- The new list of variables (values mapped by their variable names).public void prepare(JavaAst jast)
jast
- The JAST tree to be processed. It must be a COMPILE-UNIT node.RuntimeJastInterpreter.InterpreterException
- if the argument is null or not a COMPILE-UNIT node.public java.lang.Object getVariableValue(java.lang.String varName)
varName
- the name of the variable whose value will be returned.RuntimeJastInterpreter.InterpreterException
- if the argument is null or not a variable name.public java.lang.Object interpret(java.lang.String methodName, java.lang.Object... args)
methodName
- The name of the method to be called.args
- The list of actual values for the parameters.RuntimeJastInterpreter.InterpreterException
- if the argument is null or not a method name or other exception occurred during
the evaluation.private void compileUnit(Aast cu)
cu
- The main node of a tree. It must be a COMPILE-UNIT node.private void collectImportPaths(Aast cu)
cu
- The main node of a tree. It must be a COMPILE-UNIT node.private void collectStaticImports(Aast cu)
cu
- The main node of a tree. It must be a COMPILE-UNIT node.private void declareVars(Aast classVars)
classVars
- The main node of a tree. It must be a CS_INSTANCE_VARS node.private java.lang.Object execMethod(Aast aMethod, java.lang.Object... params)
Each statement from this method definition is executed/evaluated in sequence. If the method returns a value (function) it it will be obtained as the result.
Only simple methods, without parameters are supported. As workaround, the parameters can be added to the list of initial pre-initialized variables.
aMethod
- The method node to be processed.params
- The arguments for this method.private java.lang.Object doAssign(java.lang.String varName, Aast assignNode)
varName
- The name of the variable.assignNode
- The node that contain the tree for evaluation of the variable value.private java.lang.Object callCtor(Aast ctorNode)
The method will try to use the cached annotation from the node to speed up evaluation. If not available, it will use reflection to resolve it. Once computed, the Constructor object will be stored for future calls.
Before calling the constructor, all children nodes are (recursively) evaluated.
ctorNode
- A CONSTRUCTOR node used to build an object.private java.lang.Object[] collectParameters(Aast methNode, java.lang.Class[] signature, int startAt)
methNode
- The method node whose children will be inspected. It can be any type of node,
but the operation only makes sense for METHOD_CALL, STATIC_METHOD_CALL and
CONSTRUCTOR nodes.signature
- Optional. If not null, at return, it will be populated with the method signature.
If provided (not null), it must be allocated to match the signature size.startAt
- The number of children to be ignored. In the case of standard METHOD_CALLs, the
first child is the object on which the method will be applied so it must be
skipped (startAt = 1). Otherwise should be 0.private java.lang.Object callMethod(Aast aMethod)
The method will try to use the cached annotation from the node to speed up evaluation. If not available, it will use reflection to resolve it. Once computed, the Method object will be stored for future calls.
Before calling the method, all children nodes are (recursively) evaluated.
aMethod
- The method node to be evaluated.private int readMinArgs(Aast node)
node
- The node to check.private java.lang.Object[] fixupParameters(java.lang.reflect.Method method, boolean isStatic, java.lang.Object[] parValues)
method
- The method to be considered.isStatic
- true
if this is a static method, false
for an instance
method.parValues
- The parameters as passed by the converted code.private java.lang.Object callStaticMethod(Aast staticMethod)
The method will try to use the cached annotation from the node to speed up evaluation. If not available, it will use reflection to resolve it. Once computed, the Method object will be stored for future calls.
Before calling the method, all children nodes are (recursively) evaluated.
staticMethod
- The method node to be evaluated.private java.lang.Object evalExpression(Aast exprNode)
exprNode
- The JAST node that contains the expression.private Resolvable buildTypedExpression(Aast typedExpr, java.lang.String retType)
IntegerExpression
, LogicalExpression
, DateExpression
etc.typedExpr
- The JAST node that describe the new object to be built.retType
- The return type JAST of the execute
method of the expected class type.
Must not be null
or empty.private java.lang.Class<?> getClass(java.lang.String nodeText)
At this moment this is only used by CAST nodes.
For performance issues, the classes are hardcoded in a String switch. Using the reflection and iterating through all all packages imported uses a great amount of CPU cycles. Other classes will be possible added in the future as they occur in the client code.
nodeText
- The class name.private static java.lang.reflect.Constructor findMatchingConstructor(java.lang.Class aClass, java.lang.Class[] signature)
aClass
- The class whose constructor list is checked.signature
- An array with the java types of the arguments of the required constructor. Use null
as a wildcard for unknown or null parameter type.private java.lang.reflect.Method findMatchingMethod(java.lang.Class aClass, java.lang.String methodName, java.lang.Class[] signature, int minargs)
This method calls findMatchingMethodFromClass(java.lang.Class, java.lang.String, java.lang.Class[], int)
, for each super class until a method
with requested name is found, or reaching the top of the hierarchy, in which case
null
is returned.
aClass
- The class whose constructor list is checked.methodName
- The method name being searched for.signature
- An array with the java types of the arguments of the required constructor. Use null
as a wildcard for unknown or null parameter type.minargs
- The minimum number of arguments for this call (used to detect varargs matches) or
-1 if no varargs support is used.private java.lang.reflect.Method findMatchingMethodFromClass(java.lang.Class aClass, java.lang.String methodName, java.lang.Class[] signature, int minargs)
This method is called only from findMatchingMethod(java.lang.Class, java.lang.String, java.lang.Class[], int)
.
aClass
- The class whose constructor list is checked.methodName
- The method name being searched for.signature
- An array with the java types of the arguments of the required constructor. Use
null
as a wildcard for unknown or null
parameter type.minargs
- The minimum number of arguments for this call (used to detect varargs matches) or
-1 if no varargs support is used.null
is returned. null
is also returned if multiple constructors are found to match the signature.private java.lang.Object getCache(Aast node, java.lang.String key)
node
- The JAST node to be inspected.key
- The annotation name.