stanfordnlp/CoreNLP · error · ClassCreationException
Cannot cast " + classname + " into " + type.getName()
Error message
Cannot cast " + classname + " into " + type.getName()
What it means
This two-argument createInstance(Class<E> type, Object... params) first builds the object reflectively, then verifies it is actually an instance of the requested type before returning it. If the instantiated class is not assignable to the requested type, it throws ClassCreationException with the class name and target type. It is a runtime type-safety check over the unchecked reflective cast.
Solutions
- Check that the class being instantiated actually implements/extends the requested type
- Print/verify the 'classname' in the message against your configuration keys
- Correct the generic type parameter at the call site (e.g. createInstance with the right Class token)
- Ensure the properties file maps each component key to a class of the expected interface
Example fix
// before
MyType t = new MetaClass("java.lang.String").createInstance(MyType.class);
// after
MyType t = new MetaClass("com.example.MyTypeImpl").createInstance(MyType.class); Defensive patterns
Strategy: type-guard
Validate before calling
Class<?> impl = Class.forName(classname);
if (!TargetType.class.isAssignableFrom(impl)) {
throw new IllegalArgumentException(classname + " does not implement " + TargetType.class.getName());
} Type guard
static boolean canCreateAs(MetaClass mc, Class<?> target) {
try { Class<?> c = Class.forName(mc.getClassName()); return target.isAssignableFrom(c); }
catch (ClassNotFoundException e) { return false; }
} Try / catch
try {
E obj = metaClass.createInstance(Target.class, params);
} catch (MetaClass.ClassCreationException e) {
if (e.getMessage().startsWith("Cannot cast")) {
throw new IllegalArgumentException("Configured class is not a " + Target.class.getSimpleName(), e);
}
throw e;
} Prevention
- Verify configured class names implement the expected interface before deployment
- Watch for interface changes when upgrading library versions
- Use compiler-checked factories instead of string-based reflection where possible
When it happens
Trigger: Calling metaClass.createInstance(SomeType.class, params) where the class name held by the MetaClass resolves to a class that does not extend/implement SomeType.
Common situations: Typo or copy-paste in a properties file pointing a 'serializer' key at an unrelated class; a class was refactored to no longer implement the expected interface; wrong generic type parameter used at the call site.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- CoreMap is actually a CoreLabel
- Loading: failed with
- MetaClass couldn't create " + constructor + " with args " +…
- addFeature was called with a features object that is…
- Shouldn't happen:
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/433f9ab16f30b33e.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/util/MetaClass.java:392
/**
* Creates an instance of the class, forcing a cast to a certain type and
* given an array of objects as constructor parameters NOTE: the resulting
* instance will [unlike java] invoke the most narrow constructor rather
* than the one which matches the signature passed to this function
*
* @param <E> The type of the object returned
* @param type The class of the object returned
* @param params The arguments to the constructor of the class
* @return An instance of the class
*/
@SuppressWarnings("unchecked")
public <E,F extends E> F createInstance(Class<E> type, Object... params) {
Object obj = createInstance(params);
if (type.isInstance(obj)) {
return (F) obj;
} else {
throw new ClassCreationException("Cannot cast " + classname
+ " into " + type.getName());
}
}
public boolean checkConstructor(Object... params){
try {
createInstance(params);
return true;
} catch(ConstructorNotFoundException e){
return false;
}
}
@Override
public String toString() {
return classname;
}
View on GitHub (pinned to 1b7edd19c4)