{"record":{"id":"f1b0626ab77120bd","repo":"java-native-access/jna","slug":"unable-to-cast-to-cfboolean-type-id-gettypeid","errorCode":null,"errorMessage":"Unable to cast to CFBoolean. Type ID: {getTypeID()}","messagePattern":"Unable to cast to CFBoolean\\. Type ID: (.+?)","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/mac/CoreFoundation.java","lineNumber":287,"sourceCode":"         * @return a {@link CFIndex} representing the enum ordinal.\n         */\n        public CFIndex typeIndex() {\n            return new CFIndex(this.ordinal());\n        }\n    }\n\n    /**\n     * A reference to a {@code CFBoolean} object.\n     */\n    class CFBooleanRef extends CFTypeRef {\n        public CFBooleanRef() {\n            super();\n        }\n\n        public CFBooleanRef(Pointer p) {\n            super(p);\n            if (!isTypeID(BOOLEAN_TYPE_ID)) {\n                throw new ClassCastException(\"Unable to cast to CFBoolean. Type ID: \" + getTypeID());\n            }\n        }\n\n        /**\n         * Convert a reference to a Core Foundations Boolean into its {@code boolean}\n         *\n         * @return The corresponding {@code boolean}\n         */\n        public boolean booleanValue() {\n            return 0 != INSTANCE.CFBooleanGetValue(this);\n        }\n    }\n\n    /**\n     * A reference to an immutable {@code CFArray} object.\n     * <p>\n     * CFArray is “toll-free bridged” with its Cocoa Foundation counterpart,\n     * {@code NSArray}. Therefore, in a method where you see an {@code NSArray *}","sourceCodeStart":269,"sourceCodeEnd":305,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/mac/CoreFoundation.java#L269-L305","documentation":"CFBooleanRef's Pointer constructor verifies via CFGetTypeID that the wrapped native object is a CFBoolean. When the type id differs (including id 0 for a NULL pointer), it throws ClassCastException to prevent an invalid CFBoolean downcast.","triggerScenarios":"Constructing new CFBooleanRef(pointer) from a value fetched via CFDictionaryGetValue, CFArrayGetValueAtIndex, or notification/user-default lookups where the value is a CFString, CFNumber, CFArray, or NULL.","commonSituations":"Assuming a boolean flag in a CFDictionary is stored as CFBoolean when the producer stored 0/1 as CFNumber; reading nil dictionary entries; mismatched expectations after an API/OS version changed the stored representation.","solutions":["Test the type first: new CFTypeRef(p).isTypeID(CoreFoundation.BOOLEAN_TYPE_ID) before constructing CFBooleanRef.","Handle CFNumber 0/1 as a boolean fallback when dictionaries encode flags as numbers.","Null-check the pointer; a missing key yields NULL with type id 0.","Coerce common alternatives (CFNumber 0/1, CFString \"true\"/\"YES\") if the source data is heterogeneous."],"exampleFix":"// before\nCFBooleanRef b = new CFBooleanRef(value); // throws ClassCastException\n// after\nCFTypeRef ref = new CFTypeRef(value);\nCFBooleanRef b = ref.isTypeID(CoreFoundation.BOOLEAN_TYPE_ID)\n    ? new CFBooleanRef(value)\n    : null; // handle non-boolean case","handlingStrategy":"type-guard","validationCode":"if (value != null && new CFTypeRef(value).isTypeID(CoreFoundation.BOOLEAN_TYPE_ID)) {\n    CFBooleanRef b = new CFBooleanRef(value);\n}","typeGuard":"boolean isCFBoolean(Pointer p) {\n    return p != null && new CFTypeRef(p).isTypeID(CoreFoundation.BOOLEAN_TYPE_ID);\n}","tryCatchPattern":"try {\n    boolean flag = new CFBooleanRef(value).booleanValue();\n} catch (ClassCastException e) {\n    // fall back: treat CFNumber 0/1 as boolean or default false\n}","preventionTips":["Check the type id before casting any polymorphic CFTypeRef","Handle producers that encode booleans as CFNumber 0/1","Null-check values fetched by key before casting","Validate against CoreFoundation.BOOLEAN_TYPE_ID constant, not a literal"],"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"}