Revision [1580]
This is an old revision of JavaObjects made by DavidLee on 2010-11-18 13:47:27.
Java Objects
Version 1.1 introduced direct support for creating native Java Objects, invoking both static and instance methods, storing Java Objects in Variables and passing object references through Command Invocation and Function Calls.
Java Objects can be created in xmlsh scripting language or passed in externally via one of the integration API's.
Java Objects may be passed to commands and functions (including user defined modules) as well as set into variables accessible by any xmlsh script code, functions, commands or modules.
Creating a Java Object
To create a Java Object use either the Function jnew or the Command jset.
For example, to create a Date object and assign it to variable "date" you can either
date=jnew(java.util.Date)
or
jset -v date -c java.util.Date
To see the object has been created properly and of the right type, use the Commmand xtype.
xtype $date
Result
java.util.Date
With jnew you can create Java Objects with non-default constructors.
Example: create an Integer with the value 123
i=jnew(java.lang.Integer 123) xtype $i echo $i
Result
java.lang.Integer 123
Converting To Strings
To convert an object to a String, simply use it in any string context or pass it as an argument to a command that accepts strings.
( Note this actually converts the object to an XDM string type, not the java String type.
xtype $date echo $date xtype "$date" echo "The date is $date"
Result
Thu Nov 18 16:40:58 EST 2010 xs:string echo "The date is $date"
To get a real Java java.lang.String object you can use the toString() method ( using the Method Call syntax below)
xtype date.toString()
Result:
java.lang.String
For most uses, xs:string and java.lang.String are interchangable.