{"record":{"id":"1061a936ee765585","repo":"stanfordnlp/CoreNLP","slug":"can-t-set-tree-labels","errorCode":null,"errorMessage":"Can't set Tree labels","messagePattern":"Can't set Tree labels","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/Tree.java","lineNumber":1763,"sourceCode":"   *\n   * @return the {@code Collection} (actually, Set) of all values\n   *         in the tree.\n   */\n  @Override\n  public Collection<Label> labels() {\n    Set<Label> n = Generics.newHashSet();\n    n.add(label());\n    Tree[] kids = children();\n    for (Tree kid : kids) {\n      n.addAll(kid.labels());\n    }\n    return n;\n  }\n\n\n  @Override\n  public void setLabels(Collection<Label> c) {\n    throw new UnsupportedOperationException(\"Can't set Tree labels\");\n  }\n\n\n  /**\n   * Return a flattened version of a tree.  In many circumstances, this\n   * will just return the tree, but if the tree is something like a\n   * binarized version of a dependency grammar tree, then it will be\n   * flattened back to a dependency grammar tree representation.  Formally,\n   * a node will be removed from the tree when: it is not a terminal or\n   * preterminal, and its {@code label()} is {@code equal()} to\n   * the {@code label()} of its parent, and all its children will\n   * then be promoted to become children of the parent (in the same\n   * position in the sequence of daughters.\n   *\n   * @return A flattened version of this tree.\n   */\n  public Tree flatten() {\n    return flatten(treeFactory());","sourceCodeStart":1745,"sourceCodeEnd":1781,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/Tree.java#L1745-L1781","documentation":"Tree.setLabels(Collection<Label>) is deliberately unimplemented: a Tree's identity is defined by its labeled nodes, so bulk-replacing the label collection is not a meaningful operation. Any call immediately throws UnsupportedOperationException.","triggerScenarios":"Calling setLabels(...) on any Tree instance, often because code written against a generic Labeled interface treats the Tree like a simple labeled edge — e.g. copying labels between structures in a loop.","commonSituations":"Generic label-manipulation utilities operating on Label/Labeled abstractions; ported code from other tree libraries where setLabels exists; reflection- or framework-driven property setters invoking it.","solutions":["Set labels per-node instead: call tree.label().setValue(...) on individual nodes","Traverse the tree and update each node's label via a TreeTransformer or a manual recursion","Remove the setLabels call; restructure code to operate on node labels rather than the whole tree"],"exampleFix":"// before\ntree.setLabels(newLabels); // throws\n// after\nfor (Tree node : tree) {\n  node.label().setValue(nextValue());\n}","handlingStrategy":"validation","validationCode":"if (node instanceof Tree) { /* setLabels unsupported; mutate node.label() per node instead */ }","typeGuard":"boolean supportsSetLabels(Object o) { return !(o instanceof Tree) && o instanceof Labeled; }","tryCatchPattern":"try { tree.setLabels(labels); } catch (UnsupportedOperationException e) { /* rewrite via per-node label updates */ }","preventionTips":["Never call setLabels on Tree; treat it as read-only at the label-collection level","Mutate individual node labels via label().setValue(...)","Use TreeTransformer for bulk label changes"],"tags":["nlp","trees","unsupported-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}