{"record":{"id":"56f7d54434803ecf","repo":"MyCATApache/Mycat-Server","slug":"could-not-set-field-fieldname-objectclass","errorCode":null,"errorMessage":"Could not set field ${fieldName}@${objectClass}","messagePattern":"Could not set field (.+?)@(.+?)","errorType":"exception","errorClass":"ObjectAccessException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/config/util/ReflectionProvider.java","lineNumber":110,"sourceCode":"            validateFieldAccess(field);\n            try {\n                Object value = field.get(object);\n                visitor.visit(field.getName(), field.getType(), field.getDeclaringClass(), value);\n            } catch (IllegalArgumentException e) {\n                throw new ObjectAccessException(\"Could not get field \" + field.getClass() + \".\" + field.getName(), e);\n            } catch (IllegalAccessException e) {\n                throw new ObjectAccessException(\"Could not get field \" + field.getClass() + \".\" + field.getName(), e);\n            }\n        }\n    }\n\n    public void writeField(Object object, String fieldName, Object value, Class<?> definedIn) {\n        Field field = fieldDictionary.field(object.getClass(), fieldName, definedIn);\n        validateFieldAccess(field);\n        try {\n            field.set(object, value);\n        } catch (IllegalArgumentException e) {\n            throw new ObjectAccessException(\"Could not set field \" + field.getName() + \"@\" + object.getClass(), e);\n        } catch (IllegalAccessException e) {\n            throw new ObjectAccessException(\"Could not set field \" + field.getName() + \"@\" + object.getClass(), e);\n        }\n    }\n\n    public void invokeMethod(Object object, String methodName, Object value, Class<?> definedIn) {\n        try {\n            Method method = object.getClass().getMethod(methodName, new Class[] { value.getClass() });\n            method.invoke(object, new Object[] { value });\n        } catch (Exception e) {\n            throw new ObjectAccessException(\"Could not invoke \" + object.getClass() + \".\" + methodName, e);\n        }\n    }\n\n    public Class<?> getFieldType(Object object, String fieldName, Class<?> definedIn) {\n        return fieldDictionary.field(object.getClass(), fieldName, definedIn).getType();\n    }\n","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/config/util/ReflectionProvider.java#L92-L128","documentation":"writeField sets a field value via reflection (field.set). When the JVM rejects the write with IllegalArgumentException (value/object type mismatch) or IllegalAccessException (field not accessible), it throws ObjectAccessException 'Could not set field <name>@<class>'. This guards the deserialization path from silently leaving fields unset.","triggerScenarios":"field.set(object, value) throws IllegalArgumentException because value's type is not assignable to the field type (or object is not an instance of the declaring class), or IllegalAccessException because the field is private/protected and not made accessible.","commonSituations":"Deserialized XML value type does not match the target field type (e.g. String into int); field became private in a newer class version; wrong object instance passed with a fieldName defined in a different class in the hierarchy.","solutions":["Verify the value's runtime type matches the field's declared type before calling writeField","Call field.setAccessible(true) or add JVM --add-opens for the package holding the class","Check that object.getClass() actually declares/inherits the field; pass the correct definedIn class","Read the cause exception (IllegalArgumentException vs IllegalAccessException) to pinpoint type vs access problem"],"exampleFix":"// before\nprovider.writeField(obj, \"port\", \"8080\", null); // String into int field\n// after\nprovider.writeField(obj, \"port\", 8080, null); // correct type","handlingStrategy":"validation","validationCode":"Field f = obj.getClass().getDeclaredField(fieldName);\nf.setAccessible(true);\nif (value != null && !f.getType().isInstance(value)) {\n    throw new IllegalArgumentException(fieldName + \" expects \" + f.getType() + \", got \" + value.getClass());\n}","typeGuard":"boolean writable = f.getType().isInstance(value) && declaringClass.isInstance(obj);","tryCatchPattern":"try {\n    provider.writeField(obj, name, value, definedIn);\n} catch (ObjectAccessException e) {\n    log.error(\"cannot set {} on {}: {}\", name, obj.getClass(), e.getCause());\n}","preventionTips":["Match value runtime type to field declared type","Make fields accessible via setAccessible before writes","Pass the correct definedIn class for inherited fields","Prefer setters over direct field writes"],"tags":["reflection","deserialization","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}