stanfordnlp/CoreNLP · error · UnsupportedOperationException

Child should only be set on a UniqPattern at creation time

Error message

Child should only be set on a UniqPattern at creation time

What it means

UniqPattern overrides setChild to be unsupported: its single child is fixed at construction time, so any later attempt to attach or rewire a child throws this UnsupportedOperationException. It protects the invariant that a UniqPattern's child (the node whose matches must be unique) never changes after creation.

Solutions

  1. Pass the child to the UniqPattern constructor instead of calling setChild afterwards.
  2. Special-case UniqPattern in any generic tree-rewriting code and skip (or rebuild) it rather than calling setChild.
  3. Rebuild the UniqPattern with the new child if the child must change — replace the node in the parent tree.
  4. If you control the traversal, check instanceof UniqPattern before invoking setChild on pattern nodes.

Example fix

// before
UniqPattern up = new UniqPattern(node, keys);
up.setChild(newChild); // UnsupportedOperationException
// after
UniqPattern up = new UniqPattern(newChild, keys); // child at construction time
Defensive patterns

Strategy: type-guard

Validate before calling

if (pattern instanceof UniqPattern) { /* rebuild with new child instead of setChild */ }

Type guard

boolean isUniq = (p instanceof UniqPattern);
if (isUniq) { UniqPattern up = (UniqPattern) p; /* treat as immutable */ }

Try / catch

try { node.setChild(newChild); } catch (UnsupportedOperationException e) { /* rebuild UniqPattern with the child in its constructor */ }

Prevention

When it happens

Trigger: Calling setChild(...) on a UniqPattern instance — typically via generic pattern-rewriting code, traversal utilities, or SemgrexParser paths that call the abstract setChild on all node patterns without special-casing UniqPattern.

Common situations: Custom Semgrex tooling that mutates pattern trees after compilation; serialization/deserialization or transformation frameworks that blindly call setChild on every node type; writing a parser extension that builds UniqPattern first and attaches its child later.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/253feb8e47e407a1. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/semgraph/semgrex/UniqPattern.java:87

  @Override
  public String localString() {
    return toString(true, false);
  }

  @Override
  public String toString() {
    return toString(true, true);
  }

  @Override
  public String toString(boolean hasPrecedence) {
    return toString(hasPrecedence, true);
  }

  @Override
  public void setChild(SemgrexPattern n) {
    throw new UnsupportedOperationException("Child should only be set on a UniqPattern at creation time");
  }

  @Override
  public List<SemgrexPattern> getChildren() {
    if (child == null) {
      return Collections.emptyList();
    } else {
      return Collections.singletonList(child);
    }
  }

  public String toString(boolean hasPrecedence, boolean addChild) {
    StringBuilder sb = new StringBuilder();
    if (addChild) {
      sb.append(child.toString(true));
    }
    sb.append(" :: uniq");
    for (String key : keys) {

View on GitHub (pinned to 1b7edd19c4)