Skip to content

Commit fec1166

Browse files
vogellaakurtakov
authored andcommitted
[GTK] Look up the accessibility class once in os_custom.c
call_accessible_object_function() called FindClass for every accessibility callback. The class is now looked up once and kept as a global reference, primed when an accessible is registered, and a failed lookup clears the pending exception instead of leaving it for the next JNI call. FindClass resolves against the class loader of the calling Java method, so it only finds AccessibleObject while the C code is entered from a JNI native method. Assisted-by: multiple AI agents and layers of automated tooling 🤖
1 parent 7802111 commit fec1166

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

  • bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library

bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/os_custom.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,23 @@ G_DEFINE_TYPE_WITH_CODE (SwtFixedAccessible, swt_fixed_accessible, GTK_TYPE_CONT
10401040
// Fully qualified Java class name for the Java implementation of ATK functions
10411041
const char *ACCESSIBILITY_CLASS_NAME = "org/eclipse/swt/accessibility/AccessibleObject";
10421042

1043+
static jclass accessibility_class = NULL;
1044+
1045+
// FindClass resolves against the class loader of the calling Java method, which only is SWT's
1046+
// when called from a JNI native method, so the class is looked up once and kept.
1047+
static jclass get_accessibility_class (JNIEnv *env) {
1048+
if (accessibility_class == NULL) {
1049+
jclass cls = (*env)->FindClass(env, ACCESSIBILITY_CLASS_NAME);
1050+
if (cls == NULL) {
1051+
(*env)->ExceptionClear(env);
1052+
return NULL;
1053+
}
1054+
accessibility_class = (*env)->NewGlobalRef(env, cls);
1055+
(*env)->DeleteLocalRef(env, cls);
1056+
}
1057+
return accessibility_class;
1058+
}
1059+
10431060
static void swt_fixed_accessible_init (SwtFixedAccessible *accessible) {
10441061
// Initialize the SwtFixedAccessiblePrivate struct
10451062
accessible->priv = swt_fixed_accessible_get_instance_private (accessible);
@@ -1105,6 +1122,11 @@ void swt_fixed_accessible_register_accessible (AtkObject *obj, gboolean is_nativ
11051122
SwtFixedAccessiblePrivate *private = fixed->priv;
11061123
private->has_accessible = TRUE;
11071124

1125+
JNIEnv *env;
1126+
if ((*JVM)->GetEnv(JVM, (void **)&env, JNI_VERSION_10) == JNI_OK) {
1127+
get_accessibility_class(env);
1128+
}
1129+
11081130
if (!is_native) {
11091131
gtk_accessible_set_widget (GTK_ACCESSIBLE (obj), to_map);
11101132
private->widget = to_map;
@@ -2122,7 +2144,7 @@ jlong call_accessible_object_function (const char *method_name, const char *meth
21222144
}
21232145

21242146
// Find the class pointer
2125-
cls = (*env)->FindClass(env, ACCESSIBILITY_CLASS_NAME);
2147+
cls = get_accessibility_class(env);
21262148
if (cls == NULL) {
21272149
g_critical("JNI class pointer is NULL for class %s\n", ACCESSIBILITY_CLASS_NAME);
21282150
return 0;

0 commit comments

Comments
 (0)