quarkusio/quarkus · error · java.lang.RuntimeException

Cannot set field value: ${className}#${name}

Error message

Cannot set field value: ${className}#${name}

What it means

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.

Source

Thrown at independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/Reflections.java:172

            Field field = clazz.getDeclaredField(name);
            if (!field.canAccess(instance)) {
                field.setAccessible(true);
            }
            return field.get(instance);
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
            throw new RuntimeException("Cannot read field value: " + clazz.getName() + "#" + name, e);
        }
    }

    public static void writeField(Class<?> clazz, String name, Object instance, Object value) {
        try {
            Field field = clazz.getDeclaredField(name);
            if (!field.canAccess(instance)) {
                field.setAccessible(true);
            }
            field.set(instance, value);
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
            throw new RuntimeException("Cannot set field value: " + clazz.getName() + "#" + name, e);
        }
    }

    public static Object invokeMethod(Class<?> clazz, String name, Class<?>[] paramTypes, Object instance, Object[] args) {
        try {
            Method method = clazz.getDeclaredMethod(name, paramTypes);
            if (!method.canAccess(instance)) {
                method.setAccessible(true);
            }
            return method.invoke(instance, args);
        } catch (InvocationTargetException e) {
            Throwable t = e.getTargetException();
            if (t instanceof RuntimeException) {
                throw (RuntimeException) t;
            }
            throw new RuntimeException("Cannot invoke method: " + clazz.getName() + "#" + name + " on " + instance, e);
        } catch (NoSuchMethodException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
            throw new RuntimeException("Cannot invoke method: " + clazz.getName() + "#" + name + " on " + instance, e);

View on GitHub (pinned to e1c734241f)

Solutions

  1. Check field name spelling and that the field is declared on the given class
  2. Ensure the value type is assignable to the field type (boxing/primitives)
  3. Do not write to final fields; set the value via constructor or non-final field
  4. Register the field for reflection for native-image builds

Example fix

// before
Reflections.writeField(Service.class, "repositry", service, new Repo());
// after
Reflections.writeField(Service.class, "repository", service, new Repo());
Defensive patterns

Strategy: validation

Validate before calling

Field f = clazz.getDeclaredField(name);
if (Modifier.isFinal(f.getModifiers())) throw new IllegalArgumentException("Field is final: " + name);
if (!f.getType().isInstance(value)) throw new IllegalArgumentException("Value not assignable to " + f.getType());

Type guard

static boolean isWritableField(Class<?> c, String name, Object value) {
    try { Field f = c.getDeclaredField(name); return !Modifier.isFinal(f.getModifiers()) && f.getType().isInstance(value); }
    catch (NoSuchFieldException e) { return false; }
}

Try / catch

try { Reflections.writeField(clazz, name, instance, value); }
catch (RuntimeException e) { log.warn("Field write failed: " + clazz.getName() + "#" + name, e); }

Prevention

When it happens

Trigger: 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.

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

Related errors


AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05). Data as JSON: /api/errors/16a6f2c8bdf72d38. Report an issue: GitHub.