[FR] add exception handling after JNI calls
See original GitHub issueCurrently all JNI calls made unchecked. The checked call demo:
function test1(name)
return android.jni:context(android.app.activity.vm, function(jni)
local clz, e = jni.env[0].FindClass(jni.env, name), jni.env[0].ExceptionCheck(jni.env)
if e == 0 then
e = nil
else
local ex = jni.env[0].ExceptionOccurred(jni.env)
jni.env[0].ExceptionClear(jni.env)
e = jni:to_string(
jni:callObjectMethod(ex, "getMessage", "()Ljava/lang/String;")
)
end
return clz, e
end)
end
local clzz, e = test1("java/lang/WRONGNAME")
if e then
print(e)
end
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
How to catch JNI/Java Exception - Stack Overflow
if you invoke a Java method from JNI, calling ExceptionCheck afterwards will return JNI_TRUE if an exception was thrown by the Java.
Read more >Exception Handling in JNI | Developer.com
In the native code, what we can do is call either the ExceptionOccurred() or ExceptionCheck() JNI function just after the native function call....
Read more >Exception handling in JNI and throwing it in Java
Here I explain exception handling in JNI (Java Native Interface). Check for ... This is done through the following JNI function call: void ......
Read more >JNI: Check & Handle Exception after calling back into Java
If you do not check, the next time you go through the JNI, the JNI code detects a pending exception and throws it....
Read more >[Java API] Validate that there is a check for an exception after ...
"For new JNI code validate that there is a check for an exception after all JNI calls that can raise an exception." Current...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
jni.env[0].FindClass(jni.env, name)
is a cool stuff if you’re implementing method invokation via reflection, because the name of the class in that case is not part of the API, and you’ll need to know if it is available on a given device. In that case makes sense to check exceptions before calling the method.Not interested doesn’t mean annoyed. PRs are welcome for stuff that doesn’t relate to KOReader. But in this case is not a PR, but a feature request which doesn’t serve any purpose.
To call java from lua you’ll need to know:
Any typo or mismatch in any element would make your code crash. And because lua is dynamically typed luacheck won’t alert you as both “()Ljava/lang/String;” and “()Ljava/io/File;” are valid lua strings, but one is a valid signature for the method
"getMessage"
and the other is not.The same happens when you’re checking exceptions: you call
jni:callObjectMethod
. If you calljni:callStaticObjectMethod
you’ll trigger an error, because there isn’t any static method which returns an object named “getMessage” for the classex
.Catching JNI exceptions isn’t a bad thing to add, but to serve a purpose it needs to thow the exception and interrupt the app flow, so the result would be exactly the same: a crash, but the way to retrieve the bug would be better as it would be associated to the app name and not a RuntimeError (mismatch signature) or a native crash (LuaJIT error dealing with libraries using FFI)