Blankj/AndroidUtilCode · info · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

CrashUtils is a final utility class that installs an UncaughtExceptionHandler to log crashes to disk; its private constructor throws UnsupportedOperationException("u can't instantiate me..."). It is used entirely through static init/handler methods, so no instance is required and the constructor only blocks instantiation, matching the library-wide *Utils convention.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/CrashUtils.java:26

import java.util.Map;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/09/27
 *     desc  : utils about crash
 * </pre>
 */
public final class CrashUtils {

    private static final String FILE_SEP = System.getProperty("file.separator");

    private static final UncaughtExceptionHandler DEFAULT_UNCAUGHT_EXCEPTION_HANDLER =
        Thread.getDefaultUncaughtExceptionHandler();

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

    /**
     * Initialization.
     */
    public static void init() {
        init("");
    }

    /**
     * Initialization
     *
     * @param crashDir The directory of saving crash information.
     */
    public static void init(@NonNull final File crashDir) {
        init(crashDir.getAbsolutePath(), null);
    }

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Call the static entry point directly: CrashUtils.init() and read its callbacks statically — never construct an instance.
  2. Configure any reflective instantiation tool to skip final classes or specifically exclude CrashUtils.
  3. For a contract test, assert UnsupportedOperationException is thrown rather than using the produced instance.

Example fix

// before (reflection)
CrashUtils cu = CrashUtils.class.getDeclaredConstructor().newInstance();

// after
CrashUtils.init();
Defensive patterns

Strategy: validation

Validate before calling

// Never construct CrashUtils; it is static-only.
if (clazz == CrashUtils.class || Modifier.isFinal(clazz.getModifiers())) {
    // skip instantiation; call CrashUtils.init() statically
}

Type guard

if (clazz.getName().endsWith("Utils") && Modifier.isFinal(clazz.getModifiers())) {
    // static-only; do not newInstance()
}

Try / catch

try {
    Constructor<?> c = CrashUtils.class.getDeclaredConstructor();
    c.setAccessible(true);
    c.newInstance();
} catch (UnsupportedOperationException e) {
    // expected; use CrashUtils.init() / static API
}

Prevention

When it happens

Trigger: Reflective instantiation (getDeclaredConstructor().newInstance() with setAccessible) or a framework that new-ups classes by Class reference. A normal `new CrashUtils()` is rejected at compile time because the constructor is private.

Common situations: Coverage tooling that instantiates every class; DI or service-loader-style scans that try to construct discovered classes; a developer who believes init() must be called on an instance; mutation testing that targets constructors.

Related errors


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