Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mobile: Part 2: Update JNI usages with JniHelper #30748

Merged
merged 4 commits into from
Nov 6, 2023

Conversation

fredyw
Copy link
Member

@fredyw fredyw commented Nov 6, 2023

This updates the following methods to JniHelper.

  • FindClass
  • NewStringUTF

Risk Level: low (refactor)
Testing: unit tests & integration tests
Docs Changes: n/a
Release Notes: n/a
Platform Specific Features: mobile

This updates the use of `FindClass` with `JniHelper`.

Signed-off-by: Fredy Wijaya <[email protected]>
Copy link

As a reminder, PRs marked as draft will not be automatically assigned reviewers,
or be handled by maintainer-oncall triage.

Please mark your PR as ready when you want it to be reviewed!

🐱

Caused by: #30748 was opened by fredyw.

see: more, trace.

Signed-off-by: Fredy Wijaya <[email protected]>
Signed-off-by: Fredy Wijaya <[email protected]>
@fredyw fredyw marked this pull request as ready for review November 6, 2023 17:51
@fredyw
Copy link
Member Author

fredyw commented Nov 6, 2023

/assign @abeyad @RyanTheOptimist

Copy link
Contributor

@abeyad abeyad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

jmethodID jmid_hashMapPut = jni_helper.getMethodId(
jcls_hashMap, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
jcls_hashMap.get(), "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: not super important and not for this PR, but perhaps at some point we may want to consider adding the dereferencing operator (i.e. *) to LocalRefUniquePtr so we can use it instead of .get(). OTOH, .get() makes it clearer, so I'm indifferent either way, just thought I'd mention as food for thought.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::unique_ptr already implements operator *(), but it will dereference it whereas std::unique_ptr<>::get() will simply return the borrowed pointer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm slightly confused by this. It looks like the method signature for getMethodId() is:

jmethodID getMethodId(jclass clazz, const char* name, const char* signature);

The type of jcls_hashMap is LocalRefUniquePtr<jclass> so I would expect we would want to pass *jcls_hashMap into getMethodId since we want a jclass not jclass*. But clearly your code works so I must be misunderstanding. Can you explains?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JNI types are extremely confusing.

Here's the definition.

typedef _jclass *jclass;
LocalRefUniquePtr<jclass> foo = jni_helper.findClass("...");
jclass jc1 = foo.get(); // will return jclass
_jclass jc2 = *foo; // deference, will return _jclass instead of jclass

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, ffs! That's crazy. Thanks, java :)

@abeyad abeyad merged commit 29ddf84 into envoyproxy:main Nov 6, 2023
@fredyw fredyw deleted the jni_helper_update2 branch November 6, 2023 17:57
jmethodID jmid_hashMapPut = jni_helper.getMethodId(
jcls_hashMap, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
jcls_hashMap.get(), "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm slightly confused by this. It looks like the method signature for getMethodId() is:

jmethodID getMethodId(jclass clazz, const char* name, const char* signature);

The type of jcls_hashMap is LocalRefUniquePtr<jclass> so I would expect we would want to pass *jcls_hashMap into getMethodId since we want a jclass not jclass*. But clearly your code works so I must be misunderstanding. Can you explains?

@@ -76,7 +76,7 @@ jlongArray native_final_stream_intel_to_array(JniHelper& jni_helper,
*/
jobject native_map_to_map(JniHelper& jni_helper, envoy_map map);

jstring native_data_to_string(JniHelper& jni_helper, envoy_data data);
LocalRefUniquePtr<jstring> native_data_to_string(JniHelper& jni_helper, envoy_data data);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too late for this PR, but as we're moving away from a "C style" API here, should we consider moving these functions over to Envoy's camelCaseStyle?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's also my plan once we have migrated everything to JniHelper.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants