Working with arrays
Data structures are one of the essential aspects of every piece of software that has ever been written. Any application, from simple command line util to scalable enterprise systems, constantly process various information, that very often require specific grouping and access strategies. This aspect is addressed by arrays and more advanced collection types available both in Java and .NET environments. By using the Javonet framework, users gain ability to easily and effectively work with data structures originating from the other platform.
I'll be working with the following pseudo-service class, which main purpose is to produce and consume various array-typed objects.
Retrieving and handling array from any runtime
Javonet framework allows for simple retrieval of arrays of objects, as presented in the following examples. Such objects are treated as usual any language arrays, therefore all the standard any language programming techniques apply. Although examples present objects' retrieval in result to method calls, the same approach is applicable for getting class properties, direct member access etc.
For Java developers - It's worth mentioning that, when dealing with primitive, value-typed .NET arrays, Javonet framework automatically translates them to respective Java primitive type. In such cases, it is necessary to keep in mind, that the returned array will contain boxing-type objects, otherwise we might face an InvalidClassCastException.
Example of calling method which returns array of Integers:
As for arrays containing reference-type objects, we're simply dealing with an NObject/JObject array. Working with such object is no different that for any other NObject/JObject provided by the framework from the any runtime
Example of calling method which returns array of Objects:
In the previous examples, we've been retrieving a single-dimensional arrays. It is possible however, to get an array of arrays, either for primitive- and reference-type objects. Here are the examples:
Value-typed nested arrays:
Reference-type nested arrays:
For Java developers - Unfortunately, current version of Javonet framework does not support retrieval of arrays containing arraytype objects, nested more than once. Also, the multidimensional arrays (in the .NET way of things) are not supported either. Thankfully, despite the fact, that such cases are not that common and/or easy workarounds exist, it is considered as viable enhancement, which most probably will be shipped in one of the upcoming versions.
Passing arrays
For Java developers - Being aware of the automatic type conversion for value-typed arrays, it is absolutely safe to pass regular Java arrays as arguments to the .NET code. Similarily as when retrieving primitive arrays, it is necessary to work with the boxed wrappers.
Passing array of primitive types:
In case we need to send a reference-type array, all we need to do is create an array of NObjects/JObjects and populate it, as shown in the next example.
Unlike the array-retrieval scenario, sending nested arrays is not supported at the moment, it is however considered for shipment, depending on the demand for such feature.
Summary
In this article we've focused on working with arrays using Javonet framework. As for array objects, differences of handling arrays of value- and reference-type objects have been discussed, as well as framework's current limitations have been presented.
Was this article helpful?