chinabugotech/hutool · error · IllegalArgumentException
[{}] is not support to get empty!
Error message
[{}] is not support to get empty! What it means
Thrown by CollUtil.empty(Class<?> collectionClass) when the requested collection class is not assignable from Set or List. The method only supports returning empty instances for NavigableSet, SortedSet, Set, and List types. Any other Collection subtype (Queue, Deque, BlockingQueue, etc.) or unrelated class triggers this error.
Source
Thrown at hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java:3008
public static <E, T extends Collection<E>> T empty(Class<?> collectionClass) {
if (null == collectionClass) {
return (T) Collections.emptyList();
}
if (Set.class.isAssignableFrom(collectionClass)) {
if (NavigableSet.class == collectionClass) {
return (T) Collections.emptyNavigableSet();
} else if (SortedSet.class == collectionClass) {
return (T) Collections.emptySortedSet();
} else {
return (T) Collections.emptySet();
}
} else if (List.class.isAssignableFrom(collectionClass)) {
return (T) Collections.emptyList();
}
// 不支持空集合的集合类型
throw new IllegalArgumentException(StrUtil.format("[{}] is not support to get empty!", collectionClass));
}
/**
* 清除一个或多个集合内的元素,每个集合调用clear()方法
*
* @param collections 一个或多个集合
* @since 5.3.6
*/
public static void clear(Collection<?>... collections) {
for (Collection<?> collection : collections) {
if (isNotEmpty(collection)) {
collection.clear();
}
}
}
/**
* 填充List,以达到最小长度View on GitHub (pinned to 8870454b2a)
Solutions
- Only call empty() with Set.class, SortedSet.class, NavigableSet.class, or List.class.
- For Queue/Deque types, create the empty instance directly: new ArrayDeque<>() or Collections.emptyDeque() (if available).
- Check the collectionClass is Set.class.isAssignableFrom or List.class.isAssignableFrom before calling empty().
Example fix
// before Queue<String> q = CollUtil.empty(Queue.class); // throws // after Queue<String> q = new ArrayDeque<>(); // or if you need Set/List: Set<String> s = CollUtil.empty(Set.class); // works
Defensive patterns
Strategy: validation
Validate before calling
// Check supported types before calling empty()
if (Set.class.isAssignableFrom(collectionClass) || List.class.isAssignableFrom(collectionClass)) {
return CollUtil.empty(collectionClass);
} else {
throw new IllegalArgumentException("Unsupported collection type: " + collectionClass);
} Prevention
- Only pass Set.class, SortedSet.class, NavigableSet.class, or List.class to empty().
- For Queue/Deque, instantiate directly: new ArrayDeque<>().
- Document the supported types when exposing APIs that use CollUtil.empty().
When it happens
Trigger: Calling CollUtil.empty(Queue.class), CollUtil.empty(Deque.class), CollUtil.empty(LinkedList.class) (since LinkedList is a List it actually works, but Queue.class does not), CollUtil.empty(ArrayDeque.class), or any non-Set/non-List collection type. Passing null returns an empty List (no error).
Common situations: Attempting to get an empty Queue or Deque for a generic return type. Passing a concrete class like ArrayDeque.class expecting an empty instance. Misunderstanding the limited scope of the empty() method's supported types.
Related errors
- Unsupported object type: {className}
- Error Machine number!
- No method for alias: [{}]
- Duplicate iterator
- remove() method not supported for a NodeListIterator.
AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14).
Data as JSON: /api/errors/7bfbcdc5f1d6758f.
Report an issue: GitHub.