public class RuntimeJastInterpreter
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
(package private) static class |
RuntimeJastInterpreter.CharacterExprAdapter
Implements an interpreted
CharacterExpr lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) static class |
RuntimeJastInterpreter.CharacterExpressionAdapter
Adapter for an adapter class built using an anonymous constructor of the class
CharacterExpression . |
(package private) static class |
RuntimeJastInterpreter.ClientWhere
Implements an interpreted
Supplier<logical> lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) static class |
RuntimeJastInterpreter.DateExprAdapter
Implements an interpreted
DateExpr lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) static class |
RuntimeJastInterpreter.DateExpressionAdapter
Adapter for an adapter class built using an anonymous constructor of the class
DateExpression . |
(package private) static class |
RuntimeJastInterpreter.DatetimeExprAdapter
Implements an interpreted
DatetimeExpr lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) static class |
RuntimeJastInterpreter.DatetimeExpressionAdapter
Adapter for an adapter class built using an anonymous constructor of the class
DatetimeExpression . |
(package private) static class |
RuntimeJastInterpreter.DatetimeTzExprAdapter
Implements an interpreted
DatetimeTzExpr lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) static class |
RuntimeJastInterpreter.DatetimeTzExpressionAdapter
Adapter for an adapter class built using an anonymous constructor of the class
DatetimeTzExpression . |
(package private) static class |
RuntimeJastInterpreter.DecimalExprAdapter
Implements an interpreted
DecimalExpr lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) static class |
RuntimeJastInterpreter.DecimalExpressionAdapter
Adapter for an adapter class built using an anonymous constructor of the class
DecimalExpression . |
(package private) static class |
RuntimeJastInterpreter.Int64ExprAdapter
Implements an interpreted
Int64Expr lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) static class |
RuntimeJastInterpreter.Int64ExpressionAdapter
Adapter for an adapter class built using an anonymous constructor of the class
Int64Expression . |
(package private) static class |
RuntimeJastInterpreter.IntegerExprAdapter
Implements an interpreted
IntegerExpr lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) static 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) static class |
RuntimeJastInterpreter.Lambda
Implements an interpreted generic lambda expression stored as a JAST node, using the
RuntimeJastInterpreter . |
(package private) static 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) static class |
RuntimeJastInterpreter.LogicalLambda
Implements an interpreted
LogicalOp lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) static class |
RuntimeJastInterpreter.P2JQueryParameter
Implements an interpreted
P2JQuery.Parameter lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) static class |
RuntimeJastInterpreter.RawExprAdapter
Implements an interpreted
RawExpr lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) static class |
RuntimeJastInterpreter.RawExpressionAdapter
Adapter for an adapter class built using an anonymous constructor of the class
RawExpression . |
(package private) static class |
RuntimeJastInterpreter.RecidExprAdapter
Implements an interpreted
RecidExpr lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) static class |
RuntimeJastInterpreter.ResolvableAdapter
Implements an interpreted
Resolvable lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) static class |
RuntimeJastInterpreter.RowidExprAdapter
Implements an interpreted
RowidExpr lambda expression stored as a JAST node,
using the RuntimeJastInterpreter . |
(package private) static 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 static java.util.Map<java.lang.String,java.lang.Class<?>> |
classCache
Cache of classes for faster lookup than
Class.forName alone |
private java.util.Map<java.lang.String,Aast> |
classMethods
The list of the method defined in this class.
|
private java.lang.String |
DYNAMIC_CALL
Identifies a DYNAMIC-FUNCTION call.
|
private java.util.Map<Aast,java.lang.Object> |
dynamicCalls
The set of all DYNAMIC-FUNCTION calls from this compile unit.
|
private java.lang.String[] |
imports
The list of import paths.
|
private java.util.function.Consumer<java.lang.Object> |
instanceProcessor
A consumer to process newly created instances.
|
private static java.util.logging.Logger |
log
Logger.
|
private ScopedDictionary<java.lang.String,java.lang.Object> |
methodArgs
The stack of parameters from calling methods with actual values.
|
private java.lang.Object |
NOT_EVALUATED
This is a marker for not evaluated nodes.
|
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 static java.lang.Class<?> |
classForName(java.lang.String className)
Get the class with the given name.
|
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.
|
void |
evaluateDynamicCalls() |
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.
|
java.lang.Object |
interpretIfExist(java.lang.String methodName,
java.lang.Object... args)
Executes a method.
|
void |
prepare(JavaAst jast,
boolean dynamicEvaluation)
The preparation step.
|
(package private) void |
processInstancesWith(java.util.function.Consumer<java.lang.Object> expr)
Set a function which will process any newly created instances.
|
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 final java.lang.Object NOT_EVALUATED
null
valid values already evaluated.private static final java.util.logging.Logger log
private static final java.util.Map<java.lang.String,java.lang.Class<?>> classCache
Class.forName
aloneprivate 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 final java.lang.String DYNAMIC_CALL
private java.util.Map<Aast,java.lang.Object> dynamicCalls
null
then it
contains no DYNAMIC-FUNCTION calls. These nodes will be evaluated only once, when the query
is open and the result is cached mapped to respective node. These values are valid for this
interpreter, ie for an individual dynamic query.private java.lang.Class<?>[] staticImports
private java.util.Map<java.lang.String,Aast> classMethods
private java.util.function.Consumer<java.lang.Object> instanceProcessor
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).private static java.lang.Class<?> classForName(java.lang.String className) throws java.lang.ClassNotFoundException
className
- Fully qualified class name.className
, if found.java.lang.ClassNotFoundException
- if a class with the given name is not found.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, boolean dynamicEvaluation)
jast
- The JAST tree to be processed. It must be a COMPILE-UNIT node.dynamicEvaluation
- Use true
when there is at least on DYNAMIC-FUNCTION in interpreted
expression.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 interpretIfExist(java.lang.String methodName, java.lang.Object... args)
This method does not throw any exception if no method if sound to match the requested name
and signature as interpret(java.lang.String, java.lang.Object...)
does. In this case this method returns null
.
methodName
- The name of the method to be called.args
- The list of actual values for the parameters.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.public void evaluateDynamicCalls()
void processInstancesWith(java.util.function.Consumer<java.lang.Object> expr)
expr
- The function.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.