Hi Christian,
Thanks for the advice. I've tried a few variations based on your example. There seems to be a problem passing the Java array value to the Java function that has the varargs parameter.
I tried constructing the list in this manner, which produces the expected list.
declare namespace Language = "java:com.github.pemistahl.lingua.api.Language"; declare namespace List = 'java:java.util.List'; List:toArray(List:of( Language:ENGLISH(), Language:GERMAN() )) ! (., string())
When I try passing the list to the Java constructor:
declare namespace Builder = "java:com.github.pemistahl.lingua.api.LanguageDetectorBuilder"; declare namespace Language = "java:com.github.pemistahl.lingua.api.Language"; declare namespace List = 'java:java.util.List'; Builder:fromLanguages·com.github.pemistahl.lingua.api.Language...( List:toArray(List:of( Language:ENGLISH(), Language:GERMAN() )))
I get this error:
[XPTY0004] com.github.pemistahl.lingua.api.LanguageDetectorBuilder:fromLanguages(com.github.pemistahl.lingua.api.Language[]) expected, () found.
When I try constructing the list first and then passing the value using ArrayList:
declare namespace Builder = "java:com.github.pemistahl.lingua.api.LanguageDetectorBuilder"; declare namespace Language = "java:com.github.pemistahl.lingua.api.Language"; declare namespace list = 'java.util.ArrayList'; let $list := list:new() where list:add($list, Language:ENGLISH()) where list:add($list, Language:GERMAN()) let $array := list:toArray($list) return Builder:fromLanguages·com.github.pemistahl.lingua.api.Language...($array)
I get this error:
[XPTY0004] com.github.pemistahl.lingua.api.LanguageDetectorBuilder:fromLanguages(com.github.pemistahl.lingua.api.Language[]) expected, (VarRef) found.
I've tried other variations but each time get a slightly different error message, for such as:
[XPTY0004] com.github.pemistahl.lingua.api.LanguageDetectorBuilder:fromLanguages(com.github.pemistahl.lingua.api.Language[]) expected, (GFLWOR) found.
Do you have any suggestions?
Thank you, Vincent
_____________________________________________ Vincent M. Lizzi Head of Information Standards | Taylor & Francis Group vincent.lizzi@taylorandfrancis.com
Information Classification: General -----Original Message----- From: Christian Grün christian.gruen@gmail.com Sent: Saturday, July 1, 2023 2:29 AM To: Lizzi, Vincent Vincent.Lizzi@taylorandfrancis.com Cc: BaseX basex-talk@mailman.uni-konstanz.de Subject: Re: [basex-talk] Calling a Java function with varargs parameter from XQuery
Hi Vincent,
In general, varargs parameters can be invoked similarly as functions with arrays, but it can be tricky to prepare the arguments in a way that they will be passed on as arrays. An explicit array conversion may help. This Java code…
ArrayList<String> list = new ArrayList<>(); list.add("a"); list.add("b"); System.out.println(String.join("/", list.toArray(String[]::new)));
…can be written as:
declare namespace list = 'java.util.ArrayList'; let $list := list:new() where list:add($list, 'a') where list:add($list, 'b') let $array := list:toArray($list) (: with types: "join·CharSequence·CharSequence..." :) return Q{java.lang.String}join('/', $array)
When the code gets more complex, it’s still more convenient to write an additional Java wrapper class.
Hope this helps, Christian