Blankj/AndroidUtilCode · info · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

CloneUtils is a final utility class with static deep-clone 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/CloneUtils.java:16

package com.blankj.utilcode.util;

import java.lang.reflect.Type;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2018/01/30
 *     desc  : utils about clone
 * </pre>
 */
public final class CloneUtils {

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

    /**
     * Deep clone.
     *
     * @param data The data.
     * @param type The type.
     * @param <T>  The value type.
     * @return The object of cloned.
     */
    public static <T> T deepClone(final T data, final Type type) {
        try {
            return UtilsBridge.fromJson(UtilsBridge.toJson(data), type);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Call static methods directly (e.g., CloneUtils.deepClone(data, type)) 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 CloneUtils.class.
Defensive patterns

Strategy: validation

Validate before calling

// Never instantiate; call static methods directly.
T copy = CloneUtils.deepClone(data, type);

Try / catch

try {
    Constructor<CloneUtils> c = CloneUtils.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/2bed571e1ffa522a. Report an issue: GitHub.