{"record":{"id":"7e19c022db143c8a","repo":"alibaba/COLA","slug":"value-for-key-key-is-not-of-type-type","errorCode":null,"errorMessage":"Value for key=[${key}] is not of type: [${type}], it is [(${value.getClass()})${value}]","messagePattern":"Value for key=\\[(.+?)\\] is not of type: \\[(.+?)\\], it is \\[\\((.+?)\\)(.+?)\\]","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"cola-components/cola-component-job/src/main/java/com/alibaba/cola/job/ExecutionContext.java","lineNumber":145,"sourceCode":"\n    @Nullable\n    public Object get(String key) {\n        return this.extensions.get(key);\n    }\n\n    @Nullable\n    public <V> V get(String key, Class<V> type) {\n        Object value = this.extensions.get(key);\n        return value == null ? null : this.get(key, type, null);\n    }\n\n    @Nullable\n    public <V> V get(String key, Class<V> type, @Nullable V defaultValue) {\n        Object value = this.extensions.get(key);\n        if (value == null) {\n            return defaultValue;\n        } else if (!type.isInstance(value)) {\n            throw new ClassCastException(\n                    \"Value for key=[\" + key + \"] is not of type: [\" + type + \"], it is [(\" + value.getClass() + \")\" + value\n                            + \"]\");\n        } else {\n            return type.cast(value);\n        }\n    }\n\n    public boolean containsKey(String key) {\n        return this.extensions.containsKey(key);\n    }\n\n    @Nullable\n    public Object remove(String key) {\n        return this.extensions.remove(key);\n    }\n\n    public boolean containsValue(Object value) {\n        return this.extensions.containsValue(value);","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/alibaba/COLA/blob/352e1a867538d9cd40e1b11e4028fa652fc97557/cola-components/cola-component-job/src/main/java/com/alibaba/cola/job/ExecutionContext.java#L127-L163","documentation":"ExecutionContext.get(key, type, defaultValue) throws ClassCastException when the stored value for the key exists but is not an instance of the requested type V. It is an eager, message-rich substitute for a lazy ClassCastException from type.cast.","triggerScenarios":"Calling executionContext.get(\"someKey\", Foo.class, def) while a value of an incompatible class (e.g. Integer vs String, or two different versions of the same class) was stored under that key via put/extension map.","commonSituations":"Writing a String and reading as Integer; job step A stores one type and step B reads another; classloader duplicates (same class name, different Class objects) in app-server deployments.","solutions":["Align the Class<V> argument with the type actually put into the context","Fix the writer side to store the expected type under that key","Use a distinct key per type to avoid collisions between steps"],"exampleFix":"// before\nString name = ctx.get(\"count\", String.class, \"0\"); // count stored as Integer\n// after\nInteger count = ctx.get(\"count\", Integer.class, 0);","handlingStrategy":"type-guard","validationCode":"Object raw = executionContext.get(key);\nif (raw != null && !type.isInstance(raw)) {\n    throw new IllegalStateException(\"key '\" + key + \"' holds \" + raw.getClass() + \", expected \" + type);\n}","typeGuard":"<V> V safeGet(ExecutionContext ctx, String key, Class<V> type, V def) {\n    Object v = ctx.get(key);\n    return type.isInstance(v) ? type.cast(v) : def;\n}","tryCatchPattern":"try {\n    value = executionContext.get(key, Foo.class, defaultFoo);\n} catch (ClassCastException e) {\n    log.warn(\"Type mismatch for key {} in execution context; using default\", key, e);\n    value = defaultFoo;\n}","preventionTips":["Use one key per type; encode type in the key name (e.g. 'retryCount:Integer')","Centralize context read/write in typed accessor methods","Watch for duplicate classes from different classloaders in app servers"],"tags":["java","type-mismatch","context"],"backgroundTag":"type-mismatch","analyzedSha":"352e1a867538d9cd40e1b11e4028fa652fc97557","analyzedAt":"2026-09-08T00:46:23.972Z","contentChangedAt":"2026-09-08T00:46:23.972Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}