Writing reflexive extension functions in Java

Reflexive extension functions written in Java map the expanded name (namespace plus local-name) of the XPath function to a Java fully-qualified class name and method or field name. There are two ways of doing the mapping:

There are a number of extension functions supplied with the Saxon product: for details, see Extensions. The source code of these methods, which in most cases is extremely simple, can be used as an example for writing other user extension functions. It is found in class com.saxonica.functions.extfn.

The command line option -TJ is useful for debugging the loading of Java extensions. It gives detailed information about the methods that are examined for a possible match.

Identifying the Java Class from the Namespace URI

There are various ways a mapping from URIs to Java classes can be established. The simplest is to use a URI that identifies the Java class explicitly. The namespace URI should be java: followed by the fully-qualified class name (for example xmlns:date="java:java.util.Date"). The class must be on the classpath.

Optionally, this URI can be followed by ?void=this (for example, xmlns:date="java:java.util.Date?void=this". The effect of this option, which was introduced in Saxon 9.7, is that calling a non-static method with a declared type of void returns the object to which it is applied, rather than returning an empty sequence. This makes it easier to avoid the problems that can arise from calls on such methods being optimized away.

For example, with the help of the XPath 3.1 arrow operator, it makes it possible to chain function calls like this: let $x := c:myObject.new() => c:setLength(3) => c:setColor('blue').

The Saxon namespace URI http://saxon.sf.net/ is recognised as a special case. In most cases it causes the function to be loaded from the class com.saxonica.functions.extfn but in a few cases, such as saxon:evaluate, the function is recognized by the compiler as if it were a built-in function. The various EXSLT and EXpath namespaces are also recognized specially.

In XSLT, the system function function-available(String name) returns true if there appears to be a method available with the right name. The function also has an optional second argument to test whether there is a method with the appropriate number of arguments. However, it is not possible to test whether the arguments are of appropriate types. If the function name is "new" it returns true so long as the class is not an abstract class or interface, and so long as it has at least one constructor.

Identifying the Java constructor, method, or field

The local name used in the XPath function call determines which constructor, method, or field of the Java class is invoked. This decision (called binding) is always made at the time the XPath expression is compiled. If methods are overloaded, static type information will be used to decide between them.

Further information can be found in the following sections: