public final class Function
extends java.lang.Object
SymbolResolver
on
behalf of the ExpressionParser
when introspecting classes to match
method invocation semantics in a user expression, with a target method
in a Java class. The following information is available:
This class provides methods to conduct fuzzy matches of method parameter
signatures to one another. Fuzzy matches are those which match parameters
of assignment compatible
types. These may
not be exact type matches as required by the VM when invoking methods,
but the assumption is that the expression compiler
will
be able to generate the necessary bytecode at assemble time to convert
the provided parameter type to the required parameter type. This will
occur through the assembly of the necessary instructions to wrap/unwrap
primitive values, cast object references, and/or perform widening or
narrowing conversions on primitive values. The primary worker method which
performs this fuzzy matching is matchSignature(Class[], Class[],
int, boolean)
.
Bootstrap Methods. Several methods are treated
specially by the parser, symbol resolver, and compiler. All but the most
trivial user expressions must access application specific data. The access
points for this data are callback
libraries
and user variables
,
which the application registers with the expression engine. A compiled
expression must have a way to access these constructs. When a reference
to a callback library object or a user variable is encountered in an
expression, the compiler embeds instructions into the expression object
to invoke methods which access these objects at expression execution time.
These are termed bootstrap methods, because they permit access to these
primordial access points into the application's data. There are currently
three bootstrap methods supported:
CompiledExpression.getLib(int)
- loads a callback library object,
so that the expression can invoke a method upon it.CompiledExpression.getVar(int)
- loads the current value
of a user variable, so that the expression can invoke a method upon
it, use it in an operation, or return it.CompiledExpression.setVar(int, java.lang.Object)
- sets the current value
of a user variable as the result of an assignment operation in the
expression.At the time an expression is parsed, it is determined whether a bootstrap method invocation must be compiled into that expression. An instance of this class is created accordingly, and its type is set to the appropriate constant to represent which of the above bootstrap methods it represents.
Modifier and Type | Field and Description |
---|---|
private static java.lang.Class[] |
COMPAT_PAIRS
Pairs of data types used to test strict assignment compatibility
|
(package private) static int |
GENERAL
General purpose method
|
(package private) static int |
GET_LIB
Bootstrap method which retrieves a callback library object
|
(package private) static int |
GET_VAR
Bootstrap method which retrieves the value of a variable
|
private java.lang.reflect.Method |
method
Method invoked by the compiled version of this function
|
private int |
minArgs
Number of required arguments in a var-arg scenario
|
private java.lang.Class[] |
parameterTypes
Parameter types required by the method
|
private Function |
prerequisite
Function which must be executed to provide this function's target
|
private java.lang.Class |
returnType
Type of object which must be returned by this function
|
(package private) static int |
SET_VAR
Bootstrap method which sets the value of a variable
|
private java.lang.String |
symbol
Symbol assigned to this object for use in AST building
|
private java.lang.Class |
target
Invocation target class of the compiled version of this function
|
private int |
type
Method type represented by this object
|
Constructor and Description |
---|
Function(java.lang.Class target,
java.lang.Class returnType,
java.lang.reflect.Method method)
Construct an instance of this object to represent a regular method
which does not accept variable-length argument lists.
|
Function(java.lang.Class target,
java.lang.Class returnType,
java.lang.reflect.Method method,
java.lang.Class[] parameterTypes,
boolean enableVarArgs)
Construct an instance of this object to represent a method which may
or may not accept variable-length argument lists.
|
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
asString(java.lang.String name,
java.lang.Class returnType,
int minArgs,
java.lang.Class[] signature)
Compose a string indicating a full function signature.
|
(package private) java.lang.reflect.Method |
getMethod()
Get the method backing this function.
|
(package private) int |
getMinArgs()
Get the minimum number of arguments required by this function.
|
(package private) java.lang.Class[] |
getParameterTypes()
Get the array of parameter types expected by this function object.
|
(package private) Function |
getPrerequisite()
Get a
Function object which encapsulates another method
associated with this object. |
(package private) java.lang.Class |
getReturnType()
Get the return type of this function, which may be more specific than
the return type of the underlying method.
|
(package private) java.lang.String |
getSymbol()
Get the optional symbol which represents a name or identifier for the
invocation target object.
|
(package private) java.lang.Class |
getTarget()
Get the invocation target class of this function.
|
(package private) int |
getType()
Get the type of method this function object represents: either one of
the bootstrap types or a general purpose method.
|
static boolean |
isAssignmentCompatible(java.lang.Class required,
java.lang.Class provided,
boolean strict)
Test a specific pair of data types for assignment compatibility.
|
(package private) boolean |
matchSignature(java.lang.Class[] match,
boolean strict)
Compare the specified argument signature against this function's
signature to determine whether
match is compatible with
this function's signature. |
static boolean |
matchSignature(java.lang.Class[] sig,
java.lang.Class[] match,
int minArgs,
boolean strict)
Compare the specified argument signature against the specified match
signature to determine whether
match is compatible with
sig . |
(package private) void |
setPrerequisite(Function prerequisite)
Set the
Function instance which represents the
prerequisite bootstrap method for this object. |
(package private) void |
setSymbol(java.lang.String symbol)
Set the optional symbol which represents a name or identifier for the
invocation target object.
|
(package private) void |
setType(int type)
Set the type of method this function object represents: either one of
the bootstrap types or a general purpose method.
|
java.lang.String |
toString()
Compose a string indicating a full function signature.
|
static final int GENERAL
static final int GET_LIB
static final int GET_VAR
static final int SET_VAR
private static final java.lang.Class[] COMPAT_PAIRS
private java.lang.Class target
private java.lang.Class returnType
private java.lang.reflect.Method method
private java.lang.Class[] parameterTypes
private int minArgs
private int type
private java.lang.String symbol
private Function prerequisite
Function(java.lang.Class target, java.lang.Class returnType, java.lang.reflect.Method method)
target
- Invocation target class.returnType
- Type of object which caller of this method expects to receive.method
- Target method to be invoked.Function(java.lang.Class target, java.lang.Class returnType, java.lang.reflect.Method method, java.lang.Class[] parameterTypes, boolean enableVarArgs)
target
- Invocation target class.returnType
- Type of object which caller of this method expects to receive.method
- Target method to be invoked.parameterTypes
- Array of parameter types required by the underlying method.
These may be different than the underlying method reports,
in the case of methods which use var-args. Only used if the
underlying method accepts a var-arg list; may be
null
otherwise.enableVarArgs
- true
if the underlying method might
accept a var-arg list; false
if it definitely
cannot. If true
, the fact whether the method
accepts var-args will be determined for certain in the
constructor. Only methods of registered callback libraries
can support var-arg lists.java.lang.ClassCastException
- if method
cannot be invoked on an instance of
target
; if method
's return type is
not (even leniently) assignment
compatible
with returnType
.public static java.lang.String asString(java.lang.String name, java.lang.Class returnType, int minArgs, java.lang.Class[] signature)
returnType name([parm1[, parm2[..., parmN]]])
name
- Function name.returnType
- Type which this function returns.minArgs
- Minimum number of required arguments. If -1
,
var-args support is disabled.signature
- Array of classes representing the parameters this function
expects, in order.public static boolean matchSignature(java.lang.Class[] sig, java.lang.Class[] match, int minArgs, boolean strict)
match
is compatible with
sig
. They match if they contain the same number of
minimum required parameters and for each index i
, either:
match[i]
is compatible with sig[i]
; or
match[i] == null
.
int
and
java.lang.Integer
).
sig
- Array of argument types which define the baseline signature.match
- Array of classes which indicate a parameter signature we are
trying to match; null
s are permitted.minArgs
- Minimum arguments required by the baseline signature. If
negative, it is assumed var-arg lists are not to be
considered.strict
- Whether to enforce a strict match or not.true
if the signatures match as specified above,
else false
.public static boolean isAssignmentCompatible(java.lang.Class required, java.lang.Class provided, boolean strict)
int
and java.lang.Integer
).
required
- Required data type.provided
- Provided data type.strict
- Whether to perform a strict match, as defined above.true
if a value of type provided
can be assigned (possibly with some conversion required) to
a parameter/variable of type required
, given
the strictness constraint specified. Otherwise,
false
.public java.lang.String toString()
returnType name([parm1[, parm2[..., parmN]]])
toString
in class java.lang.Object
int getType()
void setType(int type)
java.lang.String getSymbol()
void setSymbol(java.lang.String symbol)
symbol
- A symbol to be associated with a bootstrap method AST node
in the postfix form of a parsed expression.Function getPrerequisite()
Function
object which encapsulates another method
associated with this object. The returned object represents a
bootstrap method which must be invoked as a
prerequisite to invoking the method represented by this
Function
instance. Invocation of the bootstrap method
load's the target object upon which the method represented by this
object is invoked.Function
which describes this object's
prerequisite bootstrap method. If no such prerequisite exists,
null
is returned.setPrerequisite(com.goldencode.expr.Function)
void setPrerequisite(Function prerequisite)
Function
instance which represents the
prerequisite bootstrap method for this object.
This method should only be called by the SymbolResolver
.prerequisite
- A Function
which describes this object's
prerequisite bootstrap method.java.lang.ClassCastException
- if the object returned by the specified bootstrap method
does not match the target object type required by this
function.getPrerequisite()
java.lang.Class getTarget()
java.lang.reflect.Method getMethod()
java.lang.Class getReturnType()
int getMinArgs()
-1
if
this function does not support variable argument lists.java.lang.Class[] getParameterTypes()
Object
as its last parameter, while this method will break out the specific
types of each of the variable-length list arguments.boolean matchSignature(java.lang.Class[] match, boolean strict)
match
is compatible with
this function's signature. They match if they contain the same number
of minimum required parameters and for each index i
, either:
match[i]
is compatible with this function's parameter
at the same position; or
match[i] == null
.
match
- Array of classes which indicate a parameter signature we are
trying to match; null
s are permitted.true
if the signatures match as specified above,
else false
.