{"record":{"id":"a4e1fda655a40e60","repo":"java-native-access/jna","slug":"unable-to-cast-to-cfnumber-type-id-gettypeid","errorCode":null,"errorMessage":"Unable to cast to CFNumber. Type ID: {getTypeID()}","messagePattern":"Unable to cast to CFNumber\\. Type ID: (.+?)","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/mac/CoreFoundation.java","lineNumber":148,"sourceCode":"     * A reference type used in many Core Foundation parameters and function\n     * results. It refers to a {@code CFAllocator} object, which allocates,\n     * reallocates, and deallocates memory for Core Foundation objects.\n     */\n    class CFAllocatorRef extends CFTypeRef {\n    }\n\n    /**\n     * A reference to a {@code CFNumber} object.\n     */\n    class CFNumberRef extends CFTypeRef {\n        public CFNumberRef() {\n            super();\n        }\n\n        public CFNumberRef(Pointer p) {\n            super(p);\n            if (!isTypeID(NUMBER_TYPE_ID)) {\n                throw new ClassCastException(\"Unable to cast to CFNumber. Type ID: \" + getTypeID());\n            }\n        }\n\n        /**\n         * Convert this {@code CFNumber} to a {@code long}.\n         * <p>\n         * This method assumes a 64-bit integer is stored and does not do type checking.\n         * Users should use {@link #CFNumberGetType} to determine the appropriate type\n         * conversion. If this object's type differs from the return type, and the\n         * conversion is lossy or the return value is out of range, then this method\n         * returns an approximate value.\n         *\n         * @return The corresponding {@code long}\n         */\n        public long longValue() {\n            LongByReference lbr = new LongByReference();\n            INSTANCE.CFNumberGetValue(this, CFNumberType.kCFNumberLongLongType.typeIndex(), lbr);\n            return lbr.getValue();","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/mac/CoreFoundation.java#L130-L166","documentation":"CFNumberRef wraps a Core Foundation pointer and validates on construction that the native object's CFTypeID matches the CFNumber type. If the Pointer points at some other CFType (a string, array, dictionary, null type id 0), the constructor throws ClassCastException immediately rather than allowing an invalid downcast to be used later.","triggerScenarios":"Constructing new CFNumberRef(pointer) with a pointer obtained from CFDictionaryGetValue, CFArrayGetValueAtIndex, or any polymorphic CFTypeRef API where the underlying object is not actually a CFNumber (or the pointer is NULL, whose type id is 0).","commonSituations":"Reading heterogeneous CFDictionary values and blindly casting every value to CFNumberRef when some values are strings or booleans; stale/released CF objects whose memory was reused; passing a NULL Pointer (e.g. missing dictionary key).","solutions":["Before constructing, call INSTANCE.CFGetTypeID(pointer) (or wrap in CFTypeRef and use getTypeID()) and compare with CoreFoundation.NUMBER_TYPE_ID.","Wrap polymorphic lookups in a CFTypeRef first and branch on getTypeID() before narrowing.","Check for a NULL pointer (missing key) before casting.","If you expected a number but got a different type, fix the key/type assumption in the code reading the native structure."],"exampleFix":"// before\nCFNumberRef num = new CFNumberRef(dictValue); // throws if value is a CFString\n// after\nCFTypeRef ref = new CFTypeRef(dictValue);\nif (ref.isTypeID(CoreFoundation.NUMBER_TYPE_ID)) {\n    CFNumberRef num = new CFNumberRef(dictValue);\n} else {\n    // handle non-number value\n}","handlingStrategy":"type-guard","validationCode":"if (value != null && new CFTypeRef(value).isTypeID(CoreFoundation.NUMBER_TYPE_ID)) {\n    CFNumberRef num = new CFNumberRef(value);\n}","typeGuard":"boolean isCFNumber(Pointer p) {\n    return p != null && new CFTypeRef(p).isTypeID(CoreFoundation.NUMBER_TYPE_ID);\n}","tryCatchPattern":"try {\n    CFNumberRef num = new CFNumberRef(value);\n    long v = num.longValue();\n} catch (ClassCastException e) {\n    // value is not a CFNumber; inspect new CFTypeRef(value).getTypeID()\n}","preventionTips":["Always narrow via CFTypeRef.isTypeID before constructing typed refs","Null-check dictionary lookups — missing keys return NULL (type id 0)","Remember CF collections hold CFTypeRefs; check each element's type","CFTypeIDs are process-specific; use the interface constants, never hardcoded ids"],"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"}