{"record":{"id":"d6e1e558a7a34d4a","repo":"alibaba/spring-ai-alibaba","slug":"constructor-did-not-produce-a-map-instance","errorCode":null,"errorMessage":"Constructor did not produce a Map instance: ","messagePattern":"Constructor did not produce a Map instance: ","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"warning","filePath":"spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/utils/SerializationUtils.java","lineNumber":71,"sourceCode":"\t * @param original the original Map object\n\t * @return the deep copied Map object, returns null if the original object is null\n\t */\n\t@SuppressWarnings(\"unchecked\")\n\tpublic static Map<String, Object> deepCopyMap(Map<String, Object> original) {\n\t\tif (original == null) {\n\t\t\treturn null;\n\t\t}\n\n\t\t// Preserve the original Map type if it's not a standard Map implementation\n\t\t// This handles cases like fastjson2's JSONObject (which extends LinkedHashMap)\n\t\tMap<String, Object> copy;\n\t\ttry {\n\t\t\t// Try to create an instance of the same class\n\t\t\tvar constructor = original.getClass().getDeclaredConstructor();\n\t\t\tconstructor.setAccessible(true);  // Handle non-public constructors\n\t\t\tObject instance = constructor.newInstance();\n\t\t\tif (!(instance instanceof Map)) {\n\t\t\t\tthrow new IllegalStateException(\"Constructor did not produce a Map instance: \" + original.getClass().getName());\n\t\t\t}\n\t\t\tcopy = (Map<String, Object>) instance;\n\t\t\tlog.debug(\"Successfully preserved Map type: {}\", original.getClass().getName());\n\t\t} catch (Exception e) {\n\t\t\t// If instantiation fails, fall back to HashMap\n\t\t\tlog.debug(\"Could not preserve Map type {}, falling back to HashMap: {}\", \n\t\t\t\toriginal.getClass().getName(), e.getMessage());\n\t\t\tcopy = new HashMap<>();\n\t\t}\n\t\t\n\t\tfor (Map.Entry<String, Object> entry : original.entrySet()) {\n\t\t\tcopy.put(entry.getKey(), deepCopyValue(entry.getValue()));\n\t\t}\n\t\treturn copy;\n\t}\n\n\t/**\n\t * Recursively deep copy values of any type","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/utils/SerializationUtils.java#L53-L89","documentation":"SerializationUtils.deepCopyMap attempts to deep-copy a Map by reflectively instantiating the original map's concrete class with a no-arg constructor. If the created instance is not a Map (or reflection fails), it falls back to HashMap after logging; the IllegalStateException is thrown when the no-arg constructor somehow produced a non-Map object — an invariant that should be impossible for a Map subclass.","triggerScenarios":"Calling deepCopy (via deepCopyValue) on a state Map whose concrete class has a no-arg constructor returning a non-Map instance, or whose reflection-based instantiation path misbehaves; mainly reachable when custom Map implementations are placed into graph state.","commonSituations":"Users put exotic Map subclasses (e.g. wrapper/delegating maps, immutable builders) into OverAllState; classpath anomalies or bytecode tricks cause the check to fire.","solutions":["Use standard Map implementations (HashMap, LinkedHashMap, TreeMap) in graph state.","Remove the custom Map subclass or give it a public no-arg constructor that returns a proper Map.","Wrap state values in plain collections before adding them to OverAllState."],"exampleFix":"// before\nstate.put(\"data\", new MyCustomMap<String,Object>(delegate));\n// after\nstate.put(\"data\", new LinkedHashMap<>(myCustomMap));","handlingStrategy":"validation","validationCode":"Object v = state.get(\"data\");\nif (!(v instanceof HashMap || v instanceof LinkedHashMap || v instanceof TreeMap)) {\n  // normalize before adding to graph state\n  v = new LinkedHashMap<>((Map<String,Object>) v);\n}","typeGuard":"boolean isCopySafeMap(Object v) {\n  return v instanceof Map && v.getClass().getDeclaredConstructors().length > 0\n      && v instanceof HashMap || v instanceof LinkedHashMap || v instanceof TreeMap;\n}","tryCatchPattern":"try {\n  Map<String,Object> copy = SerializationUtils.deepCopy(state.data());\n} catch (IllegalStateException e) {\n  log.warn(\"deepCopy failed: {}\", e.getMessage());\n  Map<String,Object> copy = new HashMap<>(state.data());\n}","preventionTips":["Keep plain Map implementations in graph state.","Avoid custom/delegating Map subclasses in OverAllState.","Unit-test state serialization for custom value types."],"tags":["reflection","deep-copy","map"],"backgroundTag":"internal-invariant-violation","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}