{"record":{"id":"c6719941b169b498","repo":"java-native-access/jna","slug":"attempt-to-write-to-read-only-field-field-getname-within","errorCode":null,"errorMessage":"Attempt to write to read-only field '\" + field.getName() + \"' within \" + getClass()","messagePattern":"Attempt to write to read-only field '\" \\+ field\\.getName\\(\\) \\+ \"' within \" \\+ getClass\\(\\)","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/Structure.java","lineNumber":682,"sourceCode":"     */\n    void setFieldValue(Field field, Object value) {\n        setFieldValue(field, value, false);\n    }\n\n    private void setFieldValue(Field field, Object value, boolean overrideFinal) {\n\n        try {\n            field.set(this, value);\n        }\n        catch(IllegalAccessException e) {\n            int modifiers = field.getModifiers();\n            if (Modifier.isFinal(modifiers)) {\n                if (overrideFinal) {\n                    // WARNING: setAccessible(true) on J2ME does *not* allow\n                    // overwriting of a final field.\n                    throw new UnsupportedOperationException(\"This VM does not support Structures with final fields (field '\" + field.getName() + \"' within \" + getClass() + \")\", e);\n                }\n                throw new UnsupportedOperationException(\"Attempt to write to read-only field '\" + field.getName() + \"' within \" + getClass(), e);\n            }\n            throw new Error(\"Unexpectedly unable to write to field '\" + field.getName() + \"' within \" + getClass(), e);\n        }\n    }\n\n    /** Only keep the original structure if its native address is unchanged.\n     * Otherwise replace it with a new object.\n     * @param type Structure subclass\n     * @param s Original Structure object\n     * @param address the native <code>struct *</code>\n     * @return Updated <code>Structure.ByReference</code> object\n     */\n    static <T extends Structure> T updateStructureByReference(Class<T> type, T s, Pointer address) {\n        if (address == null) {\n            s = null;\n        }\n        else {\n            if (s == null || !address.equals(s.getPointer())) {","sourceCodeStart":664,"sourceCodeEnd":700,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/Structure.java#L664-L700","documentation":"Structure.setFieldValue catches IllegalAccessException while writing a field; if the field is final and override is not requested, JNA throws UnsupportedOperationException stating the field is read-only. It prevents silently writing to fields the developer declared immutable.","triggerScenarios":"Calling writeField(name, value) or setFieldValue on a final instance field of a Structure without the internal final-override path enabled; read() attempting to refresh a final field when the VM disallows the override.","commonSituations":"Declaring fields final expecting them to be populated once by a constructor, then trying to update them via writeField; migrating code from mutable to final fields and keeping JNA write calls.","solutions":["Remove the final modifier so the field can be written to native memory and back","Do not call writeField on fields intended to be immutable; compute them as derived values instead","Make the field static final if it is a true constant (static fields are not part of the layout)","Set a different design: keep a mutable private field and expose an unmodifiable getter"],"exampleFix":"// before\npublic final int flags;\n...\nstruct.writeField(\"flags\", 7);\n// after\npublic int flags;\n...\nstruct.writeField(\"flags\", 7);","handlingStrategy":"type-guard","validationCode":"if (java.lang.reflect.Modifier.isFinal(struct.getClass().getField(name).getModifiers())) {\n    throw new IllegalArgumentException(\"Cannot write final structure field: \" + name);\n}","typeGuard":"if (java.lang.reflect.Modifier.isFinal(field.getModifiers())) return false; // skip write","tryCatchPattern":"try { struct.writeField(name, value); } catch (UnsupportedOperationException e) { /* read-only field: ignore or fail */ }","preventionTips":["Keep all mapped Structure fields mutable","Don't call writeField on immutable-by-design fields","Expose constants via static final or getters instead of final instance fields","Document which fields are native-mapped vs derived"],"tags":["reflection","jna","final-field","read-only"],"backgroundTag":"unsupported-operation","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}