chinabugotech/hutool · error · IllegalArgumentException

Unsupported object type: {className}

Error message

Unsupported object type: {className}

What it means

Thrown by CollUtil.size(Object object) when the object is not one of the supported size-measurable types: Map, Collection, Iterable, Iterator, Enumeration, or array. Any other object type (String, Integer, custom POJO, etc.) is rejected. The method is a polymorphic size calculator with a bounded set of accepted types.

Source

Thrown at hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java:3170

		int total = 0;
		if (object instanceof Map<?, ?>) {
			total = ((Map<?, ?>) object).size();
		} else if (object instanceof Collection<?>) {
			total = ((Collection<?>) object).size();
		} else if (object instanceof Iterable<?>) {
			total = IterUtil.size((Iterable<?>) object);
		} else if (object instanceof Iterator<?>) {
			total = IterUtil.size((Iterator<?>) object);
		} else if (object instanceof Enumeration<?>) {
			final Enumeration<?> it = (Enumeration<?>) object;
			while (it.hasMoreElements()) {
				total++;
				it.nextElement();
			}
		} else if (ArrayUtil.isArray(object)) {
			total = ArrayUtil.length(object);
		} else {
			throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());
		}
		return total;
	}

	/**
	 * 判断两个{@link Collection} 是否元素和顺序相同,返回{@code true}的条件是:
	 * <ul>
	 *     <li>两个{@link Collection}必须长度相同</li>
	 *     <li>两个{@link Collection}元素相同index的对象必须equals,满足{@link Objects#equals(Object, Object)}</li>
	 * </ul>
	 * 此方法来自Apache-Commons-Collections4。
	 *
	 * @param list1 列表1
	 * @param list2 列表2
	 * @return 是否相同
	 * @since 5.6.0
	 */
	public static boolean isEqualList(final Collection<?> list1, final Collection<?> list2) {

View on GitHub (pinned to 8870454b2a)

Solutions

  1. Check the object type before calling size(), or use type-specific size methods.
  2. For String length use StrUtil.length() or String.length().
  3. For Map use map.size(), for Collection use collection.size(), for arrays use ArrayUtil.length().
  4. If the object may be any of the supported types, guard with instanceof checks before calling.

Example fix

// before
int len = CollUtil.size("hello world"); // throws — String not supported

// after
int len = StrUtil.length("hello world"); // correct for strings
// or for mixed types:
if (object instanceof Collection<?>) {
    size = ((Collection<?>) object).size();
} else if (object instanceof Map<?, ?>) {
    size = ((Map<?, ?>) object).size();
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (object instanceof Collection<?>) {
    return ((Collection<?>) object).size();
} else if (object instanceof Map<?, ?>) {
    return ((Map<?, ?>) object).size();
} else if (object == null) {
    return 0;
}

Type guard

public static boolean isSizeable(Object obj) {
    return obj == null || obj instanceof Map<?, ?> || obj instanceof Collection<?>
        || obj instanceof Iterable<?> || obj instanceof Iterator<?>
        || obj instanceof Enumeration<?> || ArrayUtil.isArray(obj);
}

Prevention

When it happens

Trigger: Calling CollUtil.size("hello"), CollUtil.size(42), CollUtil.size(somePojo), or any object that is not a Map, Collection, Iterable, Iterator, Enumeration, or array. Passing null returns 0 (no error).

Common situations: Passing a String to size() expecting character count (use StrUtil.length instead). Passing a primitive wrapper or custom object. Using size() as a generic 'get length' utility without knowing its type restrictions. Confusing CollUtil.size() with other size methods.

Related errors


AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14). Data as JSON: /api/errors/4693f75a3d65b985. Report an issue: GitHub.