stanfordnlp/CoreNLP · error · UnsupportedOperationException

Unexpected failure to instantiate - is your key class fancy?

Error message

Unexpected failure to instantiate - is your key class fancy?

What it means

AnnotationLookup.getValueType throws UnsupportedOperationException when a CoreAnnotation key class cannot be instantiated via newInstance() to read its generic value type. The exception wraps the reflective failure (no public no-arg constructor, non-static inner class, or a throwing constructor).

Solutions

  1. Make the key class public, static, final, with a public no-arg constructor.
  2. Convert a non-static inner annotation class into a static nested class or top-level class.
  3. Ensure the no-arg constructor does not throw; move any initialization out of the constructor.
  4. Check the wrapped cause (getCause()) for the exact reflective error (IllegalAccessException, InstantiationException, InvocationTargetException).

Example fix

// before
class MyKey implements CoreAnnotation<String> {
  MyKey(String x) {} // no public no-arg constructor
  public Class<String> getType() { return String.class; }
}
// after
public static class MyKey implements CoreAnnotation<String> {
  public MyKey() {}
  @Override
  public Class<String> getType() { return String.class; }
}
Defensive patterns

Strategy: try-catch

Validate before calling

static boolean isSimpleInstantiable(Class<?> c) {
  int mods = c.getModifiers();
  return Modifier.isPublic(mods) && !Modifier.isAbstract(mods)
      && !c.isInterface() && !c.isMemberClass()
      && Arrays.stream(c.getDeclaredConstructors())
           .anyMatch(k -> k.getParameterCount() == 0 && Modifier.isPublic(k.getModifiers()));
}

Type guard

if (!isSimpleInstantiable(keyClass)) throw new IllegalArgumentException("Key must be public, static, with public no-arg constructor: " + keyClass);

Try / catch

try {
  Class<?> type = AnnotationLookup.getValueType(key);
} catch (UnsupportedOperationException e) {
  e.getCause().printStackTrace(); // real reflective failure
}

Prevention

When it happens

Trigger: Passing an abstract class, interface, a non-static inner class, or a class without a public no-arg constructor as the key to getValueType / CoreLabel operations that use it; also when the key's no-arg constructor throws.

Common situations: Defining a custom CoreAnnotation as an inner (non-static) class of an enclosing Stanford pipeline class; making a key constructor require arguments; anonymous key classes. Hence the message 'is your key class fancy?'

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/ling/AnnotationLookup.java:165

  }

  private static final Map<Class<? extends CoreAnnotation<?>>,Class<?>> valueCache = Generics.newHashMap();

  /**
   * Returns the runtime value type associated with the given key.  Caches
   * results in a private Map.
   *
   * @param key The annotation key (non-null)
   * @return The type of the value of that key (non-null)
   */
  @SuppressWarnings("unchecked")
  public static Class<?> getValueType(Class<? extends CoreAnnotation<?>> key) {
    Class type = valueCache.get(key);
    if (type == null) {
      try {
        type = key.newInstance().getType();
      } catch (Exception e) {
        throw new UnsupportedOperationException("Unexpected failure to instantiate - is your key class fancy?", e);
      }
      valueCache.put((Class)key, type);
    }
    return type;
  }

}

View on GitHub (pinned to 1b7edd19c4)