{"record":{"id":"6ffa91a6c342740f","repo":"spring-projects/spring-security","slug":"could-not-locate-field-x-on-class-y","errorCode":null,"errorMessage":"Could not locate field 'X' on class Y","messagePattern":"Could not locate field 'X' on class Y","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/springframework/security/util/FieldUtils.java","lineNumber":55,"sourceCode":"\t/**\n\t * Attempts to locate the specified field on the class.\n\t * @param clazz the class definition containing the field\n\t * @param fieldName the name of the field to locate\n\t * @return the Field (never null)\n\t * @throws IllegalStateException if field could not be found\n\t */\n\tpublic static Field getField(Class<?> clazz, String fieldName) throws IllegalStateException {\n\t\tAssert.notNull(clazz, \"Class required\");\n\t\tAssert.hasText(fieldName, \"Field name required\");\n\t\ttry {\n\t\t\treturn clazz.getDeclaredField(fieldName);\n\t\t}\n\t\tcatch (NoSuchFieldException ex) {\n\t\t\t// Try superclass\n\t\t\tif (clazz.getSuperclass() != null) {\n\t\t\t\treturn getField(clazz.getSuperclass(), fieldName);\n\t\t\t}\n\t\t\tthrow new IllegalStateException(\"Could not locate field '\" + fieldName + \"' on class \" + clazz);\n\t\t}\n\t}\n\n\t/**\n\t * Returns the value of a (nested) field on a bean. Intended for testing.\n\t * @param bean the object\n\t * @param fieldName the field name, with \".\" separating nested properties\n\t * @return the value of the nested field\n\t */\n\tpublic static Object getFieldValue(Object bean, String fieldName) throws IllegalAccessException {\n\t\tAssert.notNull(bean, \"Bean cannot be null\");\n\t\tAssert.hasText(fieldName, \"Field name required\");\n\t\tString[] nestedFields = StringUtils.tokenizeToStringArray(fieldName, \".\");\n\t\tClass<?> componentClass = bean.getClass();\n\t\tObject value = bean;\n\t\tfor (String nestedField : nestedFields) {\n\t\t\tField field = getField(componentClass, nestedField);\n\t\t\tfield.setAccessible(true);","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/spring-projects/spring-security/blob/96852e8860138a482cb13d1479573f24ff6443c6/core/src/main/java/org/springframework/security/util/FieldUtils.java#L37-L73","documentation":"FieldUtils.getField walks the class hierarchy (superclasses) looking for a declared field by name; if neither the class nor any superclass declares it, it throws IllegalStateException('Could not locate field X on class Y'). This utility is intended for testing/reflection use, so the exception signals the assumed field genuinely does not exist on the type (or its name was mistyped).","triggerScenarios":"Calling FieldUtils.getField(bean, \"fieldName\") where the target class (and all superclasses) lack that field; typos or renamed/refactored fields not updated at the reflection call site; accessing a nested field via dotted path where an intermediate segment's type does not declare the next field; library upgrade changed an internal field name.","commonSituations":"Test helpers asserting on private/internal state after refactoring; accessing framework internals (e.g., inside Spring Security filters) that changed between versions; reflection on fields that are inherited through interfaces rather than classes (which the superclass walk does not cover).","solutions":["Correct the field name and verify the declaring class with clazz.getDeclaredFields() before the call","Call getField against the actual declaring class rather than a subclass/interface","Use standard reflection with setAccessible(true) or the Spring ReflectionUtils if you need interface-declared or more flexible lookup","Replace reflection with a getter/public API where available to remove fragility"],"exampleFix":"// before\nObject v = FieldUtils.getField(bean, \"usrName\"); // typo -> IllegalStateException\n// after\nObject v = FieldUtils.getField(bean, \"userName\");","handlingStrategy":"try-catch","validationCode":"boolean declared = false;\nfor (Class<?> c = bean.getClass(); c != null; c = c.getSuperclass()) {\n    try { c.getDeclaredField(\"userName\"); declared = true; break; }\n    catch (NoSuchFieldException ignored) { }\n}\nif (!declared) throw new IllegalStateException(\"Field 'userName' not found\");","typeGuard":"static boolean hasField(Class<?> clazz, String name) {\n    for (Class<?> c = clazz; c != null; c = c.getSuperclass()) {\n        try { c.getDeclaredField(name); return true; }\n        catch (NoSuchFieldException ignored) { }\n    }\n    return false;\n}","tryCatchPattern":"try {\n    Object value = FieldUtils.getField(bean, \"userName\");\n} catch (IllegalStateException e) {\n    if (e.getMessage().startsWith(\"Could not locate field\")) {\n        // fall back to getter or fail the test with a clear message\n    } else { throw e; }\n}","preventionTips":["Prefer getters/public APIs over reflective field access","Re-verify reflected field names after refactors (search usages of FieldUtils)","Remember the walk covers classes (superclasses), not interfaces","Add a smoke test that resolves every reflected field name used in test helpers"],"tags":["reflection","field-not-found","testing","illegal-state"],"backgroundTag":"resource-not-found","analyzedSha":"96852e8860138a482cb13d1479573f24ff6443c6","analyzedAt":"2026-09-10T23:25:23.477Z","contentChangedAt":"2026-09-10T23:25:23.477Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}