I am calling an instance method of Java class from C++ using JNI. My Java's method return set and I need to catch this in C++.Can someone please help?
My Java method look like below.
@Override
public Set<String> getRealmRights()
{
Set<String> realmRights = new HashSet<>();
-----------------
-----------------
return realmRights;
}
I am invoking the method from C++ like below.
jmethodID getRealmRightsMethodId = env->GetMethodID(keycloakAdapterApplicationClassId, "getRealmRights", "()Ljava/util/Set;");
if(getRealmRightsMethodId != nullptr)
{
jobject rights = (jobject ) env->CallObjectMethod(keycloakAdapterApplication, getRealmRightsMethodId);
//@ this stage I want to read the information from jobject,Please help?
}
I am getting method id correctly and even able to execute the method.
I tried to type cast jobject in set but failed. Can I directly catch this in a set OR can i convert jobject into set ?
I want to print the information returned by java function.Please help?
You have to access
Setclass methods and retrieve objects stored inside collection.I don't have out of the box sample for sets, but it will be very similar to code that you can find here:
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo037
What you need to do, is to get methods from
Setobject usingGetMethodIDand then, call them.This way, you can descent to data stored inside
Set. In your case, you have to:GetMethodID(and passSetobject into it) to getIteratorfromSethttps://docs.oracle.com/javase/7/docs/api/java/util/Set.html#iterator()
call method
GetMethodIDfor object of classIteratorso you can get two methods:hasNextandnextthen, you have to iterate over
Setusing the Iterator and retrieve elements one by one (they will be ofjobjects type)You can also take a look here:
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo042