Changes

Jump to navigation Jump to search
1,100 bytes added ,  15:10, 16 July 2021
===Conversion to XQuery===
By default, Java values with the most common types (as shown in the second and third column of the table) are converted to XQuery values. All other values are returned as ''Java items'', which are function items with an embedded Java value. The results of constructor calls will always be returned as Java items.
The conversion of the embedded value to XQuery is enforced by invoking the function item: Values in {{Code|Iterator}} and {{Code|Iterable}} instances (Lists, Sets and Collections) are converted to items, and maps are converted to XQuery maps:
</syntaxhighlight>
If no conversion is defined, the string representation of the Java value is returned, which is returned by the object’s {{Code|toString()}} method. In contrast to other This function items, it is legal to retrieve also called if the string value repesentation of a Java itemis requested:
<syntaxhighlight lang="xquery">
(: returns the string representation representations of the Java object a HashMap and an ArrayList instance :)
'Map: ' || Q{java.util.HashMap}new(),
string(Q{java:java.util.ArrayList}new())
</syntaxhighlight>
The implicit conversion can be further controlled with the {{Option|WRAPJAVA}} option. The following values exist:
{| class="wikitable"
| {{Code|all}}
| All values (excluding those inheriting the internal type {{Code|org.basex.query.value.Value}}) are returned as Java items. This is helpful if Java function calls are chained, and if no intermediate conversion is required or desired.
|- valign="top"
| {{Code|instance}}
| The result of a Java call will be ignored, and a supplied class instance (if there is any) is returned instead.
|- valign="top"
| {{Code|void}}
| The result of a Java call will be ignored, and an empty sequence is returned instead.
|}
In the following example, the result of the first function – a char array – is wrapped and passed on to a {{Code|CharBuffer}} function. Without the option, the single-value array would be converted to an {{Code|xs:unsignedShort}} item and the second function call would fail:
<syntaxhighlight lang="xquery">
</syntaxhighlight>
Without The next example demonstrates a use case for the {{Code|instance}} option: <syntaxhighlight lang="xquery">declare namespace set = 'java:java.util.HashSet';let $set := (# db:wrapjava instance #) { set:new() => set:add('1') => set:add('2')}return $set()</syntaxhighlight> The {{Code|void}} option is helpful if side-effecting methods return values that do not contribute to the final result: <syntaxhighlight lang="xquery">declare namespace set = 'java:java.util.HashSet';let $set := set:new()return ( (# db:wrapjava void #) { for $i in 1 to 100 return set:add($set, the single-value array would $i) }, $set())</syntaxhighlight> The irrelevant results could also be converted to an dropped with {{CodeFunction|Profiling|xsprof:unsignedShortvoid}} item and the second function call would fail.
{| class="wikitable"
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu