java-native-access/jna · error · ClassCastException

Unable to cast to CFData. Type ID

Error message

Unable to cast to CFData. Type ID: {getTypeID()}

What it means

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.

Solutions

  1. Check new CFTypeRef(p).isTypeID(CoreFoundation.DATA_TYPE_ID) before constructing CFDataRef.
  2. Null-check the lookup result before casting.
  3. Log the actual type id returned to identify what the key really contains.
  4. Correct the key name or parsing assumptions if the data is stored under a different key/type.

Example fix

// before
CFDataRef data = new CFDataRef(value); // throws ClassCastException
// after
CFTypeRef ref = new CFTypeRef(value);
if (ref.isTypeID(CoreFoundation.DATA_TYPE_ID)) {
    CFDataRef data = new CFDataRef(value);
} else {
    // handle non-data value
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (value != null && new CFTypeRef(value).isTypeID(CoreFoundation.DATA_TYPE_ID)) {
    CFDataRef data = new CFDataRef(value);
}

Type guard

boolean isCFData(Pointer p) {
    return p != null && new CFTypeRef(p).isTypeID(CoreFoundation.DATA_TYPE_ID);
}

Try / catch

try {
    byte[] bytes = new CFDataRef(value).getBytes();
} catch (ClassCastException e) {
    // value is not binary CFData; check actual type id
}

Prevention

When it happens

Trigger: Constructing new CFDataRef(pointer) from CFDictionaryGetValue or similar generic CFType APIs when the value is a CFString, CFNumber, CFArray, or NULL.

Common situations: 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.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12). Data as JSON: /api/errors/da0fd66319502b6c. Report an issue: GitHub.

Appendix: source

Thrown at contrib/platform/src/com/sun/jna/platform/mac/CoreFoundation.java:352

         * @return The value at the {@code idx} index.
         */
        public Pointer getValueAtIndex(int idx) {
            return INSTANCE.CFArrayGetValueAtIndex(this, new CFIndex(idx));
        }
    }

    /**
     * A reference to an immutable {@code CFData} object.
     */
    class CFDataRef extends CFTypeRef {
        public CFDataRef() {
            super();
        }

        public CFDataRef(Pointer p) {
            super(p);
            if (!isTypeID(DATA_TYPE_ID)) {
                throw new ClassCastException("Unable to cast to CFData. Type ID: " + getTypeID());
            }
        }

        /**
         * Convenience method for {@link #CFDataGetLength} on this object
         *
         * @return An index that specifies the number of bytes associated with this
         *         object.
         */
        public int getLength() {
            return INSTANCE.CFDataGetLength(this).intValue();
        }

        /**
         * Convenience method for {@link #CFDataGetBytePtr} on this object
         *
         * @return A read-only pointer to the bytes associated with this object.
         */

View on GitHub (pinned to d036ad9781)