{"record":{"id":"03b136a657032cea","repo":"java-native-access/jna","slug":"unable-to-cast-to-cfstring-type-id-typeid","errorCode":null,"errorMessage":"Unable to cast to CFString. Type ID: {typeId}","messagePattern":"Unable to cast to CFString\\. Type ID: (.+?)","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/mac/CoreFoundation.java","lineNumber":516,"sourceCode":"\n        /**\n         * Placeholder for a reference to a {@code CFString} object.\n         */\n        public static class ByReference extends PointerByReference {\n            public ByReference() {\n                this(null);\n            }\n\n            public ByReference(CoreFoundation.CFStringRef value) {\n                super(value != null ? value.getPointer() : null);\n            }\n\n            @Override\n            public void setValue(Pointer value) {\n                if (value != null) {\n                    CFTypeID typeId = INSTANCE.CFGetTypeID(value);\n                    if (!STRING_TYPE_ID.equals(typeId)) {\n                        throw new ClassCastException(\"Unable to cast to CFString. Type ID: \" + typeId);\n                    }\n                }\n\n                super.setValue(value);\n            }\n\n            public CoreFoundation.CFStringRef getStringRefValue() {\n                Pointer value = super.getValue();\n                if (value == null) {\n                    return null;\n                }\n\n                return new CoreFoundation.CFStringRef(value);\n            }\n        }\n\n        public CFStringRef() {\n            super();","sourceCodeStart":498,"sourceCodeEnd":534,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/mac/CoreFoundation.java#L498-L534","documentation":"CFStringRef.ByReference.setValue checks that the Pointer assigned is a CFString by comparing CFGetTypeID against STRING_TYPE_ID and throws ClassCastException if it is not (null is permitted and bypasses the check). It guards dictionary/string-typed out-parameters against receiving other CFTypes.","triggerScenarios":"Calling setValue(pointer) on a CFStringRef.ByReference with a pointer to a CFNumber, CFArray, CFBoolean, CFData, or any non-string CFType.","commonSituations":"Assigning results from generic CF functions that return multiple possible types; passing the wrong variable or wrong ByReference slot; populating struct fields where the producer wrote a non-string type.","solutions":["Validate with new CFTypeRef(value).isTypeID(CoreFoundation.STRING_TYPE_ID) before calling setValue.","Create CFStrings explicitly via CFStringRef.createCFString(javaString) when you intend to store a string.","Pass null if the slot should be cleared — null skips the type check.","Inspect the reported type id in the exception to identify what object was mistakenly passed."],"exampleFix":"// before\nstringRef.setValue(somePointer); // throws if not a CFString\n// after\nCFStringRef str = CFStringRef.createCFString(\"value\");\nstringRef.setValue(str);","handlingStrategy":"type-guard","validationCode":"if (value == null || new CFTypeRef(value).isTypeID(CoreFoundation.STRING_TYPE_ID)) {\n    stringRef.setValue(value);\n}","typeGuard":"boolean isCFString(Pointer p) {\n    return p != null && new CFTypeRef(p).isTypeID(CoreFoundation.STRING_TYPE_ID);\n}","tryCatchPattern":"try {\n    stringRef.setValue(value);\n} catch (ClassCastException e) {\n    // non-string assigned; create a CFString explicitly instead\n}","preventionTips":["Build CFStrings with CFStringRef.createCFString rather than wrapping raw pointers","Verify STRING_TYPE_ID before assigning pointers to ByReference slots","Pass null to clear slots instead of zeroed/garbage pointers","Separate raw CFTypeRef plumbing from typed CFStringRef usage"],"tags":["macos","corefoundation","classcastexception","native"],"backgroundTag":"type-mismatch","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}