{"record":{"id":"8beb4ea625961e5e","repo":"stanfordnlp/CoreNLP","slug":"attempt-to-change-value-of-immutable-field-key-g","errorCode":null,"errorMessage":"Attempt to change value of immutable field ${key.getSimpleName()}","messagePattern":"Attempt to change value of immutable field (.+?)","errorType":"exception","errorClass":"HashableCoreMapException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/HashableCoreMap.java","lineNumber":76,"sourceCode":"      valueHashcode += super.get((Class)key).hashCode();\n    }\n    \n    this.immutableKeys = hashkey;\n    this.hashcode = keyHashcode * 31 + valueHashcode;\n  }\n  \n  /**\n   * Sets the value associated with the given key; if the the key is one\n   * of the hashable keys, throws an exception.\n   * \n   * @throws HashableCoreMapException Attempting to set the value for an\n   *   immutable, hashable key.\n   */\n  @Override\n  public <VALUE> VALUE set(Class<? extends Key<VALUE>> key, VALUE value) {\n    \n    if (immutableKeys.contains(key)) {\n      throw new HashableCoreMapException(\"Attempt to change value \" +\n      \t\t\"of immutable field \"+key.getSimpleName());\n    }\n    \n    return super.set(key, value);\n  }\n  \n  /**\n   * Provides a hash code based on the immutable keys and values provided\n   * to the constructor.\n   */\n  @Override\n  public int hashCode() {\n    return hashcode;\n  }\n  \n  /**\n   * If the provided object is a HashableCoreMap, equality is based only\n   * upon the values of the immutable hashkeys; otherwise, defaults to","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/HashableCoreMap.java#L58-L94","documentation":"HashableCoreMap refuses set() on keys registered as immutable (immutableKeys), because changing them would invalidate the precomputed hash code. It throws HashableCoreMapException with the key's simple name.","triggerScenarios":"Calling map.set(SomeKey.class, value) where SomeKey was added via makeImmutable(...) earlier — e.g. trying to overwrite a key after the map's hash was frozen.","commonSituations":"Annotation pipelines that freeze keys (like CoreMap keys used in hash-based caching) and later code attempting to update those same fields; accidentally reusing a frozen map object instead of copying it.","solutions":["Use a non-immutable key, or create a new HashableCoreMap with the desired values","Call makeImmutable only after all writes to that key are complete","Copy the needed values into a regular ArrayMap/CoreMap if you need mutability"],"exampleFix":"// before\nmap.makeImmutable(MyKey.class);\nmap.set(MyKey.class, newValue); // throws\n// after\nmap.set(MyKey.class, newValue); // write first\nmap.makeImmutable(MyKey.class); // freeze afterwards","handlingStrategy":"try-catch","validationCode":"// cannot inspect immutableKeys externally; guard by construction: never set a key after makeImmutable","typeGuard":null,"tryCatchPattern":"try { map.set(key, value); } catch (HashableCoreMapException e) { /* rebuild a new map with the desired value */ }","preventionTips":["Call makeImmutable only after finishing all writes to that key","Create a new HashableCoreMap instead of mutating frozen ones","Avoid sharing frozen HashableCoreMaps with code that writes annotations"],"tags":["immutability","map","hash"],"backgroundTag":"invalid-state-transition","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}