public class CachedMethod
extends java.lang.Object
This class is used to represent two distinct types of Java methods
that can be invoked. The METHOD type corresponds to an instance
method of a java class, invoked on an object of an appropriate class
(the base class). The FUNCTION type corresponds to a static
method of a java class. These types corresponds to the two distinct
expression constructs that can be used to invoke Java methods. The
type of construct reflected can be queried using the
getCachedMethodType() method, which returns either FUNCTION
or METHOD
. Additionally, this class can be used to represent
Java methods that were not found. If the CachedMethod corresponds to
an invokeable Java method, then the isValid() method will return true.
CachedMethods that are not valid cannot be invoked by the invoke()
method.
This class provides several services that distinguish it from Java's built-in reflection mechanism:
The automatic conversions that are allowed on the arguments of reflected Java methods can be particularly tricky to understand. The findMethod() method is fairly aggressive about finding valid methods to invoke. In particular, given a set of arguments with token types, the findMethod() method might return a cached method that:
ConversionUtilities
class, which has more specific documentation the
underlying conversions. The inverse of the same conversions are
performed on the results of a Java method invocation, in order to
convert the result back into a Ptolemy token.
Since there may be many methods that match a particular function application or method invocation, under the above conversions, the findMethod() method attempts to return the most specific Java method that can be called. Generally speaking, conversions are preferred in the above order. If one Java method is not clearly preferable to all others, then the findMethod() method will throw an exception. This may happen if there are multiple functions defined with varying argument types.
Additionally, the findMethod() method may return a CachedMethod that automatically "maps" arrays and matrices over a scalar function. The result of invoking the CachedMethod is an array or matrix of whatever type is returned by the original function.
As an example of how this works, evaluation of the expression "fix([0.5, 0.1; 0.4, 0.3], 16, 1)" performs results in the invocation of the method named "fix" in the ptolemy.data.expr.FixPointFunctions that takes a Java double and two Java ints and returns an instance of ptolemy.math.FixPoint. This function is invoked once for each element of the matrix (converting each DoubleToken into the corresponding double, and each IntToken into the corresponding int), and the results are packaged back into a 2x2 FixMatrixToken.
Additional classes to be searched for static methods can be added through the method registerFunctionClass() in PtParser. This class assumes that new classes are added to the search path before models are constructed, and simply clears the internal cache and index when new classes are registered.
ASTPtFunctionApplicationNode
,
PtParser
Yellow (neuendor) |
Green (neuendor) |
Modifier and Type | Class and Description |
---|---|
static class |
CachedMethod.ArgumentConversion
Class representing an argument conversion.
|
static class |
CachedMethod.ArrayMapCachedMethod
A class representing the invocation of a scalar method on
an array of elements.
|
static class |
CachedMethod.BaseConvertCachedMethod
A cached method that converts the object on which the method
is invoked as well as the arguments.
|
static class |
CachedMethod.MatrixMapCachedMethod
A class representing the invocation of a scalar method on
a matrix of elements.
|
static class |
CachedMethod.TypeArgumentConversion
A class representing an argument conversion to another ptolemy type,
followed by the given conversion.
|
Modifier and Type | Field and Description |
---|---|
static CachedMethod.ArgumentConversion |
ARRAYTOKEN_CONVERSION
Conversion from an ArrayToken to a Token array (Token[]).
|
static int |
FUNCTION
Indicator of a function (vs. method).
|
static CachedMethod.ArgumentConversion |
IDENTITY_CONVERSION
Identity conversion.
|
static CachedMethod.ArgumentConversion |
IMPOSSIBLE_CONVERSION
Impossible argument conversion.
|
static int |
METHOD
Indicator of a method (vs. function).
|
static CachedMethod.ArgumentConversion |
NATIVE_CONVERSION
Conversion from tokens to Java native types.
|
Modifier | Constructor and Description |
---|---|
protected |
CachedMethod(java.lang.String methodName,
Type[] argumentTypes,
java.lang.reflect.Method method,
CachedMethod.ArgumentConversion[] conversions,
int type)
Construct a new CachedMethod.
|
Modifier and Type | Method and Description |
---|---|
protected static boolean |
_areConversionsPreferable(CachedMethod.ArgumentConversion[] conversions1,
java.lang.Class[] arguments1,
CachedMethod.ArgumentConversion[] conversions2,
java.lang.Class[] arguments2)
Return true if the conversions in the first argument are
preferable to the conversions in the third argument, for methods
with argument types given by the second and fourth arguments.
|
protected static CachedMethod.ArgumentConversion |
_getConversion(java.lang.Class formal,
Type actual)
Return a conversion to convert the second argument into the class
given by the first argument.
|
protected static java.lang.reflect.Method |
_polymorphicGetMethod(java.lang.Class library,
java.lang.String methodName,
Type[] argumentTypes,
CachedMethod.ArgumentConversion[] conversions)
Return the first method in the specified class that has the
specified name and can be invoked with the specified argument
types.
|
static void |
clear()
Clear the cache.
|
boolean |
equals(java.lang.Object object)
Return true if the argument is an instance of CachedMethod
that represents the same method or function as this instance.
|
static CachedMethod |
findMethod(java.lang.String methodName,
Type[] argumentTypes,
int type)
Find a method or function with the specified name and argument types.
|
int |
getCachedMethodType()
Return the type of this class, which is one of METHOD or FUNCTION.
|
CachedMethod.ArgumentConversion[] |
getConversions()
Return the conversions the are applied to the arguments of
this function or method.
|
java.lang.reflect.Method |
getMethod()
Return the method giving the operation associated with this
object, or null if none was found.
|
Type |
getReturnType()
Return the type of the token that results from an invocation
of this method.
|
int |
hashCode()
Return the hash code.
|
Token |
invoke(java.lang.Object[] argValues)
Apply the operation represented by this object to
the specified arguments.
|
boolean |
isFunction()
Return true if this instance represents a function (vs. a method).
|
boolean |
isMethod()
Return true if this instance represents a method (vs. a function).
|
boolean |
isValid()
Return true if the search for the method or function represented
by this object found an invokeable method.
|
java.lang.String |
methodDescription()
Return a verbose description of the cached method being invoked.
|
java.lang.String |
toString()
Return a string representation.
|
public static final int FUNCTION
public static final int METHOD
public static final CachedMethod.ArgumentConversion IMPOSSIBLE_CONVERSION
public static final CachedMethod.ArgumentConversion ARRAYTOKEN_CONVERSION
public static final CachedMethod.ArgumentConversion NATIVE_CONVERSION
public static final CachedMethod.ArgumentConversion IDENTITY_CONVERSION
protected CachedMethod(java.lang.String methodName, Type[] argumentTypes, java.lang.reflect.Method method, CachedMethod.ArgumentConversion[] conversions, int type) throws IllegalActionException
methodName
- The name of the encapsulated method.argumentTypes
- An array of token types that can be passed to
the method, subject to the given set of conversions. For a
FUNCTION, the number of argument types must be the same as the
number of arguments to the given method. For a METHOD, there
is an additional type in the array (the first) corresponding
to the type of object the method is getting invoked on.method
- The Java method that will be invoked by the
invoke() method. If this parameter is null, then the method is
not valid and cannot be invoked.conversions
- An array of conversions that will convert
arguments of the corresponding argument types to arguments
that the method will accept. If the method accepts Token
arguments, then this array will contain IDENTITY_CONVERSION
conversions. This array must be the same size as the number
of arguments to the method.type
- The type of the method.IllegalActionException
- If the return type of the
cached method cannot be determined.public static void clear()
public boolean equals(java.lang.Object object)
equals
in class java.lang.Object
object
- The object to compare to.public static CachedMethod findMethod(java.lang.String methodName, Type[] argumentTypes, int type) throws IllegalActionException
This method first attempts to resolve the function in the registered function classes by finding a method and a set of argument conversions that allow the method to be invoked on the given types. If the above fails and at least one argument is an array type or matrix type, a map is attempted over those argument types and the registered function classes are searched again. This process is repeated until all arguments are scalars or a function signature match is found.
methodName
- The method or function name.argumentTypes
- The argument types, including as the first element
the type of object on which the method is invoked, if this is a
method invocation.type
- FUNCTION or METHOD.IllegalActionException
- If the method cannot be found.public int getCachedMethodType()
public CachedMethod.ArgumentConversion[] getConversions()
public java.lang.reflect.Method getMethod() throws IllegalActionException
IllegalActionException
- If the method was not found,
or this class represents a method mapped over an array or
matrix.public Type getReturnType() throws IllegalActionException
IllegalActionException
- If a method or function with
the correct argument types was not found.public int hashCode()
hashCode
in class java.lang.Object
public Token invoke(java.lang.Object[] argValues) throws IllegalActionException
argValues
- An array of Token objects that will be used
as the arguments.IllegalActionException
- If this cached method is
not valid, or the invoked method throws it.public boolean isFunction()
public boolean isMethod()
public boolean isValid()
public java.lang.String methodDescription()
public java.lang.String toString()
toString
in class java.lang.Object
protected static boolean _areConversionsPreferable(CachedMethod.ArgumentConversion[] conversions1, java.lang.Class[] arguments1, CachedMethod.ArgumentConversion[] conversions2, java.lang.Class[] arguments2)
conversions1
- The first set of conversions.arguments1
- The arguments of the first method.conversions2
- The second set of conversions.arguments2
- The arguments of the second method.protected static CachedMethod.ArgumentConversion _getConversion(java.lang.Class formal, Type actual)
formal
- The class to which the type shall be converted.actual
- The type to be converted.protected static java.lang.reflect.Method _polymorphicGetMethod(java.lang.Class library, java.lang.String methodName, Type[] argumentTypes, CachedMethod.ArgumentConversion[] conversions)
library
- A class to be searched.methodName
- The name of the method.argumentTypes
- The types of the arguments.conversions
- An array of the same length as argumentTypes
that will be populated by this method with the conversions to
use for the arguments.