Blankj/AndroidUtilCode · error · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

ObjectUtils is a final utility class providing static methods for object comparison, null-checking (requireNonNull, requireNonNulls), emptiness checks, and defaulting (getOrDefault). Its private constructor throws UnsupportedOperationException to enforce static-only access.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/ObjectUtils.java:31

import java.util.Map;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.collection.LongSparseArray;
import androidx.collection.SimpleArrayMap;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2017/12/24
 *     desc  : utils about object
 * </pre>
 */
public final class ObjectUtils {

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

    /**
     * Return whether object is empty.
     *
     * @param obj The object.
     * @return {@code true}: yes<br>{@code false}: no
     */
    public static boolean isEmpty(final Object obj) {
        if (obj == null) {
            return true;
        }
        if (obj.getClass().isArray() && Array.getLength(obj) == 0) {
            return true;
        }
        if (obj instanceof CharSequence && obj.toString().length() == 0) {
            return true;
        }

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Use static methods: ObjectUtils.requireNonNull(obj), ObjectUtils.isEmpty(obj). Never call new ObjectUtils().
  2. Exclude utility classes from DI scanning and serialization configs.
  3. Mock static methods with mockito-inline for testing.

Example fix

// before
ObjectUtils ou = new ObjectUtils();
boolean empty = ou.isEmpty(list);

// after
boolean empty = ObjectUtils.isEmpty(list);
Defensive patterns

Strategy: type-guard

Validate before calling

// ObjectUtils is static-only
boolean empty = ObjectUtils.isEmpty(obj);
T nonNull = ObjectUtils.requireNonNull(obj, "must not be null");

Type guard

static boolean isStaticUtility(Class<?> c) {
    return Modifier.isFinal(c.getModifiers()) && c.getSimpleName().endsWith("Utils");
}

Try / catch

try {
    ObjectUtils.class.getDeclaredConstructor().setAccessible(true);
    ObjectUtils.class.getDeclaredConstructor().newInstance();
} catch (UnsupportedOperationException e) {
    // Use static methods
}

Prevention

When it happens

Trigger: Direct or reflective instantiation by DI frameworks, mocking libraries, serialization frameworks, or manual reflection via Constructor.newInstance().

Common situations: DI component scanning traverses the utility package and attempts construction; a test suite instantiates all classes; a serialization library targets the class.

Related errors


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