Blankj/AndroidUtilCode · warning · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

LocationUtils is a final utility class with a private constructor that throws UnsupportedOperationException. It holds only static fields/listeners and exposes static methods; instantiation is explicitly forbidden.

Source

Thrown at lib/subutil/src/main/java/com/blankj/subutil/util/LocationUtils.java:43

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 16/11/13
 *     desc  : 定位相关工具类
 * </pre>
 */
public final class LocationUtils {

    private static final int TWO_MINUTES = 1000 * 60 * 2;

    private static OnLocationChangeListener mListener;
    private static MyLocationListener       myLocationListener;
    private static LocationManager          mLocationManager;

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


//    /**
//     * you have to check for Location Permission before use this method
//     * add this code <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> to your Manifest file.
//     * you have also implement LocationListener and passed it to the method.
//     *
//     * @param Context
//     * @param LocationListener
//     * @return {@code Location}
//     */
//
//    @SuppressLint("MissingPermission")
//    public static Location getLocation(Context context, LocationListener listener) {
//        Location location = null;
//        try {
//            mLocationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Call the static methods, e.g. LocationUtils.registerLocation(...) / unregisterLocation(); never instantiate.
  2. Exclude LocationUtils from reflective scanning/bean creation.
  3. Remove any newInstance path targeting the class.

Example fix

// before
LocationUtils loc = reflectNew(LocationUtils.class); // throws

// after
LocationUtils.registerLocation(UPDATE_MIN_TIME, UPDATE_MIN_DISTANCE, listener);
Defensive patterns

Strategy: validation

Validate before calling

// LocationUtils is static-only; never instantiate.
LocationUtils.registerLocation(minTime, minDistance, listener);

Type guard

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

Prevention

When it happens

Trigger: Reflective instantiation or unsafe allocation of LocationUtils rather than using its static API.

Common situations: Reflection-driven frameworks or tests attempting to construct the class; misconfigured DI treating it as a bean.

Related errors


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