Blankj/AndroidUtilCode · error · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

PhoneUtils is a final utility class providing static methods for telephony operations (isPhone, getDeviceIMEI, getSerial, dial, etc.). Its private constructor throws UnsupportedOperationException to enforce static-only access.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/PhoneUtils.java:29

import androidx.annotation.NonNull;
import androidx.annotation.RequiresPermission;

import static android.Manifest.permission.CALL_PHONE;
import static android.Manifest.permission.READ_PHONE_STATE;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/08/02
 *     desc  : utils about phone
 * </pre>
 */
public final class PhoneUtils {

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

    /**
     * Return whether the device is phone.
     *
     * @return {@code true}: yes<br>{@code false}: no
     */
    public static boolean isPhone() {
        TelephonyManager tm = getTelephonyManager();
        return tm.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE;
    }

    /**
     * Return the unique device id.
     * <p>If the version of SDK is greater than 28, it will return an empty string.</p>
     * <p>Must hold {@code <uses-permission android:name="android.permission.READ_PHONE_STATE" />}</p>
     *
     * @return the unique device id

View on GitHub (pinned to 7b4caf9e54)

Solutions

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

Example fix

// before
PhoneUtils pu = new PhoneUtils();
boolean isPhone = pu.isPhone();

// after
boolean isPhone = PhoneUtils.isPhone();
Defensive patterns

Strategy: type-guard

Validate before calling

// PhoneUtils is static-only
boolean isPhone = PhoneUtils.isPhone();
String imei = PhoneUtils.getDeviceIMEI();

Type guard

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

Try / catch

try {
    PhoneUtils.class.getDeclaredConstructor().setAccessible(true);
    PhoneUtils.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.

Common situations: DI component scanning traverses the utility package; Mockito mock creation targeting PhoneUtils; a serialization library targets the class; ProGuard keep rules enable reflective construction.

Related errors


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