{"record":{"id":"16a6f2c8bdf72d38","repo":"quarkusio/quarkus","slug":"cannot-set-field-value-classname-name","errorCode":null,"errorMessage":"Cannot set field value: ${className}#${name}","messagePattern":"Cannot set field value: (.+?)#(.+?)","errorType":"exception","errorClass":"java.lang.RuntimeException","httpStatus":null,"severity":"error","filePath":"independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/Reflections.java","lineNumber":172,"sourceCode":"            Field field = clazz.getDeclaredField(name);\n            if (!field.canAccess(instance)) {\n                field.setAccessible(true);\n            }\n            return field.get(instance);\n        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {\n            throw new RuntimeException(\"Cannot read field value: \" + clazz.getName() + \"#\" + name, e);\n        }\n    }\n\n    public static void writeField(Class<?> clazz, String name, Object instance, Object value) {\n        try {\n            Field field = clazz.getDeclaredField(name);\n            if (!field.canAccess(instance)) {\n                field.setAccessible(true);\n            }\n            field.set(instance, value);\n        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {\n            throw new RuntimeException(\"Cannot set field value: \" + clazz.getName() + \"#\" + name, e);\n        }\n    }\n\n    public static Object invokeMethod(Class<?> clazz, String name, Class<?>[] paramTypes, Object instance, Object[] args) {\n        try {\n            Method method = clazz.getDeclaredMethod(name, paramTypes);\n            if (!method.canAccess(instance)) {\n                method.setAccessible(true);\n            }\n            return method.invoke(instance, args);\n        } catch (InvocationTargetException e) {\n            Throwable t = e.getTargetException();\n            if (t instanceof RuntimeException) {\n                throw (RuntimeException) t;\n            }\n            throw new RuntimeException(\"Cannot invoke method: \" + clazz.getName() + \"#\" + name + \" on \" + instance, e);\n        } catch (NoSuchMethodException | SecurityException | IllegalArgumentException | IllegalAccessException e) {\n            throw new RuntimeException(\"Cannot invoke method: \" + clazz.getName() + \"#\" + name + \" on \" + instance, e);","sourceCodeStart":154,"sourceCodeEnd":190,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/Reflections.java#L154-L190","documentation":"Reflections.writeField() sets a declared field's value reflectively, widening access with setAccessible(true) when needed. NoSuchFieldException, SecurityException, IllegalArgumentException, or IllegalAccessException are wrapped in a RuntimeException naming the class and field. Common causes are a nonexistent field, an illegal value type, or writing to a final field.","triggerScenarios":"Calling Reflections.writeField(clazz, name, instance, value) where the field doesn't exist, value's type is not assignable to the field type, the field is final (IllegalAccessException on set), or access is blocked by module/secure settings.","commonSituations":"Typos in field names in test setup code; attempting to mutate final fields (e.g. injected or config fields); wrong instance type passed; native-image builds lacking reflection metadata for the field.","solutions":["Check field name spelling and that the field is declared on the given class","Ensure the value type is assignable to the field type (boxing/primitives)","Do not write to final fields; set the value via constructor or non-final field","Register the field for reflection for native-image builds"],"exampleFix":"// before\nReflections.writeField(Service.class, \"repositry\", service, new Repo());\n// after\nReflections.writeField(Service.class, \"repository\", service, new Repo());","handlingStrategy":"validation","validationCode":"Field f = clazz.getDeclaredField(name);\nif (Modifier.isFinal(f.getModifiers())) throw new IllegalArgumentException(\"Field is final: \" + name);\nif (!f.getType().isInstance(value)) throw new IllegalArgumentException(\"Value not assignable to \" + f.getType());","typeGuard":"static boolean isWritableField(Class<?> c, String name, Object value) {\n    try { Field f = c.getDeclaredField(name); return !Modifier.isFinal(f.getModifiers()) && f.getType().isInstance(value); }\n    catch (NoSuchFieldException e) { return false; }\n}","tryCatchPattern":"try { Reflections.writeField(clazz, name, instance, value); }\ncatch (RuntimeException e) { log.warn(\"Field write failed: \" + clazz.getName() + \"#\" + name, e); }","preventionTips":["Avoid writing to final fields","Check field type vs value type (autoboxing not applied by reflection)","Register reflection metadata for native images"],"tags":["cdi","reflection","field-access","quarkus-arc"],"backgroundTag":"reflective-access-denied","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}