Accessing Java constant from XQuery
Wednesday, 16 February 2022 · 3 messages
PJ
Paul L. Merchant Jr.16 Feb 2022 14:55 #
Hi everyone, I'm using a Java library in XQuery in BaseX and I haven't been able to determine if there's a way to access a constant string (final static String... or even just final String...) in the Java class from XQuery.
For example, if the Java class is declared as
package my.module;
public class MyModule {
public static final String TITLE ="I'm a module!";
public String hello(final String world) {
return "Hello " + world;
}
}The XQuery to call the "hello" method is
import module namespace java = 'java:my.module.MyModule';
java:hello("World!")But is there any way to access "TITLE"?
I'd like to share some constants between XQuery and Java, and I could write accessor functions for these constants, but it'd be cleaner if I didn't have to.
Thanks!
JM
Johan Mörén16 Feb 2022 16:15 #
In your case, i think, java:TITLE() would work.
Just trying with java.lang.Integer i got the following code to work
declare namespace integer="java:java.lang.Integer";integer:MIN_VALUE()
Quoted text · Paul L. Merchant Jr. · 36 lines
On Wed, Feb 16, 2022 at 2:55 PM Paul L. Merchant Jr. < Paul.L.Merchant.Jr at dartmouth.edu> wrote: > Hi everyone, I'm using a Java library in XQuery in BaseX and I haven't > been able to determine if there's a way to access a constant string (final > static String... or even just final String...) in the Java class from > XQuery. > > For example, if the Java class is declared as > > package my.module; > > public class MyModule { > public static final String TITLE ="I'm a module!"; > > public String hello(final String world) { > return "Hello " + world; > } > } > > The XQuery to call the "hello" method is > > import module namespace java = 'java:my.module.MyModule'; > > java:hello("World!") > > > But is there any way to access "TITLE"? > > I'd like to share some constants between XQuery and Java, and I could > write accessor functions for these constants, but it'd be cleaner if I > didn't have to. > > Thanks! > > >
PJ
Paul L. Merchant Jr.16 Feb 2022 16:26 #
Thanks, that helped! The key was to use "declare namespace java...." instead of "import module namespace java....".
The subtle difference was lost on my first reading of the documentation, but I now see and understand the example on the Java_Bindings page thanks to your reply.
Much appreciated!
Quoted text · Johan Mörén · 1 line
On Feb 16, 2022, at 10:15 AM, Johan Mörén <johan.moren at gmail.com<mailto:johan.moren at gmail.com>> wrote:
In your case, i think, java:TITLE() would work.
Just trying with java.lang.Integer i got the following code to work
declare namespace integer="java:java.lang.Integer";integer:MIN_VALUE()
Quoted text · Paul L. Merchant Jr. · 1 line
On Wed, Feb 16, 2022 at 2:55 PM Paul L. Merchant Jr. <Paul.L.Merchant.Jr at dartmouth.edu<mailto:Paul.L.Merchant.Jr at dartmouth.edu>> wrote:
Hi everyone, I'm using a Java library in XQuery in BaseX and I haven't been able to determine if there's a way to access a constant string (final static String... or even just final String...) in the Java class from XQuery.
For example, if the Java class is declared as
package my.module;
public class MyModule {
public static final String TITLE ="I'm a module!";
public String hello(final String world) {
return "Hello " + world;
}
}The XQuery to call the "hello" method is
import module namespace java = 'java:my.module.MyModule';
java:hello("World!")But is there any way to access "TITLE"?
I'd like to share some constants between XQuery and Java, and I could write accessor functions for these constants, but it'd be cleaner if I didn't have to.
Thanks!
Written for BaseX 9.6. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery