apache/shenyu · error · IllegalArgumentException

Could not find field [" + fieldName + "] on target [" + obj…

Error message

Could not find field [" + fieldName + "] on target [" + obj + "]

What it means

ReflectUtils.setFieldValue locates a field by name on the target object (including superclass fields) via getAccessibleField. If no such field exists it throws IllegalArgumentException 'Could not find field [name] on target [obj]'; the message is a template whose placeholders are filled with the actual field name and object.

Solutions

  1. Correct the field-name string to match the declared field exactly (case-sensitive).
  2. Verify the target object's actual type/class at runtime before setting (obj.getClass().getDeclaredField check).
  3. Add a setter or use a typed API instead of reflection where possible.
  4. If the field is genuinely optional, use getAccessibleField directly and null-check instead of setFieldValue.

Example fix

// before
ReflectUtils.setFieldValue(rule, "handelType", handleType); // typo
// after
ReflectUtils.setFieldValue(rule, "handleType", handleType);
Defensive patterns

Strategy: type-guard

Validate before calling

boolean hasField = false;
for (Class<?> c = obj.getClass(); c != null; c = c.getSuperclass()) {
    try { c.getDeclaredField(fieldName); hasField = true; break; } catch (NoSuchFieldException ignored) {}
}

Type guard

boolean hasField(Object obj, String name) { try { obj.getClass().getDeclaredField(name); return true; } catch (NoSuchFieldException e) { return false; } }

Try / catch

try { ReflectUtils.setFieldValue(obj, name, value); } catch (IllegalArgumentException e) { LOG.error("field {} missing on {}", name, obj.getClass(), e); }

Prevention

When it happens

Trigger: Calling ReflectUtils.setFieldValue(obj, "fieldName", value) where the object's class (and superclasses) declare no field with that exact name, including case or renamed-field mismatches.

Common situations: Refactoring renamed a field but config/serialization code still references the old name; typos in field-name strings; using the utility on DTOs of a different version than expected; obfuscated/proguarded classes dropping fields.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of apache/shenyu@567142e072 (2026-09-12). Data as JSON: /api/errors/7ed4fa4771443bac. Report an issue: GitHub.

Appendix: source

Thrown at shenyu-common/src/main/java/org/apache/shenyu/common/utils/ReflectUtils.java:155

     * @param method method
     * @param args   param
     * @return Method object
     */
    public static Object invokeMethod(final Object object, final String method, final Object... args) {
        return invokeMethod(object, method, e -> LOG.error("invoke method error"), args);
    }

    /**
     * Set object property values directly.
     *
     * @param obj       object
     * @param fieldName tje field name
     * @param value     the field value
     */
    public static void setFieldValue(final Object obj, final String fieldName, final Object value) {
        Field field = getAccessibleField(obj, fieldName);
        if (Objects.isNull(field)) {
            throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]");
        }
        try {
            field.set(obj, value);
        } catch (IllegalAccessException e) {
            LOG.error("Failed to assign to the element.", e);
            throw new ShenyuException(e.getMessage());
        }
    }

    /**
     * get the object's declared field.
     *
     * @param obj       object
     * @param fieldName tje field name
     * @return {@linkplain Field}
     */
    private static Field getAccessibleField(final Object obj, final String fieldName) {
        Validate.notNull(obj, "object can't be null");

View on GitHub (pinned to 567142e072)