Class JavaExtensionFunctionCall

All Implemented Interfaces:
Callable, ExportAgent, Locatable, IdentityComparable, Traceable

public class JavaExtensionFunctionCall extends FunctionCall implements Callable
This class acts as a container for an extension function defined to call a method in a user-defined class.

Note that the binding of an XPath function call to a Java method is done in class JavaExtensionLibrary

  • Constructor Details

    • JavaExtensionFunctionCall

      public JavaExtensionFunctionCall()
      Default constructor
  • Method Details

    • init

      public void init(StructuredQName functionName, Class<?> theClass, AccessibleObject object, Configuration config)
      Initialization: creates an ExtensionFunctionCall
      Parameters:
      functionName - the name of the function, for display purposes
      theClass - the Java class containing the method to be called
      object - the method, field, or constructor of the Java class to be called
      config - the Configuration
    • getExpressionName

      public String getExpressionName()
      Get a name identifying the kind of expression, in terms meaningful to a user.
      Overrides:
      getExpressionName in class FunctionCall
      Returns:
      a name identifying the kind of expression, in terms meaningful to a user. The name will always be in the form of a lexical XML QName, and should match the name used in explain() output displaying the expression.
    • getFunctionName

      public StructuredQName getFunctionName()
      Get the qualified of the function being called
      Specified by:
      getFunctionName in class FunctionCall
      Returns:
      the qualified name
    • getTargetFunction

      public FunctionItem getTargetFunction(XPathContext context) throws XPathException
      Get the target function to be called
      Specified by:
      getTargetFunction in class FunctionCall
      Parameters:
      context - the dynamic evaluation context
      Returns:
      the target function
      Throws:
      XPathException - if the target function cannot be determined
    • preEvaluate

      public Expression preEvaluate(ExpressionVisitor visitor)
      preEvaluate: this method suppresses compile-time evaluation by doing nothing (because the external function might have side-effects and might use the context)
      Overrides:
      preEvaluate in class FunctionCall
      Parameters:
      visitor - an expression visitor
      Returns:
      the result of the early evaluation, or the original expression, or potentially a simplified expression
    • typeCheck

      public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException
      Description copied from class: FunctionCall
      Type-check the expression. This also calls preEvaluate() to evaluate the function if all the arguments are constant; functions that do not require this behavior can override the preEvaluate method.
      Overrides:
      typeCheck in class FunctionCall
      Parameters:
      visitor - an expression visitor
      contextInfo - Information available statically about the context item: whether it is (possibly) absent; its static type; its streaming posture.
      Returns:
      the original expression, rewritten to perform necessary run-time type checks, and to perform other type-related optimizations
      Throws:
      XPathException - if an error is discovered during this phase (typically a type error)
    • prepareForDynamicCall

      public void prepareForDynamicCall(Configuration config) throws XPathException
      Throws:
      XPathException
    • adjustRequiredType

      public boolean adjustRequiredType(JavaExternalObjectType requiredType) throws XPathException
      When a call to a Java extension function appears in a context where there the required type is a Java external object (typically an xsl:variable with a declared type, or as an argument to another Java extension function), notify this required type so that the process of converting the result to an XDM value can be short-circuited.
      Overrides:
      adjustRequiredType in class FunctionCall
      Parameters:
      requiredType - the required type of the result of the function, determined by the context in which the function call appears
      Returns:
      Ok if the type has been successfully adjusted
      Throws:
      XPathException - if the required return type is incompatible with the type actually returned by the Java method
    • copy

      public Expression copy(RebindingMap rebindings)
      Copy an expression. This makes a deep copy.
      Specified by:
      copy in class Expression
      Parameters:
      rebindings - variables to be re-bound
      Returns:
      the copy of the original expression
    • getIntrinsicDependencies

      public int getIntrinsicDependencies()
      Determine which aspects of the context the expression depends on. The result is a bitwise-or'ed value composed from constants such as XPathContext.VARIABLES and XPathContext.CURRENT_NODE
      Overrides:
      getIntrinsicDependencies in class Expression
      Returns:
      an integer containing bit-significant flags identifying the "intrinsic" dependencies. The flags are documented in class net.sf.saxon.value.StaticProperty
    • isSubtreeExpression

      public boolean isSubtreeExpression()
      Determine whether the expression can be evaluated without reference to the part of the context document outside the subtree rooted at the context node.
      Overrides:
      isSubtreeExpression in class Expression
      Returns:
      true if the expression has no dependencies on the context node, or if the only dependencies on the context node are downward selections using the self, child, descendant, attribute, and namespace axes.
    • addToPathMap

      public PathMap.PathMapNodeSet addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet)
      Add a representation of this expression to a PathMap. The PathMap captures a map of the nodes visited by an expression in a source tree.

      The default implementation of this method assumes that an expression does no navigation other than the navigation done by evaluating its subexpressions, and that the subexpressions are evaluated in the same context as the containing expression. The method must be overridden for any expression where these assumptions do not hold. For example, implementations exist for AxisExpression, ParentExpression, and RootExpression (because they perform navigation), and for the doc(), document(), and collection() functions because they create a new navigation root. Implementations also exist for PathExpression and FilterExpression because they have subexpressions that are evaluated in a different context from the calling expression.

      Overrides:
      addToPathMap in class Expression
      Parameters:
      pathMap - the PathMap to which the expression should be added
      pathMapNodeSet - the PathMapNodeSet to which the paths embodied in this expression should be added
      Returns:
      the pathMapNode representing the focus established by this expression, in the case where this expression is the first operand of a path expression or filter expression. For an expression that does navigation, it represents the end of the arc in the path map that describes the navigation route. For other expressions, it is the same as the input pathMapNode.
    • isNodeCheckRequired

      public boolean isNodeCheckRequired()
      Ask whether the result needs to be checked to ensure it contains no nodes from the wrong Configuration
      Returns:
      true if the result needs to be checked
    • iterate

      public SequenceIterator iterate(XPathContext context) throws XPathException
      Evaluate the function.
      Overrides:
      iterate in class FunctionCall
      Parameters:
      context - The context in which the function is to be evaluated
      Returns:
      a Value representing the result of the function.
      Throws:
      XPathException - if the function cannot be evaluated.
    • evaluateItem

      public Item evaluateItem(XPathContext context) throws XPathException
      Evaluate the function returning a single item or null.
      Overrides:
      evaluateItem in class FunctionCall
      Parameters:
      context - The context in which the function is to be evaluated
      Returns:
      a Value representing the result of the function.
      Throws:
      XPathException - if the function cannot be evaluated.
    • getTargetClass

      public Class<?> getTargetClass()
      Get the class containing the method being called
      Returns:
      the class containing the target method
    • getTargetMethod

      public AccessibleObject getTargetMethod()
      Get the target method (or field, or constructor) being called
      Returns:
      the target method, field, or constructor
    • getParameterTypes

      public Class<?>[] getParameterTypes()
      Get the types of the arguments
      Returns:
      an array of classes representing the declared types of the arguments to the method or constructor
    • call

      public Sequence call(XPathContext context, Sequence[] argValues) throws XPathException
      Call an extension function previously identified using the bind() method. A subclass can override this method.
      Specified by:
      call in interface Callable
      Parameters:
      context - The XPath dynamic evaluation context
      argValues - The values of the arguments
      Returns:
      The value returned by the extension function
      Throws:
      XPathException - if a dynamic error occurs during the evaluation of the expression
    • getItemType

      public ItemType getItemType()
      Determine the data type of the expression, if possible. All expressions return sequences, in general; this method determines the type of the items within the sequence, assuming that (a) this is known in advance, and (b) it is the same for all items in the sequence.

      This method will always return a result, though it may be the best approximation that is available at the time.

      Specified by:
      getItemType in class Expression
      Returns:
      the item type
    • computeCardinality

      protected int computeCardinality()
      Description copied from class: Expression
      Compute the static cardinality of this expression
      Specified by:
      computeCardinality in class Expression
      Returns:
      the computed cardinality, as one of the values StaticProperty.ALLOWS_ZERO_OR_ONE, StaticProperty.EXACTLY_ONE, StaticProperty.ALLOWS_ONE_OR_MORE, StaticProperty.ALLOWS_ZERO_OR_MORE. May also return StaticProperty.ALLOWS_ZERO if the result is known to be an empty sequence, or StaticProperty.ALLOWS_MANY if if is known to return a sequence of length two or more.
    • getReturnClass

      public Class<?> getReturnClass()
      Get the Java class of the value returned by the method
      Returns:
      the Java class of the value returned by the method
    • getArgumentConverters

      public PJConverter[] getArgumentConverters()
      Get the converters used to convert the arguments from XPath values to Java values
      Returns:
      an array of converters, one per argument
    • getResultConverter

      public JPConverter getResultConverter()
      Get the converter used to convert the result from a Java object to an XPath value
      Returns:
      the converter that is used
    • invokeConstructor

      protected Object invokeConstructor(Constructor<?> constructor, Object[] params) throws InstantiationException, IllegalAccessException, InvocationTargetException
      Invoke a constructor. This method is provided separately so that it can be refined in a subclass. For example, a subclass might perform tracing of calls, or might trap exceptions.
      Parameters:
      constructor - The constructor to be invoked
      params - The parameters to be passed to the constructor
      Returns:
      The object returned by the constructor
      Throws:
      InstantiationException - if the invocation throws an InstantiationException
      IllegalAccessException - if the invocation throws an IllegalAccessException
      InvocationTargetException - if the invocation throws an InvocationTargetException (which happens when the constructor itself throws an exception)
    • invokeMethod

      protected Object invokeMethod(Method method, Object instance, Object[] params) throws IllegalAccessException, InvocationTargetException
      Invoke a method. This method is provided separately so that it can be refined in a subclass. For example, a subclass might perform tracing of calls, or might trap exceptions.
      Parameters:
      method - The method to be invoked
      instance - The object on which the method is to be invoked. This is set to null if the method is static.
      params - The parameters to be passed to the method
      Returns:
      The object returned by the method
      Throws:
      IllegalAccessException - if the invocation throws an IllegalAccessException
      InvocationTargetException - if the invocation throws an InvocationTargetException (which happens when the method itself throws an exception)
    • getField

      protected Object getField(Field field, Object instance) throws IllegalAccessException
      Access a field. This method is provided separately so that it can be refined in a subclass. For example, a subclass might perform tracing of calls, or might trap exceptions.
      Parameters:
      field - The field to be retrieved
      instance - The object whose field is to be retrieved. This is set to null if the field is static.
      Returns:
      The value of the field
      Throws:
      IllegalAccessException - if the invocation throws an IllegalAccessException
    • applyFunctionConversionRules

      public static Sequence applyFunctionConversionRules(Sequence suppliedValue, SequenceType requiredType, StructuredQName functionName, int argPosition, XPathContext context) throws XPathException
      Utility method available for use in called extension functions to check that a supplied argument conforms to a required type, and perform conversion using the function conversion rules if not
      Parameters:
      suppliedValue - the value supplied by the caller
      requiredType - the type required for the argument
      functionName - the name of the function (for diagnostics)
      argPosition - the ordinal position of the argument being checked in the XPath function call, starting at 0 for the first argument
      context - the XPath dynamic context
      Returns:
      the result of applying the function conversion rules (with XPath 1.0 compatibility mode set to false)
      Throws:
      XPathException - if type checking fails
    • toCamelCase

      public static String toCamelCase(String name, boolean debug, Logger diag)
      Convert a name to camelCase (by removing hyphens and changing the following letter to capitals)
      Parameters:
      name - the name to be converted to camelCase
      debug - true if tracing is required
      diag - the output stream for diagnostic trace output
      Returns:
      the camelCased name
    • isReturnVoidAsThis

      public boolean isReturnVoidAsThis()
    • export

      public void export(ExpressionPresenter out) throws XPathException
      Diagnostic print of expression structure. The abstract expression tree is written to the supplied output destination.
      Specified by:
      export in interface ExportAgent
      Overrides:
      export in class FunctionCall
      Parameters:
      out - the destination of the output
      Throws:
      XPathException - if the export fails, for example if an expression is found that won't work in the target environment.
    • getElaborator

      public Elaborator getElaborator()
      Make an elaborator for this expression
      Overrides:
      getElaborator in class Expression
      Returns:
      an appropriate Elaborator