The last few days I've been experimenting of using native Java data types in the JavaScript execution environment of vRealize Automation. Here, a behavior is visible that I cannot explain.
I tried to output all types of all Java primitive data types and the non-primitive data type string. But they were converted into JavaScript data types.
If I tried the same with Rhino engine in the REPL mode, but here the data type object is always correctly returned.
Also I tried the same approach directly in the vco-app-server container of the k8s cluster, with exactly the same result. On this way I can be quite sure that the type conversion is not dependent on the OS or the JDK.
var varBool = java.lang.Boolean.TRUE;
java.lang.System.out.println(typeof varBool + ": " + varBool);
var testBool = org.mozilla.javascript.Context.jsToJava(varBool, java.lang.Boolean);
java.lang.System.out.println(typeof testBool + ": " + testBool);
typeof java.lang.Boolean.TRUE;
If the data type object is returned, I can access to the corresponding methods. With the conversion into the JavaScript data types, I cannot use these methods.
This behavior can be reproduced with the following image:
In the first step, a variable of type Boolean is created and its type is output. In the second step I tried to convert it back to the Java data type Boolean, but with the same result as described above. In the third step I used a non-primitive data type, it delivers object but a method call occurs the error message, that the object can not convert into Property. A type conversion seems to have been made here as well.
How to prevent automatic conversion, aka force conversion, into JavaScript data types? Or the other way around, how can I use native Java data types in the JavaScript execution environment?
Thanks for hints and tips.
Addendum 03.05.2023
How to handle the primitive data types and strings can be read in the reply. Now there is still the question of how to handle the non-primitive data types.
Addendum 04.06.2023
// Begin----------------------------------------------------------------
try {
var ints = java.lang.reflect.Array.newInstance(java.lang.Integer, 1);
System.log(ints.constructor.name); //Array
// System.log(ints.getClass()); // TypeError
var int = java.lang.Integer(2147483647);
// System.log(int.constructor.name); // TypeError
System.log(int.getClass()); // class java.lang.Integer
} catch (exception) {
System.log(exception);
}
// End------------------------------------------------------------------
Addendum 17.09.2023
It seems that the Dunes framework of Aria Automation changes native Java data types into in its opinion equivalent JavaScript data types automatically, Here an example:
/**
* Hint: The Method getClass is inherited from the class java.lang.Object
* and it is available in both classes, because both extends the
* Object class.
*/
/**
* The variable of the java.io.File class is automatically type casted
* into the JavaScript data type File from the Dunes Framework.
*/
var javaIoFile = java.io.File.createTempFile("vco-", null);
System.log(javaIoFile.constructor.name);
// System.log(javaIoFile.getClass().getName());
// TypeError: Cannot find function getClass in object
// The method getClass is not available here, so this variable could not
// be from type java.io.File.
/**
* The variable of the java.nio.file.Files class not type casted,
* it is a Java data type.
*/
var javaNioFile = java.nio.file.Files.createTempFile("vco-", null);
// System.log(javaNioFile.constructor.name);
// TypeError: Cannot read property "name" from undefined
System.log(javaNioFile.getClass().getName());
The result of creating a temporary file using the java.io.File class is a JavaScript data type File from the Dunes Framework. If I do the same with the java.nio.file.Files class, then a native Java data type is returned. In the latter case, the native Java class methods can be used. This approach is not very sustainable, especially when using native Java data types in arrays. Because any array is automatically type casted into a JavaScript array by the Dunes framework. This makes the result unusable for further use in the native Java context.
Addendum 22.12.2023
List of native types, which are automatically type casted.
Native Type | Converted Type |
java.lang.reflect.Array | Array |
java.io.File | File |
java.util.Properties | Properties |
java.util.HashMap | Properties |
java.net.URL | URL |
java.util.Date | Date |