Skip to content

Commit 85eb8e4

Browse files
committed
Fix: JNI loading of optional SLF4J classes
As mentioned by a Stack Overflow answer: When `FindClass` is unable to find the class you are looking for, it will throw a `NoClassDefFoundError`. At the very least, this exception needs to be cleared before execution proceeds any further. Ref: https://stackoverflow.com/a/69725342/17173324
1 parent b21bb7c commit 85eb8e4

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

lib/ch_usi_si_seart_treesitter.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,12 +394,16 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) {
394394
"(Lch/usi/si/seart/treesitter/Language;)V")
395395

396396
_loadClass(_loggerClass, "org/slf4j/Logger")
397-
if (_loggerClass != NULL) {
397+
if (env->ExceptionCheck() == JNI_TRUE) {
398+
_clearException();
399+
} else {
398400
_loadMethod(_loggerDebugMethod, _loggerClass, "debug", "(Lorg/slf4j/Marker;Ljava/lang/String;)V");
399401
}
400402

401403
_loadClass(_markerFactoryClass, "org/slf4j/MarkerFactory")
402-
if (_markerFactoryClass != NULL) {
404+
if (env->ExceptionCheck() == JNI_TRUE) {
405+
_clearException();
406+
} else {
403407
_loadStaticMethod(_markerFactoryGetMarkerStaticMethod, _markerFactoryClass, "getMarker",
404408
"(Ljava/lang/String;)Lorg/slf4j/Marker;");
405409
}

0 commit comments

Comments
 (0)