{"record":{"id":"c41d66063f571e37","repo":"dromara/Sa-Token","slug":"fieldname","errorCode":null,"errorMessage":"属性取值出错：${fieldName}","messagePattern":"属性取值出错：(.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"sa-token-demo/sa-token-demo-oauth2/sa-token-demo-oauth2-client/src/main/java/com/pj/utils/SoMap.java","lineNumber":234,"sourceCode":"\t}\n\t\n\t/** 从map中取值，塞到一个对象中 */\n\tpublic <T> T getModelByObject(T obj) {\n\t\t// 获取类型 \n\t\tClass<?> cs = obj.getClass();\n\t\t// 循环复制  \n\t\tfor (Field field : cs.getDeclaredFields()) {\n\t\t\ttry {\n\t\t\t\t// 获取对象 \n\t\t\t\tObject value = this.get(field.getName());\t\n\t\t\t\tif(value == null) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tfield.setAccessible(true);\t\n\t\t\t\tObject valueConvert = getValueByClass(value, field.getType());\n\t\t\t\tfield.set(obj, valueConvert);\n\t\t\t} catch (IllegalArgumentException | IllegalAccessException e) {\n\t\t\t\tthrow new RuntimeException(\"属性取值出错：\" + field.getName(), e);\n\t\t\t}\n\t\t}\n\t\treturn obj;\n\t}\n\n\t\n\n\t/**\n\t * 将指定值转化为指定类型并返回\n\t * @param obj\n\t * @param cs\n\t * @param <T>\n\t * @return\n\t */\n\t@SuppressWarnings(\"unchecked\")\n\tpublic static <T> T getValueByClass(Object obj, Class<T> cs) {\n\t\tString obj2 = String.valueOf(obj);\n\t\tObject obj3 = null;","sourceCodeStart":216,"sourceCodeEnd":252,"githubUrl":"https://github.com/dromara/Sa-Token/blob/ac2c7f6e94a78573cf0bcb932dd8b04e68fad189/sa-token-demo/sa-token-demo-oauth2/sa-token-demo-oauth2-client/src/main/java/com/pj/utils/SoMap.java#L216-L252","documentation":"Thrown inside SoMap's reflection copy loop (getModelValue-style) when Field.set fails with IllegalArgumentException or IllegalAccessException while copying a map entry onto a bean field. The message names the offending fieldName; the original reflection exception is chained as the cause.","triggerScenarios":"Calling soMap.getModel(key, BeanClass) / the field-copy loop when a map value cannot be converted to the bean field's declared type by getValueByClass (IllegalArgumentException), or the field belongs to a module/class where setAccessible is denied (IllegalAccessException, e.g. JDK 17+ strong encapsulation of java.* internals).","commonSituations":"A request parameter that is a String (e.g. \"123\") being pushed into a field whose converter path fails, a null primitive field, or running the demo on JDK 16+ where reflective access to certain classes is blocked by default.","solutions":["Read the chained cause (e.getCause()) to see whether it is an argument-type or access failure.","Align parameter types: ensure the map value for the named field is convertible (e.g. numeric string into int is fine, but a Map into int is not).","If IllegalAccessException under JDK 16+, add --add-opens opens for the affected package or avoid reflecting into JDK internals.","Rename mismatched keys or add @JsonIgnore-equivalent handling so unconvertible entries are skipped instead of fatal."],"exampleFix":"// before\nUser user = soMap.getModel(\"user\", User.class);\n\n// after\ntry {\n    User user = soMap.getModel(\"user\", User.class);\n} catch (RuntimeException e) {\n    Throwable cause = e.getCause() != null ? e.getCause() : e;\n    log.warn(\"bind failed: {}\", cause.getMessage());\n    throw new IllegalArgumentException(\"invalid user payload\");\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { User u = soMap.getModel(\"user\", User.class); } catch (RuntimeException e) { Throwable c = e.getCause() != null ? e.getCause() : e; if (c instanceof IllegalAccessException) { /* add-opens / access issue */ } else { /* type mismatch on named field */ } }","preventionTips":["Keep bean field types aligned with the parameter types the client sends (avoid primitives for optional fields).","On JDK 16+, add required --add-opens JVM args for any packages you reflect into.","Always inspect the chained cause — the field name in the message plus cause type pinpoints the bad entry."],"tags":["reflection","somap","bean-binding","jdk-strong-encapsulation"],"backgroundTag":null,"analyzedSha":"ac2c7f6e94a78573cf0bcb932dd8b04e68fad189","analyzedAt":"2026-08-14T14:36:10.271Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}