Blankj/AndroidUtilCode · info · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

FragmentUtils is a final utility class whose private constructor throws UnsupportedOperationException("u can't instantiate me..."). All fragment operations (add, show, hide, replace, remove, pop) are static and driven by integer TYPE_* flags plus static ARGS_* bundles; no instance is needed and the constructor only blocks instantiation, per the library-wide *Utils convention.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/FragmentUtils.java:49

 * </pre>
 */
public final class FragmentUtils {

    private static final int TYPE_ADD_FRAGMENT       = 0x01;
    private static final int TYPE_SHOW_FRAGMENT      = 0x01 << 1;
    private static final int TYPE_HIDE_FRAGMENT      = 0x01 << 2;
    private static final int TYPE_SHOW_HIDE_FRAGMENT = 0x01 << 3;
    private static final int TYPE_REPLACE_FRAGMENT   = 0x01 << 4;
    private static final int TYPE_REMOVE_FRAGMENT    = 0x01 << 5;
    private static final int TYPE_REMOVE_TO_FRAGMENT = 0x01 << 6;

    private static final String ARGS_ID           = "args_id";
    private static final String ARGS_IS_HIDE      = "args_is_hide";
    private static final String ARGS_IS_ADD_STACK = "args_is_add_stack";
    private static final String ARGS_TAG          = "args_tag";

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

    /**
     * Add fragment.
     *
     * @param fm          The manager of fragment.
     * @param add         The fragment will be add.
     * @param containerId The id of container.
     */
    public static void add(@NonNull final FragmentManager fm,
                           @NonNull final Fragment add,
                           @IdRes final int containerId) {
        add(fm, add, containerId, null, false, false);
    }

    /**
     * Add fragment.
     *

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Call the static API directly: FragmentUtils.add(fm, fragment, containerId), FragmentUtils.replace(...), etc.
  2. Add FragmentUtils to the exclusion list of any tool that reflectively instantiates classes.
  3. For a contract test, expect UnsupportedOperationException from the reflected constructor.

Example fix

// before (reflection)
FragmentUtils fu = FragmentUtils.class.getDeclaredConstructor().newInstance();

// after
FragmentUtils.add(getSupportFragmentManager(), frag, R.id.container);
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

try {
    Constructor<?> c = FragmentUtils.class.getDeclaredConstructor();
    c.setAccessible(true);
    c.newInstance();
} catch (UnsupportedOperationException e) {
    // expected; use FragmentUtils.xxx() instead
}

Prevention

When it happens

Trigger: Reflective instantiation via getDeclaredConstructor().newInstance() with setAccessible, or passing the Class to a reflective factory/coverage tool. A normal `new FragmentUtils()` is a compile error because the constructor is private.

Common situations: Coverage/quality tools that instantiate every class; DI or reflective object mappers; a developer who expects instance methods; service loaders that scan packages.

Related errors


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