{"record":{"id":"da0fd66319502b6c","repo":"java-native-access/jna","slug":"unable-to-cast-to-cfdata-type-id-gettypeid","errorCode":null,"errorMessage":"Unable to cast to CFData. Type ID: {getTypeID()}","messagePattern":"Unable to cast to CFData\\. Type ID: (.+?)","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/mac/CoreFoundation.java","lineNumber":352,"sourceCode":"         * @return The value at the {@code idx} index.\n         */\n        public Pointer getValueAtIndex(int idx) {\n            return INSTANCE.CFArrayGetValueAtIndex(this, new CFIndex(idx));\n        }\n    }\n\n    /**\n     * A reference to an immutable {@code CFData} object.\n     */\n    class CFDataRef extends CFTypeRef {\n        public CFDataRef() {\n            super();\n        }\n\n        public CFDataRef(Pointer p) {\n            super(p);\n            if (!isTypeID(DATA_TYPE_ID)) {\n                throw new ClassCastException(\"Unable to cast to CFData. Type ID: \" + getTypeID());\n            }\n        }\n\n        /**\n         * Convenience method for {@link #CFDataGetLength} on this object\n         *\n         * @return An index that specifies the number of bytes associated with this\n         *         object.\n         */\n        public int getLength() {\n            return INSTANCE.CFDataGetLength(this).intValue();\n        }\n\n        /**\n         * Convenience method for {@link #CFDataGetBytePtr} on this object\n         *\n         * @return A read-only pointer to the bytes associated with this object.\n         */","sourceCodeStart":334,"sourceCodeEnd":370,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/mac/CoreFoundation.java#L334-L370","documentation":"CFDataRef's Pointer constructor verifies the wrapped native object is a CFData by comparing CFGetTypeID against the CFData type id, throwing ClassCastException otherwise. A NULL pointer (type id 0) also fails this check.","triggerScenarios":"Constructing new CFDataRef(pointer) from CFDictionaryGetValue or similar generic CFType APIs when the value is a CFString, CFNumber, CFArray, or NULL.","commonSituations":"Extracting binary blobs (e.g. preferences, token data) from dictionaries where the key holds text rather than raw bytes; keys absent from the dictionary; mistyping which key holds the CFData payload.","solutions":["Check new CFTypeRef(p).isTypeID(CoreFoundation.DATA_TYPE_ID) before constructing CFDataRef.","Null-check the lookup result before casting.","Log the actual type id returned to identify what the key really contains.","Correct the key name or parsing assumptions if the data is stored under a different key/type."],"exampleFix":"// before\nCFDataRef data = new CFDataRef(value); // throws ClassCastException\n// after\nCFTypeRef ref = new CFTypeRef(value);\nif (ref.isTypeID(CoreFoundation.DATA_TYPE_ID)) {\n    CFDataRef data = new CFDataRef(value);\n} else {\n    // handle non-data value\n}","handlingStrategy":"type-guard","validationCode":"if (value != null && new CFTypeRef(value).isTypeID(CoreFoundation.DATA_TYPE_ID)) {\n    CFDataRef data = new CFDataRef(value);\n}","typeGuard":"boolean isCFData(Pointer p) {\n    return p != null && new CFTypeRef(p).isTypeID(CoreFoundation.DATA_TYPE_ID);\n}","tryCatchPattern":"try {\n    byte[] bytes = new CFDataRef(value).getBytes();\n} catch (ClassCastException e) {\n    // value is not binary CFData; check actual type id\n}","preventionTips":["Confirm the key actually stores CFData before casting","Null-check lookup results (NULL fails the type check)","Log the actual type id when a cast fails to identify the real content type","Use CFTypeRef first for all generic dictionary/array values"],"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"}