Blankj/AndroidUtilCode · warning · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

BitUtils is a final utility class with a private constructor that throws UnsupportedOperationException. The class is designed for static access only; the throw is the guard against reflection-based instantiation.

Source

Thrown at lib/subutil/src/main/java/com/blankj/subutil/util/BitUtils.java:16

package com.blankj.subutil.util;

import android.util.Log;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2018/03/21
 *     desc  : 位运算工具类
 * </pre>
 */
public final class BitUtils {

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

    /**
     * 获取运算数指定位置的值<br>
     * 例如: 0000 1011 获取其第 0 位的值为 1, 第 2 位 的值为 0<br>
     *
     * @param source 需要运算的数
     * @param pos    指定位置 (0...7)
     * @return 指定位置的值(0 or 1)
     */
    public static byte getBitValue(byte source, int pos) {
        return (byte) ((source >> pos) & 1);

    }


    /**
     * 将运算数指定位置的值置为指定值<br>

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Do not instantiate BitUtils; call its static methods directly, e.g. BitUtils.getBitValue(source, pos).
  2. If a framework forces instantiation, exclude BitUtils from its scanning or configure it to use static-only access.
  3. Remove any reflection-based newInstance call targeting utility classes.

Example fix

// before
BitUtils b = BitUtils.class.getDeclaredConstructor().newInstance(); // throws

// after
byte v = BitUtils.getBitValue(source, 2);
Defensive patterns

Strategy: validation

Validate before calling

// BitUtils is static-only; never instantiate.
byte bit = BitUtils.getBitValue(source, 2);

Type guard

private static boolean isStaticUtility(Class<?> c) {
    return Modifier.isFinal(c.getModifiers()) && hasOnlyStaticMethods(c);
}

Prevention

When it happens

Trigger: Instantiating via reflection (Constructor.newInstance) or an unsafe allocator, since the private constructor prevents normal `new BitUtils()` at compile time.

Common situations: Reflection/serialization/DI frameworks that try to instantiate any class; tooling that scans and constructs classes; accidental test helpers that reflectively build utility classes.

Related errors


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