{"record":{"id":"e65c5f554476696f","repo":"java-native-access/jna","slug":"native-size-for-type-cls-getname-is-unknown","errorCode":null,"errorMessage":"Native size for type \"cls.getName()\" is unknown","messagePattern":"Native size for type \"cls\\.getName\\(\\)\" is unknown","errorType":"exception","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/Native.java","lineNumber":1515,"sourceCode":"        if (cls == char.class || cls == Character.class) return WCHAR_SIZE;\n        if (cls == int.class || cls == Integer.class) return 4;\n        if (cls == long.class || cls == Long.class) return 8;\n        if (cls == float.class || cls == Float.class) return 4;\n        if (cls == double.class || cls == Double.class) return 8;\n        if (Structure.class.isAssignableFrom(cls)) {\n            if (Structure.ByValue.class.isAssignableFrom(cls)) {\n                return Structure.size((Class<? extends Structure>) cls);\n            }\n            return POINTER_SIZE;\n        }\n        if (Pointer.class.isAssignableFrom(cls)\n            || (Platform.HAS_BUFFERS && Buffers.isBuffer(cls))\n            || Callback.class.isAssignableFrom(cls)\n            || String.class == cls\n            || WString.class == cls) {\n            return POINTER_SIZE;\n        }\n        throw new IllegalArgumentException(\"Native size for type \\\"\" + cls.getName()\n                                           + \"\\\" is unknown\");\n    }\n\n    /**\n     * @param cls The Java class\n     * @return {@code true} whether the given class is supported as a native argument type.\n     */\n    public static boolean isSupportedNativeType(Class<?> cls) {\n        if (Structure.class.isAssignableFrom(cls)) {\n            return true;\n        }\n        try {\n            return getNativeSize(cls) != 0;\n        }\n        catch(IllegalArgumentException e) {\n            return false;\n        }\n    }","sourceCodeStart":1497,"sourceCodeEnd":1533,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/Native.java#L1497-L1533","documentation":"The no-value Native.getNativeSize(Class) enumerates every category JNA can size (primitives, wrappers, Structure, arrays, Pointer, Buffer, Callback, String, WString, etc.). If the class matches none, JNA cannot know how many bytes it occupies natively and throws this IllegalArgumentException. It signals an unsupported mapping, mirroring error 364 but for the type-only size lookup.","triggerScenarios":"Calling Native.getNativeSize(SomeClass.class) with an unsupported class, or mapping structures/callbacks whose field/parameter types are not JNA-mappable, causing internal size queries on unknown types.","commonSituations":"Size-querying a custom class to plan memory allocations, using boxed generic types (List, Optional) as mapped fields, or typos where an unsupported wrapper type replaced a primitive in a Structure definition.","solutions":["Only call Native.getNativeSize for JNA-supported classes; for structs use Structure.size(class) instead.","Convert unsupported types to supported mappings (Structure, IntegerType, PointerType, arrays of primitives, String/WString).","If you need the pointer-sized opaque value, use Pointer.class which resolves to POINTER_SIZE.","Check Native.isSupportedNativeType(cls) (the guard right below this throw) before querying sizes."],"exampleFix":"// before\nint sz = Native.getNativeSize(MyPojo.class); // unknown native size\n\n// after\nif (Native.isSupportedNativeType(MyPojo.class)) {\n    int sz = Native.getNativeSize(MyPojo.class);\n} else {\n    int sz = Structure.size(MyStruct.class); // map it as a Structure\n}","handlingStrategy":"validation","validationCode":"if (!Native.isSupportedNativeType(cls)) {\n    throw new IllegalArgumentException(\"Cannot query native size for unsupported type: \" + cls);\n}\nint size = Native.getNativeSize(cls);","typeGuard":"static boolean hasKnownNativeSize(Class<?> cls) {\n    return Native.isSupportedNativeType(cls);\n}","tryCatchPattern":"try {\n    return Native.getNativeSize(cls);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"is unknown\")) {\n        return Structure.size((Class<? extends Structure>) cls); // if it is a struct\n    } else { throw e; }\n}","preventionTips":["Call Native.isSupportedNativeType before any getNativeSize(Class) query.","Use Structure.size(...) for struct classes instead of getNativeSize.","Keep a whitelist of supported classes in size-computation utilities.","Add tests that iterate all mapped field types and assert support."],"tags":["jna","type-mapping","native-size","unsupported-type"],"backgroundTag":"type-mismatch","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}