Blankj/AndroidUtilCode · error · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

ShellUtils is a final static-only utility for executing shell commands (including optional root). Its private constructor throws UnsupportedOperationException to block instantiation. All public surface is static (execCmd, execCmdAsync), so construction is always a misuse.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/ShellUtils.java:24

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/08/07
 *     desc  : utils about shell
 * </pre>
 */
public final class ShellUtils {

    private static final String LINE_SEP = System.getProperty("line.separator");

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

    /**
     * Execute the command asynchronously.
     *
     * @param command  The command.
     * @param isRooted True to use root, false otherwise.
     * @param consumer The consumer.
     * @return the task
     */
    public static Utils.Task<CommandResult> execCmdAsync(final String command,
                                                         final boolean isRooted,
                                                         final Utils.Consumer<CommandResult> consumer) {
        return execCmdAsync(new String[]{command}, isRooted, true, consumer);
    }

    /**
     * Execute the command asynchronously.

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Use the static API: ShellUtils.execCmd(cmd, isRooted) instead of constructing the class.
  2. Provide a Gson InstanceCreator or Jackson mixin that returns a sentinel/null so the constructor is never invoked.
  3. For mocking, use Mockito.mockStatic on execCmd rather than constructing ShellUtils.
  4. Keep the class final and constructor private to prevent accidental subclassing/instantiation.

Example fix

// before
ShellUtils su = new ShellUtils(); // or via reflection

// after
ShellUtils.CommandResult r = ShellUtils.execCmd("ls", false);
Defensive patterns

Strategy: validation

Validate before calling

if (Modifier.isFinal(ShellUtils.class.getModifiers())) {
  throw new IllegalStateException("ShellUtils is static-only; call execCmd directly.");
}

Prevention

When it happens

Trigger: Reflective instantiation via ShellUtils.class.getDeclaredConstructor().setAccessible(true).newInstance(), or transitively via Gson/Jackson deserialization, Mockito/PowerMock mocking, Kotlin reflection, or a DI classpath scan.

Common situations: Tests mocking shell execution; JSON mapping a field to ShellUtils; coverage tooling probing classes; accidental instantiation after a fork weakened access modifiers.

Related errors


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