{"record":{"id":"7ed4fa4771443bac","repo":"apache/shenyu","slug":"could-not-find-field-fieldname-on-target-obj","errorCode":null,"errorMessage":"Could not find field [\" + fieldName + \"] on target [\" + obj + \"]","messagePattern":"Could not find field \\[\" \\+ fieldName \\+ \"\\] on target \\[\" \\+ obj \\+ \"\\]","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"shenyu-common/src/main/java/org/apache/shenyu/common/utils/ReflectUtils.java","lineNumber":155,"sourceCode":"     * @param method method\n     * @param args   param\n     * @return Method object\n     */\n    public static Object invokeMethod(final Object object, final String method, final Object... args) {\n        return invokeMethod(object, method, e -> LOG.error(\"invoke method error\"), args);\n    }\n\n    /**\n     * Set object property values directly.\n     *\n     * @param obj       object\n     * @param fieldName tje field name\n     * @param value     the field value\n     */\n    public static void setFieldValue(final Object obj, final String fieldName, final Object value) {\n        Field field = getAccessibleField(obj, fieldName);\n        if (Objects.isNull(field)) {\n            throw new IllegalArgumentException(\"Could not find field [\" + fieldName + \"] on target [\" + obj + \"]\");\n        }\n        try {\n            field.set(obj, value);\n        } catch (IllegalAccessException e) {\n            LOG.error(\"Failed to assign to the element.\", e);\n            throw new ShenyuException(e.getMessage());\n        }\n    }\n\n    /**\n     * get the object's declared field.\n     *\n     * @param obj       object\n     * @param fieldName tje field name\n     * @return {@linkplain Field}\n     */\n    private static Field getAccessibleField(final Object obj, final String fieldName) {\n        Validate.notNull(obj, \"object can't be null\");","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/apache/shenyu/blob/567142e07261b3e615ae8850b30f4421f455cc5d/shenyu-common/src/main/java/org/apache/shenyu/common/utils/ReflectUtils.java#L137-L173","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Correct the field-name string to match the declared field exactly (case-sensitive).","Verify the target object's actual type/class at runtime before setting (obj.getClass().getDeclaredField check).","Add a setter or use a typed API instead of reflection where possible.","If the field is genuinely optional, use getAccessibleField directly and null-check instead of setFieldValue."],"exampleFix":"// before\nReflectUtils.setFieldValue(rule, \"handelType\", handleType); // typo\n// after\nReflectUtils.setFieldValue(rule, \"handleType\", handleType);","handlingStrategy":"type-guard","validationCode":"boolean hasField = false;\nfor (Class<?> c = obj.getClass(); c != null; c = c.getSuperclass()) {\n    try { c.getDeclaredField(fieldName); hasField = true; break; } catch (NoSuchFieldException ignored) {}\n}","typeGuard":"boolean hasField(Object obj, String name) { try { obj.getClass().getDeclaredField(name); return true; } catch (NoSuchFieldException e) { return false; } }","tryCatchPattern":"try { ReflectUtils.setFieldValue(obj, name, value); } catch (IllegalArgumentException e) { LOG.error(\"field {} missing on {}\", name, obj.getClass(), e); }","preventionTips":["Grep field-name strings after refactors; prefer constants for field names","Prefer setters/direct typed access over reflection","Unit-test reflective mappings against the real classes","Guard against ProGuard obfuscation keeping required fields"],"tags":["reflection","field-access","type-mismatch"],"backgroundTag":"resource-not-found","analyzedSha":"567142e07261b3e615ae8850b30f4421f455cc5d","analyzedAt":"2026-09-12T10:08:21.293Z","contentChangedAt":"2026-09-12T10:08:21.293Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}