<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi, I’m hoping to add CF_SWIFT_NAME annotations to an imported Clang module (JNI) without editing the original header file itself. Is this possible, or is there a working alternative other than creating a Swift wrapper? Maybe this is trivial but C is not my strong point.<div class=""><br class=""></div><div class="">Specifically, there are a bunch of type names in the Android JNI that I’d rather just keep „Swifty“ (the CF_SWIFT_NAME additions are my own of course, working with a copy of the original file for now):<br class=""><div class=""><br class=""></div><div class=""><div class="">typedef unsigned char <span class="Apple-tab-span" style="white-space:pre">                </span>jboolean; // can be 0 or 1 - is this the same as CFBoolean?</div><div class="">typedef signed char <span class="Apple-tab-span" style="white-space:pre">                </span>jbyte CF_SWIFT_NAME(Int8);</div><div class="">typedef unsigned short <span class="Apple-tab-span" style="white-space:pre">                </span>jchar CF_SWIFT_NAME(UInt8);</div><div class="">typedef short <span class="Apple-tab-span" style="white-space:pre">                </span>jshort CF_SWIFT_NAME(Int16);</div><div class="">typedef int <span class="Apple-tab-span" style="white-space:pre">                        </span>jint CF_SWIFT_NAME(Int32);</div><div class="">typedef long long <span class="Apple-tab-span" style="white-space:pre">                </span>jlong CF_SWIFT_NAME(Int);</div><div class="">typedef float <span class="Apple-tab-span" style="white-space:pre">                        </span>jfloat CF_SWIFT_NAME(Float);</div><div class="">typedef double <span class="Apple-tab-span" style="white-space:pre">                </span>jdouble CF_SWIFT_NAME(Double);</div></div><div class=""><br class=""></div><div class=""><br class=""></div></div><div class="">Note that there’s no good way of wrapping these in pure Swift, as far as I can see (they show up throughout the rest of the APIs). Also, it’d be great if the following was possible from outside the header. Instead of:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">typedef</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">enum </span><span style="font-variant-ligatures: no-common-ligatures;" class="">jobjectRefType {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> JNIInvalidRefType = </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(39, 42, 216);" class="">0</span><span style="font-variant-ligatures: no-common-ligatures;" class="">,</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> JNILocalRefType = </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(39, 42, 216);" class="">1</span><span style="font-variant-ligatures: no-common-ligatures;" class="">,</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> JNIGlobalRefType = </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(39, 42, 216);" class="">2</span><span style="font-variant-ligatures: no-common-ligatures;" class="">,</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> JNIWeakGlobalRefType = </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(39, 42, 216);" class="">3</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">} jobjectRefType;</span></div></div><div class=""><br class=""></div><div class="">… to use:</div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class=""><br class=""></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">typedef</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">NS_ENUM</span><span style="font-variant-ligatures: no-common-ligatures" class="">(NSInteger, jobjectRefType) {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> JNIInvalidRefType CF_SWIFT_NAME(invalid) = </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span><span style="font-variant-ligatures: no-common-ligatures" class="">,</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> JNILocalRefType CF_SWIFT_NAME(local) = </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span><span style="font-variant-ligatures: no-common-ligatures" class="">,</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> JNIGlobalRefType CF_SWIFT_NAME(global) = </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2</span><span style="font-variant-ligatures: no-common-ligatures" class="">,</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> JNIWeakGlobalRefType CF_SWIFT_NAME(weakGlobal) = </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">3</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">};</span></div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">The final question is whether it’s at all possible to annotate this kind of API:</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures" class=""> JNINativeInterface {</span></div><div style="margin: 0px; line-height: normal;" class=""><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> jclass (*DefineClass)(JNIEnv*, </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">const</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">char</span><span style="font-variant-ligatures: no-common-ligatures" class="">*, jobject, </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">const</span><span style="font-variant-ligatures: no-common-ligatures" class=""> jbyte*,</span></div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> jsize);</span></div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> // and about 100 more of these ...</span></div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">This imports into Swift as </span><b class="">jni.DefineClass(env, string, obj, bytes, size)</b>. Attempting to put a type annotation into the header results like this:</div><div style="margin: 0px; line-height: normal;" class=""><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">jclass (*DefineClass)(JNIEnv*, </span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">const</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">char</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">*, jobject, </span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">const</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class=""> jbyte*, </span><font face="Menlo" class=""><span style="font-size: 11px;" class="">size)</span></font></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-family: Menlo; font-size: 11px;" class=""> CF_SWIFT_NAME(defineClass(env:name:object:byteContents:size:))</span><span style="font-family: Menlo; font-size: 11px;" class="">;</span></div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">results in a strange warning: "‚swift_name‘ attribute has invalid identifier for base name“. It <i class="">is</i> possible to just rename <b class="">DefineClass</b> to e.g. <b class="">defineClass</b>, via <span style="font-family: Menlo; font-size: 11px;" class="">CF_SWIFT_NAME(defineClass); </span>I assume this is because this is a function pointer on a struct rather than a global function.</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">Any help regarding the above would be much appreciated. Playing around with those changes locally has made huge improvements to the readability of Android/JNI-specific Swift code. If I can find a clean solution to this I’d like to finally clean up and upstream the SwiftJNI module I started here: <a href="https://github.com/SwiftAndroid/swift-jni" class="">https://github.com/SwiftAndroid/swift-jni</a></div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">Thanks,</div><div style="margin: 0px; line-height: normal;" class="">Geordie</div></div></div></body></html>