{"record":{"id":"c3cf313f4654224f","repo":"java-native-access/jna","slug":"unable-to-cast-to-cfdictionary-type-id-typeid","errorCode":null,"errorMessage":"Unable to cast to CFDictionary. Type ID: {typeId}","messagePattern":"Unable to cast to CFDictionary\\. Type ID: (.+?)","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/mac/CoreFoundation.java","lineNumber":398,"sourceCode":"\n        /**\n         * Placeholder for a reference to a {@code CFDictionary} object.\n         */\n        public static class ByReference extends PointerByReference {\n            public ByReference() {\n                this(null);\n            }\n\n            public ByReference(CoreFoundation.CFDictionaryRef 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 (!DICTIONARY_TYPE_ID.equals(typeId)) {\n                        throw new ClassCastException(\"Unable to cast to CFDictionary. Type ID: \" + typeId);\n                    }\n                }\n\n                super.setValue(value);\n            }\n\n            public CoreFoundation.CFDictionaryRef getDictionaryRefValue() {\n                Pointer value = super.getValue();\n                if (value == null) {\n                    return null;\n                }\n\n                return new CoreFoundation.CFDictionaryRef(value);\n            }\n        }\n\n        public CFDictionaryRef() {\n            super();","sourceCodeStart":380,"sourceCodeEnd":416,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/mac/CoreFoundation.java#L380-L416","documentation":"CFDictionaryRef.ByReference.setValue validates that the Pointer being assigned is a CFDictionary by comparing CFGetTypeID with DICTIONARY_TYPE_ID, throwing ClassCastException if it is not (null values are allowed and skip the check). This prevents storing a non-dictionary CFType into a dictionary-typed ByReference slot.","triggerScenarios":"Calling setValue(pointer) on a CFDictionaryRef.ByReference with a pointer to a CFString, CFArray, CFNumber, CFData, or any non-dictionary CFType obtained from generic CF calls.","commonSituations":"Populating out-parameters or struct fields from heterogeneous native results; wiring up the wrong variable to setValue; passing a result of a failed lookup that returned a different CFType.","solutions":["Before setValue, verify new CFTypeRef(value).isTypeID(CoreFoundation.DICTIONARY_TYPE_ID).","If a null assignment is intended, pass null explicitly (the check is skipped for null).","Confirm the variable holding the pointer was produced by a Create/Copy CFDictionary call.","Log the offending type id to identify what object was actually passed."],"exampleFix":"// before\nbyRef.setValue(somePointer); // throws if somePointer is a CFString\n// after\nCFTypeRef ref = new CFTypeRef(somePointer);\nif (ref.isTypeID(CoreFoundation.DICTIONARY_TYPE_ID)) {\n    byRef.setValue(somePointer);\n} else {\n    byRef.setValue(null); // or handle the mismatch\n}","handlingStrategy":"type-guard","validationCode":"if (value == null || new CFTypeRef(value).isTypeID(CoreFoundation.DICTIONARY_TYPE_ID)) {\n    byRef.setValue(value);\n}","typeGuard":"boolean isCFDictionary(Pointer p) {\n    return p != null && new CFTypeRef(p).isTypeID(CoreFoundation.DICTIONARY_TYPE_ID);\n}","tryCatchPattern":"try {\n    byRef.setValue(value);\n} catch (ClassCastException e) {\n    // assigned non-dictionary; inspect type id in message\n}","preventionTips":["Only pass pointers produced by CFDictionary Create/Copy APIs to setValue","Pass null explicitly to clear the slot (check is skipped for null)","Validate with DICTIONARY_TYPE_ID before assignment","Keep CFTypeRef producers and typed consumers clearly separated in code"],"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"}