stanfordnlp/CoreNLP · error · IllegalArgumentException
Cannot convert type to class: " + type
Error message
Cannot convert type to class: " + type
What it means
MetaClass.type2class(Type) converts a java.lang.reflect Type (Class, ParameterizedType, TypeVariable, WildcardType) into its raw Class. If the Type is none of those recognized forms (e.g. a GenericArrayType like T[] or some custom Type implementation), it throws IllegalArgumentException. This is an internal utility hit when reflective generics contain shapes the helper does not handle.
Solutions
- Avoid calling type2class with generic array types; unwrap arrays yourself via GenericArrayType handling
- Convert array component types: extract the generic component type and call type2class on it
- Inspect the offending Type's runtime class in a debugger before conversion
- Upgrade/patch CoreNLP if your Type shape is legitimate but unsupported
Example fix
// before
Class<?> c = MetaClass.type2class(field.getGenericType()); // String[] generic array -> throws
// after
Type t = field.getGenericType();
Class<?> c = (t instanceof GenericArrayType)
? MetaClass.type2class(((GenericArrayType) t).getGenericComponentType())
: MetaClass.type2class(t); Defensive patterns
Strategy: type-guard
Validate before calling
static boolean isConvertable(Type t) {
return t instanceof Class || t instanceof ParameterizedType
|| t instanceof TypeVariable<?> || t instanceof WildcardType;
} Type guard
static Class<?> safeType2class(Type t) {
if (t instanceof GenericArrayType) {
Class<?> comp = safeType2class(((GenericArrayType) t).getGenericComponentType());
return java.lang.reflect.Array.newInstance(comp, 0).getClass();
}
if (t instanceof Class) return (Class<?>) t;
if (t instanceof ParameterizedType) return safeType2class(((ParameterizedType) t).getRawType());
if (t instanceof TypeVariable<?>) return safeType2class(((TypeVariable<?>) t).getBounds()[0]);
if (t instanceof WildcardType) return safeType2class(((WildcardType) t).getUpperBounds()[0]);
throw new IllegalArgumentException("Unsupported type: " + t);
} Try / catch
try {
Class<?> c = MetaClass.type2class(type);
} catch (IllegalArgumentException e) {
logger.warning("Unhandled reflective Type: " + type + " (" + type.getClass().getName() + ")");
// fall back to Object.class or skip
} Prevention
- Resolve TypeVariables against the declaring class before reflective conversion
- Handle GenericArrayType explicitly in any Type-walking code
- Prefer field.getType() over field.getGenericType() when generics are not needed
When it happens
Trigger: Calling type2class (directly or via cast helpers) with a GenericArrayType (e.g. String[]), a custom Type implementation, or any Type that is not Class/ParameterizedType/TypeVariable/WildcardType.
Common situations: Reflecting over a field or method with a generic array type; passing a Type obtained from exotic generic declarations into MetaClass casting utilities; library version differences producing new Type shapes.
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
- Cannot cast to type (unhandled type): " + type
- Cannot cast " + classname + " into " + type.getName()
- Cannot get field from
- Class at path
- Class is in classpath multiple times
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/cfc38c5bf2cc563d.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/util/MetaClass.java:459
return new MetaClass(clazz);
}
/**
* Utility method for cast
* @param type The type to cast into a class
* @return The class corresponding to the passed in type
*/
private static Class <?> type2class(Type type){
if(type instanceof Class <?>){
return (Class <?>) type; //base case
}else if(type instanceof ParameterizedType){
return type2class( ((ParameterizedType) type).getRawType() );
}else if(type instanceof TypeVariable<?>){
return type2class( ((TypeVariable<?>) type).getBounds()[0] );
}else if(type instanceof WildcardType){
return type2class( ((WildcardType) type).getUpperBounds()[0] );
}else{
throw new IllegalArgumentException("Cannot convert type to class: " + type);
}
}
/**
* Cast a String representation of an object into that object.
* E.g. "5.4" will be cast to a Double; "[1,2,3]" will be cast
* to an Integer[].
*
* NOTE: Date parses from a Long
*
* @param <E> The type of the object returned (same as type)
* @param value The string representation of the object
* @param type The type (usually class) to be returned (same as E)
* @return An object corresponding to the String value passed
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <E> E cast(String value, Type type){
//--Get TypeView on GitHub (pinned to 1b7edd19c4)