I need to pass a java Map to a java function from Ballerina. In order to fill the java map, I need to convert Ballerina string
to java.lang.Object
type as put method in java map accepts javalang:Object
(ie. Generated bal file relevant to java.lang.Object
). java:fromString
returns a handle
. AFAIU handle
is a reference to the java object, not the object itself. So any idea how to achieve above requirement?
Here is the I logic need to implement;
javautil:Map? javaMap = balMaptoJavaMap(balMap);
function balMaptoJavaMap(map<string> properties) returns javautil:Map? {
javautil:HashMap hashMap = javautil:newHashMap1();
if (properties.length() > 0) {
foreach string prop in properties.keys() {
javalang:Object|error propKeyObj = prop.ensureType(javalang:Object);
javalang:Object|error propObj = (properties.get(prop)).ensureType(javalang:Object);
if (propKeyObj is error || propObj is error) {
continue;
}
_ = hashMap.put(propKeyObj, propObj);
}
}
javautil:Map|error javaMap = java:cast(hashMap);
if javaMap is error {
return ();
}
return javaMap;
}
In here I am getting TypeCastingErrors
for following lines;
javalang:Object|error propKeyObj = prop.ensureType(javalang:Object);
Actual requirement of this line is to convert a bal string
to javalang:Object
in order to call hashMap.put()
.
javautil:Map|error javaMap = java:cast(hashMap);
As explained in Bindgen Tool this line shouldn't be giving an error. Or only downcasting is supported through java:cast?
ensureType
and casting simply check if the value belongs to the target type (except with numeric values where it does a conversion). So something likejavalang:Object|error propKeyObj = prop.ensureType(javalang:Object);
will result in an error because you are checking ifprop
(which is a Ballerina string value) belongs tojavalang:Object
, which is not a type definition that includes string.With generated bindings, you can create the
javalang:Object
wrapping thehandle
value.But you can create a HashMap without using the bindgen tool in Ballerina as well. Simple example with HashMap without code generated using the bindgen tool as follows.