{"record":{"id":"41cf257b76b65235","repo":"stanfordnlp/CoreNLP","slug":"unexpected-failure-to-instantiate-is-your-key-cl","errorCode":null,"errorMessage":"Unexpected failure to instantiate - is your key class fancy?","messagePattern":"Unexpected failure to instantiate - is your key class fancy\\?","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/AnnotationLookup.java","lineNumber":165,"sourceCode":"  }\n\n  private static final Map<Class<? extends CoreAnnotation<?>>,Class<?>> valueCache = Generics.newHashMap();\n\n  /**\n   * Returns the runtime value type associated with the given key.  Caches\n   * results in a private Map.\n   *\n   * @param key The annotation key (non-null)\n   * @return The type of the value of that key (non-null)\n   */\n  @SuppressWarnings(\"unchecked\")\n  public static Class<?> getValueType(Class<? extends CoreAnnotation<?>> key) {\n    Class type = valueCache.get(key);\n    if (type == null) {\n      try {\n        type = key.newInstance().getType();\n      } catch (Exception e) {\n        throw new UnsupportedOperationException(\"Unexpected failure to instantiate - is your key class fancy?\", e);\n      }\n      valueCache.put((Class)key, type);\n    }\n    return type;\n  }\n\n}\n\n","sourceCodeStart":147,"sourceCodeEnd":174,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/AnnotationLookup.java#L147-L174","documentation":"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).","triggerScenarios":"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.","commonSituations":"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?'","solutions":["Make the key class public, static, final, with a public no-arg constructor.","Convert a non-static inner annotation class into a static nested class or top-level class.","Ensure the no-arg constructor does not throw; move any initialization out of the constructor.","Check the wrapped cause (getCause()) for the exact reflective error (IllegalAccessException, InstantiationException, InvocationTargetException)."],"exampleFix":"// before\nclass MyKey implements CoreAnnotation<String> {\n  MyKey(String x) {} // no public no-arg constructor\n  public Class<String> getType() { return String.class; }\n}\n// after\npublic static class MyKey implements CoreAnnotation<String> {\n  public MyKey() {}\n  @Override\n  public Class<String> getType() { return String.class; }\n}","handlingStrategy":"try-catch","validationCode":"static boolean isSimpleInstantiable(Class<?> c) {\n  int mods = c.getModifiers();\n  return Modifier.isPublic(mods) && !Modifier.isAbstract(mods)\n      && !c.isInterface() && !c.isMemberClass()\n      && Arrays.stream(c.getDeclaredConstructors())\n           .anyMatch(k -> k.getParameterCount() == 0 && Modifier.isPublic(k.getModifiers()));\n}","typeGuard":"if (!isSimpleInstantiable(keyClass)) throw new IllegalArgumentException(\"Key must be public, static, with public no-arg constructor: \" + keyClass);","tryCatchPattern":"try {\n  Class<?> type = AnnotationLookup.getValueType(key);\n} catch (UnsupportedOperationException e) {\n  e.getCause().printStackTrace(); // real reflective failure\n}","preventionTips":["Declare custom CoreAnnotation keys as public static classes with a public no-arg constructor.","Keep constructors of annotation key classes empty and side-effect free.","Call e.getCause() to see the real reflective error before guessing."],"tags":["reflection","instantiation","corelabel","annotations"],"backgroundTag":"class-not-found","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"}