Class CoercedFunction

  • All Implemented Interfaces:
    Callable, Function, GroundedValue, Item, Sequence

    public class CoercedFunction
    extends AbstractFunction
    A function item obtained by coercing a supplied function; this adds a wrapper to perform dynamic type checking of the arguments in any call, and type checking of the result.
    • Constructor Detail

      • CoercedFunction

        public CoercedFunction​(Function targetFunction,
                               SpecificFunctionType requiredType)
                        throws XPathException
        Create a CoercedFunction as a wrapper around a target function
        Parameters:
        targetFunction - the function to be wrapped by a type-checking layer
        requiredType - the type of the coerced function, that is the type required by the context in which the target function is being used
        Throws:
        XPathException - if the arity of the supplied function does not match the arity of the required type
      • CoercedFunction

        public CoercedFunction​(SpecificFunctionType requiredType)
        Create a CoercedFunction whose target function is not yet known (happens during package re-loading)
        Parameters:
        requiredType - the type of the coerced function, that is the type required by the context in which the target function is being used
    • Method Detail

      • setTargetFunction

        public void setTargetFunction​(Function targetFunction)
                               throws XPathException
        Set the target function
        Parameters:
        targetFunction - the function to be wrapped by a type-checking layer
        Throws:
        XPathException - if the arity of the supplied function does not match the arity of the required type
      • typeCheck

        public void typeCheck​(ExpressionVisitor visitor,
                              ContextItemStaticInfo contextItemType)
                       throws XPathException
        Type check the function (may modify it by adding code for converting the arguments)
        Overrides:
        typeCheck in class AbstractFunction
        Parameters:
        visitor - the expression visitor, supplies context information
        contextItemType - the context item type at the point where the function definition appears
        Throws:
        XPathException - if type checking of the target function fails
      • getFunctionItemType

        public FunctionItemType getFunctionItemType()
        Get the item type of the function item
        Returns:
        the function item's type
      • getFunctionName

        public StructuredQName getFunctionName()
        Get the name of the function, or null if it is anonymous
        Returns:
        null indicating that this is an anonymous inline function
      • getDescription

        public java.lang.String getDescription()
        Get a description of this function for use in error messages. For named functions, the description is the function name (as a lexical QName). For others, it might be, for example, "inline function", or "partially-applied ends-with function".
        Returns:
        a description of the function for use in error messages
      • getArity

        public int getArity()
        Get the arity of the function
        Returns:
        the number of arguments in the function signature
      • call

        public Sequence call​(XPathContext context,
                             Sequence[] args)
                      throws XPathException
        Invoke the function
        Parameters:
        context - the XPath dynamic evaluation context
        args - the actual arguments to be supplied
        Returns:
        the result of invoking the function
        Throws:
        XPathException - if execution of the function fails
      • coerce

        public static CoercedFunction coerce​(Function suppliedFunction,
                                             SpecificFunctionType requiredType,
                                             RoleDiagnostic role)
                                      throws XPathException
        Factory method to create a CoercedFunction with a given type, for a given targetFunction. It is assumed that we have already established that coercion is needed. Called from Bytecode compiler.
        Parameters:
        suppliedFunction - the function to be coerced
        requiredType - the target type for the coercion
        role - diagnostic information about the role of the expression being coerced
        Returns:
        the function after coercion
        Throws:
        XPathException - if the function cannot be coerced to the required type (for example, because it has the wrong arity)