{"record":{"id":"90825fd870b00082","repo":"java-native-access/jna","slug":"unable-to-cast-to-cfdictionary-type-id-gettypeid","errorCode":null,"errorMessage":"Unable to cast to CFDictionary. Type ID: {getTypeID()}","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":422,"sourceCode":"\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();\n        }\n\n        public CFDictionaryRef(Pointer p) {\n            super(p);\n            if (!isTypeID(DICTIONARY_TYPE_ID)) {\n                throw new ClassCastException(\"Unable to cast to CFDictionary. Type ID: \" + getTypeID());\n            }\n        }\n\n        /**\n         * Convenience method for {@link CoreFoundation#CFDictionaryGetValue} on this\n         * object.\n         *\n         * @param key\n         *            The key for which to find a match.\n         * @return The value associated with key, or {@code null} if no key-value pair\n         *         matching key exists.\n         */\n        public Pointer getValue(PointerType key) {\n            return INSTANCE.CFDictionaryGetValue(this, key);\n        }\n\n        /**\n         * Convenience method for {@link CoreFoundation#CFDictionaryGetCount(CFDictionaryRef)}","sourceCodeStart":404,"sourceCodeEnd":440,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/mac/CoreFoundation.java#L404-L440","documentation":"CFDictionaryRef's Pointer constructor validates that the wrapped Core Foundation object is a CFDictionary by comparing its CFTypeID against DICTIONARY_TYPE_ID, throwing ClassCastException if it is not. This prevents narrowing arbitrary CFTypeRefs into dictionary references.","triggerScenarios":"Constructing new CFDictionaryRef(pointer) from a generic CFTypeRef result — e.g. a CFArray element, CFNumber, CFString, or a NULL pointer — where CFGetTypeID does not report the dictionary type id.","commonSituations":"Fetching a value from a parent dictionary that turns out to be a string or array, not a nested dictionary; missing keys returning NULL; iterating collections whose element types vary.","solutions":["Guard with new CFTypeRef(p).isTypeID(CoreFoundation.DICTIONARY_TYPE_ID) before constructing.","Null-check the pointer before the cast.","For polymorphic values, inspect getTypeID() and dispatch per type (string/number/array/dictionary).","Fix the key/lookup path if a nested dictionary was expected but a scalar was returned."],"exampleFix":"// before\nCFDictionaryRef dict = new CFDictionaryRef(value); // throws\n// after\nCFTypeRef ref = new CFTypeRef(value);\nif (ref.isTypeID(CoreFoundation.DICTIONARY_TYPE_ID)) {\n    CFDictionaryRef dict = new CFDictionaryRef(value);\n} else {\n    // handle non-dictionary value\n}","handlingStrategy":"type-guard","validationCode":"if (value != null && new CFTypeRef(value).isTypeID(CoreFoundation.DICTIONARY_TYPE_ID)) {\n    CFDictionaryRef dict = new CFDictionaryRef(value);\n}","typeGuard":"boolean isCFDictionary(Pointer p) {\n    return p != null && new CFTypeRef(p).isTypeID(CoreFoundation.DICTIONARY_TYPE_ID);\n}","tryCatchPattern":"try {\n    CFDictionaryRef dict = new CFDictionaryRef(value);\n} catch (ClassCastException e) {\n    // nested value was a scalar/array, not a dictionary\n}","preventionTips":["Guard all nested-dictionary lookups with isTypeID before casting","Null-check values fetched by key","Handle polymorphic payloads by branching on getTypeID()","Assume nothing about dictionary value types across macOS versions"],"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"}