Blankj/AndroidUtilCode · info · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

CollectionUtils is a final utility class with static collection helpers; its private constructor throws UnsupportedOperationException('u can't instantiate me...') to forbid construction. The throw is an intentional guard reachable only via reflection, because the class is final and the constructor is private.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/CollectionUtils.java:31

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
 * <pre>
 *     author: blankj
 *     blog  : http://blankj.com
 *     time  : 2019/07/26
 *     desc  : utils about collection
 * </pre>
 */
public final class CollectionUtils {

    private CollectionUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    ///////////////////////////////////////////////////////////////////////////
    // listOf
    ///////////////////////////////////////////////////////////////////////////

    /**
     * Returns a new read-only list of given elements.
     *
     * @param array The array.
     * @return a new read-only list of given elements
     */
    @SafeVarargs
    public static <E> List<E> newUnmodifiableList(E... array) {
        return Collections.unmodifiableList(newArrayList(array));
    }

    /**

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Call static methods directly (e.g., CollectionUtils.get(col, i)) and never instantiate.
  2. Exclude the constructor from coverage or rely on the coverage helper.
  3. If testing the guard, assert UnsupportedOperationException rather than trying to succeed.
  4. Remove reflective newInstance() calls against CollectionUtils.class.
Defensive patterns

Strategy: validation

Validate before calling

// Never instantiate; call static methods directly.
Object v = CollectionUtils.get(col, i);

Try / catch

try {
    Constructor<CollectionUtils> c = CollectionUtils.class.getDeclaredConstructor();
    c.setAccessible(true);
    c.newInstance();
} catch (UnsupportedOperationException | InvocationTargetException e) {
    // expected: instantiation is forbidden by design
}

Prevention

When it happens

Trigger: Reflectively invoking the private constructor (setAccessible(true) + newInstance()); coverage or generic reflection tooling that instantiates every class; frameworks that auto-construct helper classes.

Common situations: Code-coverage tools (JaCoCo/Cobertura) covering private constructors; blanket reflection tests across a package; DI/serialization frameworks attempting default construction.

Related errors


AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14). Data as JSON: /api/errors/b6248fd7759ed911. Report an issue: GitHub.