hibernate/hibernate-orm · error · UnsupportedOperationException

Can't get java type class from type: " + type

Error message

Can't get java type class from type: " + type

What it means

ReflectHelper.getClass(java.lang.reflect.Type) reduces a reflective Type to its raw Class. It handles plain Class, ParameterizedType (raw type), TypeVariable (first bound), and WildcardType (first upper bound). Every other shape — most notably GenericArrayType such as List<String>[] and hand-rolled Type implementations — falls through to UnsupportedOperationException.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/internal/util/ReflectHelper.java:898

	}

	public static <T> Class<T> getClass(java.lang.reflect.Type type) {
		if ( type == null ) {
			return null;
		}
		else if ( type instanceof Class<?> ) {
			return (Class<T>) type;
		}
		else if ( type instanceof ParameterizedType parameterizedType ) {
			return (Class<T>) parameterizedType.getRawType();
		}
		else if ( type instanceof TypeVariable<?> typeVariable ) {
			return getClass( typeVariable.getBounds()[0] );
		}
		else if ( type instanceof WildcardType wildcardType ) {
			return getClass( wildcardType.getUpperBounds()[0] );
		}
		throw new UnsupportedOperationException( "Can't get java type class from type: " + type );
	}

	public static Class<?> getPropertyType(Member member) {
		if (member instanceof Field field) {
			return field.getType();
		}
		else if (member instanceof Method method) {
			return method.getReturnType();
		}
		else {
			throw new AssertionFailure("member should have been a method or field");
		}
	}

	public static boolean isClass(Class<?> resultClass) {
		return !resultClass.isArray()
			&& !resultClass.isPrimitive()
			&& !resultClass.isEnum()

View on GitHub (pinned to fad1729dce)

Solutions

  1. Replace array-of-generic attributes with a concrete collection or plain array (List<String>[] -> List<String> or String[]).
  2. Give the attribute an explicit type annotation (@JdbcTypeCode/@Type) so Hibernate never has to reduce the generic Type to a Class.
  3. If you call getClass(Type) yourself, pre-reduce GenericArrayType by recursing on getGenericComponentType().

Example fix

// before
@Embeddable
public class ReportFilter {
    private List<String>[] labels; // GenericArrayType -> UnsupportedOperationException
}

// after
@Embeddable
public class ReportFilter {
    @ElementCollection
    private List<String> labels;
}
Defensive patterns

Strategy: type-guard

Validate before calling

static Class<?> safeClassFor(java.lang.reflect.Type t) {
    if (t instanceof java.lang.reflect.GenericArrayType gat) {
        Class<?> component = safeClassFor(gat.getGenericComponentType());
        return java.lang.reflect.Array.newInstance(component, 0).getClass();
    }
    return org.hibernate.internal.util.ReflectHelper.getClass(t);
}

Type guard

static boolean reducibleToClass(java.lang.reflect.Type t) {
    if (t instanceof Class<?> || t instanceof java.lang.reflect.ParameterizedType
            || t instanceof java.lang.reflect.TypeVariable<?> || t instanceof java.lang.reflect.WildcardType) {
        return true;
    }
    if (t instanceof java.lang.reflect.GenericArrayType gat) {
        return reducibleToClass(gat.getGenericComponentType());
    }
    return false;
}

Try / catch

try {
    Class<?> raw = ReflectHelper.getClass(type);
} catch (UnsupportedOperationException e) {
    // type is a GenericArrayType or custom Type; restructure the attribute or reduce it yourself
}

Prevention

When it happens

Trigger: Resolving a mapped attribute, query return, or generic signature where the reflected Type is a generic array (e.g., an attribute declared List<String>[], whose GenericArrayType component is itself generic), or calling ReflectHelper.getClass(type) directly with such a Type.

Common situations: Entities or embeddables with array-of-generic attributes; generic super-type walking that lands on T[] bounds; custom java.lang.reflect.Type implementations plugged into metadata resolution; migrations to Hibernate 6 exposing exotic generic signatures that earlier versions tolerated.

Related errors


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/4acd61597371b6c6. Report an issue: GitHub.