Skip to content

Commit e5dc93d

Browse files
committed
[Cocoa] Fix deprecated SEL→jlong cast in sel_registerName via flags=cast
The Clang compiler (-Wcast-of-sel-type) deprecates any direct cast of the Objective-C SEL type to an arithmetic or pointer type, recommending the use of sel_getName() instead. The warning appeared in os.c: rc = (jlong)sel_registerName(lparg0); // warning: cast of type 'SEL _Nonnull' to 'jlong' is deprecated; // use sel_getName instead [-Wcast-of-sel-type] Rather than routing through sel_getName() (which returns a const char* that would still require an integer cast), the fix applies the same pattern already used for objc_msgSend throughout SWT: the `flags=cast` JNI generator annotation, which casts the entire C function pointer to the desired return type instead of casting the returned value: rc = (jlong)((jlong (*)(const char*))sel_registerName)(lparg0); This avoids any explicit cast of the SEL value itself. The function pointer cast tells the compiler that the function returns jlong directly, bypassing the SEL type in the return position entirely. This is semantically safe on 64-bit platforms where SEL (a pointer) and jlong are both 8 bytes with the same bit representation. The annotation changes in OS.java drive the code generation: - @method flags=cast — use function-pointer-cast call pattern - @param selectorName cast=(const char*) — match the native signature
1 parent d820f7e commit e5dc93d

File tree

2 files changed

+5
-1
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT PI/cocoa

2 files changed

+5
-1
lines changed

bundles/org.eclipse.swt/Eclipse SWT PI/cocoa/library/os.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7645,7 +7645,7 @@ JNIEXPORT jlong JNICALL OS_NATIVE(sel_1registerName)
76457645
jlong rc = 0;
76467646
OS_NATIVE_ENTER(env, that, sel_1registerName_FUNC);
76477647
if (arg0) if ((lparg0 = (*env)->GetStringUTFChars(env, arg0, NULL)) == NULL) goto fail;
7648-
rc = (jlong)sel_registerName(lparg0);
7648+
rc = (jlong)((jlong (*)(const char*))sel_registerName)((const char*)lparg0);
76497649
fail:
76507650
if (arg0 && lparg0) (*env)->ReleaseStringUTFChars(env, arg0, lparg0);
76517651
OS_NATIVE_EXIT(env, that, sel_1registerName_FUNC);

bundles/org.eclipse.swt/Eclipse SWT PI/cocoa/org/eclipse/swt/internal/cocoa/OS.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,10 @@ public static boolean isBigSurOrLater () {
562562
* @param clazz cast=(Class)
563563
*/
564564
public static final native long object_setClass(long obj, long clazz);
565+
/**
566+
* @method flags=cast
567+
* @param selectorName cast=(const char*)
568+
*/
565569
public static final native long sel_registerName(String selectorName);
566570
public static final native int objc_super_sizeof();
567571

0 commit comments

Comments
 (0)