Blankj/AndroidUtilCode · error · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

ServiceUtils is a final static-only utility class for querying running services. Its private constructor throws UnsupportedOperationException to make accidental instantiation fail loudly. Because every method is static, the constructor is never meant to run; the guard converts a silent misuse into an immediate, diagnosable exception.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/ServiceUtils.java:27

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import androidx.annotation.NonNull;

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

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

    /**
     * Return all of the services are running.
     *
     * @return all of the services are running
     */
    public static Set<String> getAllRunningServices() {
        ActivityManager am = (ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningServiceInfo> info = am.getRunningServices(0x7FFFFFFF);
        Set<String> names = new HashSet<>();
        if (info == null || info.size() == 0) return null;
        for (RunningServiceInfo aInfo : info) {
            names.add(aInfo.service.getClassName());
        }
        return names;
    }

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Call the static API directly: ServiceUtils.getAllRunningServices() instead of `new ServiceUtils()`.
  2. Register a no-op InstanceCreator/TypeAdapter (Gson) or mixin (Jackson) so the deserializer never invokes the constructor.
  3. For mocking, use Mockito.mockStatic on the specific static method rather than instantiating the class.
  4. Keep the class marked final and the constructor private so IDE tooling does not suggest constructing it.

Example fix

// before
Gson gson = new Gson();
ServiceUtils su = gson.fromJson(json, ServiceUtils.class);

// after
Set<String> running = ServiceUtils.getAllRunningServices();
Defensive patterns

Strategy: validation

Validate before calling

if (Modifier.isFinal(ServiceUtils.class.getModifiers())) {
  throw new IllegalStateException("ServiceUtils is static-only; call methods directly.");
}

Prevention

When it happens

Trigger: Triggered by reflective instantiation: ServiceUtils.class.getDeclaredConstructor().setAccessible(true).newInstance(), or transitively through Gson/Jackson deserialization, Mockito/PowerMock mocking attempts, Kotlin reflection, or a DI/container classpath scan that treats the class as instantiable.

Common situations: Test suites that mock or parameterize over utility classes; JSON mappers binding a field to ServiceUtils; CI coverage tooling that reflectively probes classes; accidental instantiation after a refactor weakened access modifiers.

Related errors


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